| 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 29 matching lines...) Expand all Loading... |
| 130 | 124 |
| 131 image()->SetFillsBoundsOpaquely(true); | 125 image()->SetFillsBoundsOpaquely(true); |
| 132 image()->SetPaintToLayer(false); | 126 image()->SetPaintToLayer(false); |
| 133 SetPaintToLayer(false); | 127 SetPaintToLayer(false); |
| 134 } | 128 } |
| 135 | 129 |
| 136 gfx::Point ToolbarActionView::CalculateInkDropCenter() const { | 130 gfx::Point ToolbarActionView::CalculateInkDropCenter() const { |
| 137 return GetLocalBounds().CenterPoint(); | 131 return GetLocalBounds().CenterPoint(); |
| 138 } | 132 } |
| 139 | 133 |
| 134 views::InkDropDelegate* ToolbarActionView::GetInkDropDelegate() const { |
| 135 return ink_drop_delegate_.get(); |
| 136 } |
| 137 |
| 140 gfx::Size ToolbarActionView::GetPreferredSize() const { | 138 gfx::Size ToolbarActionView::GetPreferredSize() const { |
| 141 return gfx::Size(ToolbarActionsBar::IconWidth(false), | 139 return gfx::Size(ToolbarActionsBar::IconWidth(false), |
| 142 ToolbarActionsBar::IconHeight()); | 140 ToolbarActionsBar::IconHeight()); |
| 143 } | 141 } |
| 144 | 142 |
| 145 void ToolbarActionView::Layout() { | |
| 146 MenuButton::Layout(); | |
| 147 ink_drop_animation_controller_->SetInkDropCenter(CalculateInkDropCenter()); | |
| 148 } | |
| 149 | |
| 150 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) { | |
| 151 // views::MenuButton actions are only triggered by left mouse clicks. | |
| 152 if (event.IsLeftMouseButton()) { | |
| 153 ink_drop_animation_controller_->AnimateToState( | |
| 154 views::InkDropState::ACTION_PENDING); | |
| 155 } | |
| 156 return MenuButton::OnMousePressed(event); | |
| 157 } | |
| 158 | |
| 159 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) { | 143 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) { |
| 160 // TODO(varkha): refactor similar implementations in ToolbarButton, | |
| 161 // FindBarButton and here. | |
| 162 if (menu_) { | 144 if (menu_) { |
| 163 // While dropdown menu is showing the button should not handle gestures. | 145 // While dropdown menu is showing the button should not handle gestures. |
| 164 event->StopPropagation(); | 146 event->StopPropagation(); |
| 165 return; | 147 return; |
| 166 } | 148 } |
| 167 | |
| 168 MenuButton::OnGestureEvent(event); | 149 MenuButton::OnGestureEvent(event); |
| 169 | |
| 170 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN; | |
| 171 switch (event->type()) { | |
| 172 case ui::ET_GESTURE_TAP_DOWN: | |
| 173 ink_drop_state = views::InkDropState::ACTION_PENDING; | |
| 174 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that | |
| 175 // subsequent events for the gesture are sent to |this|. | |
| 176 event->SetHandled(); | |
| 177 break; | |
| 178 case ui::ET_GESTURE_LONG_PRESS: | |
| 179 ink_drop_state = views::InkDropState::SLOW_ACTION_PENDING; | |
| 180 break; | |
| 181 case ui::ET_GESTURE_LONG_TAP: | |
| 182 ink_drop_state = views::InkDropState::SLOW_ACTION; | |
| 183 break; | |
| 184 case ui::ET_GESTURE_SCROLL_BEGIN: | |
| 185 case ui::ET_GESTURE_END: | |
| 186 ink_drop_state = views::InkDropState::HIDDEN; | |
| 187 break; | |
| 188 default: | |
| 189 return; | |
| 190 } | |
| 191 | |
| 192 views::InkDropState current_ink_drop_state = | |
| 193 ink_drop_animation_controller_->GetInkDropState(); | |
| 194 | |
| 195 if (ink_drop_state == views::InkDropState::HIDDEN && | |
| 196 (current_ink_drop_state == views::InkDropState::QUICK_ACTION || | |
| 197 current_ink_drop_state == views::InkDropState::SLOW_ACTION || | |
| 198 current_ink_drop_state == views::InkDropState::DEACTIVATED)) { | |
| 199 // These InkDropStates automatically transition to the HIDDEN state so we | |
| 200 // don't make an explicit call. Explicitly animating to HIDDEN in this case | |
| 201 // would prematurely pre-empt these animations. | |
| 202 return; | |
| 203 } | |
| 204 ink_drop_animation_controller_->AnimateToState(ink_drop_state); | |
| 205 } | 150 } |
| 206 | 151 |
| 207 void ToolbarActionView::OnDragDone() { | 152 void ToolbarActionView::OnDragDone() { |
| 208 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
| 209 views::MenuButton::OnDragDone(); | 153 views::MenuButton::OnDragDone(); |
| 210 delegate_->OnToolbarActionViewDragDone(); | 154 delegate_->OnToolbarActionViewDragDone(); |
| 211 } | 155 } |
| 212 | 156 |
| 213 void ToolbarActionView::ViewHierarchyChanged( | 157 void ToolbarActionView::ViewHierarchyChanged( |
| 214 const ViewHierarchyChangedDetails& details) { | 158 const ViewHierarchyChangedDetails& details) { |
| 215 if (details.is_add && !called_register_command_ && GetFocusManager()) { | 159 if (details.is_add && !called_register_command_ && GetFocusManager()) { |
| 216 view_controller_->RegisterCommand(); | 160 view_controller_->RegisterCommand(); |
| 217 called_register_command_ = true; | 161 called_register_command_ = true; |
| 218 } | 162 } |
| 219 | 163 |
| 220 MenuButton::ViewHierarchyChanged(details); | 164 MenuButton::ViewHierarchyChanged(details); |
| 221 } | 165 } |
| 222 | 166 |
| 223 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { | 167 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) { |
| 224 views::MenuButton::GetAccessibleState(state); | 168 views::MenuButton::GetAccessibleState(state); |
| 225 state->role = ui::AX_ROLE_BUTTON; | 169 state->role = ui::AX_ROLE_BUTTON; |
| 226 } | 170 } |
| 227 | 171 |
| 228 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, | 172 void ToolbarActionView::OnMenuButtonClicked(views::View* sender, |
| 229 const gfx::Point& point) { | 173 const gfx::Point& point) { |
| 230 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { | 174 if (!view_controller_->IsEnabled(GetCurrentWebContents())) { |
| 231 // We should only get a button pressed event with a non-enabled action if | 175 // We should only get a button pressed event with a non-enabled action if |
| 232 // the left-click behavior should open the menu. | 176 // the left-click behavior should open the menu. |
| 233 DCHECK(view_controller_->DisabledClickOpensMenu()); | 177 DCHECK(view_controller_->DisabledClickOpensMenu()); |
| 234 context_menu_controller()->ShowContextMenuForView(this, point, | 178 context_menu_controller()->ShowContextMenuForView(this, point, |
| 235 ui::MENU_SOURCE_NONE); | 179 ui::MENU_SOURCE_NONE); |
| 236 } else { | 180 } else { |
| 237 ink_drop_animation_controller_->AnimateToState( | |
| 238 views::InkDropState::QUICK_ACTION); | |
| 239 view_controller_->ExecuteAction(true); | 181 view_controller_->ExecuteAction(true); |
| 240 } | 182 } |
| 241 } | 183 } |
| 242 | 184 |
| 243 void ToolbarActionView::OnMenuButtonClickCanceled(views::View* sender) { | |
| 244 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN); | |
| 245 } | |
| 246 | |
| 247 void ToolbarActionView::UpdateState() { | 185 void ToolbarActionView::UpdateState() { |
| 248 content::WebContents* web_contents = GetCurrentWebContents(); | 186 content::WebContents* web_contents = GetCurrentWebContents(); |
| 249 if (SessionTabHelper::IdForTab(web_contents) < 0) | 187 if (SessionTabHelper::IdForTab(web_contents) < 0) |
| 250 return; | 188 return; |
| 251 | 189 |
| 252 if (!view_controller_->IsEnabled(web_contents) && | 190 if (!view_controller_->IsEnabled(web_contents) && |
| 253 !view_controller_->DisabledClickOpensMenu()) { | 191 !view_controller_->DisabledClickOpensMenu()) { |
| 254 SetState(views::CustomButton::STATE_DISABLED); | 192 SetState(views::CustomButton::STATE_DISABLED); |
| 255 } else if (state() == views::CustomButton::STATE_DISABLED) { | 193 } else if (state() == views::CustomButton::STATE_DISABLED) { |
| 256 SetState(views::CustomButton::STATE_NORMAL); | 194 SetState(views::CustomButton::STATE_NORMAL); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; | 333 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; |
| 396 if (delegate_->ShownInsideMenu()) | 334 if (delegate_->ShownInsideMenu()) |
| 397 run_types |= views::MenuRunner::IS_NESTED; | 335 run_types |= views::MenuRunner::IS_NESTED; |
| 398 | 336 |
| 399 // RunMenuAt expects a nested menu to be parented by the same widget as the | 337 // RunMenuAt expects a nested menu to be parented by the same widget as the |
| 400 // already visible menu, in this case the Chrome menu. | 338 // already visible menu, in this case the Chrome menu. |
| 401 views::Widget* parent = delegate_->ShownInsideMenu() ? | 339 views::Widget* parent = delegate_->ShownInsideMenu() ? |
| 402 delegate_->GetOverflowReferenceView()->GetWidget() : | 340 delegate_->GetOverflowReferenceView()->GetWidget() : |
| 403 GetWidget(); | 341 GetWidget(); |
| 404 | 342 |
| 405 ink_drop_animation_controller_->AnimateToState( | 343 views::InkDropDelegate* ink_drop_delegate = GetInkDropDelegate(); |
| 406 views::InkDropState::ACTIVATED); | 344 if (ink_drop_delegate) |
| 345 ink_drop_delegate->OnAction(views::InkDropState::ACTIVATED); |
| 407 | 346 |
| 408 views::MenuModelAdapter adapter(context_menu_model); | 347 views::MenuModelAdapter adapter(context_menu_model); |
| 409 menu_ = adapter.CreateMenu(); | 348 menu_ = adapter.CreateMenu(); |
| 410 menu_runner_.reset(new views::MenuRunner(menu_, run_types)); | 349 menu_runner_.reset(new views::MenuRunner(menu_, run_types)); |
| 411 | 350 |
| 412 if (context_menu_callback) | 351 if (context_menu_callback) |
| 413 context_menu_callback->Run(this); | 352 context_menu_callback->Run(this); |
| 414 if (menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()), | 353 if (menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()), |
| 415 views::MENU_ANCHOR_TOPLEFT, | 354 views::MENU_ANCHOR_TOPLEFT, |
| 416 source_type) == views::MenuRunner::MENU_DELETED) { | 355 source_type) == views::MenuRunner::MENU_DELETED) { |
| 417 menu_ = nullptr; | 356 menu_ = nullptr; |
| 418 return; | 357 return; |
| 419 } | 358 } |
| 420 ink_drop_animation_controller_->AnimateToState( | 359 if (ink_drop_delegate) |
| 421 views::InkDropState::DEACTIVATED); | 360 ink_drop_delegate->OnAction(views::InkDropState::DEACTIVATED); |
| 422 | 361 |
| 423 menu_runner_.reset(); | 362 menu_runner_.reset(); |
| 424 menu_ = nullptr; | 363 menu_ = nullptr; |
| 425 context_menu_owner = nullptr; | 364 context_menu_owner = nullptr; |
| 426 view_controller_->OnContextMenuClosed(); | 365 view_controller_->OnContextMenuClosed(); |
| 427 | 366 |
| 428 // If another extension action wants to show its context menu, allow it to. | 367 // If another extension action wants to show its context menu, allow it to. |
| 429 if (!followup_context_menu_task_.is_null()) { | 368 if (!followup_context_menu_task_.is_null()) { |
| 430 base::Closure task = followup_context_menu_task_; | 369 base::Closure task = followup_context_menu_task_; |
| 431 followup_context_menu_task_ = base::Closure(); | 370 followup_context_menu_task_ = base::Closure(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 446 if (menu_controller->in_nested_run()) { | 385 if (menu_controller->in_nested_run()) { |
| 447 // There is another menu showing. Close the outermost menu (since we are | 386 // There is another menu showing. Close the outermost menu (since we are |
| 448 // shown in the same menu, we don't want to close the whole thing). | 387 // shown in the same menu, we don't want to close the whole thing). |
| 449 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); | 388 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); |
| 450 return true; | 389 return true; |
| 451 } | 390 } |
| 452 } | 391 } |
| 453 | 392 |
| 454 return false; | 393 return false; |
| 455 } | 394 } |
| OLD | NEW |