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 "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/themes/theme_properties.h"
12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
13 #include "ui/accessibility/ax_view_state.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/models/menu_model.h"
16 #include "ui/base/resource/material_design/material_design_controller.h"
17 #include "ui/base/theme_provider.h"
18 #include "ui/gfx/display.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/strings/grit/ui_strings.h"
21 #include "ui/views/animation/ink_drop_animation_controller.h"
22 #include "ui/views/controls/button/label_button_border.h"
23 #include "ui/views/controls/menu/menu_item_view.h"
24 #include "ui/views/controls/menu/menu_model_adapter.h"
25 #include "ui/views/controls/menu/menu_runner.h"
26 #include "ui/views/widget/widget.h"
28 ToolbarButton::ToolbarButton(views::ButtonListener
* listener
,
30 : views::LabelButton(listener
, base::string16()),
33 y_position_on_lbuttondown_(0),
34 show_menu_factory_(this) {
35 #if defined(OS_CHROMEOS)
36 // The ink drop animation is only targeted at ChromeOS because there is
37 // concern it will conflict with OS level touch feedback in a bad way.
38 if (ui::MaterialDesignController::IsModeMaterial()) {
39 ink_drop_animation_controller_
.reset(
40 new views::InkDropAnimationController(this));
41 layer()->SetFillsBoundsOpaquely(false);
42 image()->SetPaintToLayer(true);
43 image()->SetFillsBoundsOpaquely(false);
45 #endif // defined(OS_CHROMEOS)
47 set_context_menu_controller(this);
50 ToolbarButton::~ToolbarButton() {
53 void ToolbarButton::Init() {
55 SetAccessibilityFocusable(true);
56 image()->EnableCanvasFlippingForRTLUI(true);
59 void ToolbarButton::ClearPendingMenu() {
60 show_menu_factory_
.InvalidateWeakPtrs();
63 bool ToolbarButton::IsMenuShowing() const {
67 gfx::Size
ToolbarButton::GetPreferredSize() const {
68 gfx::Size
size(image()->GetPreferredSize());
69 gfx::Size label_size
= label()->GetPreferredSize();
70 if (label_size
.width() > 0)
71 size
.Enlarge(label_size
.width() + LocationBarView::kItemPadding
, 0);
72 // For non-material assets the entire size of the button is captured in the
73 // image resource. For Material Design the excess whitespace is being removed
74 // from the image assets. Enlarge the button by the theme provided insets.
75 if (ui::MaterialDesignController::IsModeMaterial()) {
76 ui::ThemeProvider
* provider
= GetThemeProvider();
77 if (provider
&& provider
->UsingSystemTheme()) {
78 int inset
= provider
->GetDisplayProperty(
79 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET
);
80 size
.Enlarge(2 * inset
, 2 * inset
);
86 bool ToolbarButton::OnMousePressed(const ui::MouseEvent
& event
) {
87 if (enabled() && ShouldShowMenu() &&
88 IsTriggerableEvent(event
) && HitTestPoint(event
.location())) {
89 // Store the y pos of the mouse coordinates so we can use them later to
90 // determine if the user dragged the mouse down (which should pop up the
91 // drag down menu immediately, instead of waiting for the timer)
92 y_position_on_lbuttondown_
= event
.y();
94 // Schedule a task that will show the menu.
95 const int kMenuTimerDelay
= 500;
96 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
97 FROM_HERE
, base::Bind(&ToolbarButton::ShowDropDownMenu
,
98 show_menu_factory_
.GetWeakPtr(),
99 ui::GetMenuSourceTypeForEvent(event
)),
100 base::TimeDelta::FromMilliseconds(kMenuTimerDelay
));
102 return LabelButton::OnMousePressed(event
);
105 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent
& event
) {
106 bool result
= LabelButton::OnMouseDragged(event
);
108 if (show_menu_factory_
.HasWeakPtrs()) {
109 // If the mouse is dragged to a y position lower than where it was when
110 // clicked then we should not wait for the menu to appear but show
112 if (event
.y() > y_position_on_lbuttondown_
+ GetHorizontalDragThreshold()) {
113 show_menu_factory_
.InvalidateWeakPtrs();
114 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event
));
121 void ToolbarButton::OnMouseReleased(const ui::MouseEvent
& event
) {
122 if (IsTriggerableEvent(event
) ||
123 (event
.IsRightMouseButton() && !HitTestPoint(event
.location()))) {
124 LabelButton::OnMouseReleased(event
);
127 if (IsTriggerableEvent(event
))
128 show_menu_factory_
.InvalidateWeakPtrs();
131 void ToolbarButton::OnMouseCaptureLost() {
134 void ToolbarButton::OnMouseExited(const ui::MouseEvent
& event
) {
135 // Starting a drag results in a MouseExited, we need to ignore it.
136 // A right click release triggers an exit event. We want to
137 // remain in a PUSHED state until the drop down menu closes.
138 if (state_
!= STATE_DISABLED
&& !InDrag() && state_
!= STATE_PRESSED
)
139 SetState(STATE_NORMAL
);
142 void ToolbarButton::OnGestureEvent(ui::GestureEvent
* event
) {
144 // While dropdown menu is showing the button should not handle gestures.
145 event
->StopPropagation();
149 LabelButton::OnGestureEvent(event
);
152 void ToolbarButton::GetAccessibleState(ui::AXViewState
* state
) {
153 CustomButton::GetAccessibleState(state
);
154 state
->role
= ui::AX_ROLE_BUTTON_DROP_DOWN
;
155 state
->default_action
= l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS
);
156 state
->AddStateFlag(ui::AX_STATE_HASPOPUP
);
159 scoped_ptr
<views::LabelButtonBorder
>
160 ToolbarButton::CreateDefaultBorder() const {
161 scoped_ptr
<views::LabelButtonBorder
> border
;
162 if (ui::MaterialDesignController::IsModeMaterial()) {
163 #if defined(OS_CHROMEOS)
164 border
.reset(new views::LabelButtonBorder());
165 border
->set_insets(views::LabelButtonAssetBorder::GetDefaultInsetsForStyle(
166 Button::STYLE_TEXTBUTTON
));
168 scoped_ptr
<views::LabelButtonAssetBorder
> asset_border(
169 new views::LabelButtonAssetBorder(Button::STYLE_TEXTBUTTON
));
170 // The material design spec does not include a visual effect for the
171 // STATE_HOVERED button state so we have to remove the default one added by
172 // LabelButtonAssetBorder.
173 asset_border
->SetPainter(false, Button::STATE_HOVERED
, nullptr);
174 border
= asset_border
.Pass();
177 border
.reset(new views::LabelButtonAssetBorder(Button::STYLE_TEXTBUTTON
));
180 ui::ThemeProvider
* provider
= GetThemeProvider();
181 if (provider
&& provider
->UsingSystemTheme()) {
182 // Theme provided insets.
183 int inset
= provider
->GetDisplayProperty(
184 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET
);
185 border
->set_insets(gfx::Insets(inset
, inset
, inset
, inset
));
188 return border
.Pass();
191 void ToolbarButton::ShowContextMenuForView(View
* source
,
192 const gfx::Point
& point
,
193 ui::MenuSourceType source_type
) {
197 show_menu_factory_
.InvalidateWeakPtrs();
198 ShowDropDownMenu(source_type
);
201 bool ToolbarButton::ShouldEnterPushedState(const ui::Event
& event
) {
202 // Enter PUSHED state on press with Left or Right mouse button or on taps.
203 // Remain in this state while the context menu is open.
204 return event
.type() == ui::ET_GESTURE_TAP
||
205 event
.type() == ui::ET_GESTURE_TAP_DOWN
||
206 (event
.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON
|
207 ui::EF_RIGHT_MOUSE_BUTTON
) & event
.flags()) != 0);
210 bool ToolbarButton::ShouldShowMenu() {
211 return model_
!= NULL
;
214 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type
) {
215 if (!ShouldShowMenu())
218 gfx::Rect lb
= GetLocalBounds();
220 // Both the menu position and the menu anchor type change if the UI layout
222 gfx::Point
menu_position(lb
.origin());
223 menu_position
.Offset(0, lb
.height() - 1);
224 if (base::i18n::IsRTL())
225 menu_position
.Offset(lb
.width() - 1, 0);
227 View::ConvertPointToScreen(this, &menu_position
);
230 int left_bound
= GetSystemMetrics(SM_XVIRTUALSCREEN
);
231 #elif defined(OS_CHROMEOS)
232 // A window won't overlap between displays on ChromeOS.
233 // Use the left bound of the display on which
234 // the menu button exists.
235 gfx::NativeView view
= GetWidget()->GetNativeView();
236 gfx::Display display
= gfx::Screen::GetScreenFor(
237 view
)->GetDisplayNearestWindow(view
);
238 int left_bound
= display
.bounds().x();
240 // The window might be positioned over the edge between two screens. We'll
241 // want to position the dropdown on the screen the mouse cursor is on.
242 gfx::NativeView view
= GetWidget()->GetNativeView();
243 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(view
);
244 gfx::Display display
= screen
->GetDisplayNearestPoint(
245 screen
->GetCursorScreenPoint());
246 int left_bound
= display
.bounds().x();
248 if (menu_position
.x() < left_bound
)
249 menu_position
.set_x(left_bound
);
251 // Make the button look depressed while the menu is open.
252 SetState(STATE_PRESSED
);
254 menu_showing_
= true;
256 // Create and run menu. Display an empty menu if model is NULL.
258 views::MenuModelAdapter
menu_delegate(model_
.get());
259 menu_delegate
.set_triggerable_event_flags(triggerable_event_flags());
260 menu_runner_
.reset(new views::MenuRunner(menu_delegate
.CreateMenu(),
261 views::MenuRunner::HAS_MNEMONICS
));
262 views::MenuRunner::RunResult result
=
263 menu_runner_
->RunMenuAt(GetWidget(),
265 gfx::Rect(menu_position
, gfx::Size(0, 0)),
266 views::MENU_ANCHOR_TOPLEFT
,
268 if (result
== views::MenuRunner::MENU_DELETED
)
271 views::MenuDelegate menu_delegate
;
272 views::MenuItemView
* menu
= new views::MenuItemView(&menu_delegate
);
274 new views::MenuRunner(menu
, views::MenuRunner::HAS_MNEMONICS
));
275 views::MenuRunner::RunResult result
=
276 menu_runner_
->RunMenuAt(GetWidget(),
278 gfx::Rect(menu_position
, gfx::Size(0, 0)),
279 views::MENU_ANCHOR_TOPLEFT
,
281 if (result
== views::MenuRunner::MENU_DELETED
)
285 menu_showing_
= false;
287 // Need to explicitly clear mouse handler so that events get sent
288 // properly after the menu finishes running. If we don't do this, then
289 // the first click to other parts of the UI is eaten.
290 SetMouseHandler(NULL
);
292 // Set the state back to normal after the drop down menu is closed.
293 if (state_
!= STATE_DISABLED
)
294 SetState(STATE_NORMAL
);
297 const char* ToolbarButton::GetClassName() const {
298 return "ToolbarButton";