Chromium Code Reviews| 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/themes/theme_properties.h" | 11 #include "chrome/browser/themes/theme_properties.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/ink_drop_animation_controller.h" |
| 22 #include "ui/views/animation/ink_drop_animation_controller_factory.h" | 22 #include "ui/views/animation/ink_drop_animation_controller_factory.h" |
| 23 #include "ui/views/controls/button/label_button_border.h" | 23 #include "ui/views/controls/button/label_button_border.h" |
| 24 #include "ui/views/controls/menu/menu_item_view.h" | 24 #include "ui/views/controls/menu/menu_item_view.h" |
| 25 #include "ui/views/controls/menu/menu_model_adapter.h" | 25 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 26 #include "ui/views/controls/menu/menu_runner.h" | 26 #include "ui/views/controls/menu/menu_runner.h" |
| 27 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
| 28 | 28 |
| 29 // Sizes for the the ink drop. | |
| 30 const int kInkDropLargeSize = 32; | |
|
Peter Kasting
2015/09/03 21:53:06
Nit: Move all these to LayoutInkDrop() since that'
bruthig
2015/09/09 18:00:35
Done.
| |
| 31 const int kInkDropLargeCornerRadius = 5; | |
| 32 const int kInkDropSmallSize = 24; | |
| 33 const int kInkDropSmallCornerRadius = 2; | |
| 34 | |
| 29 ToolbarButton::ToolbarButton(views::ButtonListener* listener, | 35 ToolbarButton::ToolbarButton(views::ButtonListener* listener, |
| 30 ui::MenuModel* model) | 36 ui::MenuModel* model) |
| 31 : views::LabelButton(listener, base::string16()), | 37 : views::LabelButton(listener, base::string16()), |
| 32 model_(model), | 38 model_(model), |
| 33 menu_showing_(false), | 39 menu_showing_(false), |
| 34 y_position_on_lbuttondown_(0), | 40 y_position_on_lbuttondown_(0), |
| 35 show_menu_factory_(this) { | 41 show_menu_factory_(this) { |
| 36 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory:: | 42 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory:: |
| 37 CreateInkDropAnimationController(this); | 43 CreateInkDropAnimationController(this); |
| 38 set_context_menu_controller(this); | 44 set_context_menu_controller(this); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 | 98 |
| 93 // Schedule a task that will show the menu. | 99 // Schedule a task that will show the menu. |
| 94 const int kMenuTimerDelay = 500; | 100 const int kMenuTimerDelay = 500; |
| 95 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 101 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 96 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, | 102 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
| 97 show_menu_factory_.GetWeakPtr(), | 103 show_menu_factory_.GetWeakPtr(), |
| 98 ui::GetMenuSourceTypeForEvent(event)), | 104 ui::GetMenuSourceTypeForEvent(event)), |
| 99 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 105 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
| 100 } | 106 } |
| 101 | 107 |
| 102 ink_drop_animation_controller_->AnimateToState( | 108 // Button actions are only triggered by left and middle mouse clicks. |
|
Peter Kasting
2015/09/03 21:53:06
Today in Chrome I can right-click the back button
bruthig
2015/09/09 18:00:35
No it does not, the menu shown due to a right-clic
| |
| 103 views::InkDropState::ACTION_PENDING); | 109 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) { |
| 110 ink_drop_animation_controller_->AnimateToState( | |
| 111 views::InkDropState::ACTION_PENDING); | |
| 112 } | |
| 104 | 113 |
| 105 return LabelButton::OnMousePressed(event); | 114 return LabelButton::OnMousePressed(event); |
| 106 } | 115 } |
| 107 | 116 |
| 108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 117 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
| 109 bool result = LabelButton::OnMouseDragged(event); | 118 bool result = LabelButton::OnMouseDragged(event); |
| 110 | 119 |
| 111 if (show_menu_factory_.HasWeakPtrs()) { | 120 if (show_menu_factory_.HasWeakPtrs()) { |
| 112 // If the mouse is dragged to a y position lower than where it was when | 121 // 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 | 122 // clicked then we should not wait for the menu to appear but show |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 | 164 |
| 156 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | 165 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; |
| 157 switch (event->type()) { | 166 switch (event->type()) { |
| 158 case ui::ET_GESTURE_TAP_DOWN: | 167 case ui::ET_GESTURE_TAP_DOWN: |
| 159 ink_drop_state = views::InkDropState::ACTION_PENDING; | 168 ink_drop_state = views::InkDropState::ACTION_PENDING; |
| 160 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | 169 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that |
| 161 // subsequent events for the gesture are sent to |this|. | 170 // subsequent events for the gesture are sent to |this|. |
| 162 event->SetHandled(); | 171 event->SetHandled(); |
| 163 break; | 172 break; |
| 164 case ui::ET_GESTURE_LONG_PRESS: | 173 case ui::ET_GESTURE_LONG_PRESS: |
| 165 ink_drop_state = views::InkDropState::SLOW_ACTION; | 174 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING; |
| 166 break; | 175 break; |
| 167 case ui::ET_GESTURE_TAP: | 176 case ui::ET_GESTURE_TAP: |
| 168 ink_drop_state = views::InkDropState::QUICK_ACTION; | 177 ink_drop_state = views::InkDropState::QUICK_ACTION; |
| 169 break; | 178 break; |
| 179 case ui::ET_GESTURE_LONG_TAP: | |
| 180 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
| 181 break; | |
| 170 case ui::ET_GESTURE_END: | 182 case ui::ET_GESTURE_END: |
| 171 case ui::ET_GESTURE_TAP_CANCEL: | 183 case ui::ET_GESTURE_TAP_CANCEL: |
| 172 ink_drop_state = views::InkDropState::HIDDEN; | 184 ink_drop_state = views::InkDropState::HIDDEN; |
| 173 break; | 185 break; |
| 174 default: | 186 default: |
| 175 return; | 187 return; |
| 176 } | 188 } |
| 177 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | 189 ink_drop_animation_controller_->AnimateToState(ink_drop_state); |
| 178 } | 190 } |
| 179 | 191 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | 242 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
| 231 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 243 // Enter PUSHED state on press with Left or Right mouse button or on taps. |
| 232 // Remain in this state while the context menu is open. | 244 // Remain in this state while the context menu is open. |
| 233 return event.type() == ui::ET_GESTURE_TAP || | 245 return event.type() == ui::ET_GESTURE_TAP || |
| 234 event.type() == ui::ET_GESTURE_TAP_DOWN || | 246 event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 235 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 247 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
| 236 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 248 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
| 237 } | 249 } |
| 238 | 250 |
| 239 bool ToolbarButton::ShouldShowMenu() { | 251 bool ToolbarButton::ShouldShowMenu() { |
| 240 return model_ != NULL; | 252 return model_ != nullptr; |
| 241 } | 253 } |
| 242 | 254 |
| 243 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { | 255 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
| 244 if (!ShouldShowMenu()) | 256 if (!ShouldShowMenu()) |
| 245 return; | 257 return; |
| 246 | 258 |
| 247 gfx::Rect lb = GetLocalBounds(); | 259 gfx::Rect lb = GetLocalBounds(); |
| 248 | 260 |
| 249 // Both the menu position and the menu anchor type change if the UI layout | 261 // Both the menu position and the menu anchor type change if the UI layout |
| 250 // is right-to-left. | 262 // is right-to-left. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 275 int left_bound = display.bounds().x(); | 287 int left_bound = display.bounds().x(); |
| 276 #endif | 288 #endif |
| 277 if (menu_position.x() < left_bound) | 289 if (menu_position.x() < left_bound) |
| 278 menu_position.set_x(left_bound); | 290 menu_position.set_x(left_bound); |
| 279 | 291 |
| 280 // Make the button look depressed while the menu is open. | 292 // Make the button look depressed while the menu is open. |
| 281 SetState(STATE_PRESSED); | 293 SetState(STATE_PRESSED); |
| 282 | 294 |
| 283 menu_showing_ = true; | 295 menu_showing_ = true; |
| 284 | 296 |
| 297 views::MenuRunner::RunResult result = views::MenuRunner::MENU_DELETED; | |
| 298 ink_drop_animation_controller_->AnimateToState( | |
| 299 views::InkDropState::ACTIVATED); | |
| 300 | |
| 285 // Create and run menu. Display an empty menu if model is NULL. | 301 // Create and run menu. Display an empty menu if model is NULL. |
| 286 if (model_.get()) { | 302 if (model_.get()) { |
| 287 views::MenuModelAdapter menu_delegate(model_.get()); | 303 views::MenuModelAdapter menu_delegate(model_.get()); |
| 288 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 304 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
| 289 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), | 305 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), |
| 290 views::MenuRunner::HAS_MNEMONICS)); | 306 views::MenuRunner::HAS_MNEMONICS)); |
| 291 views::MenuRunner::RunResult result = | 307 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
|
Peter Kasting
2015/09/03 21:53:06
Is it safe to move this block below the conditiona
bruthig
2015/09/09 18:00:35
Negative, as you suspect |menu_delegate| must be a
| |
| 292 menu_runner_->RunMenuAt(GetWidget(), | 308 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 293 NULL, | 309 views::MENU_ANCHOR_TOPLEFT, source_type); |
| 294 gfx::Rect(menu_position, gfx::Size(0, 0)), | |
| 295 views::MENU_ANCHOR_TOPLEFT, | |
| 296 source_type); | |
| 297 if (result == views::MenuRunner::MENU_DELETED) | |
| 298 return; | |
| 299 } else { | 310 } else { |
| 300 views::MenuDelegate menu_delegate; | 311 views::MenuDelegate menu_delegate; |
| 301 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); | 312 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); |
| 302 menu_runner_.reset( | 313 menu_runner_.reset( |
| 303 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); | 314 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); |
| 304 views::MenuRunner::RunResult result = | 315 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 305 menu_runner_->RunMenuAt(GetWidget(), | 316 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 306 NULL, | 317 views::MENU_ANCHOR_TOPLEFT, source_type); |
| 307 gfx::Rect(menu_position, gfx::Size(0, 0)), | |
| 308 views::MENU_ANCHOR_TOPLEFT, | |
| 309 source_type); | |
| 310 if (result == views::MenuRunner::MENU_DELETED) | |
| 311 return; | |
| 312 } | 318 } |
| 313 | 319 |
| 314 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | 320 ink_drop_animation_controller_->AnimateToState( |
|
Peter Kasting
2015/09/03 21:53:06
This isn't safe if the result is MENU_DELETED, in
bruthig
2015/09/09 18:00:35
Done. I've also added a comment to try and make th
| |
| 321 views::InkDropState::DEACTIVATED); | |
| 322 if (result == views::MenuRunner::MENU_DELETED) | |
| 323 return; | |
| 315 | 324 |
| 316 menu_showing_ = false; | 325 menu_showing_ = false; |
| 317 | 326 |
| 318 // Need to explicitly clear mouse handler so that events get sent | 327 // Need to explicitly clear mouse handler so that events get sent |
| 319 // properly after the menu finishes running. If we don't do this, then | 328 // properly after the menu finishes running. If we don't do this, then |
| 320 // the first click to other parts of the UI is eaten. | 329 // the first click to other parts of the UI is eaten. |
| 321 SetMouseHandler(NULL); | 330 SetMouseHandler(nullptr); |
| 322 | 331 |
| 323 // Set the state back to normal after the drop down menu is closed. | 332 // Set the state back to normal after the drop down menu is closed. |
| 324 if (state_ != STATE_DISABLED) | 333 if (state_ != STATE_DISABLED) |
| 325 SetState(STATE_NORMAL); | 334 SetState(STATE_NORMAL); |
| 326 } | 335 } |
| 327 | 336 |
| 328 void ToolbarButton::LayoutInkDrop() { | 337 void ToolbarButton::LayoutInkDrop() { |
| 329 ink_drop_animation_controller_->SetInkDropSize(size()); | 338 gfx::Size ink_drop_size = gfx::Size(kInkDropLargeSize, kInkDropLargeSize); |
|
Peter Kasting
2015/09/03 21:53:06
Nit: I don't see why you made a temp for this inst
bruthig
2015/09/09 18:00:35
Done. Oversight on my part.
| |
| 339 ink_drop_animation_controller_->SetInkDropSize( | |
| 340 ink_drop_size, kInkDropLargeCornerRadius, | |
| 341 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
| 342 kInkDropSmallCornerRadius); | |
| 343 ink_drop_animation_controller_->SetInkDropCenter( | |
| 344 gfx::Point(width() / 2, height() / 2)); | |
| 345 } | |
| 346 | |
| 347 void ToolbarButton::NotifyClick(const ui::Event& event) { | |
| 348 ink_drop_animation_controller_->AnimateToState( | |
| 349 views::InkDropState::QUICK_ACTION); | |
| 350 LabelButton::NotifyClick(event); | |
| 330 } | 351 } |
| 331 | 352 |
| 332 const char* ToolbarButton::GetClassName() const { | 353 const char* ToolbarButton::GetClassName() const { |
| 333 return "ToolbarButton"; | 354 return "ToolbarButton"; |
| 334 } | 355 } |
| OLD | NEW |