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" | |
30 #include "ui/views/animation/toolbar_ink_drop_delegate.h" | |
28 #include "ui/views/controls/button/label_button_border.h" | 31 #include "ui/views/controls/button/label_button_border.h" |
29 #include "ui/views/controls/menu/menu_controller.h" | 32 #include "ui/views/controls/menu/menu_controller.h" |
30 #include "ui/views/controls/menu/menu_runner.h" | 33 #include "ui/views/controls/menu/menu_runner.h" |
31 #include "ui/views/mouse_constants.h" | 34 #include "ui/views/mouse_constants.h" |
32 #include "ui/views/resources/grit/views_resources.h" | 35 #include "ui/views/resources/grit/views_resources.h" |
33 | 36 |
34 using views::LabelButtonBorder; | 37 using views::LabelButtonBorder; |
35 | 38 |
36 namespace { | 39 namespace { |
37 | 40 |
(...skipping 15 matching lines...) Expand all Loading... | |
53 ToolbarActionView::ToolbarActionView( | 56 ToolbarActionView::ToolbarActionView( |
54 ToolbarActionViewController* view_controller, | 57 ToolbarActionViewController* view_controller, |
55 Profile* profile, | 58 Profile* profile, |
56 ToolbarActionView::Delegate* delegate) | 59 ToolbarActionView::Delegate* delegate) |
57 : MenuButton(nullptr, base::string16(), this, false), | 60 : MenuButton(nullptr, base::string16(), this, false), |
58 view_controller_(view_controller), | 61 view_controller_(view_controller), |
59 profile_(profile), | 62 profile_(profile), |
60 delegate_(delegate), | 63 delegate_(delegate), |
61 called_register_command_(false), | 64 called_register_command_(false), |
62 wants_to_run_(false), | 65 wants_to_run_(false), |
66 menu_showing_(false), | |
67 ink_drop_animation_controller_( | |
68 views::InkDropAnimationControllerFactory:: | |
69 CreateInkDropAnimationController(this)), | |
70 ink_drop_delegate_(new views::ToolbarInkDropDelegate( | |
71 ink_drop_animation_controller_.get())), | |
63 weak_factory_(this) { | 72 weak_factory_(this) { |
64 set_id(VIEW_ID_BROWSER_ACTION); | 73 set_id(VIEW_ID_BROWSER_ACTION); |
65 view_controller_->SetDelegate(this); | 74 view_controller_->SetDelegate(this); |
66 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 75 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
67 set_drag_controller(delegate_); | 76 set_drag_controller(delegate_); |
68 | 77 |
69 set_context_menu_controller(this); | 78 set_context_menu_controller(this); |
70 | 79 |
80 const int kInkDropLargeSize = 32; | |
81 const int kInkDropLargeCornerRadius = 5; | |
82 const int kInkDropSmallSize = 24; | |
83 const int kInkDropSmallCornerRadius = 2; | |
84 | |
85 ink_drop_animation_controller_->SetInkDropSize( | |
86 gfx::Size(kInkDropLargeSize, kInkDropLargeSize), | |
87 kInkDropLargeCornerRadius, | |
88 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
89 kInkDropSmallCornerRadius); | |
90 | |
71 // We also listen for browser theme changes on linux because a switch from or | 91 // 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. | 92 // to GTK requires that we regrab our browser action images. |
73 registrar_.Add( | 93 registrar_.Add( |
74 this, | 94 this, |
75 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 95 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
76 content::Source<ThemeService>( | 96 content::Source<ThemeService>( |
77 ThemeServiceFactory::GetForProfile(profile_))); | 97 ThemeServiceFactory::GetForProfile(profile_))); |
78 | 98 |
79 // If the button is within a menu, we need to make it focusable in order to | 99 // 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 | 100 // have it accessible via keyboard navigation, but it shouldn't request focus |
81 // (because that would close the menu). | 101 // (because that would close the menu). |
82 if (delegate_->ShownInsideMenu()) { | 102 if (delegate_->ShownInsideMenu()) { |
83 set_request_focus_on_press(false); | 103 set_request_focus_on_press(false); |
84 SetFocusable(true); | 104 SetFocusable(true); |
85 } | 105 } |
86 | 106 |
87 UpdateState(); | 107 UpdateState(); |
88 } | 108 } |
89 | 109 |
90 ToolbarActionView::~ToolbarActionView() { | 110 ToolbarActionView::~ToolbarActionView() { |
91 if (context_menu_owner == this) | 111 if (context_menu_owner == this) |
92 context_menu_owner = nullptr; | 112 context_menu_owner = nullptr; |
93 view_controller_->SetDelegate(nullptr); | 113 view_controller_->SetDelegate(nullptr); |
94 } | 114 } |
95 | 115 |
116 void ToolbarActionView::AddInkDropLayer(ui::Layer* ink_drop_layer) { | |
117 SetPaintToLayer(true); | |
118 image()->SetPaintToLayer(true); | |
119 image()->SetFillsBoundsOpaquely(false); | |
120 | |
121 layer()->Add(ink_drop_layer); | |
122 layer()->StackAtBottom(ink_drop_layer); | |
123 } | |
124 | |
125 void ToolbarActionView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | |
126 layer()->Remove(ink_drop_layer); | |
127 | |
128 image()->SetFillsBoundsOpaquely(true); | |
129 image()->SetPaintToLayer(false); | |
130 SetPaintToLayer(false); | |
131 } | |
132 | |
133 gfx::Point ToolbarActionView::CalculateInkDropCenter() const { | |
134 return GetLocalBounds().CenterPoint(); | |
135 } | |
136 | |
137 views::InkDropDelegate* ToolbarActionView::GetInkDropDelegate() const { | |
138 return ink_drop_delegate_.get(); | |
139 } | |
140 | |
96 gfx::Size ToolbarActionView::GetPreferredSize() const { | 141 gfx::Size ToolbarActionView::GetPreferredSize() const { |
97 return gfx::Size(ToolbarActionsBar::IconWidth(false), | 142 return gfx::Size(ToolbarActionsBar::IconWidth(false), |
98 ToolbarActionsBar::IconHeight()); | 143 ToolbarActionsBar::IconHeight()); |
99 } | 144 } |
100 | 145 |
146 void ToolbarActionView::Layout() { | |
147 MenuButton::Layout(); | |
148 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | |
149 } | |
150 | |
151 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) { | |
bruthig
2015/11/06 20:29:10
Is this an example of how the View can override an
varkha
2015/11/06 20:51:50
No, this is same as in "parent" CL (I know, I shou
| |
152 // views::MenuButton actions are only triggered by left mouse clicks. | |
153 if (event.IsLeftMouseButton()) { | |
154 ink_drop_animation_controller_->AnimateToState( | |
155 views::InkDropState::ACTION_PENDING); | |
156 } | |
157 return MenuButton::OnMousePressed(event); | |
158 } | |
159 | |
160 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) { | |
161 // TODO(varkha): refactor similar implementations in ToolbarButton, | |
162 // FindBarButton and here. | |
163 if (menu_showing_) { | |
bruthig
2015/11/06 20:29:10
Is this View state logic currently captured in the
varkha
2015/11/06 20:51:50
Not sure I follow you on this one. In this code pa
bruthig
2015/11/09 16:51:22
My mistake, I was thinking of OnGestureEvent() as
| |
164 // While dropdown menu is showing the button should not handle gestures. | |
165 event->StopPropagation(); | |
166 return; | |
167 } | |
168 | |
169 MenuButton::OnGestureEvent(event); | |
170 } | |
171 | |
101 void ToolbarActionView::OnDragDone() { | 172 void ToolbarActionView::OnDragDone() { |
173 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
102 views::MenuButton::OnDragDone(); | 174 views::MenuButton::OnDragDone(); |
103 delegate_->OnToolbarActionViewDragDone(); | 175 delegate_->OnToolbarActionViewDragDone(); |
104 } | 176 } |
105 | 177 |
106 void ToolbarActionView::ViewHierarchyChanged( | 178 void ToolbarActionView::ViewHierarchyChanged( |
107 const ViewHierarchyChangedDetails& details) { | 179 const ViewHierarchyChangedDetails& details) { |
108 if (details.is_add && !called_register_command_ && GetFocusManager()) { | 180 if (details.is_add && !called_register_command_ && GetFocusManager()) { |
109 view_controller_->RegisterCommand(); | 181 view_controller_->RegisterCommand(); |
110 called_register_command_ = true; | 182 called_register_command_ = true; |
111 } | 183 } |
112 | 184 |
113 MenuButton::ViewHierarchyChanged(details); | 185 MenuButton::ViewHierarchyChanged(details); |
114 } | 186 } |
115 | 187 |
116 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { | 188 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { |
117 views::MenuButton::GetAccessibleState(state); | 189 views::MenuButton::GetAccessibleState(state); |
118 state->role = ui::AX_ROLE_BUTTON; | 190 state->role = ui::AX_ROLE_BUTTON; |
119 } | 191 } |
120 | 192 |
121 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, | 193 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, |
122 const gfx::Point& point) { | 194 const gfx::Point& point) { |
123 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { | 195 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { |
124 // We should only get a button pressed event with a non-enabled action if | 196 // We should only get a button pressed event with a non-enabled action if |
125 // the left-click behavior should open the menu. | 197 // the left-click behavior should open the menu. |
126 DCHECK(view_controller_->DisabledClickOpensMenu()); | 198 DCHECK(view_controller_->DisabledClickOpensMenu()); |
127 context_menu_controller()->ShowContextMenuForView(this, point, | 199 context_menu_controller()->ShowContextMenuForView(this, point, |
128 ui::MENU_SOURCE_NONE); | 200 ui::MENU_SOURCE_NONE); |
129 } else { | 201 } else { |
202 ink_drop_animation_controller_->AnimateToState( | |
203 views::InkDropState::QUICK_ACTION); | |
130 view_controller_->ExecuteAction(true); | 204 view_controller_->ExecuteAction(true); |
131 } | 205 } |
132 } | 206 } |
133 | 207 |
208 void ToolbarActionView::OnMenuButtonClickCanceled(views::View* sender) { | |
209 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
210 } | |
211 | |
134 void ToolbarActionView::UpdateState() { | 212 void ToolbarActionView::UpdateState() { |
135 content::WebContents* web_contents = GetCurrentWebContents(); | 213 content::WebContents* web_contents = GetCurrentWebContents(); |
136 if (SessionTabHelper::IdForTab(web_contents) < 0) | 214 if (SessionTabHelper::IdForTab(web_contents) < 0) |
137 return; | 215 return; |
138 | 216 |
139 if (!view_controller_->IsEnabled(web_contents) && | 217 if (!view_controller_->IsEnabled(web_contents) && |
140 !view_controller_->DisabledClickOpensMenu()) { | 218 !view_controller_->DisabledClickOpensMenu()) { |
141 SetState(views::CustomButton::STATE_DISABLED); | 219 SetState(views::CustomButton::STATE_DISABLED); |
142 } else if (state() == views::CustomButton::STATE_DISABLED) { | 220 } else if (state() == views::CustomButton::STATE_DISABLED) { |
143 SetState(views::CustomButton::STATE_NORMAL); | 221 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; | 356 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; |
279 if (delegate_->ShownInsideMenu()) | 357 if (delegate_->ShownInsideMenu()) |
280 run_types |= views::MenuRunner::IS_NESTED; | 358 run_types |= views::MenuRunner::IS_NESTED; |
281 | 359 |
282 // RunMenuAt expects a nested menu to be parented by the same widget as the | 360 // RunMenuAt expects a nested menu to be parented by the same widget as the |
283 // already visible menu, in this case the Chrome menu. | 361 // already visible menu, in this case the Chrome menu. |
284 views::Widget* parent = delegate_->ShownInsideMenu() ? | 362 views::Widget* parent = delegate_->ShownInsideMenu() ? |
285 delegate_->GetOverflowReferenceView()->GetWidget() : | 363 delegate_->GetOverflowReferenceView()->GetWidget() : |
286 GetWidget(); | 364 GetWidget(); |
287 | 365 |
366 menu_showing_ = true; | |
367 ink_drop_animation_controller_->AnimateToState( | |
368 views::InkDropState::ACTIVATED); | |
369 | |
288 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types)); | 370 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types)); |
289 | |
290 if (menu_runner_->RunMenuAt(parent, | 371 if (menu_runner_->RunMenuAt(parent, |
291 this, | 372 this, |
292 gfx::Rect(screen_loc, size()), | 373 gfx::Rect(screen_loc, size()), |
293 views::MENU_ANCHOR_TOPLEFT, | 374 views::MENU_ANCHOR_TOPLEFT, |
294 source_type) == views::MenuRunner::MENU_DELETED) { | 375 source_type) == views::MenuRunner::MENU_DELETED) { |
295 return; | 376 return; |
296 } | 377 } |
378 ink_drop_animation_controller_->AnimateToState( | |
379 views::InkDropState::DEACTIVATED); | |
380 menu_showing_ = false; | |
297 | 381 |
298 context_menu_owner = nullptr; | 382 context_menu_owner = nullptr; |
299 menu_runner_.reset(); | 383 menu_runner_.reset(); |
300 view_controller_->OnContextMenuClosed(); | 384 view_controller_->OnContextMenuClosed(); |
301 | 385 |
302 // If another extension action wants to show its context menu, allow it to. | 386 // If another extension action wants to show its context menu, allow it to. |
303 if (!followup_context_menu_task_.is_null()) { | 387 if (!followup_context_menu_task_.is_null()) { |
304 base::Closure task = followup_context_menu_task_; | 388 base::Closure task = followup_context_menu_task_; |
305 followup_context_menu_task_ = base::Closure(); | 389 followup_context_menu_task_ = base::Closure(); |
306 task.Run(); | 390 task.Run(); |
(...skipping 13 matching lines...) Expand all Loading... | |
320 if (menu_controller->in_nested_run()) { | 404 if (menu_controller->in_nested_run()) { |
321 // There is another menu showing. Close the outermost menu (since we are | 405 // 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). | 406 // shown in the same menu, we don't want to close the whole thing). |
323 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); | 407 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); |
324 return true; | 408 return true; |
325 } | 409 } |
326 } | 410 } |
327 | 411 |
328 return false; | 412 return false; |
329 } | 413 } |
OLD | NEW |