1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/autofill/autofill_popup_base_view.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/ui/autofill/popup_constants.h"
12 #include "ui/views/border.h"
13 #include "ui/views/focus/focus_manager.h"
14 #include "ui/views/widget/widget.h"
18 const SkColor
AutofillPopupBaseView::kBorderColor
=
19 SkColorSetARGB(0xFF, 0xC7, 0xCA, 0xCE);
20 const SkColor
AutofillPopupBaseView::kHoveredBackgroundColor
=
21 SkColorSetARGB(0xFF, 0xCD, 0xCD, 0xCD);
22 const SkColor
AutofillPopupBaseView::kItemTextColor
=
23 SkColorSetARGB(0xFF, 0x7F, 0x7F, 0x7F);
24 const SkColor
AutofillPopupBaseView::kPopupBackground
=
25 SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
26 const SkColor
AutofillPopupBaseView::kValueTextColor
=
27 SkColorSetARGB(0xFF, 0x00, 0x00, 0x00);
28 const SkColor
AutofillPopupBaseView::kWarningTextColor
=
29 SkColorSetARGB(0xFF, 0x7F, 0x7F, 0x7F);
31 AutofillPopupBaseView::AutofillPopupBaseView(
32 AutofillPopupViewDelegate
* delegate
,
33 views::Widget
* parent_widget
)
34 : delegate_(delegate
),
35 parent_widget_(parent_widget
),
36 weak_ptr_factory_(this) {}
38 AutofillPopupBaseView::~AutofillPopupBaseView() {
40 delegate_
->ViewDestroyed();
46 void AutofillPopupBaseView::DoShow() {
47 const bool initialize_widget
= !GetWidget();
48 if (initialize_widget
) {
49 parent_widget_
->AddObserver(this);
50 views::FocusManager
* focus_manager
= parent_widget_
->GetFocusManager();
51 focus_manager
->RegisterAccelerator(
52 ui::Accelerator(ui::VKEY_RETURN
, ui::EF_NONE
),
53 ui::AcceleratorManager::kNormalPriority
,
55 focus_manager
->RegisterAccelerator(
56 ui::Accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
),
57 ui::AcceleratorManager::kNormalPriority
,
60 // The widget is destroyed by the corresponding NativeWidget, so we use
61 // a weak pointer to hold the reference and don't have to worry about
63 views::Widget
* widget
= new views::Widget
;
64 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
65 params
.delegate
= this;
66 params
.parent
= container_view();
68 widget
->SetContentsView(this);
70 // No animation for popup appearance (too distracting).
71 widget
->SetVisibilityAnimationTransition(views::Widget::ANIMATE_HIDE
);
73 show_time_
= base::Time::Now();
76 SetBorder(views::Border::CreateSolidBorder(kPopupBorderThickness
,
79 DoUpdateBoundsAndRedrawPopup();
82 // Showing the widget can change native focus (which would result in an
83 // immediate hiding of the popup). Only start observing after shown.
84 if (initialize_widget
)
85 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this);
88 void AutofillPopupBaseView::DoHide() {
89 // The controller is no longer valid after it hides us.
95 // Don't call CloseNow() because some of the functions higher up the stack
96 // assume the the widget is still valid after this point.
97 // http://crbug.com/229224
98 // NOTE: This deletes |this|.
105 void AutofillPopupBaseView::OnWidgetBoundsChanged(views::Widget
* widget
,
106 const gfx::Rect
& new_bounds
) {
107 DCHECK_EQ(widget
, parent_widget_
);
113 void AutofillPopupBaseView::RemoveObserver() {
114 parent_widget_
->GetFocusManager()->UnregisterAccelerators(this);
115 parent_widget_
->RemoveObserver(this);
116 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this);
119 void AutofillPopupBaseView::DoUpdateBoundsAndRedrawPopup() {
120 GetWidget()->SetBounds(delegate_
->popup_bounds());
124 void AutofillPopupBaseView::OnNativeFocusChanged(gfx::NativeView focused_now
) {
125 if (GetWidget() && GetWidget()->GetNativeView() != focused_now
)
129 void AutofillPopupBaseView::OnMouseCaptureLost() {
133 bool AutofillPopupBaseView::OnMouseDragged(const ui::MouseEvent
& event
) {
134 if (HitTestPoint(event
.location())) {
135 SetSelection(event
.location());
137 // We must return true in order to get future OnMouseDragged and
138 // OnMouseReleased events.
142 // If we move off of the popup, we lose the selection.
147 void AutofillPopupBaseView::OnMouseExited(const ui::MouseEvent
& event
) {
148 // Pressing return causes the cursor to hide, which will generate an
149 // OnMouseExited event. Pressing return should activate the current selection
150 // via AcceleratorPressed, so we need to let that run first.
151 base::ThreadTaskRunnerHandle::Get()->PostTask(
152 FROM_HERE
, base::Bind(&AutofillPopupBaseView::ClearSelection
,
153 weak_ptr_factory_
.GetWeakPtr()));
156 void AutofillPopupBaseView::OnMouseMoved(const ui::MouseEvent
& event
) {
157 // A synthesized mouse move will be sent when the popup is first shown.
158 // Don't preview a suggestion if the mouse happens to be hovering there.
160 // TODO(rouslan): Use event.time_stamp() and ui::EventTimeForNow() when they
161 // become comparable. http://crbug.com/453559
162 if (base::Time::Now() - show_time_
<= base::TimeDelta::FromMilliseconds(50))
165 if (event
.flags() & ui::EF_IS_SYNTHESIZED
)
169 if (HitTestPoint(event
.location()))
170 SetSelection(event
.location());
175 bool AutofillPopupBaseView::OnMousePressed(const ui::MouseEvent
& event
) {
176 return event
.GetClickCount() == 1;
179 void AutofillPopupBaseView::OnMouseReleased(const ui::MouseEvent
& event
) {
180 // We only care about the left click.
181 if (event
.IsOnlyLeftMouseButton() && HitTestPoint(event
.location()))
182 AcceptSelection(event
.location());
185 void AutofillPopupBaseView::OnGestureEvent(ui::GestureEvent
* event
) {
186 switch (event
->type()) {
187 case ui::ET_GESTURE_TAP_DOWN
:
188 case ui::ET_GESTURE_SCROLL_BEGIN
:
189 case ui::ET_GESTURE_SCROLL_UPDATE
:
190 if (HitTestPoint(event
->location()))
191 SetSelection(event
->location());
195 case ui::ET_GESTURE_TAP
:
196 case ui::ET_GESTURE_SCROLL_END
:
197 if (HitTestPoint(event
->location()))
198 AcceptSelection(event
->location());
202 case ui::ET_GESTURE_TAP_CANCEL
:
203 case ui::ET_SCROLL_FLING_START
:
212 bool AutofillPopupBaseView::AcceleratorPressed(
213 const ui::Accelerator
& accelerator
) {
214 DCHECK_EQ(accelerator
.modifiers(), ui::EF_NONE
);
216 if (accelerator
.key_code() == ui::VKEY_ESCAPE
) {
221 if (accelerator
.key_code() == ui::VKEY_RETURN
)
222 return delegate_
->AcceptSelectedLine();
228 void AutofillPopupBaseView::SetSelection(const gfx::Point
& point
) {
230 delegate_
->SetSelectionAtPoint(point
);
233 void AutofillPopupBaseView::AcceptSelection(const gfx::Point
& point
) {
237 delegate_
->SetSelectionAtPoint(point
);
238 delegate_
->AcceptSelectedLine();
241 void AutofillPopupBaseView::ClearSelection() {
243 delegate_
->SelectionCleared();
246 void AutofillPopupBaseView::HideController() {
251 gfx::NativeView
AutofillPopupBaseView::container_view() {
252 return delegate_
->container_view();
255 } // namespace autofill