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

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

Issue 1411833006: Refactoring to make adding ink drop animations easier (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor ink drop animations (comments in ui/views/) Created 5 years 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_action_view.h" 5 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/sessions/session_tab_helper.h" 12 #include "chrome/browser/sessions/session_tab_helper.h"
13 #include "chrome/browser/themes/theme_service.h" 13 #include "chrome/browser/themes/theme_service.h"
14 #include "chrome/browser/themes/theme_service_factory.h" 14 #include "chrome/browser/themes/theme_service_factory.h"
15 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" 15 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
16 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" 16 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
17 #include "chrome/browser/ui/view_ids.h" 17 #include "chrome/browser/ui/view_ids.h"
18 #include "chrome/grit/generated_resources.h" 18 #include "chrome/grit/generated_resources.h"
19 #include "content/public/browser/notification_source.h" 19 #include "content/public/browser/notification_source.h"
20 #include "grit/theme_resources.h" 20 #include "grit/theme_resources.h"
21 #include "ui/accessibility/ax_view_state.h" 21 #include "ui/accessibility/ax_view_state.h"
22 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/compositor/paint_recorder.h" 23 #include "ui/compositor/paint_recorder.h"
24 #include "ui/events/event.h" 24 #include "ui/events/event.h"
25 #include "ui/gfx/image/image_skia.h" 25 #include "ui/gfx/image/image_skia.h"
26 #include "ui/gfx/image/image_skia_operations.h" 26 #include "ui/gfx/image/image_skia_operations.h"
27 #include "ui/gfx/image/image_skia_source.h" 27 #include "ui/gfx/image/image_skia_source.h"
28 #include "ui/resources/grit/ui_resources.h" 28 #include "ui/resources/grit/ui_resources.h"
29 #include "ui/views/animation/toolbar_ink_drop_delegate.h"
29 #include "ui/views/controls/button/label_button_border.h" 30 #include "ui/views/controls/button/label_button_border.h"
30 #include "ui/views/controls/menu/menu_controller.h" 31 #include "ui/views/controls/menu/menu_controller.h"
31 #include "ui/views/controls/menu/menu_model_adapter.h" 32 #include "ui/views/controls/menu/menu_model_adapter.h"
32 #include "ui/views/controls/menu/menu_runner.h" 33 #include "ui/views/controls/menu/menu_runner.h"
33 #include "ui/views/mouse_constants.h" 34 #include "ui/views/mouse_constants.h"
34 #include "ui/views/resources/grit/views_resources.h" 35 #include "ui/views/resources/grit/views_resources.h"
35 36
36 using views::LabelButtonBorder; 37 using views::LabelButtonBorder;
37 38
38 namespace { 39 namespace {
(...skipping 21 matching lines...) Expand all
60 Profile* profile, 61 Profile* profile,
61 ToolbarActionView::Delegate* delegate) 62 ToolbarActionView::Delegate* delegate)
62 : MenuButton(nullptr, base::string16(), this, false), 63 : MenuButton(nullptr, base::string16(), this, false),
63 view_controller_(view_controller), 64 view_controller_(view_controller),
64 profile_(profile), 65 profile_(profile),
65 delegate_(delegate), 66 delegate_(delegate),
66 called_register_command_(false), 67 called_register_command_(false),
67 wants_to_run_(false), 68 wants_to_run_(false),
68 menu_(nullptr), 69 menu_(nullptr),
69 weak_factory_(this) { 70 weak_factory_(this) {
71 ink_drop_delegate_.reset(new views::ToolbarInkDropDelegate(this, this));
70 set_id(VIEW_ID_BROWSER_ACTION); 72 set_id(VIEW_ID_BROWSER_ACTION);
71 view_controller_->SetDelegate(this); 73 view_controller_->SetDelegate(this);
72 SetHorizontalAlignment(gfx::ALIGN_CENTER); 74 SetHorizontalAlignment(gfx::ALIGN_CENTER);
73 set_drag_controller(delegate_); 75 set_drag_controller(delegate_);
74 76
75 set_context_menu_controller(this); 77 set_context_menu_controller(this);
76 78
79 const int kInkDropLargeSize = 32;
80 const int kInkDropLargeCornerRadius = 5;
81 const int kInkDropSmallSize = 24;
82 const int kInkDropSmallCornerRadius = 2;
83 ink_drop_delegate()->SetInkDropSize(
84 kInkDropLargeSize, kInkDropLargeCornerRadius, kInkDropSmallSize,
85 kInkDropSmallCornerRadius);
86
77 // We also listen for browser theme changes on linux because a switch from or 87 // We also listen for browser theme changes on linux because a switch from or
78 // to GTK requires that we regrab our browser action images. 88 // to GTK requires that we regrab our browser action images.
79 registrar_.Add( 89 registrar_.Add(
80 this, 90 this,
81 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 91 chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
82 content::Source<ThemeService>( 92 content::Source<ThemeService>(
83 ThemeServiceFactory::GetForProfile(profile_))); 93 ThemeServiceFactory::GetForProfile(profile_)));
84 94
85 // If the button is within a menu, we need to make it focusable in order to 95 // If the button is within a menu, we need to make it focusable in order to
86 // have it accessible via keyboard navigation, but it shouldn't request focus 96 // have it accessible via keyboard navigation, but it shouldn't request focus
87 // (because that would close the menu). 97 // (because that would close the menu).
88 if (delegate_->ShownInsideMenu()) { 98 if (delegate_->ShownInsideMenu()) {
89 set_request_focus_on_press(false); 99 set_request_focus_on_press(false);
90 SetFocusable(true); 100 SetFocusable(true);
91 } 101 }
92 102
93 UpdateState(); 103 UpdateState();
94 } 104 }
95 105
96 ToolbarActionView::~ToolbarActionView() { 106 ToolbarActionView::~ToolbarActionView() {
97 if (context_menu_owner == this) 107 if (context_menu_owner == this)
98 context_menu_owner = nullptr; 108 context_menu_owner = nullptr;
99 view_controller_->SetDelegate(nullptr); 109 view_controller_->SetDelegate(nullptr);
100 } 110 }
101 111
102 gfx::Size ToolbarActionView::GetPreferredSize() const {
103 return gfx::Size(ToolbarActionsBar::IconWidth(false),
104 ToolbarActionsBar::IconHeight());
105 }
106
107 void ToolbarActionView::OnDragDone() {
108 views::MenuButton::OnDragDone();
109 delegate_->OnToolbarActionViewDragDone();
110 }
111
112 void ToolbarActionView::ViewHierarchyChanged(
113 const ViewHierarchyChangedDetails& details) {
114 if (details.is_add && !called_register_command_ && GetFocusManager()) {
115 view_controller_->RegisterCommand();
116 called_register_command_ = true;
117 }
118
119 MenuButton::ViewHierarchyChanged(details);
120 }
121
122 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { 112 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) {
123 views::MenuButton::GetAccessibleState(state); 113 views::MenuButton::GetAccessibleState(state);
124 state->role = ui::AX_ROLE_BUTTON; 114 state->role = ui::AX_ROLE_BUTTON;
125 } 115 }
126 116
127 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, 117 scoped_ptr<LabelButtonBorder> ToolbarActionView::CreateDefaultBorder() const {
128 const gfx::Point& point) { 118 scoped_ptr<LabelButtonBorder> border = LabelButton::CreateDefaultBorder();
129 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { 119 border->set_insets(gfx::Insets(kBorderInset, kBorderInset,
130 // We should only get a button pressed event with a non-enabled action if 120 kBorderInset, kBorderInset));
131 // the left-click behavior should open the menu. 121 return border.Pass();
132 DCHECK(view_controller_->DisabledClickOpensMenu()); 122 }
133 context_menu_controller()->ShowContextMenuForView(this, point, 123
134 ui::MENU_SOURCE_NONE); 124 void ToolbarActionView::OnMouseEntered(const ui::MouseEvent& event) {
135 } else { 125 delegate_->OnMouseEnteredToolbarActionView();
136 view_controller_->ExecuteAction(true); 126 views::MenuButton::OnMouseEntered(event);
137 } 127 }
128
129 bool ToolbarActionView::ShouldEnterPushedState(const ui::Event& event) {
130 return views::MenuButton::ShouldEnterPushedState(event) &&
131 (base::TimeTicks::Now() - popup_closed_time_).InMilliseconds() >
132 views::kMinimumMsBetweenButtonClicks;
133 }
134
135 content::WebContents* ToolbarActionView::GetCurrentWebContents() const {
136 return delegate_->GetCurrentWebContents();
138 } 137 }
139 138
140 void ToolbarActionView::UpdateState() { 139 void ToolbarActionView::UpdateState() {
141 content::WebContents* web_contents = GetCurrentWebContents(); 140 content::WebContents* web_contents = GetCurrentWebContents();
142 if (SessionTabHelper::IdForTab(web_contents) < 0) 141 if (SessionTabHelper::IdForTab(web_contents) < 0)
143 return; 142 return;
144 143
145 if (!view_controller_->IsEnabled(web_contents) && 144 if (!view_controller_->IsEnabled(web_contents) &&
146 !view_controller_->DisabledClickOpensMenu()) { 145 !view_controller_->DisabledClickOpensMenu()) {
147 SetState(views::CustomButton::STATE_DISABLED); 146 SetState(views::CustomButton::STATE_DISABLED);
(...skipping 15 matching lines...) Expand all
163 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); 162 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon));
164 } 163 }
165 164
166 SetTooltipText(view_controller_->GetTooltip(web_contents)); 165 SetTooltipText(view_controller_->GetTooltip(web_contents));
167 SetAccessibleName(view_controller_->GetAccessibleName(web_contents)); 166 SetAccessibleName(view_controller_->GetAccessibleName(web_contents));
168 167
169 Layout(); // We need to layout since we may have added an icon as a result. 168 Layout(); // We need to layout since we may have added an icon as a result.
170 SchedulePaint(); 169 SchedulePaint();
171 } 170 }
172 171
172 void ToolbarActionView::OnMenuButtonClicked(views::View* sender,
173 const gfx::Point& point) {
174 if (!view_controller_->IsEnabled(GetCurrentWebContents())) {
175 // We should only get a button pressed event with a non-enabled action if
176 // the left-click behavior should open the menu.
177 DCHECK(view_controller_->DisabledClickOpensMenu());
178 context_menu_controller()->ShowContextMenuForView(this, point,
179 ui::MENU_SOURCE_NONE);
180 } else {
181 view_controller_->ExecuteAction(true);
182 }
183 }
184
173 void ToolbarActionView::Observe(int type, 185 void ToolbarActionView::Observe(int type,
174 const content::NotificationSource& source, 186 const content::NotificationSource& source,
175 const content::NotificationDetails& details) { 187 const content::NotificationDetails& details) {
176 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); 188 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
177 UpdateState(); 189 UpdateState();
178 } 190 }
179 191
180 void ToolbarActionView::OnMouseEntered(const ui::MouseEvent& event) { 192 void ToolbarActionView::AddInkDropLayer(ui::Layer* ink_drop_layer) {
181 delegate_->OnMouseEnteredToolbarActionView(); 193 SetPaintToLayer(true);
182 views::MenuButton::OnMouseEntered(event); 194 SetFillsBoundsOpaquely(false);
195 image()->SetPaintToLayer(true);
196 image()->SetFillsBoundsOpaquely(false);
197
198 layer()->Add(ink_drop_layer);
199 layer()->StackAtBottom(ink_drop_layer);
183 } 200 }
184 201
185 bool ToolbarActionView::ShouldEnterPushedState(const ui::Event& event) { 202 void ToolbarActionView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
186 return views::MenuButton::ShouldEnterPushedState(event) && 203 layer()->Remove(ink_drop_layer);
187 (base::TimeTicks::Now() - popup_closed_time_).InMilliseconds() >
188 views::kMinimumMsBetweenButtonClicks;
189 }
190 204
191 scoped_ptr<LabelButtonBorder> ToolbarActionView::CreateDefaultBorder() const { 205 image()->SetFillsBoundsOpaquely(true);
192 scoped_ptr<LabelButtonBorder> border = LabelButton::CreateDefaultBorder(); 206 image()->SetPaintToLayer(false);
193 border->set_insets(gfx::Insets(kBorderInset, kBorderInset, 207 SetPaintToLayer(false);
194 kBorderInset, kBorderInset));
195 return border.Pass();
196 } 208 }
197 209
198 gfx::ImageSkia ToolbarActionView::GetIconForTest() { 210 gfx::ImageSkia ToolbarActionView::GetIconForTest() {
199 return GetImage(views::Button::STATE_NORMAL); 211 return GetImage(views::Button::STATE_NORMAL);
200 } 212 }
201 213
202 void ToolbarActionView::set_context_menu_callback_for_testing( 214 void ToolbarActionView::set_context_menu_callback_for_testing(
203 base::Callback<void(ToolbarActionView*)>* callback) { 215 base::Callback<void(ToolbarActionView*)>* callback) {
204 context_menu_callback = callback; 216 context_menu_callback = callback;
205 } 217 }
206 218
219 gfx::Size ToolbarActionView::GetPreferredSize() const {
220 return gfx::Size(ToolbarActionsBar::IconWidth(false),
221 ToolbarActionsBar::IconHeight());
222 }
223
224 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) {
225 // views::MenuButton actions are only triggered by left mouse clicks.
226 if (event.IsOnlyLeftMouseButton())
227 ink_drop_delegate()->OnAction(views::InkDropState::ACTION_PENDING);
228 return MenuButton::OnMousePressed(event);
229 }
230
231 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) {
232 // While the dropdown menu is showing, the button should not handle gestures.
233 if (menu_)
234 event->StopPropagation();
235 else
236 MenuButton::OnGestureEvent(event);
237 }
238
239 void ToolbarActionView::OnDragDone() {
240 views::MenuButton::OnDragDone();
241 delegate_->OnToolbarActionViewDragDone();
242 }
243
244 void ToolbarActionView::ViewHierarchyChanged(
245 const ViewHierarchyChangedDetails& details) {
246 if (details.is_add && !called_register_command_ && GetFocusManager()) {
247 view_controller_->RegisterCommand();
248 called_register_command_ = true;
249 }
250
251 MenuButton::ViewHierarchyChanged(details);
252 }
253
207 views::View* ToolbarActionView::GetAsView() { 254 views::View* ToolbarActionView::GetAsView() {
208 return this; 255 return this;
209 } 256 }
210 257
211 views::FocusManager* ToolbarActionView::GetFocusManagerForAccelerator() { 258 views::FocusManager* ToolbarActionView::GetFocusManagerForAccelerator() {
212 return GetFocusManager(); 259 return GetFocusManager();
213 } 260 }
214 261
215 views::View* ToolbarActionView::GetReferenceViewForPopup() { 262 views::View* ToolbarActionView::GetReferenceViewForPopup() {
216 // Browser actions in the overflow menu can still show popups, so we may need 263 // Browser actions in the overflow menu can still show popups, so we may need
217 // a reference view other than this button's parent. If so, use the overflow 264 // a reference view other than this button's parent. If so, use the overflow
218 // view. 265 // view.
219 return visible() ? this : delegate_->GetOverflowReferenceView(); 266 return visible() ? this : delegate_->GetOverflowReferenceView();
220 } 267 }
221 268
222 bool ToolbarActionView::IsMenuRunning() const { 269 bool ToolbarActionView::IsMenuRunning() const {
223 return menu_ != nullptr; 270 return menu_ != nullptr;
224 } 271 }
225 272
226 content::WebContents* ToolbarActionView::GetCurrentWebContents() const {
227 return delegate_->GetCurrentWebContents();
228 }
229
230 void ToolbarActionView::OnPopupShown(bool by_user) { 273 void ToolbarActionView::OnPopupShown(bool by_user) {
231 // If this was through direct user action, we press the menu button. 274 // If this was through direct user action, we press the menu button.
232 if (by_user) { 275 if (by_user) {
233 // We set the state of the menu button we're using as a reference view, 276 // We set the state of the menu button we're using as a reference view,
234 // which is either this or the overflow reference view. 277 // which is either this or the overflow reference view.
235 // This cast is safe because GetReferenceViewForPopup returns either |this| 278 // This cast is safe because GetReferenceViewForPopup returns either |this|
236 // or delegate_->GetOverflowReferenceView(), which returns a MenuButton. 279 // or delegate_->GetOverflowReferenceView(), which returns a MenuButton.
237 views::MenuButton* reference_view = 280 views::MenuButton* reference_view =
238 static_cast<views::MenuButton*>(GetReferenceViewForPopup()); 281 static_cast<views::MenuButton*>(GetReferenceViewForPopup());
239 pressed_lock_.reset(new views::MenuButton::PressedLock(reference_view)); 282 pressed_lock_.reset(new views::MenuButton::PressedLock(reference_view));
(...skipping 24 matching lines...) Expand all
264 weak_factory_.GetWeakPtr(), 307 weak_factory_.GetWeakPtr(),
265 source_type); 308 source_type);
266 } 309 }
267 if (CloseActiveMenuIfNeeded()) 310 if (CloseActiveMenuIfNeeded())
268 return; 311 return;
269 312
270 // Otherwise, no other menu is showing, and we can proceed normally. 313 // Otherwise, no other menu is showing, and we can proceed normally.
271 DoShowContextMenu(source_type); 314 DoShowContextMenu(source_type);
272 } 315 }
273 316
317 gfx::Point ToolbarActionView::CalculateInkDropCenter() const {
318 return GetLocalBounds().CenterPoint();
319 }
320
274 void ToolbarActionView::DoShowContextMenu( 321 void ToolbarActionView::DoShowContextMenu(
275 ui::MenuSourceType source_type) { 322 ui::MenuSourceType source_type) {
276 ui::MenuModel* context_menu_model = view_controller_->GetContextMenu(); 323 ui::MenuModel* context_menu_model = view_controller_->GetContextMenu();
277 // It's possible the action doesn't have a context menu. 324 // It's possible the action doesn't have a context menu.
278 if (!context_menu_model) 325 if (!context_menu_model)
279 return; 326 return;
280 327
281 DCHECK(visible()); // We should never show a context menu for a hidden item. 328 DCHECK(visible()); // We should never show a context menu for a hidden item.
282 DCHECK(!context_menu_owner); 329 DCHECK(!context_menu_owner);
283 330
284 gfx::Point screen_loc; 331 gfx::Point screen_loc;
285 ConvertPointToScreen(this, &screen_loc); 332 ConvertPointToScreen(this, &screen_loc);
286 333
287 int run_types = 334 int run_types =
288 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; 335 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU;
289 if (delegate_->ShownInsideMenu()) 336 if (delegate_->ShownInsideMenu())
290 run_types |= views::MenuRunner::IS_NESTED; 337 run_types |= views::MenuRunner::IS_NESTED;
291 338
292 // RunMenuAt expects a nested menu to be parented by the same widget as the 339 // RunMenuAt expects a nested menu to be parented by the same widget as the
293 // already visible menu, in this case the Chrome menu. 340 // already visible menu, in this case the Chrome menu.
294 views::Widget* parent = delegate_->ShownInsideMenu() ? 341 views::Widget* parent = delegate_->ShownInsideMenu() ?
295 delegate_->GetOverflowReferenceView()->GetWidget() : 342 delegate_->GetOverflowReferenceView()->GetWidget() :
296 GetWidget(); 343 GetWidget();
297 344
345 ink_drop_delegate()->OnAction(views::InkDropState::ACTIVATED);
346
298 views::MenuModelAdapter adapter(context_menu_model); 347 views::MenuModelAdapter adapter(context_menu_model);
299 menu_ = adapter.CreateMenu(); 348 menu_ = adapter.CreateMenu();
300 menu_runner_.reset(new views::MenuRunner(menu_, run_types)); 349 menu_runner_.reset(new views::MenuRunner(menu_, run_types));
301 350
302 if (context_menu_callback) 351 if (context_menu_callback)
303 context_menu_callback->Run(this); 352 context_menu_callback->Run(this);
304 if (menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()), 353 if (menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()),
305 views::MENU_ANCHOR_TOPLEFT, 354 views::MENU_ANCHOR_TOPLEFT,
306 source_type) == views::MenuRunner::MENU_DELETED) { 355 source_type) == views::MenuRunner::MENU_DELETED) {
307 return; 356 return;
308 } 357 }
358 ink_drop_delegate()->OnAction(views::InkDropState::DEACTIVATED);
309 359
310 menu_runner_.reset(); 360 menu_runner_.reset();
311 menu_ = nullptr; 361 menu_ = nullptr;
312 context_menu_owner = nullptr; 362 context_menu_owner = nullptr;
313 view_controller_->OnContextMenuClosed(); 363 view_controller_->OnContextMenuClosed();
314 364
315 // If another extension action wants to show its context menu, allow it to. 365 // If another extension action wants to show its context menu, allow it to.
316 if (!followup_context_menu_task_.is_null()) { 366 if (!followup_context_menu_task_.is_null()) {
317 base::Closure task = followup_context_menu_task_; 367 base::Closure task = followup_context_menu_task_;
318 followup_context_menu_task_ = base::Closure(); 368 followup_context_menu_task_ = base::Closure();
(...skipping 14 matching lines...) Expand all
333 if (menu_controller->in_nested_run()) { 383 if (menu_controller->in_nested_run()) {
334 // There is another menu showing. Close the outermost menu (since we are 384 // There is another menu showing. Close the outermost menu (since we are
335 // shown in the same menu, we don't want to close the whole thing). 385 // shown in the same menu, we don't want to close the whole thing).
336 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); 386 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST);
337 return true; 387 return true;
338 } 388 }
339 } 389 }
340 390
341 return false; 391 return false;
342 } 392 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698