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

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 some comments from patch set 1. 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"
(...skipping 18 matching lines...) Expand all
29 ui::MenuModel* model) 29 ui::MenuModel* model)
30 : views::LabelButton(listener, base::string16()), 30 : views::LabelButton(listener, base::string16()),
31 model_(model), 31 model_(model),
32 menu_showing_(false), 32 menu_showing_(false),
33 y_position_on_lbuttondown_(0), 33 y_position_on_lbuttondown_(0),
34 show_menu_factory_(this) { 34 show_menu_factory_(this) {
35 #if defined(OS_CHROMEOS) 35 #if defined(OS_CHROMEOS)
36 // The ink drop animation is only targeted at ChromeOS because there is 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. 37 // concern it will conflict with OS level touch feedback in a bad way.
38 if (ui::MaterialDesignController::IsModeMaterial()) { 38 if (ui::MaterialDesignController::IsModeMaterial()) {
39 SetPaintToLayer(true);
39 ink_drop_animation_controller_.reset( 40 ink_drop_animation_controller_.reset(
40 new views::InkDropAnimationController(this)); 41 new views::InkDropAnimationController(this));
41 layer()->SetFillsBoundsOpaquely(false); 42 layer()->SetFillsBoundsOpaquely(false);
42 image()->SetPaintToLayer(true); 43 image()->SetPaintToLayer(true);
43 image()->SetFillsBoundsOpaquely(false); 44 image()->SetFillsBoundsOpaquely(false);
44 } 45 }
45 #endif // defined(OS_CHROMEOS) 46 #endif // defined(OS_CHROMEOS)
46 47
47 set_context_menu_controller(this); 48 set_context_menu_controller(this);
48 } 49 }
(...skipping 30 matching lines...) Expand all
79 ui::ThemeProvider* provider = GetThemeProvider(); 80 ui::ThemeProvider* provider = GetThemeProvider();
80 if (provider && provider->UsingSystemTheme()) { 81 if (provider && provider->UsingSystemTheme()) {
81 int inset = provider->GetDisplayProperty( 82 int inset = provider->GetDisplayProperty(
82 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET); 83 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET);
83 size.Enlarge(2 * inset, 2 * inset); 84 size.Enlarge(2 * inset, 2 * inset);
84 } 85 }
85 } 86 }
86 return size; 87 return size;
87 } 88 }
88 89
90 void ToolbarButton::Layout() {
91 LabelButton::Layout();
92 if (ui::MaterialDesignController::IsModeMaterial())
93 LayoutInkDrop();
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 if (ui::MaterialDesignController::IsModeMaterial())
tdanderson 2015/08/10 16:31:01 nit: use {} since body is more than one line.
bruthig 2015/08/10 21:57:15 Done.
114 ink_drop_animation_controller_->AnimateToState(
115 views::InkDropState::ACTION_PENDING);
116
105 return LabelButton::OnMousePressed(event); 117 return LabelButton::OnMousePressed(event);
106 } 118 }
107 119
108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { 120 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) {
109 bool result = LabelButton::OnMouseDragged(event); 121 bool result = LabelButton::OnMouseDragged(event);
110 122
111 if (show_menu_factory_.HasWeakPtrs()) { 123 if (show_menu_factory_.HasWeakPtrs()) {
112 // If the mouse is dragged to a y position lower than where it was when 124 // 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 125 // clicked then we should not wait for the menu to appear but show
114 // it immediately. 126 // it immediately.
115 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { 127 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) {
116 show_menu_factory_.InvalidateWeakPtrs(); 128 show_menu_factory_.InvalidateWeakPtrs();
117 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); 129 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
118 } 130 }
119 } 131 }
120 132
121 return result; 133 return result;
122 } 134 }
123 135
124 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { 136 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) {
125 if (IsTriggerableEvent(event) || 137 if (IsTriggerableEvent(event) ||
126 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { 138 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) {
127 LabelButton::OnMouseReleased(event); 139 LabelButton::OnMouseReleased(event);
128 } 140 }
129 141
130 if (IsTriggerableEvent(event)) 142 if (IsTriggerableEvent(event))
131 show_menu_factory_.InvalidateWeakPtrs(); 143 show_menu_factory_.InvalidateWeakPtrs();
144
145 if (ui::MaterialDesignController::IsModeMaterial())
tdanderson 2015/08/10 16:31:01 What do you think of containing all of the IsModeM
bruthig 2015/08/10 21:57:15 I've implemented this with a factory, take a look
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
167 if (ui::MaterialDesignController::IsModeMaterial()) {
168 switch (event->type()) {
169 case ui::ET_GESTURE_TAP_DOWN:
170 ink_drop_animation_controller_->AnimateToState(
tdanderson 2015/08/10 16:31:01 [I think this is a repeat of one of Jon's question
bruthig 2015/08/10 21:57:15 Yes I can imagine that it would be very useful for
171 views::InkDropState::ACTION_PENDING);
172 event->SetHandled();
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 }
190 }
191
152 LabelButton::OnGestureEvent(event); 192 LabelButton::OnGestureEvent(event);
153 } 193 }
154 194
155 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) { 195 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) {
156 CustomButton::GetAccessibleState(state); 196 CustomButton::GetAccessibleState(state);
157 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN; 197 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN;
158 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); 198 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
159 state->AddStateFlag(ui::AX_STATE_HASPOPUP); 199 state->AddStateFlag(ui::AX_STATE_HASPOPUP);
160 } 200 }
161 201
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 void ToolbarButton::ShowContextMenuForView(View* source, 234 void ToolbarButton::ShowContextMenuForView(View* source,
195 const gfx::Point& point, 235 const gfx::Point& point,
196 ui::MenuSourceType source_type) { 236 ui::MenuSourceType source_type) {
197 if (!enabled()) 237 if (!enabled())
198 return; 238 return;
199 239
200 show_menu_factory_.InvalidateWeakPtrs(); 240 show_menu_factory_.InvalidateWeakPtrs();
201 ShowDropDownMenu(source_type); 241 ShowDropDownMenu(source_type);
202 } 242 }
203 243
244 void ToolbarButton::AddInkDropLayer(ui::Layer* ink_drop_layer) {
245 layer()->Add(ink_drop_layer);
246 layer()->StackAtBottom(ink_drop_layer);
247 }
248
249 void ToolbarButton::RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
250 layer()->Remove(ink_drop_layer);
tdanderson 2015/08/10 16:31:01 How will the implementations of AddInkDropLayer()
bruthig 2015/08/10 21:57:15 The most important thing that I can see changing b
tdanderson 2015/08/11 12:57:36 OK, sounds good to leave as is and have it evolve
251 }
252
204 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { 253 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) {
205 // Enter PUSHED state on press with Left or Right mouse button or on taps. 254 // Enter PUSHED state on press with Left or Right mouse button or on taps.
206 // Remain in this state while the context menu is open. 255 // Remain in this state while the context menu is open.
207 return event.type() == ui::ET_GESTURE_TAP || 256 return event.type() == ui::ET_GESTURE_TAP ||
208 event.type() == ui::ET_GESTURE_TAP_DOWN || 257 event.type() == ui::ET_GESTURE_TAP_DOWN ||
209 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | 258 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON |
210 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); 259 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0);
211 } 260 }
212 261
213 bool ToolbarButton::ShouldShowMenu() { 262 bool ToolbarButton::ShouldShowMenu() {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 views::MenuRunner::RunResult result = 327 views::MenuRunner::RunResult result =
279 menu_runner_->RunMenuAt(GetWidget(), 328 menu_runner_->RunMenuAt(GetWidget(),
280 NULL, 329 NULL,
281 gfx::Rect(menu_position, gfx::Size(0, 0)), 330 gfx::Rect(menu_position, gfx::Size(0, 0)),
282 views::MENU_ANCHOR_TOPLEFT, 331 views::MENU_ANCHOR_TOPLEFT,
283 source_type); 332 source_type);
284 if (result == views::MenuRunner::MENU_DELETED) 333 if (result == views::MenuRunner::MENU_DELETED)
285 return; 334 return;
286 } 335 }
287 336
337 if (ui::MaterialDesignController::IsModeMaterial())
338 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
339
288 menu_showing_ = false; 340 menu_showing_ = false;
289 341
290 // Need to explicitly clear mouse handler so that events get sent 342 // Need to explicitly clear mouse handler so that events get sent
291 // properly after the menu finishes running. If we don't do this, then 343 // properly after the menu finishes running. If we don't do this, then
292 // the first click to other parts of the UI is eaten. 344 // the first click to other parts of the UI is eaten.
293 SetMouseHandler(NULL); 345 SetMouseHandler(NULL);
294 346
295 // Set the state back to normal after the drop down menu is closed. 347 // Set the state back to normal after the drop down menu is closed.
296 if (state_ != STATE_DISABLED) 348 if (state_ != STATE_DISABLED)
297 SetState(STATE_NORMAL); 349 SetState(STATE_NORMAL);
298 } 350 }
299 351
352 void ToolbarButton::LayoutInkDrop() {
353 ink_drop_animation_controller_->SetSize(gfx::Size(width(), height()));
354 }
355
300 const char* ToolbarButton::GetClassName() const { 356 const char* ToolbarButton::GetClassName() const {
301 return "ToolbarButton"; 357 return "ToolbarButton";
302 } 358 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698