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/layout_constants.h" | 11 #include "chrome/browser/ui/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_( | 34 ink_drop_delegate_(new views::ButtonInkDropDelegate(this, this)), |
36 views::InkDropAnimationControllerFactory:: | |
37 CreateInkDropAnimationController(this)), | |
38 show_menu_factory_(this) { | 35 show_menu_factory_(this) { |
| 36 set_ink_drop_delegate(ink_drop_delegate_.get()); |
39 set_context_menu_controller(this); | 37 set_context_menu_controller(this); |
40 | 38 |
41 const int kInkDropLargeSize = 32; | 39 const int kInkDropLargeSize = 32; |
42 const int kInkDropLargeCornerRadius = 5; | 40 const int kInkDropLargeCornerRadius = 5; |
43 const int kInkDropSmallSize = 24; | 41 const int kInkDropSmallSize = 24; |
44 const int kInkDropSmallCornerRadius = 2; | 42 const int kInkDropSmallCornerRadius = 2; |
45 | 43 |
46 ink_drop_animation_controller_->SetInkDropSize( | 44 ink_drop_delegate()->SetInkDropSize( |
47 gfx::Size(kInkDropLargeSize, kInkDropLargeSize), | 45 kInkDropLargeSize, kInkDropLargeCornerRadius, kInkDropSmallSize, |
48 kInkDropLargeCornerRadius, | |
49 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
50 kInkDropSmallCornerRadius); | 46 kInkDropSmallCornerRadius); |
51 } | 47 } |
52 | 48 |
53 ToolbarButton::~ToolbarButton() { | 49 ToolbarButton::~ToolbarButton() { |
| 50 set_ink_drop_delegate(nullptr); |
| 51 ink_drop_delegate_.reset(); |
54 } | 52 } |
55 | 53 |
56 void ToolbarButton::Init() { | 54 void ToolbarButton::Init() { |
57 SetFocusable(false); | 55 SetFocusable(false); |
58 SetAccessibilityFocusable(true); | 56 SetAccessibilityFocusable(true); |
59 image()->EnableCanvasFlippingForRTLUI(true); | 57 image()->EnableCanvasFlippingForRTLUI(true); |
60 } | 58 } |
61 | 59 |
62 void ToolbarButton::ClearPendingMenu() { | 60 void ToolbarButton::ClearPendingMenu() { |
63 show_menu_factory_.InvalidateWeakPtrs(); | 61 show_menu_factory_.InvalidateWeakPtrs(); |
(...skipping 17 matching lines...) Expand all Loading... |
81 if (ui::MaterialDesignController::IsModeMaterial()) { | 79 if (ui::MaterialDesignController::IsModeMaterial()) { |
82 const ui::ThemeProvider* provider = GetThemeProvider(); | 80 const ui::ThemeProvider* provider = GetThemeProvider(); |
83 if (provider) { | 81 if (provider) { |
84 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON)); | 82 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON)); |
85 size.Enlarge(insets.width(), insets.height()); | 83 size.Enlarge(insets.width(), insets.height()); |
86 } | 84 } |
87 } | 85 } |
88 return size; | 86 return size; |
89 } | 87 } |
90 | 88 |
91 void ToolbarButton::Layout() { | 89 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
92 LabelButton::Layout(); | 90 if (IsTriggerableEvent(event) && enabled() && ShouldShowMenu() && |
93 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | 91 HitTestPoint(event.location())) { |
94 } | 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(); |
95 | 96 |
96 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { | 97 // Schedule a task that will show the menu. |
97 if (IsTriggerableEvent(event)) { | 98 const int kMenuTimerDelay = 500; |
98 if (enabled() && ShouldShowMenu() && HitTestPoint(event.location())) { | 99 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
99 // Store the y pos of the mouse coordinates so we can use them later to | 100 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
100 // determine if the user dragged the mouse down (which should pop up the | 101 show_menu_factory_.GetWeakPtr(), |
101 // drag down menu immediately, instead of waiting for the timer) | 102 ui::GetMenuSourceTypeForEvent(event)), |
102 y_position_on_lbuttondown_ = event.y(); | 103 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 } | 104 } |
115 | 105 |
116 return LabelButton::OnMousePressed(event); | 106 return LabelButton::OnMousePressed(event); |
117 } | 107 } |
118 | 108 |
119 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 109 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
120 bool result = LabelButton::OnMouseDragged(event); | 110 bool result = LabelButton::OnMouseDragged(event); |
121 | 111 |
122 if (show_menu_factory_.HasWeakPtrs()) { | 112 if (show_menu_factory_.HasWeakPtrs()) { |
123 // If the mouse is dragged to a y position lower than where it was when | 113 // If the mouse is dragged to a y position lower than where it was when |
(...skipping 30 matching lines...) Expand all Loading... |
154 } | 144 } |
155 | 145 |
156 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { | 146 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
157 if (menu_showing_) { | 147 if (menu_showing_) { |
158 // While dropdown menu is showing the button should not handle gestures. | 148 // While dropdown menu is showing the button should not handle gestures. |
159 event->StopPropagation(); | 149 event->StopPropagation(); |
160 return; | 150 return; |
161 } | 151 } |
162 | 152 |
163 LabelButton::OnGestureEvent(event); | 153 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 } | 154 } |
191 | 155 |
192 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { | 156 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { |
193 CustomButton::GetAccessibleState(state); | 157 CustomButton::GetAccessibleState(state); |
194 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; | 158 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; |
195 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 159 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
196 state->AddStateFlag(ui::AX_STATE_HASPOPUP); | 160 state->AddStateFlag(ui::AX_STATE_HASPOPUP); |
197 } | 161 } |
198 | 162 |
199 scoped_ptr<views::LabelButtonBorder> | 163 scoped_ptr<views::LabelButtonBorder> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 206 |
243 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | 207 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
244 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 208 // 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. | 209 // Remain in this state while the context menu is open. |
246 return event.type() == ui::ET_GESTURE_TAP || | 210 return event.type() == ui::ET_GESTURE_TAP || |
247 event.type() == ui::ET_GESTURE_TAP_DOWN || | 211 event.type() == ui::ET_GESTURE_TAP_DOWN || |
248 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 212 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
249 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 213 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
250 } | 214 } |
251 | 215 |
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() { | 216 bool ToolbarButton::ShouldShowMenu() { |
264 return model_ != nullptr; | 217 return model_ != nullptr; |
265 } | 218 } |
266 | 219 |
267 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { | 220 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
268 if (!ShouldShowMenu()) | 221 if (!ShouldShowMenu()) |
269 return; | 222 return; |
270 | 223 |
271 gfx::Rect lb = GetLocalBounds(); | 224 gfx::Rect lb = GetLocalBounds(); |
272 | 225 |
(...skipping 26 matching lines...) Expand all Loading... |
299 int left_bound = display.bounds().x(); | 252 int left_bound = display.bounds().x(); |
300 #endif | 253 #endif |
301 if (menu_position.x() < left_bound) | 254 if (menu_position.x() < left_bound) |
302 menu_position.set_x(left_bound); | 255 menu_position.set_x(left_bound); |
303 | 256 |
304 // Make the button look depressed while the menu is open. | 257 // Make the button look depressed while the menu is open. |
305 SetState(STATE_PRESSED); | 258 SetState(STATE_PRESSED); |
306 | 259 |
307 menu_showing_ = true; | 260 menu_showing_ = true; |
308 | 261 |
309 ink_drop_animation_controller_->AnimateToState( | 262 ink_drop_delegate()->OnAction(views::InkDropState::ACTIVATED); |
310 views::InkDropState::ACTIVATED); | |
311 | 263 |
312 // Create and run menu. Display an empty menu if model is NULL. | 264 // Create and run menu. Display an empty menu if model is NULL. |
313 views::MenuRunner::RunResult result; | 265 views::MenuRunner::RunResult result; |
314 if (model_.get()) { | 266 if (model_.get()) { |
315 views::MenuModelAdapter menu_delegate(model_.get()); | 267 views::MenuModelAdapter menu_delegate(model_.get()); |
316 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 268 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
317 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), | 269 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), |
318 views::MenuRunner::HAS_MNEMONICS)); | 270 views::MenuRunner::HAS_MNEMONICS)); |
319 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, | 271 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
320 gfx::Rect(menu_position, gfx::Size(0, 0)), | 272 gfx::Rect(menu_position, gfx::Size(0, 0)), |
321 views::MENU_ANCHOR_TOPLEFT, source_type); | 273 views::MENU_ANCHOR_TOPLEFT, source_type); |
322 } else { | 274 } else { |
323 views::MenuDelegate menu_delegate; | 275 views::MenuDelegate menu_delegate; |
324 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); | 276 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); |
325 menu_runner_.reset( | 277 menu_runner_.reset( |
326 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); | 278 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); |
327 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, | 279 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
328 gfx::Rect(menu_position, gfx::Size(0, 0)), | 280 gfx::Rect(menu_position, gfx::Size(0, 0)), |
329 views::MENU_ANCHOR_TOPLEFT, source_type); | 281 views::MENU_ANCHOR_TOPLEFT, source_type); |
330 } | 282 } |
331 if (result == views::MenuRunner::MENU_DELETED) | 283 if (result == views::MenuRunner::MENU_DELETED) |
332 return; | 284 return; |
333 | 285 |
334 ink_drop_animation_controller_->AnimateToState( | 286 ink_drop_delegate()->OnAction(views::InkDropState::DEACTIVATED); |
335 views::InkDropState::DEACTIVATED); | |
336 | 287 |
337 menu_showing_ = false; | 288 menu_showing_ = false; |
338 | 289 |
339 // Need to explicitly clear mouse handler so that events get sent | 290 // 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 | 291 // 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. | 292 // the first click to other parts of the UI is eaten. |
342 SetMouseHandler(nullptr); | 293 SetMouseHandler(nullptr); |
343 | 294 |
344 // Set the state back to normal after the drop down menu is closed. | 295 // Set the state back to normal after the drop down menu is closed. |
345 if (state_ != STATE_DISABLED) | 296 if (state_ != STATE_DISABLED) |
346 SetState(STATE_NORMAL); | 297 SetState(STATE_NORMAL); |
347 } | 298 } |
348 | 299 |
349 const char* ToolbarButton::GetClassName() const { | 300 const char* ToolbarButton::GetClassName() const { |
350 return "ToolbarButton"; | 301 return "ToolbarButton"; |
351 } | 302 } |
352 | 303 |
353 gfx::Point ToolbarButton::CalculateInkDropCenter() const { | 304 gfx::Point ToolbarButton::CalculateInkDropCenter() const { |
354 return GetLocalBounds().CenterPoint(); | 305 return GetLocalBounds().CenterPoint(); |
355 } | 306 } |
OLD | NEW |