Update LabelButtons in the Toolbar for Material Design
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / toolbar_button.cc
blob45e429b658cf89c1f67c7609cd6fd1629cc0b303
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"
7 #include "base/bind.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,
29 ui::MenuModel* model)
30 : views::LabelButton(listener, base::string16()),
31 model_(model),
32 menu_showing_(false),
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() {
54 SetFocusable(false);
55 SetAccessibilityFocusable(true);
56 image()->EnableCanvasFlippingForRTLUI(true);
59 void ToolbarButton::ClearPendingMenu() {
60 show_menu_factory_.InvalidateWeakPtrs();
63 bool ToolbarButton::IsMenuShowing() const {
64 return menu_showing_;
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 const int horizontal_item_padding = GetThemeProvider()->GetDisplayProperty(
72 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING);
73 size.Enlarge(label_size.width() + horizontal_item_padding, 0);
75 // For non-material assets the entire size of the button is captured in the
76 // image resource. For Material Design the excess whitespace is being removed
77 // from the image assets. Enlarge the button by the theme provided insets.
78 if (ui::MaterialDesignController::IsModeMaterial()) {
79 ui::ThemeProvider* provider = GetThemeProvider();
80 if (provider && provider->UsingSystemTheme()) {
81 int inset = provider->GetDisplayProperty(
82 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET);
83 size.Enlarge(2 * inset, 2 * inset);
86 return size;
89 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) {
90 if (enabled() && ShouldShowMenu() &&
91 IsTriggerableEvent(event) && HitTestPoint(event.location())) {
92 // Store the y pos of the mouse coordinates so we can use them later to
93 // determine if the user dragged the mouse down (which should pop up the
94 // drag down menu immediately, instead of waiting for the timer)
95 y_position_on_lbuttondown_ = event.y();
97 // Schedule a task that will show the menu.
98 const int kMenuTimerDelay = 500;
99 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
100 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu,
101 show_menu_factory_.GetWeakPtr(),
102 ui::GetMenuSourceTypeForEvent(event)),
103 base::TimeDelta::FromMilliseconds(kMenuTimerDelay));
105 return LabelButton::OnMousePressed(event);
108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) {
109 bool result = LabelButton::OnMouseDragged(event);
111 if (show_menu_factory_.HasWeakPtrs()) {
112 // If the mouse is dragged to a y position lower than where it was when
113 // clicked then we should not wait for the menu to appear but show
114 // it immediately.
115 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) {
116 show_menu_factory_.InvalidateWeakPtrs();
117 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
121 return result;
124 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) {
125 if (IsTriggerableEvent(event) ||
126 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) {
127 LabelButton::OnMouseReleased(event);
130 if (IsTriggerableEvent(event))
131 show_menu_factory_.InvalidateWeakPtrs();
134 void ToolbarButton::OnMouseCaptureLost() {
137 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) {
138 // Starting a drag results in a MouseExited, we need to ignore it.
139 // A right click release triggers an exit event. We want to
140 // remain in a PUSHED state until the drop down menu closes.
141 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED)
142 SetState(STATE_NORMAL);
145 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) {
146 if (menu_showing_) {
147 // While dropdown menu is showing the button should not handle gestures.
148 event->StopPropagation();
149 return;
152 LabelButton::OnGestureEvent(event);
155 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) {
156 CustomButton::GetAccessibleState(state);
157 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN;
158 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
159 state->AddStateFlag(ui::AX_STATE_HASPOPUP);
162 scoped_ptr<views::LabelButtonBorder>
163 ToolbarButton::CreateDefaultBorder() const {
164 scoped_ptr<views::LabelButtonBorder> border =
165 views::LabelButton::CreateDefaultBorder();
167 ui::ThemeProvider* provider = GetThemeProvider();
168 if (provider && provider->UsingSystemTheme()) {
169 // Theme provided insets.
170 int inset = provider->GetDisplayProperty(
171 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET);
172 border->set_insets(gfx::Insets(inset, inset, inset, inset));
175 return border.Pass();
178 void ToolbarButton::ShowContextMenuForView(View* source,
179 const gfx::Point& point,
180 ui::MenuSourceType source_type) {
181 if (!enabled())
182 return;
184 show_menu_factory_.InvalidateWeakPtrs();
185 ShowDropDownMenu(source_type);
188 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) {
189 // Enter PUSHED state on press with Left or Right mouse button or on taps.
190 // Remain in this state while the context menu is open.
191 return event.type() == ui::ET_GESTURE_TAP ||
192 event.type() == ui::ET_GESTURE_TAP_DOWN ||
193 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON |
194 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0);
197 bool ToolbarButton::ShouldShowMenu() {
198 return model_ != NULL;
201 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
202 if (!ShouldShowMenu())
203 return;
205 gfx::Rect lb = GetLocalBounds();
207 // Both the menu position and the menu anchor type change if the UI layout
208 // is right-to-left.
209 gfx::Point menu_position(lb.origin());
210 menu_position.Offset(0, lb.height() - 1);
211 if (base::i18n::IsRTL())
212 menu_position.Offset(lb.width() - 1, 0);
214 View::ConvertPointToScreen(this, &menu_position);
216 #if defined(OS_WIN)
217 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN);
218 #elif defined(OS_CHROMEOS)
219 // A window won't overlap between displays on ChromeOS.
220 // Use the left bound of the display on which
221 // the menu button exists.
222 gfx::NativeView view = GetWidget()->GetNativeView();
223 gfx::Display display = gfx::Screen::GetScreenFor(
224 view)->GetDisplayNearestWindow(view);
225 int left_bound = display.bounds().x();
226 #else
227 // The window might be positioned over the edge between two screens. We'll
228 // want to position the dropdown on the screen the mouse cursor is on.
229 gfx::NativeView view = GetWidget()->GetNativeView();
230 gfx::Screen* screen = gfx::Screen::GetScreenFor(view);
231 gfx::Display display = screen->GetDisplayNearestPoint(
232 screen->GetCursorScreenPoint());
233 int left_bound = display.bounds().x();
234 #endif
235 if (menu_position.x() < left_bound)
236 menu_position.set_x(left_bound);
238 // Make the button look depressed while the menu is open.
239 SetState(STATE_PRESSED);
241 menu_showing_ = true;
243 // Create and run menu. Display an empty menu if model is NULL.
244 if (model_.get()) {
245 views::MenuModelAdapter menu_delegate(model_.get());
246 menu_delegate.set_triggerable_event_flags(triggerable_event_flags());
247 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(),
248 views::MenuRunner::HAS_MNEMONICS));
249 views::MenuRunner::RunResult result =
250 menu_runner_->RunMenuAt(GetWidget(),
251 NULL,
252 gfx::Rect(menu_position, gfx::Size(0, 0)),
253 views::MENU_ANCHOR_TOPLEFT,
254 source_type);
255 if (result == views::MenuRunner::MENU_DELETED)
256 return;
257 } else {
258 views::MenuDelegate menu_delegate;
259 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate);
260 menu_runner_.reset(
261 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS));
262 views::MenuRunner::RunResult result =
263 menu_runner_->RunMenuAt(GetWidget(),
264 NULL,
265 gfx::Rect(menu_position, gfx::Size(0, 0)),
266 views::MENU_ANCHOR_TOPLEFT,
267 source_type);
268 if (result == views::MenuRunner::MENU_DELETED)
269 return;
272 menu_showing_ = false;
274 // Need to explicitly clear mouse handler so that events get sent
275 // properly after the menu finishes running. If we don't do this, then
276 // the first click to other parts of the UI is eaten.
277 SetMouseHandler(NULL);
279 // Set the state back to normal after the drop down menu is closed.
280 if (state_ != STATE_DISABLED)
281 SetState(STATE_NORMAL);
284 const char* ToolbarButton::GetClassName() const {
285 return "ToolbarButton";