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 SetPaintToLayer(true); |
| 36 // The ink drop animation is only targeted at ChromeOS because there is | 37 // TODO(bruthig): |ink_drop_animation_controller_| has to be initialized after |
| 37 // concern it will conflict with OS level touch feedback in a bad way. | 38 // SetPaintToLayer(true) becuase the InkDropAnimationControllerImpl |
| 38 if (ui::MaterialDesignController::IsModeMaterial()) { | 39 // constructor calls back in to AddInkDropLayer() to add the ink drop layer to |
| 39 ink_drop_animation_controller_.reset( | 40 // layer(). This will be reworked so the InkDropAnimationController will call |
| 40 new views::InkDropAnimationController(this)); | 41 // AddInkDropLayer() right before the ink drop becomes visible and then |
| 41 layer()->SetFillsBoundsOpaquely(false); | 42 // |ink_drop_animation_controller_| can be initialized in the initializer |
| 42 image()->SetPaintToLayer(true); | 43 // list. |
| 43 image()->SetFillsBoundsOpaquely(false); | 44 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory:: |
| 44 } | 45 CreateInkDropAnimationController(this); |
| 45 #endif // defined(OS_CHROMEOS) | 46 layer()->SetFillsBoundsOpaquely(false); |
| 46 | 47 image()->SetPaintToLayer(true); |
| 48 image()->SetFillsBoundsOpaquely(false); | |
|
tdanderson
2015/08/11 19:07:40
Do you still want to call lines 46-48 if the MD fl
jonross
2015/08/11 20:34:46
If the factory is abstracting MD, should 46-48 pos
bruthig
2015/08/12 15:26:40
They don't need to be called but they didn't seem
bruthig
2015/08/12 15:26:40
I don't think the factory can in all general cases
jonross
2015/08/12 18:12:56
True, but could the InkDropHost implement a method
| |
| 47 set_context_menu_controller(this); | 49 set_context_menu_controller(this); |
| 48 } | 50 } |
| 49 | 51 |
| 50 ToolbarButton::~ToolbarButton() { | 52 ToolbarButton::~ToolbarButton() { |
| 51 } | 53 } |
| 52 | 54 |
| 53 void ToolbarButton::Init() { | 55 void ToolbarButton::Init() { |
| 54 SetFocusable(false); | 56 SetFocusable(false); |
| 55 SetAccessibilityFocusable(true); | 57 SetAccessibilityFocusable(true); |
| 56 image()->EnableCanvasFlippingForRTLUI(true); | 58 image()->EnableCanvasFlippingForRTLUI(true); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 79 ui::ThemeProvider* provider = GetThemeProvider(); | 81 ui::ThemeProvider* provider = GetThemeProvider(); |
| 80 if (provider && provider->UsingSystemTheme()) { | 82 if (provider && provider->UsingSystemTheme()) { |
| 81 int inset = provider->GetDisplayProperty( | 83 int inset = provider->GetDisplayProperty( |
| 82 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET); | 84 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET); |
| 83 size.Enlarge(2 * inset, 2 * inset); | 85 size.Enlarge(2 * inset, 2 * inset); |
| 84 } | 86 } |
| 85 } | 87 } |
| 86 return size; | 88 return size; |
| 87 } | 89 } |
| 88 | 90 |
| 91 void ToolbarButton::Layout() { | |
| 92 LabelButton::Layout(); | |
| 93 LayoutInkDrop(); | |
|
jonross
2015/08/11 20:34:46
Thoughts on having this changed to:
virtual void
bruthig
2015/08/12 15:26:40
Please see thread about this here: https://coderev
| |
| 94 } | |
| 95 | |
| 89 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { | 96 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
| 90 if (enabled() && ShouldShowMenu() && | 97 if (enabled() && ShouldShowMenu() && |
| 91 IsTriggerableEvent(event) && HitTestPoint(event.location())) { | 98 IsTriggerableEvent(event) && HitTestPoint(event.location())) { |
| 92 // Store the y pos of the mouse coordinates so we can use them later to | 99 // 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 | 100 // determine if the user dragged the mouse down (which should pop up the |
| 94 // drag down menu immediately, instead of waiting for the timer) | 101 // drag down menu immediately, instead of waiting for the timer) |
| 95 y_position_on_lbuttondown_ = event.y(); | 102 y_position_on_lbuttondown_ = event.y(); |
| 96 | 103 |
| 97 // Schedule a task that will show the menu. | 104 // Schedule a task that will show the menu. |
| 98 const int kMenuTimerDelay = 500; | 105 const int kMenuTimerDelay = 500; |
| 99 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 106 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 100 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, | 107 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, |
| 101 show_menu_factory_.GetWeakPtr(), | 108 show_menu_factory_.GetWeakPtr(), |
| 102 ui::GetMenuSourceTypeForEvent(event)), | 109 ui::GetMenuSourceTypeForEvent(event)), |
| 103 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 110 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
| 104 } | 111 } |
| 112 | |
| 113 ink_drop_animation_controller_->AnimateToState( | |
| 114 views::InkDropState::ACTION_PENDING); | |
| 115 | |
| 105 return LabelButton::OnMousePressed(event); | 116 return LabelButton::OnMousePressed(event); |
| 106 } | 117 } |
| 107 | 118 |
| 108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { | 119 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
| 109 bool result = LabelButton::OnMouseDragged(event); | 120 bool result = LabelButton::OnMouseDragged(event); |
| 110 | 121 |
| 111 if (show_menu_factory_.HasWeakPtrs()) { | 122 if (show_menu_factory_.HasWeakPtrs()) { |
| 112 // If the mouse is dragged to a y position lower than where it was when | 123 // 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 | 124 // clicked then we should not wait for the menu to appear but show |
| 114 // it immediately. | 125 // it immediately. |
| 115 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { | 126 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { |
| 116 show_menu_factory_.InvalidateWeakPtrs(); | 127 show_menu_factory_.InvalidateWeakPtrs(); |
| 117 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); | 128 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); |
| 118 } | 129 } |
| 119 } | 130 } |
| 120 | 131 |
| 121 return result; | 132 return result; |
| 122 } | 133 } |
| 123 | 134 |
| 124 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { | 135 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { |
| 125 if (IsTriggerableEvent(event) || | 136 if (IsTriggerableEvent(event) || |
| 126 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { | 137 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { |
| 127 LabelButton::OnMouseReleased(event); | 138 LabelButton::OnMouseReleased(event); |
| 128 } | 139 } |
| 129 | 140 |
| 130 if (IsTriggerableEvent(event)) | 141 if (IsTriggerableEvent(event)) |
| 131 show_menu_factory_.InvalidateWeakPtrs(); | 142 show_menu_factory_.InvalidateWeakPtrs(); |
| 143 | |
| 144 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
| 132 } | 145 } |
| 133 | 146 |
| 134 void ToolbarButton::OnMouseCaptureLost() { | 147 void ToolbarButton::OnMouseCaptureLost() { |
| 135 } | 148 } |
| 136 | 149 |
| 137 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { | 150 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { |
| 138 // Starting a drag results in a MouseExited, we need to ignore it. | 151 // Starting a drag results in a MouseExited, we need to ignore it. |
| 139 // A right click release triggers an exit event. We want to | 152 // A right click release triggers an exit event. We want to |
| 140 // remain in a PUSHED state until the drop down menu closes. | 153 // remain in a PUSHED state until the drop down menu closes. |
| 141 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) | 154 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) |
| 142 SetState(STATE_NORMAL); | 155 SetState(STATE_NORMAL); |
| 143 } | 156 } |
| 144 | 157 |
| 145 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { | 158 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
| 146 if (menu_showing_) { | 159 if (menu_showing_) { |
| 147 // While dropdown menu is showing the button should not handle gestures. | 160 // While dropdown menu is showing the button should not handle gestures. |
| 148 event->StopPropagation(); | 161 event->StopPropagation(); |
| 149 return; | 162 return; |
| 150 } | 163 } |
| 151 | 164 |
| 152 LabelButton::OnGestureEvent(event); | 165 LabelButton::OnGestureEvent(event); |
| 166 | |
| 167 switch (event->type()) { | |
| 168 case ui::ET_GESTURE_TAP_DOWN: | |
| 169 ink_drop_animation_controller_->AnimateToState( | |
| 170 views::InkDropState::ACTION_PENDING); | |
| 171 CHECK(event->handled()) << "event should be marked handled so that " | |
|
tdanderson
2015/08/11 19:07:41
I'm still on the fence about whether this is neede
jonross
2015/08/11 20:34:46
This is just ensuring that it is handled. A valid
| |
| 172 "subsequent gestures are received."; | |
| 173 break; | |
| 174 case ui::ET_GESTURE_LONG_PRESS: | |
| 175 ink_drop_animation_controller_->AnimateToState( | |
| 176 views::InkDropState::SLOW_ACTION); | |
| 177 break; | |
| 178 case ui::ET_GESTURE_TAP: | |
| 179 ink_drop_animation_controller_->AnimateToState( | |
| 180 views::InkDropState::QUICK_ACTION); | |
| 181 break; | |
| 182 case ui::ET_GESTURE_END: | |
| 183 case ui::ET_GESTURE_TAP_CANCEL: | |
| 184 ink_drop_animation_controller_->AnimateToState( | |
| 185 views::InkDropState::HIDDEN); | |
| 186 break; | |
| 187 default: | |
| 188 break; | |
| 189 } | |
| 153 } | 190 } |
| 154 | 191 |
| 155 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { | 192 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { |
| 156 CustomButton::GetAccessibleState(state); | 193 CustomButton::GetAccessibleState(state); |
| 157 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; | 194 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; |
| 158 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 195 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
| 159 state->AddStateFlag(ui::AX_STATE_HASPOPUP); | 196 state->AddStateFlag(ui::AX_STATE_HASPOPUP); |
| 160 } | 197 } |
| 161 | 198 |
| 162 scoped_ptr<views::LabelButtonBorder> | 199 scoped_ptr<views::LabelButtonBorder> |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 178 void ToolbarButton::ShowContextMenuForView(View* source, | 215 void ToolbarButton::ShowContextMenuForView(View* source, |
| 179 const gfx::Point& point, | 216 const gfx::Point& point, |
| 180 ui::MenuSourceType source_type) { | 217 ui::MenuSourceType source_type) { |
| 181 if (!enabled()) | 218 if (!enabled()) |
| 182 return; | 219 return; |
| 183 | 220 |
| 184 show_menu_factory_.InvalidateWeakPtrs(); | 221 show_menu_factory_.InvalidateWeakPtrs(); |
| 185 ShowDropDownMenu(source_type); | 222 ShowDropDownMenu(source_type); |
| 186 } | 223 } |
| 187 | 224 |
| 225 void ToolbarButton::AddInkDropLayer(ui::Layer* ink_drop_layer) { | |
| 226 layer()->Add(ink_drop_layer); | |
| 227 layer()->StackAtBottom(ink_drop_layer); | |
| 228 } | |
| 229 | |
| 230 void ToolbarButton::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | |
| 231 layer()->Remove(ink_drop_layer); | |
| 232 } | |
| 233 | |
| 188 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | 234 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
| 189 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 235 // 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. | 236 // Remain in this state while the context menu is open. |
| 191 return event.type() == ui::ET_GESTURE_TAP || | 237 return event.type() == ui::ET_GESTURE_TAP || |
| 192 event.type() == ui::ET_GESTURE_TAP_DOWN || | 238 event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 193 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 239 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
| 194 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 240 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
| 195 } | 241 } |
| 196 | 242 |
| 197 bool ToolbarButton::ShouldShowMenu() { | 243 bool ToolbarButton::ShouldShowMenu() { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 views::MenuRunner::RunResult result = | 308 views::MenuRunner::RunResult result = |
| 263 menu_runner_->RunMenuAt(GetWidget(), | 309 menu_runner_->RunMenuAt(GetWidget(), |
| 264 NULL, | 310 NULL, |
| 265 gfx::Rect(menu_position, gfx::Size(0, 0)), | 311 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 266 views::MENU_ANCHOR_TOPLEFT, | 312 views::MENU_ANCHOR_TOPLEFT, |
| 267 source_type); | 313 source_type); |
| 268 if (result == views::MenuRunner::MENU_DELETED) | 314 if (result == views::MenuRunner::MENU_DELETED) |
| 269 return; | 315 return; |
| 270 } | 316 } |
| 271 | 317 |
| 318 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
| 319 | |
| 272 menu_showing_ = false; | 320 menu_showing_ = false; |
| 273 | 321 |
| 274 // Need to explicitly clear mouse handler so that events get sent | 322 // 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 | 323 // 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. | 324 // the first click to other parts of the UI is eaten. |
| 277 SetMouseHandler(NULL); | 325 SetMouseHandler(NULL); |
| 278 | 326 |
| 279 // Set the state back to normal after the drop down menu is closed. | 327 // Set the state back to normal after the drop down menu is closed. |
| 280 if (state_ != STATE_DISABLED) | 328 if (state_ != STATE_DISABLED) |
| 281 SetState(STATE_NORMAL); | 329 SetState(STATE_NORMAL); |
| 282 } | 330 } |
| 283 | 331 |
| 332 void ToolbarButton::LayoutInkDrop() { | |
| 333 ink_drop_animation_controller_->SetInkDropSize(gfx::Size(width(), height())); | |
| 334 } | |
| 335 | |
| 284 const char* ToolbarButton::GetClassName() const { | 336 const char* ToolbarButton::GetClassName() const { |
| 285 return "ToolbarButton"; | 337 return "ToolbarButton"; |
| 286 } | 338 } |
| OLD | NEW |