actionable_view.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/tray/actionable_view.h"
  5. #include "ash/style/style_util.h"
  6. #include "ash/system/tray/tray_popup_utils.h"
  7. #include "ash/system/tray/tray_utils.h"
  8. #include "base/bind.h"
  9. #include "ui/accessibility/ax_enums.mojom.h"
  10. #include "ui/accessibility/ax_node_data.h"
  11. #include "ui/events/keycodes/keyboard_codes.h"
  12. #include "ui/gfx/canvas.h"
  13. #include "ui/gfx/geometry/rect_f.h"
  14. #include "ui/views/animation/ink_drop.h"
  15. #include "ui/views/animation/ink_drop_highlight.h"
  16. #include "ui/views/animation/ink_drop_ripple.h"
  17. #include "ui/views/painter.h"
  18. namespace ash {
  19. // static
  20. const char ActionableView::kViewClassName[] = "tray/ActionableView";
  21. ActionableView::ActionableView(TrayPopupInkDropStyle ink_drop_style)
  22. : views::Button(base::BindRepeating(&ActionableView::ButtonPressed,
  23. base::Unretained(this))),
  24. ink_drop_style_(ink_drop_style) {
  25. SetFocusBehavior(FocusBehavior::ALWAYS);
  26. SetHasInkDropActionOnClick(false);
  27. SetNotifyEnterExitOnChild(true);
  28. // TODO(pbos): Replace the use of FocusPainter with the FocusRing (using the
  29. // below HighlightPathGenerator).
  30. SetInstallFocusRingOnFocus(false);
  31. SetFocusPainter(TrayPopupUtils::CreateFocusPainter());
  32. TrayPopupUtils::InstallHighlightPathGenerator(this, ink_drop_style_);
  33. views::InkDrop::Get(this)->SetCreateInkDropCallback(base::BindRepeating(
  34. [](Button* host) { return StyleUtil::CreateInkDrop(host); }, this));
  35. views::InkDrop::Get(this)->SetCreateHighlightCallback(
  36. base::BindRepeating(&StyleUtil::CreateInkDropHighlight,
  37. base::Unretained(this), gfx::kPlaceholderColor));
  38. views::InkDrop::Get(this)->SetCreateRippleCallback(base::BindRepeating(
  39. [](ActionableView* host) {
  40. return StyleUtil::CreateInkDropRipple(
  41. GetInkDropInsets(host->ink_drop_style_), host);
  42. },
  43. this));
  44. }
  45. ActionableView::~ActionableView() {
  46. if (destroyed_)
  47. *destroyed_ = true;
  48. }
  49. void ActionableView::HandlePerformActionResult(bool action_performed,
  50. const ui::Event& event) {
  51. views::InkDrop::Get(this)->AnimateToState(
  52. action_performed ? views::InkDropState::ACTION_TRIGGERED
  53. : views::InkDropState::HIDDEN,
  54. ui::LocatedEvent::FromIfValid(&event));
  55. }
  56. const char* ActionableView::GetClassName() const {
  57. return kViewClassName;
  58. }
  59. bool ActionableView::OnKeyPressed(const ui::KeyEvent& event) {
  60. if (GetState() != STATE_DISABLED && event.key_code() == ui::VKEY_SPACE) {
  61. NotifyClick(event);
  62. return true;
  63. }
  64. return Button::OnKeyPressed(event);
  65. }
  66. void ActionableView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
  67. node_data->role = ax::mojom::Role::kButton;
  68. node_data->SetName(GetAccessibleName());
  69. }
  70. void ActionableView::ButtonPressed(const ui::Event& event) {
  71. bool destroyed = false;
  72. destroyed_ = &destroyed;
  73. const bool action_performed = PerformAction(event);
  74. if (destroyed)
  75. return;
  76. destroyed_ = nullptr;
  77. HandlePerformActionResult(action_performed, event);
  78. }
  79. } // namespace ash