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 "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 Loading... | |
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 // TODO(varkha): refactor similar implementations in ToolbarButton, | |
155 // FindBarButton and here. | |
156 if (menu_showing_) { | |
157 // While dropdown menu is showing the button should not handle gestures. | |
158 event->StopPropagation(); | |
159 return; | |
160 } | |
161 | |
162 MenuButton::OnGestureEvent(event); | |
163 | |
164 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | |
165 switch (event->type()) { | |
166 case ui::ET_GESTURE_TAP_DOWN: | |
167 ink_drop_state = views::InkDropState::ACTION_PENDING; | |
168 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | |
169 // subsequent events for the gesture are sent to |this|. | |
170 event->SetHandled(); | |
171 break; | |
172 case ui::ET_GESTURE_LONG_PRESS: | |
173 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING; | |
174 break; | |
175 case ui::ET_GESTURE_LONG_TAP: | |
176 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
177 break; | |
178 case ui::ET_GESTURE_END: | |
179 case ui::ET_GESTURE_TAP_CANCEL: | |
bruthig
2015/10/22 15:25:43
You should probably add the same changes here that
varkha
2015/10/23 14:37:41
Done. Yes, there's enough evidence. Let's land the
| |
180 ink_drop_state = views::InkDropState::HIDDEN; | |
181 break; | |
182 default: | |
183 return; | |
184 } | |
185 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | |
186 } | |
187 | |
101 void ToolbarActionView::OnDragDone() { | 188 void ToolbarActionView::OnDragDone() { |
189 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
102 views::MenuButton::OnDragDone(); | 190 views::MenuButton::OnDragDone(); |
103 delegate_->OnToolbarActionViewDragDone(); | 191 delegate_->OnToolbarActionViewDragDone(); |
104 } | 192 } |
105 | 193 |
106 void ToolbarActionView::ViewHierarchyChanged( | 194 void ToolbarActionView::ViewHierarchyChanged( |
107 const ViewHierarchyChangedDetails& details) { | 195 const ViewHierarchyChangedDetails& details) { |
108 if (details.is_add && !called_register_command_ && GetFocusManager()) { | 196 if (details.is_add && !called_register_command_ && GetFocusManager()) { |
109 view_controller_->RegisterCommand(); | 197 view_controller_->RegisterCommand(); |
110 called_register_command_ = true; | 198 called_register_command_ = true; |
111 } | 199 } |
112 | 200 |
113 MenuButton::ViewHierarchyChanged(details); | 201 MenuButton::ViewHierarchyChanged(details); |
114 } | 202 } |
115 | 203 |
116 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { | 204 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { |
117 views::MenuButton::GetAccessibleState(state); | 205 views::MenuButton::GetAccessibleState(state); |
118 state->role = ui::AX_ROLE_BUTTON; | 206 state->role = ui::AX_ROLE_BUTTON; |
119 } | 207 } |
120 | 208 |
121 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, | 209 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, |
122 const gfx::Point& point) { | 210 const gfx::Point& point) { |
123 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { | 211 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { |
124 // We should only get a button pressed event with a non-enabled action if | 212 // We should only get a button pressed event with a non-enabled action if |
125 // the left-click behavior should open the menu. | 213 // the left-click behavior should open the menu. |
126 DCHECK(view_controller_->DisabledClickOpensMenu()); | 214 DCHECK(view_controller_->DisabledClickOpensMenu()); |
127 context_menu_controller()->ShowContextMenuForView(this, point, | 215 context_menu_controller()->ShowContextMenuForView(this, point, |
128 ui::MENU_SOURCE_NONE); | 216 ui::MENU_SOURCE_NONE); |
129 } else { | 217 } else { |
218 ink_drop_animation_controller_->AnimateToState( | |
219 views::InkDropState::QUICK_ACTION); | |
130 view_controller_->ExecuteAction(true); | 220 view_controller_->ExecuteAction(true); |
131 } | 221 } |
132 } | 222 } |
133 | 223 |
224 void ToolbarActionView::OnMenuButtonReleasedWithoutClick(views::View* sender) { | |
225 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
226 } | |
227 | |
134 void ToolbarActionView::UpdateState() { | 228 void ToolbarActionView::UpdateState() { |
135 content::WebContents* web_contents = GetCurrentWebContents(); | 229 content::WebContents* web_contents = GetCurrentWebContents(); |
136 if (SessionTabHelper::IdForTab(web_contents) < 0) | 230 if (SessionTabHelper::IdForTab(web_contents) < 0) |
137 return; | 231 return; |
138 | 232 |
139 if (!view_controller_->IsEnabled(web_contents) && | 233 if (!view_controller_->IsEnabled(web_contents) && |
140 !view_controller_->DisabledClickOpensMenu()) { | 234 !view_controller_->DisabledClickOpensMenu()) { |
141 SetState(views::CustomButton::STATE_DISABLED); | 235 SetState(views::CustomButton::STATE_DISABLED); |
142 } else if (state() == views::CustomButton::STATE_DISABLED) { | 236 } else if (state() == views::CustomButton::STATE_DISABLED) { |
143 SetState(views::CustomButton::STATE_NORMAL); | 237 SetState(views::CustomButton::STATE_NORMAL); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; | 372 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; |
279 if (delegate_->ShownInsideMenu()) | 373 if (delegate_->ShownInsideMenu()) |
280 run_types |= views::MenuRunner::IS_NESTED; | 374 run_types |= views::MenuRunner::IS_NESTED; |
281 | 375 |
282 // RunMenuAt expects a nested menu to be parented by the same widget as the | 376 // RunMenuAt expects a nested menu to be parented by the same widget as the |
283 // already visible menu, in this case the Chrome menu. | 377 // already visible menu, in this case the Chrome menu. |
284 views::Widget* parent = delegate_->ShownInsideMenu() ? | 378 views::Widget* parent = delegate_->ShownInsideMenu() ? |
285 delegate_->GetOverflowReferenceView()->GetWidget() : | 379 delegate_->GetOverflowReferenceView()->GetWidget() : |
286 GetWidget(); | 380 GetWidget(); |
287 | 381 |
382 menu_showing_ = true; | |
383 ink_drop_animation_controller_->AnimateToState( | |
384 views::InkDropState::ACTIVATED); | |
385 | |
288 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types)); | 386 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types)); |
289 | |
290 if (menu_runner_->RunMenuAt(parent, | 387 if (menu_runner_->RunMenuAt(parent, |
291 this, | 388 this, |
292 gfx::Rect(screen_loc, size()), | 389 gfx::Rect(screen_loc, size()), |
293 views::MENU_ANCHOR_TOPLEFT, | 390 views::MENU_ANCHOR_TOPLEFT, |
294 source_type) == views::MenuRunner::MENU_DELETED) { | 391 source_type) == views::MenuRunner::MENU_DELETED) { |
295 return; | 392 return; |
296 } | 393 } |
394 ink_drop_animation_controller_->AnimateToState( | |
395 views::InkDropState::DEACTIVATED); | |
396 menu_showing_ = false; | |
297 | 397 |
298 context_menu_owner = nullptr; | 398 context_menu_owner = nullptr; |
299 menu_runner_.reset(); | 399 menu_runner_.reset(); |
300 view_controller_->OnContextMenuClosed(); | 400 view_controller_->OnContextMenuClosed(); |
301 | 401 |
302 // If another extension action wants to show its context menu, allow it to. | 402 // If another extension action wants to show its context menu, allow it to. |
303 if (!followup_context_menu_task_.is_null()) { | 403 if (!followup_context_menu_task_.is_null()) { |
304 base::Closure task = followup_context_menu_task_; | 404 base::Closure task = followup_context_menu_task_; |
305 followup_context_menu_task_ = base::Closure(); | 405 followup_context_menu_task_ = base::Closure(); |
306 task.Run(); | 406 task.Run(); |
(...skipping 13 matching lines...) Expand all Loading... | |
320 if (menu_controller->in_nested_run()) { | 420 if (menu_controller->in_nested_run()) { |
321 // There is another menu showing. Close the outermost menu (since we are | 421 // 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). | 422 // shown in the same menu, we don't want to close the whole thing). |
323 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); | 423 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); |
324 return true; | 424 return true; |
325 } | 425 } |
326 } | 426 } |
327 | 427 |
328 return false; | 428 return false; |
329 } | 429 } |
OLD | NEW |