| 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/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/themes/theme_service.h" | 12 #include "chrome/browser/themes/theme_service.h" |
| 13 #include "chrome/browser/themes/theme_service_factory.h" | 13 #include "chrome/browser/themes/theme_service_factory.h" |
| 14 #include "chrome/browser/ui/layout_constants.h" | 14 #include "chrome/browser/ui/layout_constants.h" |
| 15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 16 #include "ui/accessibility/ax_view_state.h" | 16 #include "ui/accessibility/ax_view_state.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/models/menu_model.h" | 18 #include "ui/base/models/menu_model.h" |
| 19 #include "ui/base/resource/material_design/material_design_controller.h" | 19 #include "ui/base/resource/material_design/material_design_controller.h" |
| 20 #include "ui/base/theme_provider.h" | 20 #include "ui/base/theme_provider.h" |
| 21 #include "ui/gfx/display.h" | 21 #include "ui/gfx/display.h" |
| 22 #include "ui/gfx/screen.h" | 22 #include "ui/gfx/screen.h" |
| 23 #include "ui/strings/grit/ui_strings.h" | 23 #include "ui/strings/grit/ui_strings.h" |
| 24 #include "ui/views/animation/ink_drop_animation_controller.h" | 24 #include "ui/views/animation/button_ink_drop_delegate.h" |
| 25 #include "ui/views/animation/ink_drop_animation_controller_factory.h" | |
| 26 #include "ui/views/controls/button/label_button_border.h" | 25 #include "ui/views/controls/button/label_button_border.h" |
| 27 #include "ui/views/controls/menu/menu_item_view.h" | 26 #include "ui/views/controls/menu/menu_item_view.h" |
| 28 #include "ui/views/controls/menu/menu_model_adapter.h" | 27 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 29 #include "ui/views/controls/menu/menu_runner.h" | 28 #include "ui/views/controls/menu/menu_runner.h" |
| 30 #include "ui/views/widget/widget.h" | 29 #include "ui/views/widget/widget.h" |
| 31 | 30 |
| 32 ToolbarButton::ToolbarButton(Profile* profile, | 31 ToolbarButton::ToolbarButton(Profile* profile, |
| 33 views::ButtonListener* listener, | 32 views::ButtonListener* listener, |
| 34 ui::MenuModel* model) | 33 ui::MenuModel* model) |
| 35 : views::LabelButton(listener, base::string16()), | 34 : views::LabelButton(listener, base::string16()), |
| 36 profile_(profile), | 35 profile_(profile), |
| 37 model_(model), | 36 model_(model), |
| 38 menu_showing_(false), | 37 menu_showing_(false), |
| 39 y_position_on_lbuttondown_(0), | 38 y_position_on_lbuttondown_(0), |
| 40 ink_drop_animation_controller_( | 39 ink_drop_delegate_(new views::ButtonInkDropDelegate(this, this)), |
| 41 views::InkDropAnimationControllerFactory:: | |
| 42 CreateInkDropAnimationController(this)), | |
| 43 show_menu_factory_(this) { | 40 show_menu_factory_(this) { |
| 41 set_ink_drop_delegate(ink_drop_delegate_.get()); |
| 42 set_has_ink_drop_action_on_click(true); |
| 44 set_context_menu_controller(this); | 43 set_context_menu_controller(this); |
| 45 | 44 |
| 46 const int kInkDropLargeSize = 32; | 45 const int kInkDropLargeSize = 32; |
| 47 const int kInkDropLargeCornerRadius = 5; | 46 const int kInkDropLargeCornerRadius = 5; |
| 48 const int kInkDropSmallSize = 24; | 47 const int kInkDropSmallSize = 24; |
| 49 const int kInkDropSmallCornerRadius = 2; | 48 const int kInkDropSmallCornerRadius = 2; |
| 50 | 49 ink_drop_delegate()->SetInkDropSize( |
| 51 ink_drop_animation_controller_->SetInkDropSize( | 50 kInkDropLargeSize, kInkDropLargeCornerRadius, kInkDropSmallSize, |
| 52 gfx::Size(kInkDropLargeSize, kInkDropLargeSize), | |
| 53 kInkDropLargeCornerRadius, | |
| 54 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
| 55 kInkDropSmallCornerRadius); | 51 kInkDropSmallCornerRadius); |
| 56 } | 52 } |
| 57 | 53 |
| 58 ToolbarButton::~ToolbarButton() { | 54 ToolbarButton::~ToolbarButton() {} |
| 59 } | |
| 60 | 55 |
| 61 void ToolbarButton::Init() { | 56 void ToolbarButton::Init() { |
| 62 SetFocusable(false); | 57 SetFocusable(false); |
| 63 SetAccessibilityFocusable(true); | 58 SetAccessibilityFocusable(true); |
| 64 image()->EnableCanvasFlippingForRTLUI(true); | 59 image()->EnableCanvasFlippingForRTLUI(true); |
| 65 } | 60 } |
| 66 | 61 |
| 67 void ToolbarButton::ClearPendingMenu() { | 62 void ToolbarButton::ClearPendingMenu() { |
| 68 show_menu_factory_.InvalidateWeakPtrs(); | 63 show_menu_factory_.InvalidateWeakPtrs(); |
| 69 } | 64 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 86 if (ui::MaterialDesignController::IsModeMaterial()) { | 81 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 87 const ui::ThemeProvider* provider = GetThemeProvider(); | 82 const ui::ThemeProvider* provider = GetThemeProvider(); |
| 88 if (provider) { | 83 if (provider) { |
| 89 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON)); | 84 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON)); |
| 90 size.Enlarge(insets.width(), insets.height()); | 85 size.Enlarge(insets.width(), insets.height()); |
| 91 } | 86 } |
| 92 } | 87 } |
| 93 return size; | 88 return size; |
| 94 } | 89 } |
| 95 | 90 |
| 96 void ToolbarButton::Layout() { | 91 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
| 97 LabelButton::Layout(); | 92 if (IsTriggerableEvent(event) && enabled() && ShouldShowMenu() && |
| 98 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | 93 HitTestPoint(event.location())) { |
| 99 } | 94 // Store the y pos of the mouse coordinates so we can use them later to |
| 95 // determine if the user dragged the mouse down (which should pop up the |
| 96 // drag down menu immediately, instead of waiting for the timer) |
| 97 y_position_on_lbuttondown_ = event.y(); |
| 100 | 98 |
| 101 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { | 99 // Schedule a task that will show the menu. |
| 102 if (IsTriggerableEvent(event)) { | 100 const int kMenuTimerDelay = 500; |
| 103 if (enabled() && ShouldShowMenu() && HitTestPoint(event.location())) { | 101 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 104 // Store the y pos of the mouse coordinates so we can use them later to | 102 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
| 105 // determine if the user dragged the mouse down (which should pop up the | 103 show_menu_factory_.GetWeakPtr(), |
| 106 // drag down menu immediately, instead of waiting for the timer) | 104 ui::GetMenuSourceTypeForEvent(event)), |
| 107 y_position_on_lbuttondown_ = event.y(); | 105 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
| 108 | |
| 109 // Schedule a task that will show the menu. | |
| 110 const int kMenuTimerDelay = 500; | |
| 111 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 112 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, | |
| 113 show_menu_factory_.GetWeakPtr(), | |
| 114 ui::GetMenuSourceTypeForEvent(event)), | |
| 115 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | |
| 116 } | |
| 117 ink_drop_animation_controller_->AnimateToState( | |
| 118 views::InkDropState::ACTION_PENDING); | |
| 119 } | 106 } |
| 120 | 107 |
| 121 return LabelButton::OnMousePressed(event); | 108 return LabelButton::OnMousePressed(event); |
| 122 } | 109 } |
| 123 | 110 |
| 124 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 111 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
| 125 bool result = LabelButton::OnMouseDragged(event); | 112 bool result = LabelButton::OnMouseDragged(event); |
| 126 | 113 |
| 127 if (show_menu_factory_.HasWeakPtrs()) { | 114 if (show_menu_factory_.HasWeakPtrs()) { |
| 128 // If the mouse is dragged to a y position lower than where it was when | 115 // If the mouse is dragged to a y position lower than where it was when |
| (...skipping 30 matching lines...) Expand all Loading... |
| 159 } | 146 } |
| 160 | 147 |
| 161 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { | 148 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
| 162 if (menu_showing_) { | 149 if (menu_showing_) { |
| 163 // While dropdown menu is showing the button should not handle gestures. | 150 // While dropdown menu is showing the button should not handle gestures. |
| 164 event->StopPropagation(); | 151 event->StopPropagation(); |
| 165 return; | 152 return; |
| 166 } | 153 } |
| 167 | 154 |
| 168 LabelButton::OnGestureEvent(event); | 155 LabelButton::OnGestureEvent(event); |
| 169 | |
| 170 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | |
| 171 switch (event->type()) { | |
| 172 case ui::ET_GESTURE_TAP_DOWN: | |
| 173 ink_drop_state = views::InkDropState::ACTION_PENDING; | |
| 174 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | |
| 175 // subsequent events for the gesture are sent to |this|. | |
| 176 event->SetHandled(); | |
| 177 break; | |
| 178 case ui::ET_GESTURE_LONG_PRESS: | |
| 179 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING; | |
| 180 break; | |
| 181 case ui::ET_GESTURE_TAP: | |
| 182 ink_drop_state = views::InkDropState::QUICK_ACTION; | |
| 183 break; | |
| 184 case ui::ET_GESTURE_LONG_TAP: | |
| 185 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
| 186 break; | |
| 187 case ui::ET_GESTURE_END: | |
| 188 case ui::ET_GESTURE_TAP_CANCEL: | |
| 189 ink_drop_state = views::InkDropState::HIDDEN; | |
| 190 break; | |
| 191 default: | |
| 192 return; | |
| 193 } | |
| 194 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | |
| 195 } | 156 } |
| 196 | 157 |
| 197 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { | 158 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { |
| 198 CustomButton::GetAccessibleState(state); | 159 CustomButton::GetAccessibleState(state); |
| 199 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; | 160 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; |
| 200 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 161 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
| 201 state->AddStateFlag(ui::AX_STATE_HASPOPUP); | 162 state->AddStateFlag(ui::AX_STATE_HASPOPUP); |
| 202 } | 163 } |
| 203 | 164 |
| 204 scoped_ptr<views::LabelButtonBorder> | 165 scoped_ptr<views::LabelButtonBorder> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 207 |
| 247 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | 208 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
| 248 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 209 // Enter PUSHED state on press with Left or Right mouse button or on taps. |
| 249 // Remain in this state while the context menu is open. | 210 // Remain in this state while the context menu is open. |
| 250 return event.type() == ui::ET_GESTURE_TAP || | 211 return event.type() == ui::ET_GESTURE_TAP || |
| 251 event.type() == ui::ET_GESTURE_TAP_DOWN || | 212 event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 252 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 213 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
| 253 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 214 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
| 254 } | 215 } |
| 255 | 216 |
| 256 void ToolbarButton::NotifyClick(const ui::Event& event) { | |
| 257 LabelButton::NotifyClick(event); | |
| 258 ink_drop_animation_controller_->AnimateToState( | |
| 259 views::InkDropState::QUICK_ACTION); | |
| 260 } | |
| 261 | |
| 262 void ToolbarButton::OnClickCanceled(const ui::Event& event) { | |
| 263 LabelButton::OnClickCanceled(event); | |
| 264 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
| 265 } | |
| 266 | |
| 267 bool ToolbarButton::ShouldShowMenu() { | 217 bool ToolbarButton::ShouldShowMenu() { |
| 268 return model_ != nullptr; | 218 return model_ != nullptr; |
| 269 } | 219 } |
| 270 | 220 |
| 271 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { | 221 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
| 272 if (!ShouldShowMenu()) | 222 if (!ShouldShowMenu()) |
| 273 return; | 223 return; |
| 274 | 224 |
| 275 gfx::Rect lb = GetLocalBounds(); | 225 gfx::Rect lb = GetLocalBounds(); |
| 276 | 226 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 303 int left_bound = display.bounds().x(); | 253 int left_bound = display.bounds().x(); |
| 304 #endif | 254 #endif |
| 305 if (menu_position.x() < left_bound) | 255 if (menu_position.x() < left_bound) |
| 306 menu_position.set_x(left_bound); | 256 menu_position.set_x(left_bound); |
| 307 | 257 |
| 308 // Make the button look depressed while the menu is open. | 258 // Make the button look depressed while the menu is open. |
| 309 SetState(STATE_PRESSED); | 259 SetState(STATE_PRESSED); |
| 310 | 260 |
| 311 menu_showing_ = true; | 261 menu_showing_ = true; |
| 312 | 262 |
| 313 ink_drop_animation_controller_->AnimateToState( | 263 ink_drop_delegate()->OnAction(views::InkDropState::ACTIVATED); |
| 314 views::InkDropState::ACTIVATED); | |
| 315 | 264 |
| 316 // Create and run menu. Display an empty menu if model is NULL. | 265 // Create and run menu. Display an empty menu if model is NULL. |
| 317 views::MenuRunner::RunResult result; | 266 views::MenuRunner::RunResult result; |
| 318 if (model_.get()) { | 267 if (model_.get()) { |
| 319 views::MenuModelAdapter menu_delegate(model_.get()); | 268 views::MenuModelAdapter menu_delegate(model_.get()); |
| 320 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 269 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
| 321 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), | 270 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(), |
| 322 views::MenuRunner::HAS_MNEMONICS)); | 271 views::MenuRunner::HAS_MNEMONICS)); |
| 323 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, | 272 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 324 gfx::Rect(menu_position, gfx::Size(0, 0)), | 273 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 325 views::MENU_ANCHOR_TOPLEFT, source_type); | 274 views::MENU_ANCHOR_TOPLEFT, source_type); |
| 326 } else { | 275 } else { |
| 327 views::MenuDelegate menu_delegate; | 276 views::MenuDelegate menu_delegate; |
| 328 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); | 277 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate); |
| 329 menu_runner_.reset( | 278 menu_runner_.reset( |
| 330 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); | 279 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS)); |
| 331 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, | 280 result = menu_runner_->RunMenuAt(GetWidget(), nullptr, |
| 332 gfx::Rect(menu_position, gfx::Size(0, 0)), | 281 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 333 views::MENU_ANCHOR_TOPLEFT, source_type); | 282 views::MENU_ANCHOR_TOPLEFT, source_type); |
| 334 } | 283 } |
| 335 if (result == views::MenuRunner::MENU_DELETED) | 284 if (result == views::MenuRunner::MENU_DELETED) |
| 336 return; | 285 return; |
| 337 | 286 |
| 338 ink_drop_animation_controller_->AnimateToState( | 287 ink_drop_delegate()->OnAction(views::InkDropState::DEACTIVATED); |
| 339 views::InkDropState::DEACTIVATED); | |
| 340 | 288 |
| 341 menu_showing_ = false; | 289 menu_showing_ = false; |
| 342 | 290 |
| 343 // Need to explicitly clear mouse handler so that events get sent | 291 // Need to explicitly clear mouse handler so that events get sent |
| 344 // properly after the menu finishes running. If we don't do this, then | 292 // properly after the menu finishes running. If we don't do this, then |
| 345 // the first click to other parts of the UI is eaten. | 293 // the first click to other parts of the UI is eaten. |
| 346 SetMouseHandler(nullptr); | 294 SetMouseHandler(nullptr); |
| 347 | 295 |
| 348 // Set the state back to normal after the drop down menu is closed. | 296 // Set the state back to normal after the drop down menu is closed. |
| 349 if (state_ != STATE_DISABLED) | 297 if (state_ != STATE_DISABLED) |
| 350 SetState(STATE_NORMAL); | 298 SetState(STATE_NORMAL); |
| 351 } | 299 } |
| 352 | 300 |
| 353 const char* ToolbarButton::GetClassName() const { | 301 const char* ToolbarButton::GetClassName() const { |
| 354 return "ToolbarButton"; | 302 return "ToolbarButton"; |
| 355 } | 303 } |
| 356 | 304 |
| 357 gfx::Point ToolbarButton::CalculateInkDropCenter() const { | 305 gfx::Point ToolbarButton::CalculateInkDropCenter() const { |
| 358 return GetLocalBounds().CenterPoint(); | 306 return GetLocalBounds().CenterPoint(); |
| 359 } | 307 } |
| OLD | NEW |