Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(439)

Side by Side Diff: chrome/browser/ui/views/toolbar/toolbar_button.cc

Issue 1280953003: Enhance the material design ripple API so the ripple's state can be controlled by it's owning View. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed pkasting@'s comments from patch set 11 Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 SetPaintToLayer(true);
sadrul 2015/08/17 14:33:06 Is this only so you can add the ink-drop layers? C
bruthig 2015/08/17 17:43:39 Yes. I'm not sure how else to do it without creat
40 new views::InkDropAnimationController(this));
41 layer()->SetFillsBoundsOpaquely(false);
42 image()->SetPaintToLayer(true); 38 image()->SetPaintToLayer(true);
43 image()->SetFillsBoundsOpaquely(false); 39 image()->SetFillsBoundsOpaquely(false);
44 } 40 }
45 #endif // defined(OS_CHROMEOS) 41 // TODO(bruthig): |ink_drop_animation_controller_| has to be initialized after
42 // SetPaintToLayer(true) becuase the InkDropAnimationControllerImpl
43 // constructor calls back in to AddInkDropLayer() to add the ink drop layer to
44 // layer(). This will be reworked so the InkDropAnimationController will call
45 // AddInkDropLayer() right before the ink drop becomes visible and then
46 // |ink_drop_animation_controller_| can be initialized in the initializer
47 // list.
46 48
49 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory::
50 CreateInkDropAnimationController(this);
47 set_context_menu_controller(this); 51 set_context_menu_controller(this);
48 } 52 }
49 53
50 ToolbarButton::~ToolbarButton() { 54 ToolbarButton::~ToolbarButton() {
51 } 55 }
sadrul 2015/08/17 14:33:06 Maybe explicitly destroy the ink-drop controller h
bruthig 2015/08/17 17:43:39 Done.
52 56
53 void ToolbarButton::Init() { 57 void ToolbarButton::Init() {
54 SetFocusable(false); 58 SetFocusable(false);
55 SetAccessibilityFocusable(true); 59 SetAccessibilityFocusable(true);
56 image()->EnableCanvasFlippingForRTLUI(true); 60 image()->EnableCanvasFlippingForRTLUI(true);
57 } 61 }
58 62
59 void ToolbarButton::ClearPendingMenu() { 63 void ToolbarButton::ClearPendingMenu() {
60 show_menu_factory_.InvalidateWeakPtrs(); 64 show_menu_factory_.InvalidateWeakPtrs();
61 } 65 }
(...skipping 17 matching lines...) Expand all
79 ui::ThemeProvider* provider = GetThemeProvider(); 83 ui::ThemeProvider* provider = GetThemeProvider();
80 if (provider && provider->UsingSystemTheme()) { 84 if (provider && provider->UsingSystemTheme()) {
81 int inset = provider->GetDisplayProperty( 85 int inset = provider->GetDisplayProperty(
82 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET); 86 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET);
83 size.Enlarge(2 * inset, 2 * inset); 87 size.Enlarge(2 * inset, 2 * inset);
84 } 88 }
85 } 89 }
86 return size; 90 return size;
87 } 91 }
88 92
93 void ToolbarButton::Layout() {
94 LabelButton::Layout();
95 LayoutInkDrop();
96 }
97
89 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { 98 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) {
90 if (enabled() && ShouldShowMenu() && 99 if (enabled() && ShouldShowMenu() &&
91 IsTriggerableEvent(event) && HitTestPoint(event.location())) { 100 IsTriggerableEvent(event) && HitTestPoint(event.location())) {
92 // Store the y pos of the mouse coordinates so we can use them later to 101 // 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 102 // determine if the user dragged the mouse down (which should pop up the
94 // drag down menu immediately, instead of waiting for the timer) 103 // drag down menu immediately, instead of waiting for the timer)
95 y_position_on_lbuttondown_ = event.y(); 104 y_position_on_lbuttondown_ = event.y();
96 105
97 // Schedule a task that will show the menu. 106 // Schedule a task that will show the menu.
98 const int kMenuTimerDelay = 500; 107 const int kMenuTimerDelay = 500;
99 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 108 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
100 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu, 109 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu,
101 show_menu_factory_.GetWeakPtr(), 110 show_menu_factory_.GetWeakPtr(),
102 ui::GetMenuSourceTypeForEvent(event)), 111 ui::GetMenuSourceTypeForEvent(event)),
103 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); 112 base::TimeDelta::FromMilliseconds(kMenuTimerDelay));
104 } 113 }
114
115 ink_drop_animation_controller_->AnimateToState(
116 views::InkDropState::ACTION_PENDING);
117
105 return LabelButton::OnMousePressed(event); 118 return LabelButton::OnMousePressed(event);
106 } 119 }
107 120
108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { 121 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) {
109 bool result = LabelButton::OnMouseDragged(event); 122 bool result = LabelButton::OnMouseDragged(event);
110 123
111 if (show_menu_factory_.HasWeakPtrs()) { 124 if (show_menu_factory_.HasWeakPtrs()) {
112 // If the mouse is dragged to a y position lower than where it was when 125 // 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 126 // clicked then we should not wait for the menu to appear but show
114 // it immediately. 127 // it immediately.
115 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { 128 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) {
116 show_menu_factory_.InvalidateWeakPtrs(); 129 show_menu_factory_.InvalidateWeakPtrs();
117 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); 130 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
118 } 131 }
119 } 132 }
120 133
121 return result; 134 return result;
122 } 135 }
123 136
124 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { 137 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) {
125 if (IsTriggerableEvent(event) || 138 if (IsTriggerableEvent(event) ||
126 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { 139 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) {
127 LabelButton::OnMouseReleased(event); 140 LabelButton::OnMouseReleased(event);
128 } 141 }
129 142
130 if (IsTriggerableEvent(event)) 143 if (IsTriggerableEvent(event))
131 show_menu_factory_.InvalidateWeakPtrs(); 144 show_menu_factory_.InvalidateWeakPtrs();
145
146 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
132 } 147 }
133 148
134 void ToolbarButton::OnMouseCaptureLost() { 149 void ToolbarButton::OnMouseCaptureLost() {
135 } 150 }
136 151
137 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { 152 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) {
138 // Starting a drag results in a MouseExited, we need to ignore it. 153 // Starting a drag results in a MouseExited, we need to ignore it.
139 // A right click release triggers an exit event. We want to 154 // A right click release triggers an exit event. We want to
140 // remain in a PUSHED state until the drop down menu closes. 155 // remain in a PUSHED state until the drop down menu closes.
141 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) 156 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED)
142 SetState(STATE_NORMAL); 157 SetState(STATE_NORMAL);
143 } 158 }
144 159
145 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { 160 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) {
146 if (menu_showing_) { 161 if (menu_showing_) {
147 // While dropdown menu is showing the button should not handle gestures. 162 // While dropdown menu is showing the button should not handle gestures.
148 event->StopPropagation(); 163 event->StopPropagation();
149 return; 164 return;
150 } 165 }
151 166
152 LabelButton::OnGestureEvent(event); 167 LabelButton::OnGestureEvent(event);
168
169 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN;
170 switch (event->type()) {
171 case ui::ET_GESTURE_TAP_DOWN:
172 ink_drop_state = views::InkDropState::ACTION_PENDING;
173 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that
174 // subsequent events for the gesture are sent to |this|.
175 event->SetHandled();
176 break;
177 case ui::ET_GESTURE_LONG_PRESS:
178 ink_drop_state = views::InkDropState::SLOW_ACTION;
179 break;
180 case ui::ET_GESTURE_TAP:
181 ink_drop_state = views::InkDropState::QUICK_ACTION;
182 break;
183 case ui::ET_GESTURE_END:
184 case ui::ET_GESTURE_TAP_CANCEL:
185 ink_drop_state = views::InkDropState::HIDDEN;
186 break;
187 default:
188 return;
189 }
190 ink_drop_animation_controller_->AnimateToState(ink_drop_state);
153 } 191 }
154 192
155 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { 193 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) {
156 CustomButton::GetAccessibleState(state); 194 CustomButton::GetAccessibleState(state);
157 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; 195 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN;
158 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); 196 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
159 state->AddStateFlag(ui::AX_STATE_HASPOPUP); 197 state->AddStateFlag(ui::AX_STATE_HASPOPUP);
160 } 198 }
161 199
162 scoped_ptr<views::LabelButtonBorder> 200 scoped_ptr<views::LabelButtonBorder>
(...skipping 15 matching lines...) Expand all
178 void ToolbarButton::ShowContextMenuForView(View* source, 216 void ToolbarButton::ShowContextMenuForView(View* source,
179 const gfx::Point& point, 217 const gfx::Point& point,
180 ui::MenuSourceType source_type) { 218 ui::MenuSourceType source_type) {
181 if (!enabled()) 219 if (!enabled())
182 return; 220 return;
183 221
184 show_menu_factory_.InvalidateWeakPtrs(); 222 show_menu_factory_.InvalidateWeakPtrs();
185 ShowDropDownMenu(source_type); 223 ShowDropDownMenu(source_type);
186 } 224 }
187 225
226 void ToolbarButton::AddInkDropLayer(ui::Layer* ink_drop_layer) {
227 layer()->Add(ink_drop_layer);
228 layer()->StackAtBottom(ink_drop_layer);
229 }
230
231 void ToolbarButton::RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
232 layer()->Remove(ink_drop_layer);
233 }
234
188 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { 235 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) {
189 // Enter PUSHED state on press with Left or Right mouse button or on taps. 236 // 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. 237 // Remain in this state while the context menu is open.
191 return event.type() == ui::ET_GESTURE_TAP || 238 return event.type() == ui::ET_GESTURE_TAP ||
192 event.type() == ui::ET_GESTURE_TAP_DOWN || 239 event.type() == ui::ET_GESTURE_TAP_DOWN ||
193 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | 240 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON |
194 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); 241 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0);
195 } 242 }
196 243
197 bool ToolbarButton::ShouldShowMenu() { 244 bool ToolbarButton::ShouldShowMenu() {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 views::MenuRunner::RunResult result = 309 views::MenuRunner::RunResult result =
263 menu_runner_->RunMenuAt(GetWidget(), 310 menu_runner_->RunMenuAt(GetWidget(),
264 NULL, 311 NULL,
265 gfx::Rect(menu_position, gfx::Size(0, 0)), 312 gfx::Rect(menu_position, gfx::Size(0, 0)),
266 views::MENU_ANCHOR_TOPLEFT, 313 views::MENU_ANCHOR_TOPLEFT,
267 source_type); 314 source_type);
268 if (result == views::MenuRunner::MENU_DELETED) 315 if (result == views::MenuRunner::MENU_DELETED)
269 return; 316 return;
270 } 317 }
271 318
319 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
320
272 menu_showing_ = false; 321 menu_showing_ = false;
273 322
274 // Need to explicitly clear mouse handler so that events get sent 323 // 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 324 // 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. 325 // the first click to other parts of the UI is eaten.
277 SetMouseHandler(NULL); 326 SetMouseHandler(NULL);
278 327
279 // Set the state back to normal after the drop down menu is closed. 328 // Set the state back to normal after the drop down menu is closed.
280 if (state_ != STATE_DISABLED) 329 if (state_ != STATE_DISABLED)
281 SetState(STATE_NORMAL); 330 SetState(STATE_NORMAL);
282 } 331 }
283 332
333 void ToolbarButton::LayoutInkDrop() {
334 ink_drop_animation_controller_->SetInkDropSize(size());
335 }
336
284 const char* ToolbarButton::GetClassName() const { 337 const char* ToolbarButton::GetClassName() const {
285 return "ToolbarButton"; 338 return "ToolbarButton";
286 } 339 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698