OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" | 5 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
11 #include "chrome/browser/ui/views/layout_constants.h" | 11 #include "chrome/browser/ui/views/layout_constants.h" |
12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
13 #include "ui/accessibility/ax_view_state.h" | 13 #include "ui/accessibility/ax_view_state.h" |
14 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
15 #include "ui/base/models/menu_model.h" | 15 #include "ui/base/models/menu_model.h" |
16 #include "ui/base/resource/material_design/material_design_controller.h" | 16 #include "ui/base/resource/material_design/material_design_controller.h" |
17 #include "ui/base/theme_provider.h" | 17 #include "ui/base/theme_provider.h" |
18 #include "ui/gfx/display.h" | 18 #include "ui/gfx/display.h" |
19 #include "ui/gfx/screen.h" | 19 #include "ui/gfx/screen.h" |
20 #include "ui/strings/grit/ui_strings.h" | 20 #include "ui/strings/grit/ui_strings.h" |
21 #include "ui/views/animation/ink_drop_animation_controller.h" | 21 #include "ui/views/animation/button_ink_drop_delegate.h" |
22 #include "ui/views/animation/ink_drop_animation_controller_factory.h" | |
23 #include "ui/views/controls/button/label_button_border.h" | 22 #include "ui/views/controls/button/label_button_border.h" |
24 #include "ui/views/controls/menu/menu_item_view.h" | 23 #include "ui/views/controls/menu/menu_item_view.h" |
25 #include "ui/views/controls/menu/menu_model_adapter.h" | 24 #include "ui/views/controls/menu/menu_model_adapter.h" |
26 #include "ui/views/controls/menu/menu_runner.h" | 25 #include "ui/views/controls/menu/menu_runner.h" |
27 #include "ui/views/widget/widget.h" | 26 #include "ui/views/widget/widget.h" |
28 | 27 |
29 ToolbarButton::ToolbarButton(views::ButtonListener* listener, | 28 ToolbarButton::ToolbarButton(views::ButtonListener* listener, |
30 ui::MenuModel* model) | 29 ui::MenuModel* model) |
31 : views::LabelButton(listener, base::string16()), | 30 : views::LabelButton(listener, base::string16()), |
32 model_(model), | 31 model_(model), |
33 menu_showing_(false), | 32 menu_showing_(false), |
34 y_position_on_lbuttondown_(0), | 33 y_position_on_lbuttondown_(0), |
35 ink_drop_animation_controller_( | |
36 views::InkDropAnimationControllerFactory:: | |
37 CreateInkDropAnimationController(this)), | |
38 show_menu_factory_(this) { | 34 show_menu_factory_(this) { |
| 35 scoped_ptr<views::InkDropDelegate> new_ink_drop_delegate( |
| 36 new views::ButtonInkDropDelegate(this, this)); |
| 37 SetInkDropDelegate(new_ink_drop_delegate.Pass()); |
39 set_context_menu_controller(this); | 38 set_context_menu_controller(this); |
40 | 39 |
41 const int kInkDropLargeSize = 32; | 40 const int kInkDropLargeSize = 32; |
42 const int kInkDropLargeCornerRadius = 5; | 41 const int kInkDropLargeCornerRadius = 5; |
43 const int kInkDropSmallSize = 24; | 42 const int kInkDropSmallSize = 24; |
44 const int kInkDropSmallCornerRadius = 2; | 43 const int kInkDropSmallCornerRadius = 2; |
45 | 44 |
46 ink_drop_animation_controller_->SetInkDropSize( | 45 ink_drop_delegate()->SetInkDropSize( |
47 gfx::Size(kInkDropLargeSize, kInkDropLargeSize), | 46 kInkDropLargeSize, kInkDropLargeCornerRadius, kInkDropSmallSize, |
48 kInkDropLargeCornerRadius, | |
49 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
50 kInkDropSmallCornerRadius); | 47 kInkDropSmallCornerRadius); |
51 } | 48 } |
52 | 49 |
53 ToolbarButton::~ToolbarButton() { | 50 ToolbarButton::~ToolbarButton() { |
54 } | 51 } |
55 | 52 |
56 void ToolbarButton::Init() { | 53 void ToolbarButton::Init() { |
57 SetFocusable(false); | 54 SetFocusable(false); |
58 SetAccessibilityFocusable(true); | 55 SetAccessibilityFocusable(true); |
59 image()->EnableCanvasFlippingForRTLUI(true); | 56 image()->EnableCanvasFlippingForRTLUI(true); |
(...skipping 21 matching lines...) Expand all Loading... |
81 if (ui::MaterialDesignController::IsModeMaterial()) { | 78 if (ui::MaterialDesignController::IsModeMaterial()) { |
82 const ui::ThemeProvider* provider = GetThemeProvider(); | 79 const ui::ThemeProvider* provider = GetThemeProvider(); |
83 if (provider) { | 80 if (provider) { |
84 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON)); | 81 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON)); |
85 size.Enlarge(insets.width(), insets.height()); | 82 size.Enlarge(insets.width(), insets.height()); |
86 } | 83 } |
87 } | 84 } |
88 return size; | 85 return size; |
89 } | 86 } |
90 | 87 |
91 void ToolbarButton::Layout() { | 88 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
92 LabelButton::Layout(); | 89 if (IsTriggerableEvent(event) && enabled() && ShouldShowMenu() && |
93 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | 90 HitTestPoint(event.location())) { |
94 } | 91 // Store the y pos of the mouse coordinates so we can use them later to |
| 92 // determine if the user dragged the mouse down (which should pop up the |
| 93 // drag down menu immediately, instead of waiting for the timer) |
| 94 y_position_on_lbuttondown_ = event.y(); |
95 | 95 |
96 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { | 96 // Schedule a task that will show the menu. |
97 if (IsTriggerableEvent(event)) { | 97 const int kMenuTimerDelay = 500; |
98 if (enabled() && ShouldShowMenu() && HitTestPoint(event.location())) { | 98 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
99 // Store the y pos of the mouse coordinates so we can use them later to | 99 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
100 // determine if the user dragged the mouse down (which should pop up the | 100 show_menu_factory_.GetWeakPtr(), |
101 // drag down menu immediately, instead of waiting for the timer) | 101 ui::GetMenuSourceTypeForEvent(event)), |
102 y_position_on_lbuttondown_ = event.y(); | 102 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
103 | |
104 // Schedule a task that will show the menu. | |
105 const int kMenuTimerDelay = 500; | |
106 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
107 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, | |
108 show_menu_factory_.GetWeakPtr(), | |
109 ui::GetMenuSourceTypeForEvent(event)), | |
110 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | |
111 } | |
112 ink_drop_animation_controller_->AnimateToState( | |
113 views::InkDropState::ACTION_PENDING); | |
114 } | 103 } |
115 | 104 |
116 return LabelButton::OnMousePressed(event); | 105 return LabelButton::OnMousePressed(event); |
117 } | 106 } |
118 | 107 |
119 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
120 bool result = LabelButton::OnMouseDragged(event); | 109 bool result = LabelButton::OnMouseDragged(event); |
121 | 110 |
122 if (show_menu_factory_.HasWeakPtrs()) { | 111 if (show_menu_factory_.HasWeakPtrs()) { |
123 // If the mouse is dragged to a y position lower than where it was when | 112 // If the mouse is dragged to a y position lower than where it was when |
(...skipping 30 matching lines...) Expand all Loading... |
154 } | 143 } |
155 | 144 |
156 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { | 145 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
157 if (menu_showing_) { | 146 if (menu_showing_) { |
158 // While dropdown menu is showing the button should not handle gestures. | 147 // While dropdown menu is showing the button should not handle gestures. |
159 event->StopPropagation(); | 148 event->StopPropagation(); |
160 return; | 149 return; |
161 } | 150 } |
162 | 151 |
163 LabelButton::OnGestureEvent(event); | 152 LabelButton::OnGestureEvent(event); |
164 | |
165 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | |
166 switch (event->type()) { | |
167 case ui::ET_GESTURE_TAP_DOWN: | |
168 ink_drop_state = views::InkDropState::ACTION_PENDING; | |
169 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | |
170 // subsequent events for the gesture are sent to |this|. | |
171 event->SetHandled(); | |
172 break; | |
173 case ui::ET_GESTURE_LONG_PRESS: | |
174 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING; | |
175 break; | |
176 case ui::ET_GESTURE_TAP: | |
177 ink_drop_state = views::InkDropState::QUICK_ACTION; | |
178 break; | |
179 case ui::ET_GESTURE_LONG_TAP: | |
180 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
181 break; | |
182 case ui::ET_GESTURE_END: | |
183 case ui::ET_GESTURE_TAP_CANCEL: | |
184 ink_drop_state = views::InkDropState::HIDDEN; | |
185 break; | |
186 default: | |
187 return; | |
188 } | |
189 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | |
190 } | 153 } |
191 | 154 |
192 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { | 155 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { |
193 CustomButton::GetAccessibleState(state); | 156 CustomButton::GetAccessibleState(state); |
194 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; | 157 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; |
195 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 158 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
196 state->AddStateFlag(ui::AX_STATE_HASPOPUP); | 159 state->AddStateFlag(ui::AX_STATE_HASPOPUP); |
197 } | 160 } |
198 | 161 |
199 scoped_ptr<views::LabelButtonBorder> | 162 scoped_ptr<views::LabelButtonBorder> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 205 |
243 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | 206 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
244 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 207 // Enter PUSHED state on press with Left or Right mouse button or on taps. |
245 // Remain in this state while the context menu is open. | 208 // Remain in this state while the context menu is open. |
246 return event.type() == ui::ET_GESTURE_TAP || | 209 return event.type() == ui::ET_GESTURE_TAP || |
247 event.type() == ui::ET_GESTURE_TAP_DOWN || | 210 event.type() == ui::ET_GESTURE_TAP_DOWN || |
248 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 211 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
249 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 212 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
250 } | 213 } |
251 | 214 |
252 void ToolbarButton::NotifyClick(const ui::Event& event) { | |
253 LabelButton::NotifyClick(event); | |
254 ink_drop_animation_controller_->AnimateToState( | |
255 views::InkDropState::QUICK_ACTION); | |
256 } | |
257 | |
258 void ToolbarButton::OnClickCanceled(const ui::Event& event) { | |
259 LabelButton::OnClickCanceled(event); | |
260 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
261 } | |
262 | |
263 bool ToolbarButton::ShouldShowMenu() { | 215 bool ToolbarButton::ShouldShowMenu() { |
264 return model_ != nullptr; | 216 return model_ != nullptr; |
265 } | 217 } |
266 | 218 |
267 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { | 219 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
268 if (!ShouldShowMenu()) | 220 if (!ShouldShowMenu()) |
269 return; | 221 return; |
270 | 222 |
271 gfx::Rect lb = GetLocalBounds(); | 223 gfx::Rect lb = GetLocalBounds(); |
272 | 224 |
(...skipping 26 matching lines...) Expand all Loading... |
299 int left_bound = display.bounds().x(); | 251 int left_bound = display.bounds().x(); |
300 #endif | 252 #endif |
301 if (menu_position.x() < left_bound) | 253 if (menu_position.x() < left_bound) |
302 menu_position.set_x(left_bound); | 254 menu_position.set_x(left_bound); |
303 | 255 |
304 // Make the button look depressed while the menu is open. | 256 // Make the button look depressed while the menu is open. |
305 SetState(STATE_PRESSED); | 257 SetState(STATE_PRESSED); |
306 | 258 |
307 menu_showing_ = true; | 259 menu_showing_ = true; |
308 | 260 |
309 ink_drop_animation_controller_->AnimateToState( | 261 ink_drop_delegate()->OnAction(views::InkDropState::ACTIVATED); |
310 views::InkDropState::ACTIVATED); | |
311 | 262 |
312 // Create and run menu. Display an empty menu if model is NULL. | 263 // Create and run menu. Display an empty menu if model is NULL. |
313 views::MenuRunner::RunResult result; | 264 views::MenuRunner::RunResult result; |
314 if (model_.get()) { | 265 if (model_.get()) { |
315 views::MenuModelAdapter menu_delegate(model_.get()); | 266 views::MenuModelAdapter menu_delegate(model_.get()); |
316 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 267 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
317 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), | 268 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), |
318 views::MenuRunner::HAS_MNEMONICS)); | 269 views::MenuRunner::HAS_MNEMONICS)); |
319 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, | 270 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
320 gfx::Rect(menu_position, gfx::Size(0, 0)), | 271 gfx::Rect(menu_position, gfx::Size(0, 0)), |
321 views::MENU_ANCHOR_TOPLEFT, source_type); | 272 views::MENU_ANCHOR_TOPLEFT, source_type); |
322 } else { | 273 } else { |
323 views::MenuDelegate menu_delegate; | 274 views::MenuDelegate menu_delegate; |
324 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); | 275 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); |
325 menu_runner_.reset( | 276 menu_runner_.reset( |
326 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); | 277 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); |
327 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, | 278 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
328 gfx::Rect(menu_position, gfx::Size(0, 0)), | 279 gfx::Rect(menu_position, gfx::Size(0, 0)), |
329 views::MENU_ANCHOR_TOPLEFT, source_type); | 280 views::MENU_ANCHOR_TOPLEFT, source_type); |
330 } | 281 } |
331 if (result == views::MenuRunner::MENU_DELETED) | 282 if (result == views::MenuRunner::MENU_DELETED) |
332 return; | 283 return; |
333 | 284 |
334 ink_drop_animation_controller_->AnimateToState( | 285 ink_drop_delegate()->OnAction(views::InkDropState::DEACTIVATED); |
335 views::InkDropState::DEACTIVATED); | |
336 | 286 |
337 menu_showing_ = false; | 287 menu_showing_ = false; |
338 | 288 |
339 // Need to explicitly clear mouse handler so that events get sent | 289 // Need to explicitly clear mouse handler so that events get sent |
340 // properly after the menu finishes running. If we don't do this, then | 290 // properly after the menu finishes running. If we don't do this, then |
341 // the first click to other parts of the UI is eaten. | 291 // the first click to other parts of the UI is eaten. |
342 SetMouseHandler(nullptr); | 292 SetMouseHandler(nullptr); |
343 | 293 |
344 // Set the state back to normal after the drop down menu is closed. | 294 // Set the state back to normal after the drop down menu is closed. |
345 if (state_ != STATE_DISABLED) | 295 if (state_ != STATE_DISABLED) |
346 SetState(STATE_NORMAL); | 296 SetState(STATE_NORMAL); |
347 } | 297 } |
348 | 298 |
349 const char* ToolbarButton::GetClassName() const { | 299 const char* ToolbarButton::GetClassName() const { |
350 return "ToolbarButton"; | 300 return "ToolbarButton"; |
351 } | 301 } |
352 | 302 |
353 gfx::Point ToolbarButton::CalculateInkDropCenter() const { | 303 gfx::Point ToolbarButton::CalculateInkDropCenter() const { |
354 return GetLocalBounds().CenterPoint(); | 304 return GetLocalBounds().CenterPoint(); |
355 } | 305 } |
OLD | NEW |