OLD | NEW |
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/ink_drop_animation_controller.h" | 29 #include "ui/views/animation/toolbar_ink_drop_delegate.h" |
30 #include "ui/views/animation/ink_drop_animation_controller_factory.h" | |
31 #include "ui/views/controls/button/label_button_border.h" | 30 #include "ui/views/controls/button/label_button_border.h" |
32 #include "ui/views/controls/menu/menu_controller.h" | 31 #include "ui/views/controls/menu/menu_controller.h" |
33 #include "ui/views/controls/menu/menu_model_adapter.h" | 32 #include "ui/views/controls/menu/menu_model_adapter.h" |
34 #include "ui/views/controls/menu/menu_runner.h" | 33 #include "ui/views/controls/menu/menu_runner.h" |
35 #include "ui/views/mouse_constants.h" | 34 #include "ui/views/mouse_constants.h" |
36 #include "ui/views/resources/grit/views_resources.h" | 35 #include "ui/views/resources/grit/views_resources.h" |
37 | 36 |
38 using views::LabelButtonBorder; | 37 using views::LabelButtonBorder; |
39 | 38 |
40 namespace { | 39 namespace { |
(...skipping 20 matching lines...) Expand all Loading... |
61 ToolbarActionViewController* view_controller, | 60 ToolbarActionViewController* view_controller, |
62 Profile* profile, | 61 Profile* profile, |
63 ToolbarActionView::Delegate* delegate) | 62 ToolbarActionView::Delegate* delegate) |
64 : MenuButton(nullptr, base::string16(), this, false), | 63 : MenuButton(nullptr, base::string16(), this, false), |
65 view_controller_(view_controller), | 64 view_controller_(view_controller), |
66 profile_(profile), | 65 profile_(profile), |
67 delegate_(delegate), | 66 delegate_(delegate), |
68 called_register_command_(false), | 67 called_register_command_(false), |
69 wants_to_run_(false), | 68 wants_to_run_(false), |
70 menu_(nullptr), | 69 menu_(nullptr), |
71 ink_drop_animation_controller_( | 70 ink_drop_delegate_(new views::ToolbarInkDropDelegate(this, this)), |
72 views::InkDropAnimationControllerFactory:: | |
73 CreateInkDropAnimationController(this)), | |
74 weak_factory_(this) { | 71 weak_factory_(this) { |
75 set_id(VIEW_ID_BROWSER_ACTION); | 72 set_id(VIEW_ID_BROWSER_ACTION); |
76 view_controller_->SetDelegate(this); | 73 view_controller_->SetDelegate(this); |
77 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 74 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
78 set_drag_controller(delegate_); | 75 set_drag_controller(delegate_); |
79 | 76 |
80 set_context_menu_controller(this); | 77 set_context_menu_controller(this); |
81 | 78 |
82 const int kInkDropLargeSize = 32; | 79 const int kInkDropLargeSize = 32; |
83 const int kInkDropLargeCornerRadius = 5; | 80 const int kInkDropLargeCornerRadius = 5; |
84 const int kInkDropSmallSize = 24; | 81 const int kInkDropSmallSize = 24; |
85 const int kInkDropSmallCornerRadius = 2; | 82 const int kInkDropSmallCornerRadius = 2; |
86 | 83 ink_drop_delegate_->SetInkDropSize( |
87 ink_drop_animation_controller_->SetInkDropSize( | 84 kInkDropLargeSize, kInkDropLargeCornerRadius, kInkDropSmallSize, |
88 gfx::Size(kInkDropLargeSize, kInkDropLargeSize), | |
89 kInkDropLargeCornerRadius, | |
90 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
91 kInkDropSmallCornerRadius); | 85 kInkDropSmallCornerRadius); |
92 | 86 |
93 // 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 |
94 // to GTK requires that we regrab our browser action images. | 88 // to GTK requires that we regrab our browser action images. |
95 registrar_.Add( | 89 registrar_.Add( |
96 this, | 90 this, |
97 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 91 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
98 content::Source<ThemeService>( | 92 content::Source<ThemeService>( |
99 ThemeServiceFactory::GetForProfile(profile_))); | 93 ThemeServiceFactory::GetForProfile(profile_))); |
100 | 94 |
(...skipping 28 matching lines...) Expand all Loading... |
129 | 123 |
130 image()->SetFillsBoundsOpaquely(true); | 124 image()->SetFillsBoundsOpaquely(true); |
131 image()->SetPaintToLayer(false); | 125 image()->SetPaintToLayer(false); |
132 SetPaintToLayer(false); | 126 SetPaintToLayer(false); |
133 } | 127 } |
134 | 128 |
135 gfx::Point ToolbarActionView::CalculateInkDropCenter() const { | 129 gfx::Point ToolbarActionView::CalculateInkDropCenter() const { |
136 return GetLocalBounds().CenterPoint(); | 130 return GetLocalBounds().CenterPoint(); |
137 } | 131 } |
138 | 132 |
| 133 views::InkDropDelegate* ToolbarActionView::GetInkDropDelegate() const { |
| 134 return ink_drop_delegate_.get(); |
| 135 } |
| 136 |
139 gfx::Size ToolbarActionView::GetPreferredSize() const { | 137 gfx::Size ToolbarActionView::GetPreferredSize() const { |
140 return gfx::Size(ToolbarActionsBar::IconWidth(false), | 138 return gfx::Size(ToolbarActionsBar::IconWidth(false), |
141 ToolbarActionsBar::IconHeight()); | 139 ToolbarActionsBar::IconHeight()); |
142 } | 140 } |
143 | 141 |
144 void ToolbarActionView::Layout() { | |
145 MenuButton::Layout(); | |
146 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | |
147 } | |
148 | |
149 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) { | |
150 // views::MenuButton actions are only triggered by left mouse clicks. | |
151 if (event.IsLeftMouseButton()) { | |
152 ink_drop_animation_controller_->AnimateToState( | |
153 views::InkDropState::ACTION_PENDING); | |
154 } | |
155 return MenuButton::OnMousePressed(event); | |
156 } | |
157 | |
158 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) { | 142 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) { |
159 // TODO(varkha): refactor similar implementations in ToolbarButton, | |
160 // FindBarButton and here. | |
161 if (menu_) { | 143 if (menu_) { |
162 // While dropdown menu is showing the button should not handle gestures. | 144 // While dropdown menu is showing the button should not handle gestures. |
163 event->StopPropagation(); | 145 event->StopPropagation(); |
164 return; | 146 return; |
165 } | 147 } |
166 | |
167 MenuButton::OnGestureEvent(event); | 148 MenuButton::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_PENDING; | |
179 break; | |
180 case ui::ET_GESTURE_LONG_TAP: | |
181 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
182 break; | |
183 case ui::ET_GESTURE_SCROLL_BEGIN: | |
184 case ui::ET_GESTURE_END: | |
185 ink_drop_state = views::InkDropState::HIDDEN; | |
186 break; | |
187 default: | |
188 return; | |
189 } | |
190 | |
191 views::InkDropState current_ink_drop_state = | |
192 ink_drop_animation_controller_->GetInkDropState(); | |
193 | |
194 if (ink_drop_state == views::InkDropState::HIDDEN && | |
195 (current_ink_drop_state == views::InkDropState::QUICK_ACTION || | |
196 current_ink_drop_state == views::InkDropState::SLOW_ACTION || | |
197 current_ink_drop_state == views::InkDropState::DEACTIVATED)) { | |
198 // These InkDropStates automatically transition to the HIDDEN state so we | |
199 // don't make an explicit call. Explicitly animating to HIDDEN in this case | |
200 // would prematurely pre-empt these animations. | |
201 return; | |
202 } | |
203 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | |
204 } | 149 } |
205 | 150 |
206 void ToolbarActionView::OnDragDone() { | 151 void ToolbarActionView::OnDragDone() { |
207 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
208 views::MenuButton::OnDragDone(); | 152 views::MenuButton::OnDragDone(); |
209 delegate_->OnToolbarActionViewDragDone(); | 153 delegate_->OnToolbarActionViewDragDone(); |
210 } | 154 } |
211 | 155 |
212 void ToolbarActionView::ViewHierarchyChanged( | 156 void ToolbarActionView::ViewHierarchyChanged( |
213 const ViewHierarchyChangedDetails& details) { | 157 const ViewHierarchyChangedDetails& details) { |
214 if (details.is_add && !called_register_command_ && GetFocusManager()) { | 158 if (details.is_add && !called_register_command_ && GetFocusManager()) { |
215 view_controller_->RegisterCommand(); | 159 view_controller_->RegisterCommand(); |
216 called_register_command_ = true; | 160 called_register_command_ = true; |
217 } | 161 } |
218 | 162 |
219 MenuButton::ViewHierarchyChanged(details); | 163 MenuButton::ViewHierarchyChanged(details); |
220 } | 164 } |
221 | 165 |
222 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { | 166 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { |
223 views::MenuButton::GetAccessibleState(state); | 167 views::MenuButton::GetAccessibleState(state); |
224 state->role = ui::AX_ROLE_BUTTON; | 168 state->role = ui::AX_ROLE_BUTTON; |
225 } | 169 } |
226 | 170 |
227 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, | 171 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, |
228 const gfx::Point& point) { | 172 const gfx::Point& point) { |
229 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { | 173 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { |
230 // We should only get a button pressed event with a non-enabled action if | 174 // We should only get a button pressed event with a non-enabled action if |
231 // the left-click behavior should open the menu. | 175 // the left-click behavior should open the menu. |
232 DCHECK(view_controller_->DisabledClickOpensMenu()); | 176 DCHECK(view_controller_->DisabledClickOpensMenu()); |
233 context_menu_controller()->ShowContextMenuForView(this, point, | 177 context_menu_controller()->ShowContextMenuForView(this, point, |
234 ui::MENU_SOURCE_NONE); | 178 ui::MENU_SOURCE_NONE); |
235 } else { | 179 } else { |
236 ink_drop_animation_controller_->AnimateToState( | |
237 views::InkDropState::QUICK_ACTION); | |
238 view_controller_->ExecuteAction(true); | 180 view_controller_->ExecuteAction(true); |
239 } | 181 } |
240 } | 182 } |
241 | 183 |
242 void ToolbarActionView::OnMenuButtonClickCanceled(views::View* sender) { | |
243 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
244 } | |
245 | |
246 void ToolbarActionView::UpdateState() { | 184 void ToolbarActionView::UpdateState() { |
247 content::WebContents* web_contents = GetCurrentWebContents(); | 185 content::WebContents* web_contents = GetCurrentWebContents(); |
248 if (SessionTabHelper::IdForTab(web_contents) < 0) | 186 if (SessionTabHelper::IdForTab(web_contents) < 0) |
249 return; | 187 return; |
250 | 188 |
251 if (!view_controller_->IsEnabled(web_contents) && | 189 if (!view_controller_->IsEnabled(web_contents) && |
252 !view_controller_->DisabledClickOpensMenu()) { | 190 !view_controller_->DisabledClickOpensMenu()) { |
253 SetState(views::CustomButton::STATE_DISABLED); | 191 SetState(views::CustomButton::STATE_DISABLED); |
254 } else if (state() == views::CustomButton::STATE_DISABLED) { | 192 } else if (state() == views::CustomButton::STATE_DISABLED) { |
255 SetState(views::CustomButton::STATE_NORMAL); | 193 SetState(views::CustomButton::STATE_NORMAL); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; | 332 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; |
395 if (delegate_->ShownInsideMenu()) | 333 if (delegate_->ShownInsideMenu()) |
396 run_types |= views::MenuRunner::IS_NESTED; | 334 run_types |= views::MenuRunner::IS_NESTED; |
397 | 335 |
398 // RunMenuAt expects a nested menu to be parented by the same widget as the | 336 // RunMenuAt expects a nested menu to be parented by the same widget as the |
399 // already visible menu, in this case the Chrome menu. | 337 // already visible menu, in this case the Chrome menu. |
400 views::Widget* parent = delegate_->ShownInsideMenu() ? | 338 views::Widget* parent = delegate_->ShownInsideMenu() ? |
401 delegate_->GetOverflowReferenceView()->GetWidget() : | 339 delegate_->GetOverflowReferenceView()->GetWidget() : |
402 GetWidget(); | 340 GetWidget(); |
403 | 341 |
404 ink_drop_animation_controller_->AnimateToState( | 342 views::InkDropDelegate* ink_drop_delegate = GetInkDropDelegate(); |
405 views::InkDropState::ACTIVATED); | 343 if (ink_drop_delegate) |
| 344 ink_drop_delegate->OnActivated(); |
406 | 345 |
407 views::MenuModelAdapter adapter(context_menu_model); | 346 views::MenuModelAdapter adapter(context_menu_model); |
408 menu_ = adapter.CreateMenu(); | 347 menu_ = adapter.CreateMenu(); |
409 menu_runner_.reset(new views::MenuRunner(menu_, run_types)); | 348 menu_runner_.reset(new views::MenuRunner(menu_, run_types)); |
410 | 349 |
411 if (context_menu_callback) | 350 if (context_menu_callback) |
412 context_menu_callback->Run(this); | 351 context_menu_callback->Run(this); |
413 if (menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()), | 352 if (menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()), |
414 views::MENU_ANCHOR_TOPLEFT, | 353 views::MENU_ANCHOR_TOPLEFT, |
415 source_type) == views::MenuRunner::MENU_DELETED) { | 354 source_type) == views::MenuRunner::MENU_DELETED) { |
416 menu_ = nullptr; | 355 menu_ = nullptr; |
417 return; | 356 return; |
418 } | 357 } |
419 ink_drop_animation_controller_->AnimateToState( | 358 if (ink_drop_delegate) |
420 views::InkDropState::DEACTIVATED); | 359 ink_drop_delegate->OnDeactivated(); |
421 | 360 |
422 menu_runner_.reset(); | 361 menu_runner_.reset(); |
423 menu_ = nullptr; | 362 menu_ = nullptr; |
424 context_menu_owner = nullptr; | 363 context_menu_owner = nullptr; |
425 view_controller_->OnContextMenuClosed(); | 364 view_controller_->OnContextMenuClosed(); |
426 | 365 |
427 // If another extension action wants to show its context menu, allow it to. | 366 // If another extension action wants to show its context menu, allow it to. |
428 if (!followup_context_menu_task_.is_null()) { | 367 if (!followup_context_menu_task_.is_null()) { |
429 base::Closure task = followup_context_menu_task_; | 368 base::Closure task = followup_context_menu_task_; |
430 followup_context_menu_task_ = base::Closure(); | 369 followup_context_menu_task_ = base::Closure(); |
(...skipping 14 matching lines...) Expand all Loading... |
445 if (menu_controller->in_nested_run()) { | 384 if (menu_controller->in_nested_run()) { |
446 // There is another menu showing. Close the outermost menu (since we are | 385 // There is another menu showing. Close the outermost menu (since we are |
447 // shown in the same menu, we don't want to close the whole thing). | 386 // shown in the same menu, we don't want to close the whole thing). |
448 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); | 387 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); |
449 return true; | 388 return true; |
450 } | 389 } |
451 } | 390 } |
452 | 391 |
453 return false; | 392 return false; |
454 } | 393 } |
OLD | NEW |