1 // Copyright 2013 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/toolbar/toolbar_button.h"
8 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
9 #include "grit/theme_resources.h"
10 #include "grit/ui_strings.h"
11 #include "ui/accessibility/ax_view_state.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/base/models/menu_model.h"
14 #include "ui/gfx/display.h"
15 #include "ui/gfx/screen.h"
16 #include "ui/views/controls/button/label_button_border.h"
17 #include "ui/views/controls/menu/menu_item_view.h"
18 #include "ui/views/controls/menu/menu_model_adapter.h"
19 #include "ui/views/controls/menu/menu_runner.h"
20 #include "ui/views/widget/widget.h"
22 ToolbarButton::ToolbarButton(views::ButtonListener
* listener
,
24 : views::LabelButton(listener
, base::string16()),
27 y_position_on_lbuttondown_(0),
28 show_menu_factory_(this) {
29 set_context_menu_controller(this);
32 ToolbarButton::~ToolbarButton() {
35 void ToolbarButton::Init() {
37 SetAccessibilityFocusable(true);
38 image()->EnableCanvasFlippingForRTLUI(true);
41 void ToolbarButton::ClearPendingMenu() {
42 show_menu_factory_
.InvalidateWeakPtrs();
45 bool ToolbarButton::IsMenuShowing() const {
49 gfx::Size
ToolbarButton::GetPreferredSize() const {
50 gfx::Size
size(image()->GetPreferredSize());
51 gfx::Size label_size
= label()->GetPreferredSize();
52 if (label_size
.width() > 0)
53 size
.Enlarge(label_size
.width() + LocationBarView::kItemPadding
, 0);
57 bool ToolbarButton::OnMousePressed(const ui::MouseEvent
& event
) {
58 if (enabled() && ShouldShowMenu() &&
59 IsTriggerableEvent(event
) && HitTestPoint(event
.location())) {
60 // Store the y pos of the mouse coordinates so we can use them later to
61 // determine if the user dragged the mouse down (which should pop up the
62 // drag down menu immediately, instead of waiting for the timer)
63 y_position_on_lbuttondown_
= event
.y();
65 // Schedule a task that will show the menu.
66 const int kMenuTimerDelay
= 500;
67 base::MessageLoop::current()->PostDelayedTask(
69 base::Bind(&ToolbarButton::ShowDropDownMenu
,
70 show_menu_factory_
.GetWeakPtr(),
71 ui::GetMenuSourceTypeForEvent(event
)),
72 base::TimeDelta::FromMilliseconds(kMenuTimerDelay
));
74 return LabelButton::OnMousePressed(event
);
77 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent
& event
) {
78 bool result
= LabelButton::OnMouseDragged(event
);
80 if (show_menu_factory_
.HasWeakPtrs()) {
81 // If the mouse is dragged to a y position lower than where it was when
82 // clicked then we should not wait for the menu to appear but show
84 if (event
.y() > y_position_on_lbuttondown_
+ GetHorizontalDragThreshold()) {
85 show_menu_factory_
.InvalidateWeakPtrs();
86 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event
));
93 void ToolbarButton::OnMouseReleased(const ui::MouseEvent
& event
) {
94 if (IsTriggerableEvent(event
) ||
95 (event
.IsRightMouseButton() && !HitTestPoint(event
.location()))) {
96 LabelButton::OnMouseReleased(event
);
99 if (IsTriggerableEvent(event
))
100 show_menu_factory_
.InvalidateWeakPtrs();
103 void ToolbarButton::OnMouseCaptureLost() {
106 void ToolbarButton::OnMouseExited(const ui::MouseEvent
& event
) {
107 // Starting a drag results in a MouseExited, we need to ignore it.
108 // A right click release triggers an exit event. We want to
109 // remain in a PUSHED state until the drop down menu closes.
110 if (state_
!= STATE_DISABLED
&& !InDrag() && state_
!= STATE_PRESSED
)
111 SetState(STATE_NORMAL
);
114 void ToolbarButton::OnGestureEvent(ui::GestureEvent
* event
) {
116 // While dropdown menu is showing the button should not handle gestures.
117 event
->StopPropagation();
121 LabelButton::OnGestureEvent(event
);
124 void ToolbarButton::GetAccessibleState(ui::AXViewState
* state
) {
125 CustomButton::GetAccessibleState(state
);
126 state
->role
= ui::AX_ROLE_BUTTON_DROP_DOWN
;
127 state
->default_action
= l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS
);
128 state
->AddStateFlag(ui::AX_STATE_HASPOPUP
);
131 void ToolbarButton::ShowContextMenuForView(View
* source
,
132 const gfx::Point
& point
,
133 ui::MenuSourceType source_type
) {
137 show_menu_factory_
.InvalidateWeakPtrs();
138 ShowDropDownMenu(source_type
);
141 bool ToolbarButton::ShouldEnterPushedState(const ui::Event
& event
) {
142 // Enter PUSHED state on press with Left or Right mouse button or on taps.
143 // Remain in this state while the context menu is open.
144 return event
.type() == ui::ET_GESTURE_TAP
||
145 event
.type() == ui::ET_GESTURE_TAP_DOWN
||
146 (event
.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON
|
147 ui::EF_RIGHT_MOUSE_BUTTON
) & event
.flags()) != 0);
150 bool ToolbarButton::ShouldShowMenu() {
151 return model_
!= NULL
;
154 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type
) {
155 if (!ShouldShowMenu())
158 gfx::Rect lb
= GetLocalBounds();
160 // Both the menu position and the menu anchor type change if the UI layout
162 gfx::Point
menu_position(lb
.origin());
163 menu_position
.Offset(0, lb
.height() - 1);
164 if (base::i18n::IsRTL())
165 menu_position
.Offset(lb
.width() - 1, 0);
167 View::ConvertPointToScreen(this, &menu_position
);
170 int left_bound
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
171 #elif defined(OS_CHROMEOS)
172 // A window won't overlap between displays on ChromeOS.
173 // Use the left bound of the display on which
174 // the menu button exists.
175 gfx::NativeView view
= GetWidget()->GetNativeView();
176 gfx::Display display
= gfx::Screen::GetScreenFor(
177 view
)->GetDisplayNearestWindow(view
);
178 int left_bound
= display
.bounds().x();
180 // The window might be positioned over the edge between two screens. We'll
181 // want to position the dropdown on the screen the mouse cursor is on.
182 gfx::NativeView view
= GetWidget()->GetNativeView();
183 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(view
);
184 gfx::Display display
= screen
->GetDisplayNearestPoint(
185 screen
->GetCursorScreenPoint());
186 int left_bound
= display
.bounds().x();
188 if (menu_position
.x() < left_bound
)
189 menu_position
.set_x(left_bound
);
191 // Make the button look depressed while the menu is open.
192 SetState(STATE_PRESSED
);
194 menu_showing_
= true;
196 // Create and run menu. Display an empty menu if model is NULL.
198 views::MenuModelAdapter
menu_delegate(model_
.get());
199 menu_delegate
.set_triggerable_event_flags(triggerable_event_flags());
200 menu_runner_
.reset(new views::MenuRunner(menu_delegate
.CreateMenu()));
201 views::MenuRunner::RunResult result
=
202 menu_runner_
->RunMenuAt(GetWidget(),
204 gfx::Rect(menu_position
, gfx::Size(0, 0)),
205 views::MENU_ANCHOR_TOPLEFT
,
207 views::MenuRunner::HAS_MNEMONICS
);
208 if (result
== views::MenuRunner::MENU_DELETED
)
211 views::MenuDelegate menu_delegate
;
212 views::MenuItemView
* menu
= new views::MenuItemView(&menu_delegate
);
213 menu_runner_
.reset(new views::MenuRunner(menu
));
214 views::MenuRunner::RunResult result
=
215 menu_runner_
->RunMenuAt(GetWidget(),
217 gfx::Rect(menu_position
, gfx::Size(0, 0)),
218 views::MENU_ANCHOR_TOPLEFT
,
220 views::MenuRunner::HAS_MNEMONICS
);
221 if (result
== views::MenuRunner::MENU_DELETED
)
225 menu_showing_
= false;
227 // Need to explicitly clear mouse handler so that events get sent
228 // properly after the menu finishes running. If we don't do this, then
229 // the first click to other parts of the UI is eaten.
230 SetMouseHandler(NULL
);
232 // Set the state back to normal after the drop down menu is closed.
233 if (state_
!= STATE_DISABLED
)
234 SetState(STATE_NORMAL
);