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 // TODO(bruthig): Get the constants from the theme provider. | |
| 30 // Sizes for the the ink drop. | |
| 31 const int kInkDropLargeSize = 32; | |
| 32 const int kInkDropLargeCornerRadius = 5; | |
| 33 const int kInkDropSmallSize = 24; | |
| 34 const int kInkDropSmallCornerRadius = 2; | |
| 35 | |
| 29 ToolbarButton::ToolbarButton(views::ButtonListener* listener, | 36 ToolbarButton::ToolbarButton(views::ButtonListener* listener, |
| 30 ui::MenuModel* model) | 37 ui::MenuModel* model) |
| 31 : views::LabelButton(listener, base::string16()), | 38 : views::LabelButton(listener, base::string16()), |
| 32 model_(model), | 39 model_(model), |
| 33 menu_showing_(false), | 40 menu_showing_(false), |
| 34 y_position_on_lbuttondown_(0), | 41 y_position_on_lbuttondown_(0), |
| 35 show_menu_factory_(this) { | 42 show_menu_factory_(this) { |
| 36 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory:: | 43 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory:: |
| 37 CreateInkDropAnimationController(this); | 44 CreateInkDropAnimationController(this); |
| 38 set_context_menu_controller(this); | 45 set_context_menu_controller(this); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 | 99 |
| 93 // Schedule a task that will show the menu. | 100 // Schedule a task that will show the menu. |
| 94 const int kMenuTimerDelay = 500; | 101 const int kMenuTimerDelay = 500; |
| 95 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 102 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 96 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, | 103 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
| 97 show_menu_factory_.GetWeakPtr(), | 104 show_menu_factory_.GetWeakPtr(), |
| 98 ui::GetMenuSourceTypeForEvent(event)), | 105 ui::GetMenuSourceTypeForEvent(event)), |
| 99 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 106 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
| 100 } | 107 } |
| 101 | 108 |
| 102 ink_drop_animation_controller_->AnimateToState( | 109 // Button actions are only triggered by left and middle mouse clicks. |
| 103 views::InkDropState::ACTION_PENDING); | 110 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) { |
| 111 ink_drop_animation_controller_->AnimateToState( | |
| 112 views::InkDropState::ACTION_PENDING); | |
| 113 } | |
| 104 | 114 |
| 105 return LabelButton::OnMousePressed(event); | 115 return LabelButton::OnMousePressed(event); |
| 106 } | 116 } |
| 107 | 117 |
| 108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 118 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
| 109 bool result = LabelButton::OnMouseDragged(event); | 119 bool result = LabelButton::OnMouseDragged(event); |
| 110 | 120 |
| 111 if (show_menu_factory_.HasWeakPtrs()) { | 121 if (show_menu_factory_.HasWeakPtrs()) { |
| 112 // If the mouse is dragged to a y position lower than where it was when | 122 // 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 | 123 // 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 | 165 |
| 156 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | 166 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; |
| 157 switch (event->type()) { | 167 switch (event->type()) { |
| 158 case ui::ET_GESTURE_TAP_DOWN: | 168 case ui::ET_GESTURE_TAP_DOWN: |
| 159 ink_drop_state = views::InkDropState::ACTION_PENDING; | 169 ink_drop_state = views::InkDropState::ACTION_PENDING; |
| 160 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | 170 // 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|. | 171 // subsequent events for the gesture are sent to |this|. |
| 162 event->SetHandled(); | 172 event->SetHandled(); |
| 163 break; | 173 break; |
| 164 case ui::ET_GESTURE_LONG_PRESS: | 174 case ui::ET_GESTURE_LONG_PRESS: |
| 165 ink_drop_state = views::InkDropState::SLOW_ACTION; | 175 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING; |
| 166 break; | 176 break; |
| 167 case ui::ET_GESTURE_TAP: | 177 case ui::ET_GESTURE_TAP: |
| 168 ink_drop_state = views::InkDropState::QUICK_ACTION; | 178 ink_drop_state = views::InkDropState::QUICK_ACTION; |
| 169 break; | 179 break; |
| 180 case ui::ET_GESTURE_LONG_TAP: | |
| 181 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
| 182 break; | |
| 170 case ui::ET_GESTURE_END: | 183 case ui::ET_GESTURE_END: |
| 171 case ui::ET_GESTURE_TAP_CANCEL: | 184 case ui::ET_GESTURE_TAP_CANCEL: |
| 172 ink_drop_state = views::InkDropState::HIDDEN; | 185 ink_drop_state = views::InkDropState::HIDDEN; |
| 173 break; | 186 break; |
| 174 default: | 187 default: |
| 175 return; | 188 return; |
| 176 } | 189 } |
| 177 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | 190 ink_drop_animation_controller_->AnimateToState(ink_drop_state); |
| 178 } | 191 } |
| 179 | 192 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 int left_bound = display.bounds().x(); | 288 int left_bound = display.bounds().x(); |
| 276 #endif | 289 #endif |
| 277 if (menu_position.x() < left_bound) | 290 if (menu_position.x() < left_bound) |
| 278 menu_position.set_x(left_bound); | 291 menu_position.set_x(left_bound); |
| 279 | 292 |
| 280 // Make the button look depressed while the menu is open. | 293 // Make the button look depressed while the menu is open. |
| 281 SetState(STATE_PRESSED); | 294 SetState(STATE_PRESSED); |
| 282 | 295 |
| 283 menu_showing_ = true; | 296 menu_showing_ = true; |
| 284 | 297 |
| 298 views::MenuRunner::RunResult result = views::MenuRunner::MENU_DELETED; | |
| 299 ink_drop_animation_controller_->AnimateToState( | |
| 300 views::InkDropState::ACTIVATED); | |
| 301 | |
| 285 // Create and run menu. Display an empty menu if model is NULL. | 302 // Create and run menu. Display an empty menu if model is NULL. |
| 286 if (model_.get()) { | 303 if (model_.get()) { |
| 287 views::MenuModelAdapter menu_delegate(model_.get()); | 304 views::MenuModelAdapter menu_delegate(model_.get()); |
| 288 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 305 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
| 289 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), | 306 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), |
| 290 views::MenuRunner::HAS_MNEMONICS)); | 307 views::MenuRunner::HAS_MNEMONICS)); |
| 291 views::MenuRunner::RunResult result = | 308 result = menu_runner_->RunMenuAt(GetWidget(), NULL, |
|
jonross
2015/08/21 15:14:48
nit: nullptr
bruthig
2015/09/03 21:21:04
Done.
| |
| 292 menu_runner_->RunMenuAt(GetWidget(), | 309 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 293 NULL, | 310 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 { | 311 } else { |
| 300 views::MenuDelegate menu_delegate; | 312 views::MenuDelegate menu_delegate; |
| 301 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); | 313 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); |
| 302 menu_runner_.reset( | 314 menu_runner_.reset( |
| 303 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); | 315 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); |
| 304 views::MenuRunner::RunResult result = | 316 result = menu_runner_->RunMenuAt(GetWidget(), NULL, |
| 305 menu_runner_->RunMenuAt(GetWidget(), | 317 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 306 NULL, | 318 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 } | 319 } |
| 313 | 320 |
| 314 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | 321 ink_drop_animation_controller_->AnimateToState( |
| 322 views::InkDropState::DEACTIVATED); | |
| 323 if (result == views::MenuRunner::MENU_DELETED) | |
| 324 return; | |
| 315 | 325 |
| 316 menu_showing_ = false; | 326 menu_showing_ = false; |
| 317 | 327 |
| 318 // Need to explicitly clear mouse handler so that events get sent | 328 // 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 | 329 // 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. | 330 // the first click to other parts of the UI is eaten. |
| 321 SetMouseHandler(NULL); | 331 SetMouseHandler(NULL); |
| 322 | 332 |
| 323 // Set the state back to normal after the drop down menu is closed. | 333 // Set the state back to normal after the drop down menu is closed. |
| 324 if (state_ != STATE_DISABLED) | 334 if (state_ != STATE_DISABLED) |
| 325 SetState(STATE_NORMAL); | 335 SetState(STATE_NORMAL); |
| 326 } | 336 } |
| 327 | 337 |
| 328 void ToolbarButton::LayoutInkDrop() { | 338 void ToolbarButton::LayoutInkDrop() { |
| 329 ink_drop_animation_controller_->SetInkDropSize(size()); | 339 // TODO(bruthig): Get the constants from the theme provider. |
| 340 gfx::Size ink_drop_size = gfx::Size(kInkDropLargeSize, kInkDropLargeSize); | |
| 341 ink_drop_animation_controller_->SetInkDropSize( | |
| 342 ink_drop_size, kInkDropLargeCornerRadius, | |
| 343 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
| 344 kInkDropSmallCornerRadius); | |
| 345 ink_drop_animation_controller_->SetInkDropOrigin( | |
| 346 gfx::Point((width() - ink_drop_size.width()) / 2, | |
| 347 (height() - ink_drop_size.height()) / 2)); | |
| 348 } | |
| 349 | |
| 350 void ToolbarButton::NotifyClick(const ui::Event& event) { | |
| 351 ink_drop_animation_controller_->AnimateToState( | |
| 352 views::InkDropState::QUICK_ACTION); | |
| 353 LabelButton::NotifyClick(event); | |
| 330 } | 354 } |
| 331 | 355 |
| 332 const char* ToolbarButton::GetClassName() const { | 356 const char* ToolbarButton::GetClassName() const { |
| 333 return "ToolbarButton"; | 357 return "ToolbarButton"; |
| 334 } | 358 } |
| OLD | NEW |