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/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 ui::ThemeProvider* provider = GetThemeProvider(); | 79 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() { | |
| 92 LabelButton::Layout(); | |
| 93 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | |
| 94 } | |
| 95 | |
| 96 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { | 88 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
| 97 if (enabled() && ShouldShowMenu() && | 89 if (enabled() && ShouldShowMenu() && |
| 98 IsTriggerableEvent(event) && HitTestPoint(event.location())) { | 90 IsTriggerableEvent(event) && HitTestPoint(event.location())) { |
| 99 // Store the y pos of the mouse coordinates so we can use them later to | 91 // Store the y pos of the mouse coordinates so we can use them later to |
| 100 // determine if the user dragged the mouse down (which should pop up the | 92 // determine if the user dragged the mouse down (which should pop up the |
| 101 // drag down menu immediately, instead of waiting for the timer) | 93 // drag down menu immediately, instead of waiting for the timer) |
| 102 y_position_on_lbuttondown_ = event.y(); | 94 y_position_on_lbuttondown_ = event.y(); |
| 103 | 95 |
| 104 // Schedule a task that will show the menu. | 96 // Schedule a task that will show the menu. |
| 105 const int kMenuTimerDelay = 500; | 97 const int kMenuTimerDelay = 500; |
| 106 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 98 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 107 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, | 99 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
| 108 show_menu_factory_.GetWeakPtr(), | 100 show_menu_factory_.GetWeakPtr(), |
| 109 ui::GetMenuSourceTypeForEvent(event)), | 101 ui::GetMenuSourceTypeForEvent(event)), |
| 110 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 102 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
| 111 } | 103 } |
| 112 | 104 |
| 113 // views::Button actions are only triggered by left and middle mouse clicks. | 105 // views::Button actions are only triggered by left and middle mouse clicks. |
| 114 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) { | 106 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) { |
| 115 ink_drop_animation_controller_->AnimateToState( | 107 ink_drop_delegate()->OnAction(views::InkDropState::ACTION_PENDING); |
|
varkha
2015/11/27 22:07:50
Is there enough information in CustomButton to dri
bruthig
2015/12/07 18:44:46
Done.
| |
| 116 views::InkDropState::ACTION_PENDING); | |
| 117 } | 108 } |
| 118 | 109 |
| 119 return LabelButton::OnMousePressed(event); | 110 return LabelButton::OnMousePressed(event); |
| 120 } | 111 } |
| 121 | 112 |
| 122 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 113 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
| 123 bool result = LabelButton::OnMouseDragged(event); | 114 bool result = LabelButton::OnMouseDragged(event); |
| 124 | 115 |
| 125 if (show_menu_factory_.HasWeakPtrs()) { | 116 if (show_menu_factory_.HasWeakPtrs()) { |
| 126 // If the mouse is dragged to a y position lower than where it was when | 117 // If the mouse is dragged to a y position lower than where it was when |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 138 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { | 129 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { |
| 139 if (IsTriggerableEvent(event) || | 130 if (IsTriggerableEvent(event) || |
| 140 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { | 131 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { |
| 141 LabelButton::OnMouseReleased(event); | 132 LabelButton::OnMouseReleased(event); |
| 142 } | 133 } |
| 143 | 134 |
| 144 if (IsTriggerableEvent(event)) | 135 if (IsTriggerableEvent(event)) |
| 145 show_menu_factory_.InvalidateWeakPtrs(); | 136 show_menu_factory_.InvalidateWeakPtrs(); |
| 146 | 137 |
| 147 if (!HitTestPoint(event.location())) | 138 if (!HitTestPoint(event.location())) |
| 148 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | 139 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); |
|
varkha
2015/11/27 22:07:50
I think this will move OnClickCanceled with https:
bruthig
2015/12/07 18:44:46
Done.
| |
| 149 } | 140 } |
| 150 | 141 |
| 151 void ToolbarButton::OnMouseCaptureLost() { | 142 void ToolbarButton::OnMouseCaptureLost() { |
| 152 } | 143 } |
| 153 | 144 |
| 154 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { | 145 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { |
| 155 // Starting a drag results in a MouseExited, we need to ignore it. | 146 // Starting a drag results in a MouseExited, we need to ignore it. |
| 156 // A right click release triggers an exit event. We want to | 147 // A right click release triggers an exit event. We want to |
| 157 // remain in a PUSHED state until the drop down menu closes. | 148 // remain in a PUSHED state until the drop down menu closes. |
| 158 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) | 149 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) |
| 159 SetState(STATE_NORMAL); | 150 SetState(STATE_NORMAL); |
| 160 } | 151 } |
| 161 | 152 |
| 162 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { | 153 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
| 163 if (menu_showing_) { | 154 if (menu_showing_) { |
| 164 // While dropdown menu is showing the button should not handle gestures. | 155 // While dropdown menu is showing the button should not handle gestures. |
| 165 event->StopPropagation(); | 156 event->StopPropagation(); |
| 166 return; | 157 return; |
| 167 } | 158 } |
| 168 | 159 |
| 169 LabelButton::OnGestureEvent(event); | 160 LabelButton::OnGestureEvent(event); |
| 170 | |
| 171 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | |
| 172 switch (event->type()) { | |
| 173 case ui::ET_GESTURE_TAP_DOWN: | |
| 174 ink_drop_state = views::InkDropState::ACTION_PENDING; | |
| 175 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | |
| 176 // subsequent events for the gesture are sent to |this|. | |
| 177 event->SetHandled(); | |
| 178 break; | |
| 179 case ui::ET_GESTURE_LONG_PRESS: | |
| 180 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING; | |
| 181 break; | |
| 182 case ui::ET_GESTURE_TAP: | |
| 183 ink_drop_state = views::InkDropState::QUICK_ACTION; | |
| 184 break; | |
| 185 case ui::ET_GESTURE_LONG_TAP: | |
| 186 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
| 187 break; | |
| 188 case ui::ET_GESTURE_END: | |
| 189 case ui::ET_GESTURE_TAP_CANCEL: | |
| 190 ink_drop_state = views::InkDropState::HIDDEN; | |
| 191 break; | |
| 192 default: | |
| 193 return; | |
| 194 } | |
| 195 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | |
| 196 } | 161 } |
| 197 | 162 |
| 198 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { | 163 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { |
| 199 CustomButton::GetAccessibleState(state); | 164 CustomButton::GetAccessibleState(state); |
| 200 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; | 165 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; |
| 201 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 166 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
| 202 state->AddStateFlag(ui::AX_STATE_HASPOPUP); | 167 state->AddStateFlag(ui::AX_STATE_HASPOPUP); |
| 203 } | 168 } |
| 204 | 169 |
| 205 scoped_ptr<views::LabelButtonBorder> | 170 scoped_ptr<views::LabelButtonBorder> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 | 213 |
| 249 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | 214 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
| 250 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 215 // Enter PUSHED state on press with Left or Right mouse button or on taps. |
| 251 // Remain in this state while the context menu is open. | 216 // Remain in this state while the context menu is open. |
| 252 return event.type() == ui::ET_GESTURE_TAP || | 217 return event.type() == ui::ET_GESTURE_TAP || |
| 253 event.type() == ui::ET_GESTURE_TAP_DOWN || | 218 event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 254 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 219 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
| 255 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 220 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
| 256 } | 221 } |
| 257 | 222 |
| 258 void ToolbarButton::NotifyClick(const ui::Event& event) { | |
| 259 LabelButton::NotifyClick(event); | |
| 260 ink_drop_animation_controller_->AnimateToState( | |
| 261 views::InkDropState::QUICK_ACTION); | |
| 262 } | |
| 263 | |
| 264 bool ToolbarButton::ShouldShowMenu() { | 223 bool ToolbarButton::ShouldShowMenu() { |
| 265 return model_ != nullptr; | 224 return model_ != nullptr; |
| 266 } | 225 } |
| 267 | 226 |
| 268 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { | 227 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
| 269 if (!ShouldShowMenu()) | 228 if (!ShouldShowMenu()) |
| 270 return; | 229 return; |
| 271 | 230 |
| 272 gfx::Rect lb = GetLocalBounds(); | 231 gfx::Rect lb = GetLocalBounds(); |
| 273 | 232 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 300 int left_bound = display.bounds().x(); | 259 int left_bound = display.bounds().x(); |
| 301 #endif | 260 #endif |
| 302 if (menu_position.x() < left_bound) | 261 if (menu_position.x() < left_bound) |
| 303 menu_position.set_x(left_bound); | 262 menu_position.set_x(left_bound); |
| 304 | 263 |
| 305 // Make the button look depressed while the menu is open. | 264 // Make the button look depressed while the menu is open. |
| 306 SetState(STATE_PRESSED); | 265 SetState(STATE_PRESSED); |
| 307 | 266 |
| 308 menu_showing_ = true; | 267 menu_showing_ = true; |
| 309 | 268 |
| 310 ink_drop_animation_controller_->AnimateToState( | 269 ink_drop_delegate()->OnAction(views::InkDropState::ACTIVATED); |
| 311 views::InkDropState::ACTIVATED); | |
| 312 | 270 |
| 313 // Create and run menu. Display an empty menu if model is NULL. | 271 // Create and run menu. Display an empty menu if model is NULL. |
| 314 views::MenuRunner::RunResult result; | 272 views::MenuRunner::RunResult result; |
| 315 if (model_.get()) { | 273 if (model_.get()) { |
| 316 views::MenuModelAdapter menu_delegate(model_.get()); | 274 views::MenuModelAdapter menu_delegate(model_.get()); |
| 317 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 275 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
| 318 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), | 276 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), |
| 319 views::MenuRunner::HAS_MNEMONICS)); | 277 views::MenuRunner::HAS_MNEMONICS)); |
| 320 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, | 278 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 321 gfx::Rect(menu_position, gfx::Size(0, 0)), | 279 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 322 views::MENU_ANCHOR_TOPLEFT, source_type); | 280 views::MENU_ANCHOR_TOPLEFT, source_type); |
| 323 } else { | 281 } else { |
| 324 views::MenuDelegate menu_delegate; | 282 views::MenuDelegate menu_delegate; |
| 325 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); | 283 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); |
| 326 menu_runner_.reset( | 284 menu_runner_.reset( |
| 327 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); | 285 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); |
| 328 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, | 286 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 329 gfx::Rect(menu_position, gfx::Size(0, 0)), | 287 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 330 views::MENU_ANCHOR_TOPLEFT, source_type); | 288 views::MENU_ANCHOR_TOPLEFT, source_type); |
| 331 } | 289 } |
| 332 if (result == views::MenuRunner::MENU_DELETED) | 290 if (result == views::MenuRunner::MENU_DELETED) |
| 333 return; | 291 return; |
| 334 | 292 |
| 335 ink_drop_animation_controller_->AnimateToState( | 293 ink_drop_delegate()->OnAction(views::InkDropState::DEACTIVATED); |
| 336 views::InkDropState::DEACTIVATED); | |
| 337 | 294 |
| 338 menu_showing_ = false; | 295 menu_showing_ = false; |
| 339 | 296 |
| 340 // Need to explicitly clear mouse handler so that events get sent | 297 // Need to explicitly clear mouse handler so that events get sent |
| 341 // properly after the menu finishes running. If we don't do this, then | 298 // properly after the menu finishes running. If we don't do this, then |
| 342 // the first click to other parts of the UI is eaten. | 299 // the first click to other parts of the UI is eaten. |
| 343 SetMouseHandler(nullptr); | 300 SetMouseHandler(nullptr); |
| 344 | 301 |
| 345 // Set the state back to normal after the drop down menu is closed. | 302 // Set the state back to normal after the drop down menu is closed. |
| 346 if (state_ != STATE_DISABLED) | 303 if (state_ != STATE_DISABLED) |
| 347 SetState(STATE_NORMAL); | 304 SetState(STATE_NORMAL); |
| 348 } | 305 } |
| 349 | 306 |
| 350 const char* ToolbarButton::GetClassName() const { | 307 const char* ToolbarButton::GetClassName() const { |
| 351 return "ToolbarButton"; | 308 return "ToolbarButton"; |
| 352 } | 309 } |
| 353 | 310 |
| 354 gfx::Point ToolbarButton::CalculateInkDropCenter() const { | 311 gfx::Point ToolbarButton::CalculateInkDropCenter() const { |
| 355 return GetLocalBounds().CenterPoint(); | 312 return GetLocalBounds().CenterPoint(); |
| 356 } | 313 } |
| OLD | NEW |