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/controls/button/label_button_border.h" | 23 #include "ui/views/controls/button/label_button_border.h" |
| 23 #include "ui/views/controls/menu/menu_item_view.h" | 24 #include "ui/views/controls/menu/menu_item_view.h" |
| 24 #include "ui/views/controls/menu/menu_model_adapter.h" | 25 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 25 #include "ui/views/controls/menu/menu_runner.h" | 26 #include "ui/views/controls/menu/menu_runner.h" |
| 26 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
| 27 | 28 |
| 28 ToolbarButton::ToolbarButton(views::ButtonListener* listener, | 29 ToolbarButton::ToolbarButton(views::ButtonListener* listener, |
| 29 ui::MenuModel* model) | 30 ui::MenuModel* model) |
| 30 : views::LabelButton(listener, base::string16()), | 31 : views::LabelButton(listener, base::string16()), |
| 31 model_(model), | 32 model_(model), |
| 32 menu_showing_(false), | 33 menu_showing_(false), |
| 33 y_position_on_lbuttondown_(0), | 34 y_position_on_lbuttondown_(0), |
| 34 show_menu_factory_(this) { | 35 show_menu_factory_(this) { |
| 35 #if defined(OS_CHROMEOS) | |
| 36 // The ink drop animation is only targeted at ChromeOS because there is | |
| 37 // concern it will conflict with OS level touch feedback in a bad way. | |
| 38 if (ui::MaterialDesignController::IsModeMaterial()) { | 36 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 39 ink_drop_animation_controller_.reset( | 37 // TODO(bruthig): Only set the layer when the ink drop is active. |
|
sadrul
2015/08/18 17:51:49
Make sure there's a blocking crbug for this.
bruthig
2015/08/18 19:04:38
Created www.crbug.com/522175
| |
| 40 new views::InkDropAnimationController(this)); | 38 SetLayer(new ui::Layer(ui::LAYER_NOT_DRAWN)); |
|
sadrul
2015/08/18 17:51:49
You should use SetPaintToLayer() instead.
bruthig
2015/08/18 19:04:38
Done.
| |
| 41 layer()->SetFillsBoundsOpaquely(false); | |
| 42 image()->SetPaintToLayer(true); | 39 image()->SetPaintToLayer(true); |
| 43 image()->SetFillsBoundsOpaquely(false); | 40 image()->SetFillsBoundsOpaquely(false); |
| 44 } | 41 } |
| 45 #endif // defined(OS_CHROMEOS) | 42 // TODO(bruthig): |ink_drop_animation_controller_| has to be initialized after |
| 43 // SetPaintToLayer(true) becuase the InkDropAnimationControllerImpl | |
| 44 // constructor calls back in to AddInkDropLayer() to add the ink drop layer to | |
| 45 // layer(). This will be reworked so the InkDropAnimationController will call | |
| 46 // AddInkDropLayer() right before the ink drop becomes visible and then | |
| 47 // |ink_drop_animation_controller_| can be initialized in the initializer | |
| 48 // list. | |
| 46 | 49 |
| 50 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory:: | |
| 51 CreateInkDropAnimationController(this); | |
| 47 set_context_menu_controller(this); | 52 set_context_menu_controller(this); |
| 48 } | 53 } |
| 49 | 54 |
| 50 ToolbarButton::~ToolbarButton() { | 55 ToolbarButton::~ToolbarButton() { |
| 56 // Explicitly delete the ink_drop_animation_controller_ because it may call | |
| 57 // RemoveInkDropLayer(). | |
|
Peter Kasting
2015/08/17 21:18:15
Is that actually a problem? It seems like that ca
bruthig
2015/08/18 19:04:38
Originally I added this at sadrul@'s request but I
sadrul
2015/08/18 19:52:38
Reaching into virtual functions during destruction
| |
| 58 ink_drop_animation_controller_.reset(); | |
| 51 } | 59 } |
| 52 | 60 |
| 53 void ToolbarButton::Init() { | 61 void ToolbarButton::Init() { |
| 54 SetFocusable(false); | 62 SetFocusable(false); |
| 55 SetAccessibilityFocusable(true); | 63 SetAccessibilityFocusable(true); |
| 56 image()->EnableCanvasFlippingForRTLUI(true); | 64 image()->EnableCanvasFlippingForRTLUI(true); |
| 57 } | 65 } |
| 58 | 66 |
| 59 void ToolbarButton::ClearPendingMenu() { | 67 void ToolbarButton::ClearPendingMenu() { |
| 60 show_menu_factory_.InvalidateWeakPtrs(); | 68 show_menu_factory_.InvalidateWeakPtrs(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 79 ui::ThemeProvider* provider = GetThemeProvider(); | 87 ui::ThemeProvider* provider = GetThemeProvider(); |
| 80 if (provider && provider->UsingSystemTheme()) { | 88 if (provider && provider->UsingSystemTheme()) { |
| 81 int inset = provider->GetDisplayProperty( | 89 int inset = provider->GetDisplayProperty( |
| 82 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET); | 90 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET); |
| 83 size.Enlarge(2 * inset, 2 * inset); | 91 size.Enlarge(2 * inset, 2 * inset); |
| 84 } | 92 } |
| 85 } | 93 } |
| 86 return size; | 94 return size; |
| 87 } | 95 } |
| 88 | 96 |
| 97 void ToolbarButton::Layout() { | |
| 98 LabelButton::Layout(); | |
| 99 LayoutInkDrop(); | |
| 100 } | |
| 101 | |
| 89 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { | 102 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
| 90 if (enabled() && ShouldShowMenu() && | 103 if (enabled() && ShouldShowMenu() && |
| 91 IsTriggerableEvent(event) && HitTestPoint(event.location())) { | 104 IsTriggerableEvent(event) && HitTestPoint(event.location())) { |
| 92 // Store the y pos of the mouse coordinates so we can use them later to | 105 // Store the y pos of the mouse coordinates so we can use them later to |
| 93 // determine if the user dragged the mouse down (which should pop up the | 106 // determine if the user dragged the mouse down (which should pop up the |
| 94 // drag down menu immediately, instead of waiting for the timer) | 107 // drag down menu immediately, instead of waiting for the timer) |
| 95 y_position_on_lbuttondown_ = event.y(); | 108 y_position_on_lbuttondown_ = event.y(); |
| 96 | 109 |
| 97 // Schedule a task that will show the menu. | 110 // Schedule a task that will show the menu. |
| 98 const int kMenuTimerDelay = 500; | 111 const int kMenuTimerDelay = 500; |
| 99 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 112 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 100 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, | 113 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
| 101 show_menu_factory_.GetWeakPtr(), | 114 show_menu_factory_.GetWeakPtr(), |
| 102 ui::GetMenuSourceTypeForEvent(event)), | 115 ui::GetMenuSourceTypeForEvent(event)), |
| 103 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 116 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
| 104 } | 117 } |
| 118 | |
| 119 ink_drop_animation_controller_->AnimateToState( | |
| 120 views::InkDropState::ACTION_PENDING); | |
| 121 | |
| 105 return LabelButton::OnMousePressed(event); | 122 return LabelButton::OnMousePressed(event); |
| 106 } | 123 } |
| 107 | 124 |
| 108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 125 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
| 109 bool result = LabelButton::OnMouseDragged(event); | 126 bool result = LabelButton::OnMouseDragged(event); |
| 110 | 127 |
| 111 if (show_menu_factory_.HasWeakPtrs()) { | 128 if (show_menu_factory_.HasWeakPtrs()) { |
| 112 // If the mouse is dragged to a y position lower than where it was when | 129 // 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 | 130 // clicked then we should not wait for the menu to appear but show |
| 114 // it immediately. | 131 // it immediately. |
| 115 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { | 132 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { |
| 116 show_menu_factory_.InvalidateWeakPtrs(); | 133 show_menu_factory_.InvalidateWeakPtrs(); |
| 117 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); | 134 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); |
| 118 } | 135 } |
| 119 } | 136 } |
| 120 | 137 |
| 121 return result; | 138 return result; |
| 122 } | 139 } |
| 123 | 140 |
| 124 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { | 141 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { |
| 125 if (IsTriggerableEvent(event) || | 142 if (IsTriggerableEvent(event) || |
| 126 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { | 143 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { |
| 127 LabelButton::OnMouseReleased(event); | 144 LabelButton::OnMouseReleased(event); |
| 128 } | 145 } |
| 129 | 146 |
| 130 if (IsTriggerableEvent(event)) | 147 if (IsTriggerableEvent(event)) |
| 131 show_menu_factory_.InvalidateWeakPtrs(); | 148 show_menu_factory_.InvalidateWeakPtrs(); |
| 149 | |
| 150 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
| 132 } | 151 } |
| 133 | 152 |
| 134 void ToolbarButton::OnMouseCaptureLost() { | 153 void ToolbarButton::OnMouseCaptureLost() { |
| 135 } | 154 } |
| 136 | 155 |
| 137 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { | 156 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { |
| 138 // Starting a drag results in a MouseExited, we need to ignore it. | 157 // Starting a drag results in a MouseExited, we need to ignore it. |
| 139 // A right click release triggers an exit event. We want to | 158 // A right click release triggers an exit event. We want to |
| 140 // remain in a PUSHED state until the drop down menu closes. | 159 // remain in a PUSHED state until the drop down menu closes. |
| 141 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) | 160 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) |
| 142 SetState(STATE_NORMAL); | 161 SetState(STATE_NORMAL); |
| 143 } | 162 } |
| 144 | 163 |
| 145 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { | 164 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
| 146 if (menu_showing_) { | 165 if (menu_showing_) { |
| 147 // While dropdown menu is showing the button should not handle gestures. | 166 // While dropdown menu is showing the button should not handle gestures. |
| 148 event->StopPropagation(); | 167 event->StopPropagation(); |
| 149 return; | 168 return; |
| 150 } | 169 } |
| 151 | 170 |
| 152 LabelButton::OnGestureEvent(event); | 171 LabelButton::OnGestureEvent(event); |
| 172 | |
| 173 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | |
| 174 switch (event->type()) { | |
| 175 case ui::ET_GESTURE_TAP_DOWN: | |
| 176 ink_drop_state = views::InkDropState::ACTION_PENDING; | |
| 177 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | |
| 178 // subsequent events for the gesture are sent to |this|. | |
| 179 event->SetHandled(); | |
| 180 break; | |
| 181 case ui::ET_GESTURE_LONG_PRESS: | |
| 182 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
| 183 break; | |
| 184 case ui::ET_GESTURE_TAP: | |
| 185 ink_drop_state = views::InkDropState::QUICK_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); | |
| 153 } | 195 } |
| 154 | 196 |
| 155 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { | 197 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { |
| 156 CustomButton::GetAccessibleState(state); | 198 CustomButton::GetAccessibleState(state); |
| 157 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; | 199 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; |
| 158 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 200 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
| 159 state->AddStateFlag(ui::AX_STATE_HASPOPUP); | 201 state->AddStateFlag(ui::AX_STATE_HASPOPUP); |
| 160 } | 202 } |
| 161 | 203 |
| 162 scoped_ptr<views::LabelButtonBorder> | 204 scoped_ptr<views::LabelButtonBorder> |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 178 void ToolbarButton::ShowContextMenuForView(View* source, | 220 void ToolbarButton::ShowContextMenuForView(View* source, |
| 179 const gfx::Point& point, | 221 const gfx::Point& point, |
| 180 ui::MenuSourceType source_type) { | 222 ui::MenuSourceType source_type) { |
| 181 if (!enabled()) | 223 if (!enabled()) |
| 182 return; | 224 return; |
| 183 | 225 |
| 184 show_menu_factory_.InvalidateWeakPtrs(); | 226 show_menu_factory_.InvalidateWeakPtrs(); |
| 185 ShowDropDownMenu(source_type); | 227 ShowDropDownMenu(source_type); |
| 186 } | 228 } |
| 187 | 229 |
| 230 void ToolbarButton::AddInkDropLayer(ui::Layer* ink_drop_layer) { | |
| 231 layer()->Add(ink_drop_layer); | |
|
sadrul
2015/08/18 17:51:49
Maybe SetPaintToLayer etc. should be moved in here
bruthig
2015/08/18 19:04:38
This will address the TODO at this level anyway.
| |
| 232 layer()->StackAtBottom(ink_drop_layer); | |
| 233 } | |
| 234 | |
| 235 void ToolbarButton::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | |
| 236 layer()->Remove(ink_drop_layer); | |
| 237 } | |
| 238 | |
| 188 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | 239 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
| 189 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 240 // Enter PUSHED state on press with Left or Right mouse button or on taps. |
| 190 // Remain in this state while the context menu is open. | 241 // Remain in this state while the context menu is open. |
| 191 return event.type() == ui::ET_GESTURE_TAP || | 242 return event.type() == ui::ET_GESTURE_TAP || |
| 192 event.type() == ui::ET_GESTURE_TAP_DOWN || | 243 event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 193 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 244 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
| 194 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 245 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
| 195 } | 246 } |
| 196 | 247 |
| 197 bool ToolbarButton::ShouldShowMenu() { | 248 bool ToolbarButton::ShouldShowMenu() { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 views::MenuRunner::RunResult result = | 313 views::MenuRunner::RunResult result = |
| 263 menu_runner_->RunMenuAt(GetWidget(), | 314 menu_runner_->RunMenuAt(GetWidget(), |
| 264 NULL, | 315 NULL, |
| 265 gfx::Rect(menu_position, gfx::Size(0, 0)), | 316 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 266 views::MENU_ANCHOR_TOPLEFT, | 317 views::MENU_ANCHOR_TOPLEFT, |
| 267 source_type); | 318 source_type); |
| 268 if (result == views::MenuRunner::MENU_DELETED) | 319 if (result == views::MenuRunner::MENU_DELETED) |
| 269 return; | 320 return; |
| 270 } | 321 } |
| 271 | 322 |
| 323 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
| 324 | |
| 272 menu_showing_ = false; | 325 menu_showing_ = false; |
| 273 | 326 |
| 274 // Need to explicitly clear mouse handler so that events get sent | 327 // Need to explicitly clear mouse handler so that events get sent |
| 275 // properly after the menu finishes running. If we don't do this, then | 328 // properly after the menu finishes running. If we don't do this, then |
| 276 // the first click to other parts of the UI is eaten. | 329 // the first click to other parts of the UI is eaten. |
| 277 SetMouseHandler(NULL); | 330 SetMouseHandler(NULL); |
| 278 | 331 |
| 279 // Set the state back to normal after the drop down menu is closed. | 332 // Set the state back to normal after the drop down menu is closed. |
| 280 if (state_ != STATE_DISABLED) | 333 if (state_ != STATE_DISABLED) |
| 281 SetState(STATE_NORMAL); | 334 SetState(STATE_NORMAL); |
| 282 } | 335 } |
| 283 | 336 |
| 337 void ToolbarButton::LayoutInkDrop() { | |
| 338 ink_drop_animation_controller_->SetInkDropSize(size()); | |
| 339 } | |
| 340 | |
| 284 const char* ToolbarButton::GetClassName() const { | 341 const char* ToolbarButton::GetClassName() const { |
| 285 return "ToolbarButton"; | 342 return "ToolbarButton"; |
| 286 } | 343 } |
| OLD | NEW |