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/base/accessibility/accessible_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() {
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::GetItemPadding(), 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::AccessibleViewState
* state
) {
125 CustomButton::GetAccessibleState(state
);
126 state
->role
= ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN
;
127 state
->default_action
= l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS
);
128 state
->state
= ui::AccessibilityTypes::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();
183 if (menu_position
.x() < left_bound
)
184 menu_position
.set_x(left_bound
);
186 // Make the button look depressed while the menu is open.
187 SetState(STATE_PRESSED
);
189 menu_showing_
= true;
191 // Create and run menu. Display an empty menu if model is NULL.
193 views::MenuModelAdapter
menu_delegate(model_
.get());
194 menu_delegate
.set_triggerable_event_flags(triggerable_event_flags());
195 menu_runner_
.reset(new views::MenuRunner(menu_delegate
.CreateMenu()));
196 views::MenuRunner::RunResult result
=
197 menu_runner_
->RunMenuAt(GetWidget(), NULL
,
198 gfx::Rect(menu_position
, gfx::Size(0, 0)),
199 views::MenuItemView::TOPLEFT
,
201 views::MenuRunner::HAS_MNEMONICS
);
202 if (result
== views::MenuRunner::MENU_DELETED
)
205 views::MenuDelegate menu_delegate
;
206 views::MenuItemView
* menu
= new views::MenuItemView(&menu_delegate
);
207 menu_runner_
.reset(new views::MenuRunner(menu
));
208 views::MenuRunner::RunResult result
=
209 menu_runner_
->RunMenuAt(GetWidget(), NULL
,
210 gfx::Rect(menu_position
, gfx::Size(0, 0)),
211 views::MenuItemView::TOPLEFT
,
213 views::MenuRunner::HAS_MNEMONICS
);
214 if (result
== views::MenuRunner::MENU_DELETED
)
218 menu_showing_
= false;
220 // Need to explicitly clear mouse handler so that events get sent
221 // properly after the menu finishes running. If we don't do this, then
222 // the first click to other parts of the UI is eaten.
223 SetMouseHandler(NULL
);
225 // Set the state back to normal after the drop down menu is closed.
226 if (state_
!= STATE_DISABLED
)
227 SetState(STATE_NORMAL
);