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" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 int inset = provider->GetDisplayProperty( | 72 int inset = provider->GetDisplayProperty( |
| 73 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET); | 73 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET); |
| 74 size.Enlarge(2 * inset, 2 * inset); | 74 size.Enlarge(2 * inset, 2 * inset); |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 return size; | 77 return size; |
| 78 } | 78 } |
| 79 | 79 |
| 80 void ToolbarButton::Layout() { | 80 void ToolbarButton::Layout() { |
| 81 LabelButton::Layout(); | 81 LabelButton::Layout(); |
| 82 LayoutInkDrop(); | 82 |
| 83 // Sizes for the the ink drop. | |
| 84 const int kInkDropLargeSize = 32; | |
| 85 const int kInkDropLargeCornerRadius = 5; | |
| 86 const int kInkDropSmallSize = 24; | |
| 87 const int kInkDropSmallCornerRadius = 2; | |
| 88 | |
| 89 ink_drop_animation_controller_->SetInkDropSize( | |
| 90 gfx::Size(kInkDropLargeSize, kInkDropLargeSize), | |
| 91 kInkDropLargeCornerRadius, | |
| 92 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
| 93 kInkDropSmallCornerRadius); | |
| 94 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | |
| 83 } | 95 } |
| 84 | 96 |
| 85 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { | 97 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
| 86 if (enabled() && ShouldShowMenu() && | 98 if (enabled() && ShouldShowMenu() && |
| 87 IsTriggerableEvent(event) && HitTestPoint(event.location())) { | 99 IsTriggerableEvent(event) && HitTestPoint(event.location())) { |
| 88 // Store the y pos of the mouse coordinates so we can use them later to | 100 // Store the y pos of the mouse coordinates so we can use them later to |
| 89 // determine if the user dragged the mouse down (which should pop up the | 101 // determine if the user dragged the mouse down (which should pop up the |
| 90 // drag down menu immediately, instead of waiting for the timer) | 102 // drag down menu immediately, instead of waiting for the timer) |
| 91 y_position_on_lbuttondown_ = event.y(); | 103 y_position_on_lbuttondown_ = event.y(); |
| 92 | 104 |
| 93 // Schedule a task that will show the menu. | 105 // Schedule a task that will show the menu. |
| 94 const int kMenuTimerDelay = 500; | 106 const int kMenuTimerDelay = 500; |
| 95 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 107 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 96 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, | 108 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
| 97 show_menu_factory_.GetWeakPtr(), | 109 show_menu_factory_.GetWeakPtr(), |
| 98 ui::GetMenuSourceTypeForEvent(event)), | 110 ui::GetMenuSourceTypeForEvent(event)), |
| 99 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 111 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
| 100 } | 112 } |
| 101 | 113 |
| 102 ink_drop_animation_controller_->AnimateToState( | 114 // views::Button actions are only triggered by left and middle mouse clicks. |
| 103 views::InkDropState::ACTION_PENDING); | 115 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) { |
| 116 ink_drop_animation_controller_->AnimateToState( | |
| 117 views::InkDropState::ACTION_PENDING); | |
| 118 } | |
| 104 | 119 |
| 105 return LabelButton::OnMousePressed(event); | 120 return LabelButton::OnMousePressed(event); |
| 106 } | 121 } |
| 107 | 122 |
| 108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 123 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
| 109 bool result = LabelButton::OnMouseDragged(event); | 124 bool result = LabelButton::OnMouseDragged(event); |
| 110 | 125 |
| 111 if (show_menu_factory_.HasWeakPtrs()) { | 126 if (show_menu_factory_.HasWeakPtrs()) { |
| 112 // If the mouse is dragged to a y position lower than where it was when | 127 // 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 | 128 // 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 | 170 |
| 156 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | 171 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; |
| 157 switch (event->type()) { | 172 switch (event->type()) { |
| 158 case ui::ET_GESTURE_TAP_DOWN: | 173 case ui::ET_GESTURE_TAP_DOWN: |
| 159 ink_drop_state = views::InkDropState::ACTION_PENDING; | 174 ink_drop_state = views::InkDropState::ACTION_PENDING; |
| 160 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | 175 // 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|. | 176 // subsequent events for the gesture are sent to |this|. |
| 162 event->SetHandled(); | 177 event->SetHandled(); |
| 163 break; | 178 break; |
| 164 case ui::ET_GESTURE_LONG_PRESS: | 179 case ui::ET_GESTURE_LONG_PRESS: |
| 165 ink_drop_state = views::InkDropState::SLOW_ACTION; | 180 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING; |
| 166 break; | 181 break; |
| 167 case ui::ET_GESTURE_TAP: | 182 case ui::ET_GESTURE_TAP: |
| 168 ink_drop_state = views::InkDropState::QUICK_ACTION; | 183 ink_drop_state = views::InkDropState::QUICK_ACTION; |
| 169 break; | 184 break; |
| 185 case ui::ET_GESTURE_LONG_TAP: | |
| 186 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
| 187 break; | |
| 170 case ui::ET_GESTURE_END: | 188 case ui::ET_GESTURE_END: |
| 171 case ui::ET_GESTURE_TAP_CANCEL: | 189 case ui::ET_GESTURE_TAP_CANCEL: |
| 172 ink_drop_state = views::InkDropState::HIDDEN; | 190 ink_drop_state = views::InkDropState::HIDDEN; |
| 173 break; | 191 break; |
| 174 default: | 192 default: |
| 175 return; | 193 return; |
| 176 } | 194 } |
| 177 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | 195 ink_drop_animation_controller_->AnimateToState(ink_drop_state); |
| 178 } | 196 } |
| 179 | 197 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | 248 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
| 231 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 249 // 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. | 250 // Remain in this state while the context menu is open. |
| 233 return event.type() == ui::ET_GESTURE_TAP || | 251 return event.type() == ui::ET_GESTURE_TAP || |
| 234 event.type() == ui::ET_GESTURE_TAP_DOWN || | 252 event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 235 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 253 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
| 236 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 254 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
| 237 } | 255 } |
| 238 | 256 |
| 239 bool ToolbarButton::ShouldShowMenu() { | 257 bool ToolbarButton::ShouldShowMenu() { |
| 240 return model_ != NULL; | 258 return model_ != nullptr; |
| 241 } | 259 } |
| 242 | 260 |
| 243 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { | 261 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
| 244 if (!ShouldShowMenu()) | 262 if (!ShouldShowMenu()) |
| 245 return; | 263 return; |
| 246 | 264 |
| 247 gfx::Rect lb = GetLocalBounds(); | 265 gfx::Rect lb = GetLocalBounds(); |
| 248 | 266 |
| 249 // Both the menu position and the menu anchor type change if the UI layout | 267 // Both the menu position and the menu anchor type change if the UI layout |
| 250 // is right-to-left. | 268 // is right-to-left. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 275 int left_bound = display.bounds().x(); | 293 int left_bound = display.bounds().x(); |
| 276 #endif | 294 #endif |
| 277 if (menu_position.x() < left_bound) | 295 if (menu_position.x() < left_bound) |
| 278 menu_position.set_x(left_bound); | 296 menu_position.set_x(left_bound); |
| 279 | 297 |
| 280 // Make the button look depressed while the menu is open. | 298 // Make the button look depressed while the menu is open. |
| 281 SetState(STATE_PRESSED); | 299 SetState(STATE_PRESSED); |
| 282 | 300 |
| 283 menu_showing_ = true; | 301 menu_showing_ = true; |
| 284 | 302 |
| 303 views::MenuRunner::RunResult result = views::MenuRunner::MENU_DELETED; | |
|
Peter Kasting
2015/09/09 22:50:46
Nit: For clarity, I'd move this line to be between
bruthig
2015/09/10 14:57:38
Done.
| |
| 304 ink_drop_animation_controller_->AnimateToState( | |
| 305 views::InkDropState::ACTIVATED); | |
| 306 | |
| 285 // Create and run menu. Display an empty menu if model is NULL. | 307 // Create and run menu. Display an empty menu if model is NULL. |
| 286 if (model_.get()) { | 308 if (model_.get()) { |
| 287 views::MenuModelAdapter menu_delegate(model_.get()); | 309 views::MenuModelAdapter menu_delegate(model_.get()); |
| 288 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 310 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
| 289 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), | 311 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), |
| 290 views::MenuRunner::HAS_MNEMONICS)); | 312 views::MenuRunner::HAS_MNEMONICS)); |
| 291 views::MenuRunner::RunResult result = | 313 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 292 menu_runner_->RunMenuAt(GetWidget(), | 314 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 293 NULL, | 315 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 { | 316 } else { |
| 300 views::MenuDelegate menu_delegate; | 317 views::MenuDelegate menu_delegate; |
| 301 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); | 318 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); |
| 302 menu_runner_.reset( | 319 menu_runner_.reset( |
| 303 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); | 320 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); |
| 304 views::MenuRunner::RunResult result = | 321 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 305 menu_runner_->RunMenuAt(GetWidget(), | 322 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 306 NULL, | 323 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 } | 324 } |
| 313 | 325 |
| 314 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | 326 // RunMenuAt() will return MENU_DELETED if the |menu_runner_| was destroyed as |
| 327 // a result of the ToolbarButton's destructor being called. | |
|
Peter Kasting
2015/09/09 22:50:46
Nit: You probably don't need this as this ought to
bruthig
2015/09/10 14:57:38
Done.
| |
| 328 if (result == views::MenuRunner::MENU_DELETED) | |
| 329 return; | |
| 330 | |
| 331 ink_drop_animation_controller_->AnimateToState( | |
| 332 views::InkDropState::DEACTIVATED); | |
| 315 | 333 |
| 316 menu_showing_ = false; | 334 menu_showing_ = false; |
| 317 | 335 |
| 318 // Need to explicitly clear mouse handler so that events get sent | 336 // 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 | 337 // 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. | 338 // the first click to other parts of the UI is eaten. |
| 321 SetMouseHandler(NULL); | 339 SetMouseHandler(nullptr); |
| 322 | 340 |
| 323 // Set the state back to normal after the drop down menu is closed. | 341 // Set the state back to normal after the drop down menu is closed. |
| 324 if (state_ != STATE_DISABLED) | 342 if (state_ != STATE_DISABLED) |
| 325 SetState(STATE_NORMAL); | 343 SetState(STATE_NORMAL); |
| 326 } | 344 } |
| 327 | 345 |
| 328 void ToolbarButton::LayoutInkDrop() { | 346 gfx::Point ToolbarButton::CalculateInkDropCenter() const { |
| 329 ink_drop_animation_controller_->SetInkDropSize(size()); | 347 return gfx::Point(width() / 2, height() / 2); |
|
Peter Kasting
2015/09/09 22:50:46
Nit: Or just:
return GetLocalBounds().CenterPoi
bruthig
2015/09/10 14:57:38
Done.
| |
| 348 } | |
| 349 | |
| 350 void ToolbarButton::NotifyClick(const ui::Event& event) { | |
| 351 LabelButton::NotifyClick(event); | |
| 352 ink_drop_animation_controller_->AnimateToState( | |
| 353 views::InkDropState::QUICK_ACTION); | |
| 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 |