88#include " base/location.h"
99#include " base/message_loop/message_loop.h"
1010#include " chrome/browser/ui/autofill/popup_constants.h"
11- #include " ui/gfx/point.h"
12- #include " ui/gfx/screen.h"
1311#include " ui/views/border.h"
14- #include " ui/views/event_utils.h"
1512#include " ui/views/widget/widget.h"
16-
17- #if defined(USE_AURA)
1813#include " ui/wm/core/window_animations.h"
19- #endif
2014
2115namespace autofill {
2216
@@ -49,7 +43,8 @@ AutofillPopupBaseView::~AutofillPopupBaseView() {
4943}
5044
5145void AutofillPopupBaseView::DoShow () {
52- if (!GetWidget ()) {
46+ const bool initialize_widget = !GetWidget ();
47+ if (initialize_widget) {
5348 observing_widget_->AddObserver (this );
5449
5550 views::FocusManager* focus_manager = observing_widget_->GetFocusManager ();
@@ -71,11 +66,10 @@ void AutofillPopupBaseView::DoShow() {
7166 params.parent = container_view ();
7267 widget->Init (params);
7368 widget->SetContentsView (this );
74- # if defined(USE_AURA)
69+
7570 // No animation for popup appearance (too distracting).
7671 wm::SetWindowVisibilityAnimationTransition (
7772 widget->GetNativeView (), wm::ANIMATE_HIDE );
78- #endif
7973 }
8074
8175 SetBorder (views::Border::CreateSolidBorder (kPopupBorderThickness ,
@@ -84,8 +78,10 @@ void AutofillPopupBaseView::DoShow() {
8478 DoUpdateBoundsAndRedrawPopup ();
8579 GetWidget ()->Show ();
8680
87- if (ShouldHideOnOutsideClick ())
88- GetWidget ()->SetCapture (this );
81+ // Showing the widget can change native focus (which would result in an
82+ // immediate hiding of the popup). Only start observing after shown.
83+ if (initialize_widget)
84+ views::WidgetFocusManager::GetInstance ()->AddFocusChangeListener (this );
8985}
9086
9187void AutofillPopupBaseView::DoHide () {
@@ -108,13 +104,21 @@ void AutofillPopupBaseView::DoHide() {
108104void AutofillPopupBaseView::RemoveObserver () {
109105 observing_widget_->GetFocusManager ()->UnregisterAccelerators (this );
110106 observing_widget_->RemoveObserver (this );
107+ views::WidgetFocusManager::GetInstance ()->RemoveFocusChangeListener (this );
111108}
112109
113110void AutofillPopupBaseView::DoUpdateBoundsAndRedrawPopup () {
114111 GetWidget ()->SetBounds (delegate_->popup_bounds ());
115112 SchedulePaint ();
116113}
117114
115+ void AutofillPopupBaseView::OnNativeFocusChange (
116+ gfx::NativeView focused_before,
117+ gfx::NativeView focused_now) {
118+ if (GetWidget () && GetWidget ()->GetNativeView () != focused_now)
119+ HideController ();
120+ }
121+
118122void AutofillPopupBaseView::OnWidgetBoundsChanged (views::Widget* widget,
119123 const gfx::Rect& new_bounds) {
120124 DCHECK_EQ (widget, observing_widget_);
@@ -157,39 +161,10 @@ void AutofillPopupBaseView::OnMouseMoved(const ui::MouseEvent& event) {
157161}
158162
159163bool AutofillPopupBaseView::OnMousePressed (const ui::MouseEvent& event) {
160- if (HitTestPoint (event.location ()))
161- return true ;
162-
163- if (ShouldHideOnOutsideClick ()) {
164- GetWidget ()->ReleaseCapture ();
165-
166- gfx::Point screen_loc = event.location ();
167- views::View::ConvertPointToScreen (this , &screen_loc);
168-
169- ui::MouseEvent mouse_event = event;
170- mouse_event.set_location (screen_loc);
171-
172- if (ShouldRepostEvent (mouse_event)) {
173- gfx::NativeView native_view = GetWidget ()->GetNativeView ();
174- gfx::Screen* screen = gfx::Screen::GetScreenFor (native_view);
175- gfx::NativeWindow window = screen->GetWindowAtScreenPoint (screen_loc);
176- views::RepostLocatedEvent (window, mouse_event);
177- }
178-
179- HideController ();
180- // |this| is now deleted.
181- }
182-
183- return false ;
164+ return event.GetClickCount () == 1 ;
184165}
185166
186167void AutofillPopupBaseView::OnMouseReleased (const ui::MouseEvent& event) {
187- // Because this view can can be shown in response to a mouse press, it can
188- // receive an OnMouseReleased event just after showing. This breaks the mouse
189- // capture, so restart capturing here.
190- if (ShouldHideOnOutsideClick () && GetWidget ())
191- GetWidget ()->SetCapture (this );
192-
193168 // We only care about the left click.
194169 if (event.IsOnlyLeftMouseButton () && HitTestPoint (event.location ()))
195170 AcceptSelection (event.location ());
@@ -256,27 +231,13 @@ void AutofillPopupBaseView::ClearSelection() {
256231 delegate_->SelectionCleared ();
257232}
258233
259- bool AutofillPopupBaseView::ShouldHideOnOutsideClick () {
260- if (delegate_)
261- return delegate_->ShouldHideOnOutsideClick ();
262-
263- // |this| instance should be in the process of being destroyed, so the return
264- // value shouldn't matter.
265- return false ;
266- }
267-
268234void AutofillPopupBaseView::HideController () {
269235 if (delegate_)
270236 delegate_->Hide ();
271237}
272238
273- bool AutofillPopupBaseView::ShouldRepostEvent (const ui::MouseEvent& event) {
274- return delegate_->ShouldRepostEvent (event);
275- }
276-
277239gfx::NativeView AutofillPopupBaseView::container_view () {
278240 return delegate_->container_view ();
279241}
280242
281-
282243} // namespace autofill
0 commit comments