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

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 (rebased) Created 5 years, 1 month 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 20 matching lines...) Expand all
59 ToolbarActionViewController* view_controller, 60 ToolbarActionViewController* view_controller,
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),
70 ink_drop_delegate_(new views::ToolbarInkDropDelegate(this, this)),
69 weak_factory_(this) { 71 weak_factory_(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 GetInkDropDelegate()->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
112 void ToolbarActionView::AddInkDropLayer(ui::Layer* ink_drop_layer) {
113 SetPaintToLayer(true);
114 SetFillsBoundsOpaquely(false);
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
134 views::InkDropDelegate* ToolbarActionView::GetInkDropDelegate() const {
135 return ink_drop_delegate_.get();
136 }
137
102 gfx::Size ToolbarActionView::GetPreferredSize() const { 138 gfx::Size ToolbarActionView::GetPreferredSize() const {
103 return gfx::Size(ToolbarActionsBar::IconWidth(false), 139 return gfx::Size(ToolbarActionsBar::IconWidth(false),
104 ToolbarActionsBar::IconHeight()); 140 ToolbarActionsBar::IconHeight());
105 } 141 }
106 142
143 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) {
144 // views::MenuButton actions are only triggered by left mouse clicks.
145 if (event.IsOnlyLeftMouseButton())
146 GetInkDropDelegate()->OnAction(views::InkDropState::ACTION_PENDING);
147 return MenuButton::OnMousePressed(event);
148 }
149
150 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) {
151 if (menu_) {
152 // While dropdown menu is showing the button should not handle gestures.
153 event->StopPropagation();
154 return;
155 }
156 MenuButton::OnGestureEvent(event);
Peter Kasting 2015/11/24 20:42:00 Nit: Shorter: // While the dropdown menu is sho
varkha 2015/11/24 21:50:43 Done.
157 }
158
107 void ToolbarActionView::OnDragDone() { 159 void ToolbarActionView::OnDragDone() {
108 views::MenuButton::OnDragDone(); 160 views::MenuButton::OnDragDone();
109 delegate_->OnToolbarActionViewDragDone(); 161 delegate_->OnToolbarActionViewDragDone();
110 } 162 }
111 163
112 void ToolbarActionView::ViewHierarchyChanged( 164 void ToolbarActionView::ViewHierarchyChanged(
113 const ViewHierarchyChangedDetails& details) { 165 const ViewHierarchyChangedDetails& details) {
114 if (details.is_add && !called_register_command_ && GetFocusManager()) { 166 if (details.is_add && !called_register_command_ && GetFocusManager()) {
115 view_controller_->RegisterCommand(); 167 view_controller_->RegisterCommand();
116 called_register_command_ = true; 168 called_register_command_ = true;
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; 340 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU;
289 if (delegate_->ShownInsideMenu()) 341 if (delegate_->ShownInsideMenu())
290 run_types |= views::MenuRunner::IS_NESTED; 342 run_types |= views::MenuRunner::IS_NESTED;
291 343
292 // RunMenuAt expects a nested menu to be parented by the same widget as the 344 // RunMenuAt expects a nested menu to be parented by the same widget as the
293 // already visible menu, in this case the Chrome menu. 345 // already visible menu, in this case the Chrome menu.
294 views::Widget* parent = delegate_->ShownInsideMenu() ? 346 views::Widget* parent = delegate_->ShownInsideMenu() ?
295 delegate_->GetOverflowReferenceView()->GetWidget() : 347 delegate_->GetOverflowReferenceView()->GetWidget() :
296 GetWidget(); 348 GetWidget();
297 349
350 GetInkDropDelegate()->OnAction(views::InkDropState::ACTIVATED);
351
298 views::MenuModelAdapter adapter(context_menu_model); 352 views::MenuModelAdapter adapter(context_menu_model);
299 menu_ = adapter.CreateMenu(); 353 menu_ = adapter.CreateMenu();
300 menu_runner_.reset(new views::MenuRunner(menu_, run_types)); 354 menu_runner_.reset(new views::MenuRunner(menu_, run_types));
301 355
302 if (context_menu_callback) 356 if (context_menu_callback)
303 context_menu_callback->Run(this); 357 context_menu_callback->Run(this);
304 if (menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()), 358 if (menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()),
305 views::MENU_ANCHOR_TOPLEFT, 359 views::MENU_ANCHOR_TOPLEFT,
306 source_type) == views::MenuRunner::MENU_DELETED) { 360 source_type) == views::MenuRunner::MENU_DELETED) {
361 menu_ = nullptr;
Peter Kasting 2015/11/24 20:42:01 I believe if we're in this arm, |this| may be null
varkha 2015/11/24 21:50:43 Yes, I see now why it wasn't done when |menu_| was
307 return; 362 return;
308 } 363 }
364 GetInkDropDelegate()->OnAction(views::InkDropState::DEACTIVATED);
309 365
310 menu_runner_.reset(); 366 menu_runner_.reset();
311 menu_ = nullptr; 367 menu_ = nullptr;
312 context_menu_owner = nullptr; 368 context_menu_owner = nullptr;
313 view_controller_->OnContextMenuClosed(); 369 view_controller_->OnContextMenuClosed();
314 370
315 // If another extension action wants to show its context menu, allow it to. 371 // If another extension action wants to show its context menu, allow it to.
316 if (!followup_context_menu_task_.is_null()) { 372 if (!followup_context_menu_task_.is_null()) {
317 base::Closure task = followup_context_menu_task_; 373 base::Closure task = followup_context_menu_task_;
318 followup_context_menu_task_ = base::Closure(); 374 followup_context_menu_task_ = base::Closure();
(...skipping 14 matching lines...) Expand all
333 if (menu_controller->in_nested_run()) { 389 if (menu_controller->in_nested_run()) {
334 // There is another menu showing. Close the outermost menu (since we are 390 // 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). 391 // shown in the same menu, we don't want to close the whole thing).
336 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); 392 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST);
337 return true; 393 return true;
338 } 394 }
339 } 395 }
340 396
341 return false; 397 return false;
342 } 398 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698