Chromium Code Reviews| 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 ink_drop_animation_controller_( | |
| 66 views::InkDropAnimationControllerFactory:: | |
| 67 CreateInkDropAnimationController(this)), | |
| 63 weak_factory_(this) { | 68 weak_factory_(this) { |
| 64 set_id(VIEW_ID_BROWSER_ACTION); | 69 set_id(VIEW_ID_BROWSER_ACTION); |
| 65 view_controller_->SetDelegate(this); | 70 view_controller_->SetDelegate(this); |
| 66 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 71 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 67 set_drag_controller(delegate_); | 72 set_drag_controller(delegate_); |
| 68 | 73 |
| 69 set_context_menu_controller(this); | 74 set_context_menu_controller(this); |
| 70 | 75 |
| 76 const int kInkDropLargeSize = 32; | |
| 77 const int kInkDropLargeCornerRadius = 5; | |
| 78 const int kInkDropSmallSize = 24; | |
| 79 const int kInkDropSmallCornerRadius = 2; | |
| 80 | |
| 81 ink_drop_animation_controller_->SetInkDropSize( | |
| 82 gfx::Size(kInkDropLargeSize, kInkDropLargeSize), | |
| 83 kInkDropLargeCornerRadius, | |
| 84 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
| 85 kInkDropSmallCornerRadius); | |
| 86 | |
| 71 // 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 |
| 72 // to GTK requires that we regrab our browser action images. | 88 // to GTK requires that we regrab our browser action images. |
| 73 registrar_.Add( | 89 registrar_.Add( |
| 74 this, | 90 this, |
| 75 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 91 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 76 content::Source<ThemeService>( | 92 content::Source<ThemeService>( |
| 77 ThemeServiceFactory::GetForProfile(profile_))); | 93 ThemeServiceFactory::GetForProfile(profile_))); |
| 78 | 94 |
| 79 // 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 |
| 80 // 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 |
| 81 // (because that would close the menu). | 97 // (because that would close the menu). |
| 82 if (delegate_->ShownInsideMenu()) { | 98 if (delegate_->ShownInsideMenu()) { |
| 83 set_request_focus_on_press(false); | 99 set_request_focus_on_press(false); |
| 84 SetFocusable(true); | 100 SetFocusable(true); |
| 85 } | 101 } |
| 86 | 102 |
| 87 UpdateState(); | 103 UpdateState(); |
| 88 } | 104 } |
| 89 | 105 |
| 90 ToolbarActionView::~ToolbarActionView() { | 106 ToolbarActionView::~ToolbarActionView() { |
| 91 if (context_menu_owner == this) | 107 if (context_menu_owner == this) |
| 92 context_menu_owner = nullptr; | 108 context_menu_owner = nullptr; |
| 93 view_controller_->SetDelegate(nullptr); | 109 view_controller_->SetDelegate(nullptr); |
| 94 } | 110 } |
| 95 | 111 |
| 112 void ToolbarActionView::AddInkDropLayer(ui::Layer* ink_drop_layer) { | |
| 113 SetPaintToLayer(true); | |
| 114 image()->SetPaintToLayer(true); | |
| 115 image()->SetFillsBoundsOpaquely(false); | |
| 116 | |
| 117 layer()->Add(ink_drop_layer); | |
| 118 layer()->StackAtBottom(ink_drop_layer); | |
| 119 } | |
| 120 | |
| 121 void ToolbarActionView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | |
| 122 layer()->Remove(ink_drop_layer); | |
| 123 | |
| 124 image()->SetFillsBoundsOpaquely(true); | |
| 125 image()->SetPaintToLayer(false); | |
| 126 SetPaintToLayer(false); | |
| 127 } | |
| 128 | |
| 129 gfx::Point ToolbarActionView::CalculateInkDropCenter() const { | |
| 130 return GetLocalBounds().CenterPoint(); | |
| 131 } | |
| 132 | |
| 96 gfx::Size ToolbarActionView::GetPreferredSize() const { | 133 gfx::Size ToolbarActionView::GetPreferredSize() const { |
| 97 return gfx::Size(ToolbarActionsBar::IconWidth(false), | 134 return gfx::Size(ToolbarActionsBar::IconWidth(false), |
| 98 ToolbarActionsBar::IconHeight()); | 135 ToolbarActionsBar::IconHeight()); |
| 99 } | 136 } |
| 100 | 137 |
| 138 void ToolbarActionView::Layout() { | |
| 139 MenuButton::Layout(); | |
| 140 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | |
| 141 } | |
| 142 | |
| 143 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) { | |
| 144 // views::Button actions are only triggered by left and middle mouse clicks. | |
| 145 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) { | |
|
bruthig
2015/10/19 19:29:15
Does Middle mouse button actually trigger any acti
varkha
2015/10/20 15:23:04
It seems MenuButtons only trigger actions on left
| |
| 146 ink_drop_animation_controller_->AnimateToState( | |
| 147 views::InkDropState::ACTION_PENDING); | |
| 148 } | |
| 149 return MenuButton::OnMousePressed(event); | |
| 150 } | |
| 151 | |
| 152 void ToolbarActionView::OnMouseReleased(const ui::MouseEvent& event) { | |
| 153 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
|
bruthig
2015/10/19 19:29:15
You might need to guard this with "if (!HitTestPoi
varkha
2015/10/20 15:23:04
I've wired this with a help of MenuButtonListener:
| |
| 154 MenuButton::OnMouseReleased(event); | |
| 155 } | |
| 156 | |
| 101 void ToolbarActionView::OnDragDone() { | 157 void ToolbarActionView::OnDragDone() { |
| 158 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
| 102 views::MenuButton::OnDragDone(); | 159 views::MenuButton::OnDragDone(); |
| 103 delegate_->OnToolbarActionViewDragDone(); | 160 delegate_->OnToolbarActionViewDragDone(); |
| 104 } | 161 } |
| 105 | 162 |
| 106 void ToolbarActionView::ViewHierarchyChanged( | 163 void ToolbarActionView::ViewHierarchyChanged( |
| 107 const ViewHierarchyChangedDetails& details) { | 164 const ViewHierarchyChangedDetails& details) { |
| 108 if (details.is_add && !called_register_command_ && GetFocusManager()) { | 165 if (details.is_add && !called_register_command_ && GetFocusManager()) { |
| 109 view_controller_->RegisterCommand(); | 166 view_controller_->RegisterCommand(); |
| 110 called_register_command_ = true; | 167 called_register_command_ = true; |
| 111 } | 168 } |
| 112 | 169 |
| 113 MenuButton::ViewHierarchyChanged(details); | 170 MenuButton::ViewHierarchyChanged(details); |
| 114 } | 171 } |
| 115 | 172 |
| 116 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { | 173 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { |
| 117 views::MenuButton::GetAccessibleState(state); | 174 views::MenuButton::GetAccessibleState(state); |
| 118 state->role = ui::AX_ROLE_BUTTON; | 175 state->role = ui::AX_ROLE_BUTTON; |
| 119 } | 176 } |
| 120 | 177 |
| 121 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, | 178 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, |
| 122 const gfx::Point& point) { | 179 const gfx::Point& point) { |
| 123 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { | 180 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { |
| 124 // We should only get a button pressed event with a non-enabled action if | 181 // We should only get a button pressed event with a non-enabled action if |
| 125 // the left-click behavior should open the menu. | 182 // the left-click behavior should open the menu. |
| 126 DCHECK(view_controller_->DisabledClickOpensMenu()); | 183 DCHECK(view_controller_->DisabledClickOpensMenu()); |
| 127 context_menu_controller()->ShowContextMenuForView(this, point, | 184 context_menu_controller()->ShowContextMenuForView(this, point, |
| 128 ui::MENU_SOURCE_NONE); | 185 ui::MENU_SOURCE_NONE); |
| 129 } else { | 186 } else { |
| 187 ink_drop_animation_controller_->AnimateToState( | |
| 188 views::InkDropState::QUICK_ACTION); | |
| 130 view_controller_->ExecuteAction(true); | 189 view_controller_->ExecuteAction(true); |
| 131 } | 190 } |
| 132 } | 191 } |
| 133 | 192 |
| 134 void ToolbarActionView::UpdateState() { | 193 void ToolbarActionView::UpdateState() { |
| 135 content::WebContents* web_contents = GetCurrentWebContents(); | 194 content::WebContents* web_contents = GetCurrentWebContents(); |
| 136 if (SessionTabHelper::IdForTab(web_contents) < 0) | 195 if (SessionTabHelper::IdForTab(web_contents) < 0) |
| 137 return; | 196 return; |
| 138 | 197 |
| 139 if (!view_controller_->IsEnabled(web_contents) && | 198 if (!view_controller_->IsEnabled(web_contents) && |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; | 337 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; |
| 279 if (delegate_->ShownInsideMenu()) | 338 if (delegate_->ShownInsideMenu()) |
| 280 run_types |= views::MenuRunner::IS_NESTED; | 339 run_types |= views::MenuRunner::IS_NESTED; |
| 281 | 340 |
| 282 // RunMenuAt expects a nested menu to be parented by the same widget as the | 341 // RunMenuAt expects a nested menu to be parented by the same widget as the |
| 283 // already visible menu, in this case the Chrome menu. | 342 // already visible menu, in this case the Chrome menu. |
| 284 views::Widget* parent = delegate_->ShownInsideMenu() ? | 343 views::Widget* parent = delegate_->ShownInsideMenu() ? |
| 285 delegate_->GetOverflowReferenceView()->GetWidget() : | 344 delegate_->GetOverflowReferenceView()->GetWidget() : |
| 286 GetWidget(); | 345 GetWidget(); |
| 287 | 346 |
| 347 ink_drop_animation_controller_->AnimateToState( | |
| 348 views::InkDropState::ACTIVATED); | |
| 349 | |
| 288 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types)); | 350 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types)); |
| 289 | |
| 290 if (menu_runner_->RunMenuAt(parent, | 351 if (menu_runner_->RunMenuAt(parent, |
| 291 this, | 352 this, |
| 292 gfx::Rect(screen_loc, size()), | 353 gfx::Rect(screen_loc, size()), |
| 293 views::MENU_ANCHOR_TOPLEFT, | 354 views::MENU_ANCHOR_TOPLEFT, |
| 294 source_type) == views::MenuRunner::MENU_DELETED) { | 355 source_type) == views::MenuRunner::MENU_DELETED) { |
| 295 return; | 356 return; |
| 296 } | 357 } |
| 358 ink_drop_animation_controller_->AnimateToState( | |
| 359 views::InkDropState::DEACTIVATED); | |
| 297 | 360 |
| 298 context_menu_owner = nullptr; | 361 context_menu_owner = nullptr; |
| 299 menu_runner_.reset(); | 362 menu_runner_.reset(); |
| 300 view_controller_->OnContextMenuClosed(); | 363 view_controller_->OnContextMenuClosed(); |
| 301 | 364 |
| 302 // If another extension action wants to show its context menu, allow it to. | 365 // If another extension action wants to show its context menu, allow it to. |
| 303 if (!followup_context_menu_task_.is_null()) { | 366 if (!followup_context_menu_task_.is_null()) { |
| 304 base::Closure task = followup_context_menu_task_; | 367 base::Closure task = followup_context_menu_task_; |
| 305 followup_context_menu_task_ = base::Closure(); | 368 followup_context_menu_task_ = base::Closure(); |
| 306 task.Run(); | 369 task.Run(); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 320 if (menu_controller->in_nested_run()) { | 383 if (menu_controller->in_nested_run()) { |
| 321 // There is another menu showing. Close the outermost menu (since we are | 384 // 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). | 385 // shown in the same menu, we don't want to close the whole thing). |
| 323 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); | 386 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); |
| 324 return true; | 387 return true; |
| 325 } | 388 } |
| 326 } | 389 } |
| 327 | 390 |
| 328 return false; | 391 return false; |
| 329 } | 392 } |
| OLD | NEW |