Create c/b/ui/views/layout_constants.*.
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / toolbar_button.cc
blob46cdc9098b96f711b58dbe3562527aafb6223911
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/ui/views/layout_constants.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/animation/ink_drop_animation_controller_factory.h"
23 #include "ui/views/controls/button/label_button_border.h"
24 #include "ui/views/controls/menu/menu_item_view.h"
25 #include "ui/views/controls/menu/menu_model_adapter.h"
26 #include "ui/views/controls/menu/menu_runner.h"
27 #include "ui/views/widget/widget.h"
29 ToolbarButton::ToolbarButton(views::ButtonListener* listener,
30 ui::MenuModel* model)
31 : views::LabelButton(listener, base::string16()),
32 model_(model),
33 menu_showing_(false),
34 y_position_on_lbuttondown_(0),
35 show_menu_factory_(this) {
36 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory::
37 CreateInkDropAnimationController(this);
38 set_context_menu_controller(this);
41 ToolbarButton::~ToolbarButton() {
44 void ToolbarButton::Init() {
45 SetFocusable(false);
46 SetAccessibilityFocusable(true);
47 image()->EnableCanvasFlippingForRTLUI(true);
50 void ToolbarButton::ClearPendingMenu() {
51 show_menu_factory_.InvalidateWeakPtrs();
54 bool ToolbarButton::IsMenuShowing() const {
55 return menu_showing_;
58 gfx::Size ToolbarButton::GetPreferredSize() const {
59 gfx::Size size(image()->GetPreferredSize());
60 gfx::Size label_size = label()->GetPreferredSize();
61 if (label_size.width() > 0) {
62 size.Enlarge(
63 label_size.width() + GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING),
64 0);
66 // For non-material assets the entire size of the button is captured in the
67 // image resource. For Material Design the excess whitespace is being removed
68 // from the image assets. Enlarge the button by the theme provided insets.
69 if (ui::MaterialDesignController::IsModeMaterial()) {
70 ui::ThemeProvider* provider = GetThemeProvider();
71 if (provider && provider->UsingSystemTheme()) {
72 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON));
73 size.Enlarge(insets.width(), insets.height());
76 return size;
79 void ToolbarButton::Layout() {
80 LabelButton::Layout();
81 LayoutInkDrop();
84 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) {
85 if (enabled() && ShouldShowMenu() &&
86 IsTriggerableEvent(event) && HitTestPoint(event.location())) {
87 // Store the y pos of the mouse coordinates so we can use them later to
88 // determine if the user dragged the mouse down (which should pop up the
89 // drag down menu immediately, instead of waiting for the timer)
90 y_position_on_lbuttondown_ = event.y();
92 // Schedule a task that will show the menu.
93 const int kMenuTimerDelay = 500;
94 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
95 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu,
96 show_menu_factory_.GetWeakPtr(),
97 ui::GetMenuSourceTypeForEvent(event)),
98 base::TimeDelta::FromMilliseconds(kMenuTimerDelay));
101 ink_drop_animation_controller_->AnimateToState(
102 views::InkDropState::ACTION_PENDING);
104 return LabelButton::OnMousePressed(event);
107 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) {
108 bool result = LabelButton::OnMouseDragged(event);
110 if (show_menu_factory_.HasWeakPtrs()) {
111 // If the mouse is dragged to a y position lower than where it was when
112 // clicked then we should not wait for the menu to appear but show
113 // it immediately.
114 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) {
115 show_menu_factory_.InvalidateWeakPtrs();
116 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
120 return result;
123 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) {
124 if (IsTriggerableEvent(event) ||
125 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) {
126 LabelButton::OnMouseReleased(event);
129 if (IsTriggerableEvent(event))
130 show_menu_factory_.InvalidateWeakPtrs();
132 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
135 void ToolbarButton::OnMouseCaptureLost() {
138 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) {
139 // Starting a drag results in a MouseExited, we need to ignore it.
140 // A right click release triggers an exit event. We want to
141 // remain in a PUSHED state until the drop down menu closes.
142 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED)
143 SetState(STATE_NORMAL);
146 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) {
147 if (menu_showing_) {
148 // While dropdown menu is showing the button should not handle gestures.
149 event->StopPropagation();
150 return;
153 LabelButton::OnGestureEvent(event);
155 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN;
156 switch (event->type()) {
157 case ui::ET_GESTURE_TAP_DOWN:
158 ink_drop_state = views::InkDropState::ACTION_PENDING;
159 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that
160 // subsequent events for the gesture are sent to |this|.
161 event->SetHandled();
162 break;
163 case ui::ET_GESTURE_LONG_PRESS:
164 ink_drop_state = views::InkDropState::SLOW_ACTION;
165 break;
166 case ui::ET_GESTURE_TAP:
167 ink_drop_state = views::InkDropState::QUICK_ACTION;
168 break;
169 case ui::ET_GESTURE_END:
170 case ui::ET_GESTURE_TAP_CANCEL:
171 ink_drop_state = views::InkDropState::HIDDEN;
172 break;
173 default:
174 return;
176 ink_drop_animation_controller_->AnimateToState(ink_drop_state);
179 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) {
180 CustomButton::GetAccessibleState(state);
181 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN;
182 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
183 state->AddStateFlag(ui::AX_STATE_HASPOPUP);
186 scoped_ptr<views::LabelButtonBorder>
187 ToolbarButton::CreateDefaultBorder() const {
188 scoped_ptr<views::LabelButtonBorder> border =
189 views::LabelButton::CreateDefaultBorder();
191 ui::ThemeProvider* provider = GetThemeProvider();
192 if (provider && provider->UsingSystemTheme())
193 border->set_insets(GetLayoutInsets(TOOLBAR_BUTTON));
195 return border.Pass();
198 void ToolbarButton::ShowContextMenuForView(View* source,
199 const gfx::Point& point,
200 ui::MenuSourceType source_type) {
201 if (!enabled())
202 return;
204 show_menu_factory_.InvalidateWeakPtrs();
205 ShowDropDownMenu(source_type);
208 void ToolbarButton::AddInkDropLayer(ui::Layer* ink_drop_layer) {
209 SetPaintToLayer(true);
210 image()->SetPaintToLayer(true);
211 image()->SetFillsBoundsOpaquely(false);
213 layer()->Add(ink_drop_layer);
214 layer()->StackAtBottom(ink_drop_layer);
217 void ToolbarButton::RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
218 layer()->Remove(ink_drop_layer);
220 image()->SetFillsBoundsOpaquely(true);
221 image()->SetPaintToLayer(false);
222 SetPaintToLayer(false);
225 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) {
226 // Enter PUSHED state on press with Left or Right mouse button or on taps.
227 // Remain in this state while the context menu is open.
228 return event.type() == ui::ET_GESTURE_TAP ||
229 event.type() == ui::ET_GESTURE_TAP_DOWN ||
230 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON |
231 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0);
234 bool ToolbarButton::ShouldShowMenu() {
235 return model_ != NULL;
238 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
239 if (!ShouldShowMenu())
240 return;
242 gfx::Rect lb = GetLocalBounds();
244 // Both the menu position and the menu anchor type change if the UI layout
245 // is right-to-left.
246 gfx::Point menu_position(lb.origin());
247 menu_position.Offset(0, lb.height() - 1);
248 if (base::i18n::IsRTL())
249 menu_position.Offset(lb.width() - 1, 0);
251 View::ConvertPointToScreen(this, &menu_position);
253 #if defined(OS_WIN)
254 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN);
255 #elif defined(OS_CHROMEOS)
256 // A window won't overlap between displays on ChromeOS.
257 // Use the left bound of the display on which
258 // the menu button exists.
259 gfx::NativeView view = GetWidget()->GetNativeView();
260 gfx::Display display = gfx::Screen::GetScreenFor(
261 view)->GetDisplayNearestWindow(view);
262 int left_bound = display.bounds().x();
263 #else
264 // The window might be positioned over the edge between two screens. We'll
265 // want to position the dropdown on the screen the mouse cursor is on.
266 gfx::NativeView view = GetWidget()->GetNativeView();
267 gfx::Screen* screen = gfx::Screen::GetScreenFor(view);
268 gfx::Display display = screen->GetDisplayNearestPoint(
269 screen->GetCursorScreenPoint());
270 int left_bound = display.bounds().x();
271 #endif
272 if (menu_position.x() < left_bound)
273 menu_position.set_x(left_bound);
275 // Make the button look depressed while the menu is open.
276 SetState(STATE_PRESSED);
278 menu_showing_ = true;
280 // Create and run menu. Display an empty menu if model is NULL.
281 if (model_.get()) {
282 views::MenuModelAdapter menu_delegate(model_.get());
283 menu_delegate.set_triggerable_event_flags(triggerable_event_flags());
284 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(),
285 views::MenuRunner::HAS_MNEMONICS));
286 views::MenuRunner::RunResult result =
287 menu_runner_->RunMenuAt(GetWidget(),
288 NULL,
289 gfx::Rect(menu_position, gfx::Size(0, 0)),
290 views::MENU_ANCHOR_TOPLEFT,
291 source_type);
292 if (result == views::MenuRunner::MENU_DELETED)
293 return;
294 } else {
295 views::MenuDelegate menu_delegate;
296 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate);
297 menu_runner_.reset(
298 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS));
299 views::MenuRunner::RunResult result =
300 menu_runner_->RunMenuAt(GetWidget(),
301 NULL,
302 gfx::Rect(menu_position, gfx::Size(0, 0)),
303 views::MENU_ANCHOR_TOPLEFT,
304 source_type);
305 if (result == views::MenuRunner::MENU_DELETED)
306 return;
309 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
311 menu_showing_ = false;
313 // Need to explicitly clear mouse handler so that events get sent
314 // properly after the menu finishes running. If we don't do this, then
315 // the first click to other parts of the UI is eaten.
316 SetMouseHandler(NULL);
318 // Set the state back to normal after the drop down menu is closed.
319 if (state_ != STATE_DISABLED)
320 SetState(STATE_NORMAL);
323 void ToolbarButton::LayoutInkDrop() {
324 ink_drop_animation_controller_->SetInkDropSize(size());
327 const char* ToolbarButton::GetClassName() const {
328 return "ToolbarButton";