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

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

Issue 1409183003: Adds ripple animation to extension buttons on a toolbar (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adds ripple animation to extension buttons on a toolbar (gesture support) Created 5 years, 2 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_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 "chrome/browser/chrome_notification_types.h" 9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sessions/session_tab_helper.h" 11 #include "chrome/browser/sessions/session_tab_helper.h"
12 #include "chrome/browser/themes/theme_service.h" 12 #include "chrome/browser/themes/theme_service.h"
13 #include "chrome/browser/themes/theme_service_factory.h" 13 #include "chrome/browser/themes/theme_service_factory.h"
14 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" 14 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
15 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" 15 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
16 #include "chrome/browser/ui/view_ids.h" 16 #include "chrome/browser/ui/view_ids.h"
17 #include "chrome/grit/generated_resources.h" 17 #include "chrome/grit/generated_resources.h"
18 #include "content/public/browser/notification_source.h" 18 #include "content/public/browser/notification_source.h"
19 #include "grit/theme_resources.h" 19 #include "grit/theme_resources.h"
20 #include "ui/accessibility/ax_view_state.h" 20 #include "ui/accessibility/ax_view_state.h"
21 #include "ui/base/resource/resource_bundle.h" 21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/compositor/paint_recorder.h" 22 #include "ui/compositor/paint_recorder.h"
23 #include "ui/events/event.h" 23 #include "ui/events/event.h"
24 #include "ui/gfx/image/image_skia.h" 24 #include "ui/gfx/image/image_skia.h"
25 #include "ui/gfx/image/image_skia_operations.h" 25 #include "ui/gfx/image/image_skia_operations.h"
26 #include "ui/gfx/image/image_skia_source.h" 26 #include "ui/gfx/image/image_skia_source.h"
27 #include "ui/resources/grit/ui_resources.h" 27 #include "ui/resources/grit/ui_resources.h"
28 #include "ui/views/animation/ink_drop_animation_controller.h"
29 #include "ui/views/animation/ink_drop_animation_controller_factory.h"
28 #include "ui/views/controls/button/label_button_border.h" 30 #include "ui/views/controls/button/label_button_border.h"
29 #include "ui/views/controls/menu/menu_controller.h" 31 #include "ui/views/controls/menu/menu_controller.h"
30 #include "ui/views/controls/menu/menu_runner.h" 32 #include "ui/views/controls/menu/menu_runner.h"
31 #include "ui/views/mouse_constants.h" 33 #include "ui/views/mouse_constants.h"
32 #include "ui/views/resources/grit/views_resources.h" 34 #include "ui/views/resources/grit/views_resources.h"
33 35
34 using views::LabelButtonBorder; 36 using views::LabelButtonBorder;
35 37
36 namespace { 38 namespace {
37 39
(...skipping 15 matching lines...) Expand all
53 ToolbarActionView::ToolbarActionView( 55 ToolbarActionView::ToolbarActionView(
54 ToolbarActionViewController* view_controller, 56 ToolbarActionViewController* view_controller,
55 Profile* profile, 57 Profile* profile,
56 ToolbarActionView::Delegate* delegate) 58 ToolbarActionView::Delegate* delegate)
57 : MenuButton(nullptr, base::string16(), this, false), 59 : MenuButton(nullptr, base::string16(), this, false),
58 view_controller_(view_controller), 60 view_controller_(view_controller),
59 profile_(profile), 61 profile_(profile),
60 delegate_(delegate), 62 delegate_(delegate),
61 called_register_command_(false), 63 called_register_command_(false),
62 wants_to_run_(false), 64 wants_to_run_(false),
65 menu_showing_(false),
66 ink_drop_animation_controller_(
67 views::InkDropAnimationControllerFactory::
68 CreateInkDropAnimationController(this)),
63 weak_factory_(this) { 69 weak_factory_(this) {
64 set_id(VIEW_ID_BROWSER_ACTION); 70 set_id(VIEW_ID_BROWSER_ACTION);
65 view_controller_->SetDelegate(this); 71 view_controller_->SetDelegate(this);
66 SetHorizontalAlignment(gfx::ALIGN_CENTER); 72 SetHorizontalAlignment(gfx::ALIGN_CENTER);
67 set_drag_controller(delegate_); 73 set_drag_controller(delegate_);
68 74
69 set_context_menu_controller(this); 75 set_context_menu_controller(this);
70 76
77 const int kInkDropLargeSize = 32;
78 const int kInkDropLargeCornerRadius = 5;
79 const int kInkDropSmallSize = 24;
80 const int kInkDropSmallCornerRadius = 2;
81
82 ink_drop_animation_controller_->SetInkDropSize(
83 gfx::Size(kInkDropLargeSize, kInkDropLargeSize),
84 kInkDropLargeCornerRadius,
85 gfx::Size(kInkDropSmallSize, kInkDropSmallSize),
86 kInkDropSmallCornerRadius);
87
71 // We also listen for browser theme changes on linux because a switch from or 88 // We also listen for browser theme changes on linux because a switch from or
72 // to GTK requires that we regrab our browser action images. 89 // to GTK requires that we regrab our browser action images.
73 registrar_.Add( 90 registrar_.Add(
74 this, 91 this,
75 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 92 chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
76 content::Source<ThemeService>( 93 content::Source<ThemeService>(
77 ThemeServiceFactory::GetForProfile(profile_))); 94 ThemeServiceFactory::GetForProfile(profile_)));
78 95
79 // If the button is within a menu, we need to make it focusable in order to 96 // If the button is within a menu, we need to make it focusable in order to
80 // have it accessible via keyboard navigation, but it shouldn't request focus 97 // have it accessible via keyboard navigation, but it shouldn't request focus
81 // (because that would close the menu). 98 // (because that would close the menu).
82 if (delegate_->ShownInsideMenu()) { 99 if (delegate_->ShownInsideMenu()) {
83 set_request_focus_on_press(false); 100 set_request_focus_on_press(false);
84 SetFocusable(true); 101 SetFocusable(true);
85 } 102 }
86 103
87 UpdateState(); 104 UpdateState();
88 } 105 }
89 106
90 ToolbarActionView::~ToolbarActionView() { 107 ToolbarActionView::~ToolbarActionView() {
91 if (context_menu_owner == this) 108 if (context_menu_owner == this)
92 context_menu_owner = nullptr; 109 context_menu_owner = nullptr;
93 view_controller_->SetDelegate(nullptr); 110 view_controller_->SetDelegate(nullptr);
94 } 111 }
95 112
113 void ToolbarActionView::AddInkDropLayer(ui::Layer* ink_drop_layer) {
114 SetPaintToLayer(true);
115 image()->SetPaintToLayer(true);
116 image()->SetFillsBoundsOpaquely(false);
117
118 layer()->Add(ink_drop_layer);
119 layer()->StackAtBottom(ink_drop_layer);
120 }
121
122 void ToolbarActionView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
123 layer()->Remove(ink_drop_layer);
124
125 image()->SetFillsBoundsOpaquely(true);
126 image()->SetPaintToLayer(false);
127 SetPaintToLayer(false);
128 }
129
130 gfx::Point ToolbarActionView::CalculateInkDropCenter() const {
131 return GetLocalBounds().CenterPoint();
132 }
133
96 gfx::Size ToolbarActionView::GetPreferredSize() const { 134 gfx::Size ToolbarActionView::GetPreferredSize() const {
97 return gfx::Size(ToolbarActionsBar::IconWidth(false), 135 return gfx::Size(ToolbarActionsBar::IconWidth(false),
98 ToolbarActionsBar::IconHeight()); 136 ToolbarActionsBar::IconHeight());
99 } 137 }
100 138
139 void ToolbarActionView::Layout() {
140 MenuButton::Layout();
141 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter());
142 }
143
144 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) {
145 // views::MenuButton actions are only triggered by left mouse clicks.
146 if (event.IsLeftMouseButton()) {
147 ink_drop_animation_controller_->AnimateToState(
148 views::InkDropState::ACTION_PENDING);
149 }
150 return MenuButton::OnMousePressed(event);
151 }
152
153 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) {
154 if (menu_showing_) {
155 // While dropdown menu is showing the button should not handle gestures.
156 event->StopPropagation();
157 return;
158 }
159
160 MenuButton::OnGestureEvent(event);
161
162 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN;
163 switch (event->type()) {
164 case ui::ET_GESTURE_TAP_DOWN:
165 ink_drop_state = views::InkDropState::ACTION_PENDING;
166 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that
167 // subsequent events for the gesture are sent to |this|.
168 event->SetHandled();
169 break;
170 case ui::ET_GESTURE_LONG_PRESS:
171 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING;
172 break;
173 case ui::ET_GESTURE_LONG_TAP:
174 ink_drop_state = views::InkDropState::SLOW_ACTION;
175 break;
176 case ui::ET_GESTURE_END:
177 case ui::ET_GESTURE_TAP_CANCEL:
178 ink_drop_state = views::InkDropState::HIDDEN;
179 break;
180 default:
181 return;
182 }
183 ink_drop_animation_controller_->AnimateToState(ink_drop_state);
184 }
185
sadrul 2015/10/21 21:35:20 I am not a fan of how all this code looks so very
varkha 2015/10/21 22:44:08 We were discussing with bruthig@ how to refactor t
101 void ToolbarActionView::OnDragDone() { 186 void ToolbarActionView::OnDragDone() {
187 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
102 views::MenuButton::OnDragDone(); 188 views::MenuButton::OnDragDone();
103 delegate_->OnToolbarActionViewDragDone(); 189 delegate_->OnToolbarActionViewDragDone();
104 } 190 }
105 191
106 void ToolbarActionView::ViewHierarchyChanged( 192 void ToolbarActionView::ViewHierarchyChanged(
107 const ViewHierarchyChangedDetails& details) { 193 const ViewHierarchyChangedDetails& details) {
108 if (details.is_add && !called_register_command_ && GetFocusManager()) { 194 if (details.is_add && !called_register_command_ && GetFocusManager()) {
109 view_controller_->RegisterCommand(); 195 view_controller_->RegisterCommand();
110 called_register_command_ = true; 196 called_register_command_ = true;
111 } 197 }
112 198
113 MenuButton::ViewHierarchyChanged(details); 199 MenuButton::ViewHierarchyChanged(details);
114 } 200 }
115 201
116 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { 202 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) {
117 views::MenuButton::GetAccessibleState(state); 203 views::MenuButton::GetAccessibleState(state);
118 state->role = ui::AX_ROLE_BUTTON; 204 state->role = ui::AX_ROLE_BUTTON;
119 } 205 }
120 206
121 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, 207 void ToolbarActionView::OnMenuButtonClicked(views::View* sender,
122 const gfx::Point& point) { 208 const gfx::Point& point) {
123 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { 209 if (!view_controller_->IsEnabled(GetCurrentWebContents())) {
124 // We should only get a button pressed event with a non-enabled action if 210 // We should only get a button pressed event with a non-enabled action if
125 // the left-click behavior should open the menu. 211 // the left-click behavior should open the menu.
126 DCHECK(view_controller_->DisabledClickOpensMenu()); 212 DCHECK(view_controller_->DisabledClickOpensMenu());
127 context_menu_controller()->ShowContextMenuForView(this, point, 213 context_menu_controller()->ShowContextMenuForView(this, point,
128 ui::MENU_SOURCE_NONE); 214 ui::MENU_SOURCE_NONE);
129 } else { 215 } else {
216 ink_drop_animation_controller_->AnimateToState(
217 views::InkDropState::QUICK_ACTION);
130 view_controller_->ExecuteAction(true); 218 view_controller_->ExecuteAction(true);
131 } 219 }
132 } 220 }
133 221
222 void ToolbarActionView::OnMenuButtonReleasedWithoutClick(views::View* sender) {
223 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
sadrul 2015/10/21 21:35:20 Can this be done from OnMouseReleased instead?
varkha 2015/10/21 22:44:08 This is done with the new callback because it woul
224 }
225
134 void ToolbarActionView::UpdateState() { 226 void ToolbarActionView::UpdateState() {
135 content::WebContents* web_contents = GetCurrentWebContents(); 227 content::WebContents* web_contents = GetCurrentWebContents();
136 if (SessionTabHelper::IdForTab(web_contents) < 0) 228 if (SessionTabHelper::IdForTab(web_contents) < 0)
137 return; 229 return;
138 230
139 if (!view_controller_->IsEnabled(web_contents) && 231 if (!view_controller_->IsEnabled(web_contents) &&
140 !view_controller_->DisabledClickOpensMenu()) { 232 !view_controller_->DisabledClickOpensMenu()) {
141 SetState(views::CustomButton::STATE_DISABLED); 233 SetState(views::CustomButton::STATE_DISABLED);
142 } else if (state() == views::CustomButton::STATE_DISABLED) { 234 } else if (state() == views::CustomButton::STATE_DISABLED) {
143 SetState(views::CustomButton::STATE_NORMAL); 235 SetState(views::CustomButton::STATE_NORMAL);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; 370 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU;
279 if (delegate_->ShownInsideMenu()) 371 if (delegate_->ShownInsideMenu())
280 run_types |= views::MenuRunner::IS_NESTED; 372 run_types |= views::MenuRunner::IS_NESTED;
281 373
282 // RunMenuAt expects a nested menu to be parented by the same widget as the 374 // RunMenuAt expects a nested menu to be parented by the same widget as the
283 // already visible menu, in this case the Chrome menu. 375 // already visible menu, in this case the Chrome menu.
284 views::Widget* parent = delegate_->ShownInsideMenu() ? 376 views::Widget* parent = delegate_->ShownInsideMenu() ?
285 delegate_->GetOverflowReferenceView()->GetWidget() : 377 delegate_->GetOverflowReferenceView()->GetWidget() :
286 GetWidget(); 378 GetWidget();
287 379
380 menu_showing_ = true;
381 ink_drop_animation_controller_->AnimateToState(
382 views::InkDropState::ACTIVATED);
383
288 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types)); 384 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types));
289
290 if (menu_runner_->RunMenuAt(parent, 385 if (menu_runner_->RunMenuAt(parent,
291 this, 386 this,
292 gfx::Rect(screen_loc, size()), 387 gfx::Rect(screen_loc, size()),
293 views::MENU_ANCHOR_TOPLEFT, 388 views::MENU_ANCHOR_TOPLEFT,
294 source_type) == views::MenuRunner::MENU_DELETED) { 389 source_type) == views::MenuRunner::MENU_DELETED) {
295 return; 390 return;
296 } 391 }
392 ink_drop_animation_controller_->AnimateToState(
393 views::InkDropState::DEACTIVATED);
394 menu_showing_ = false;
297 395
298 context_menu_owner = nullptr; 396 context_menu_owner = nullptr;
299 menu_runner_.reset(); 397 menu_runner_.reset();
300 view_controller_->OnContextMenuClosed(); 398 view_controller_->OnContextMenuClosed();
301 399
302 // If another extension action wants to show its context menu, allow it to. 400 // If another extension action wants to show its context menu, allow it to.
303 if (!followup_context_menu_task_.is_null()) { 401 if (!followup_context_menu_task_.is_null()) {
304 base::Closure task = followup_context_menu_task_; 402 base::Closure task = followup_context_menu_task_;
305 followup_context_menu_task_ = base::Closure(); 403 followup_context_menu_task_ = base::Closure();
306 task.Run(); 404 task.Run();
(...skipping 13 matching lines...) Expand all
320 if (menu_controller->in_nested_run()) { 418 if (menu_controller->in_nested_run()) {
321 // There is another menu showing. Close the outermost menu (since we are 419 // There is another menu showing. Close the outermost menu (since we are
322 // shown in the same menu, we don't want to close the whole thing). 420 // shown in the same menu, we don't want to close the whole thing).
323 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); 421 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST);
324 return true; 422 return true;
325 } 423 }
326 } 424 }
327 425
328 return false; 426 return false;
329 } 427 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/toolbar/toolbar_action_view.h ('k') | ui/views/controls/button/menu_button.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698