| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 | 247 |
| 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 |
| 257 void ToolbarButton::NotifyClick(const ui::Event& event) { |
| 258 LabelButton::NotifyClick(event); |
| 259 ink_drop_animation_controller_->AnimateToState( |
| 260 views::InkDropState::QUICK_ACTION); |
| 261 } |
| 262 |
| 239 bool ToolbarButton::ShouldShowMenu() { | 263 bool ToolbarButton::ShouldShowMenu() { |
| 240 return model_ != NULL; | 264 return model_ != nullptr; |
| 241 } | 265 } |
| 242 | 266 |
| 243 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { | 267 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
| 244 if (!ShouldShowMenu()) | 268 if (!ShouldShowMenu()) |
| 245 return; | 269 return; |
| 246 | 270 |
| 247 gfx::Rect lb = GetLocalBounds(); | 271 gfx::Rect lb = GetLocalBounds(); |
| 248 | 272 |
| 249 // Both the menu position and the menu anchor type change if the UI layout | 273 // Both the menu position and the menu anchor type change if the UI layout |
| 250 // is right-to-left. | 274 // is right-to-left. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 275 int left_bound = display.bounds().x(); | 299 int left_bound = display.bounds().x(); |
| 276 #endif | 300 #endif |
| 277 if (menu_position.x() < left_bound) | 301 if (menu_position.x() < left_bound) |
| 278 menu_position.set_x(left_bound); | 302 menu_position.set_x(left_bound); |
| 279 | 303 |
| 280 // Make the button look depressed while the menu is open. | 304 // Make the button look depressed while the menu is open. |
| 281 SetState(STATE_PRESSED); | 305 SetState(STATE_PRESSED); |
| 282 | 306 |
| 283 menu_showing_ = true; | 307 menu_showing_ = true; |
| 284 | 308 |
| 309 ink_drop_animation_controller_->AnimateToState( |
| 310 views::InkDropState::ACTIVATED); |
| 311 |
| 285 // Create and run menu. Display an empty menu if model is NULL. | 312 // Create and run menu. Display an empty menu if model is NULL. |
| 313 views::MenuRunner::RunResult result; |
| 286 if (model_.get()) { | 314 if (model_.get()) { |
| 287 views::MenuModelAdapter menu_delegate(model_.get()); | 315 views::MenuModelAdapter menu_delegate(model_.get()); |
| 288 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 316 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
| 289 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), | 317 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), |
| 290 views::MenuRunner::HAS_MNEMONICS)); | 318 views::MenuRunner::HAS_MNEMONICS)); |
| 291 views::MenuRunner::RunResult result = | 319 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 292 menu_runner_->RunMenuAt(GetWidget(), | 320 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 293 NULL, | 321 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 { | 322 } else { |
| 300 views::MenuDelegate menu_delegate; | 323 views::MenuDelegate menu_delegate; |
| 301 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); | 324 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); |
| 302 menu_runner_.reset( | 325 menu_runner_.reset( |
| 303 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); | 326 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); |
| 304 views::MenuRunner::RunResult result = | 327 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 305 menu_runner_->RunMenuAt(GetWidget(), | 328 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 306 NULL, | 329 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 } | 330 } |
| 331 if (result == views::MenuRunner::MENU_DELETED) |
| 332 return; |
| 313 | 333 |
| 314 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | 334 ink_drop_animation_controller_->AnimateToState( |
| 335 views::InkDropState::DEACTIVATED); |
| 315 | 336 |
| 316 menu_showing_ = false; | 337 menu_showing_ = false; |
| 317 | 338 |
| 318 // Need to explicitly clear mouse handler so that events get sent | 339 // 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 | 340 // 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. | 341 // the first click to other parts of the UI is eaten. |
| 321 SetMouseHandler(NULL); | 342 SetMouseHandler(nullptr); |
| 322 | 343 |
| 323 // Set the state back to normal after the drop down menu is closed. | 344 // Set the state back to normal after the drop down menu is closed. |
| 324 if (state_ != STATE_DISABLED) | 345 if (state_ != STATE_DISABLED) |
| 325 SetState(STATE_NORMAL); | 346 SetState(STATE_NORMAL); |
| 326 } | 347 } |
| 327 | 348 |
| 328 void ToolbarButton::LayoutInkDrop() { | 349 gfx::Point ToolbarButton::CalculateInkDropCenter() const { |
| 329 ink_drop_animation_controller_->SetInkDropSize(size()); | 350 return GetLocalBounds().CenterPoint(); |
| 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 |