clipboard_history_main_button.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2020 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/clipboard/views/clipboard_history_main_button.h"
  5. #include "ash/clipboard/views/clipboard_history_item_view.h"
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/public/cpp/style/scoped_light_mode_as_default.h"
  8. #include "ash/style/ash_color_id.h"
  9. #include "ash/style/style_util.h"
  10. #include "base/bind.h"
  11. #include "ui/color/color_provider.h"
  12. #include "ui/gfx/canvas.h"
  13. #include "ui/views/accessibility/view_accessibility.h"
  14. #include "ui/views/animation/ink_drop.h"
  15. #include "ui/views/controls/focus_ring.h"
  16. namespace ash {
  17. ClipboardHistoryMainButton::ClipboardHistoryMainButton(
  18. ClipboardHistoryItemView* container)
  19. : Button(base::BindRepeating(
  20. [](ClipboardHistoryItemView* item, const ui::Event& event) {
  21. item->HandleMainButtonPressEvent(event);
  22. },
  23. base::Unretained(container))),
  24. container_(container) {
  25. SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
  26. views::InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::ON);
  27. SetID(ClipboardHistoryUtil::kMainButtonViewID);
  28. // Let the parent handle accessibility features.
  29. GetViewAccessibility().OverrideIsIgnored(/*value=*/true);
  30. // TODO(crbug.com/1205227): Revisit if this comment makes sense still. It was
  31. // attached to CreateInkDrop() but sounds more about talking about a null
  32. // CreateInkDropHighlight(), but at the time of writing the class inherited
  33. // from Button and had no other InkDrop-related override than CreateInkDrop().
  34. // This may need an upstream fix in InkDrop.
  35. //
  36. // We do not use the ripple highlight due to the following reasons:
  37. // (1) Events may be intercepted by the menu controller. As a result, the
  38. // ripple highlight may not update properly.
  39. // (2) The animation to fade in/out highlight does not look good when the menu
  40. // selection is advanced by the up/down arrow key.
  41. // Hence, highlighted background is implemented by customizing in
  42. // `PaintButtonContents()`.
  43. views::InkDrop::UseInkDropForFloodFillRipple(
  44. views::InkDrop::Get(this), /*highlight_on_hover=*/false,
  45. /*highlight_on_focus=*/!views::FocusRing::Get(this));
  46. }
  47. ClipboardHistoryMainButton::~ClipboardHistoryMainButton() = default;
  48. void ClipboardHistoryMainButton::OnHostPseudoFocusUpdated() {
  49. SetShouldHighlight(container_->ShouldHighlight());
  50. }
  51. void ClipboardHistoryMainButton::SetShouldHighlight(bool should_highlight) {
  52. if (should_highlight_ == should_highlight)
  53. return;
  54. should_highlight_ = should_highlight;
  55. SchedulePaint();
  56. }
  57. const char* ClipboardHistoryMainButton::GetClassName() const {
  58. return "ClipboardHistoryMainButton";
  59. }
  60. void ClipboardHistoryMainButton::OnClickCanceled(const ui::Event& event) {
  61. DCHECK(event.IsMouseEvent());
  62. container_->OnMouseClickOnDescendantCanceled();
  63. views::Button::OnClickCanceled(event);
  64. }
  65. void ClipboardHistoryMainButton::OnThemeChanged() {
  66. views::Button::OnThemeChanged();
  67. // Use the light mode as default because the light mode is the default mode
  68. // of the native theme which decides the context menu's background color.
  69. // TODO(andrewxu): remove this line after https://crbug.com/1143009 is
  70. // fixed.
  71. ScopedLightModeAsDefault scoped_light_mode_as_default;
  72. StyleUtil::ConfigureInkDropAttributes(
  73. this, StyleUtil::kBaseColor | StyleUtil::kInkDropOpacity);
  74. }
  75. void ClipboardHistoryMainButton::OnGestureEvent(ui::GestureEvent* event) {
  76. // Give `container_` a chance to handle `event`.
  77. container_->MaybeHandleGestureEventFromMainButton(event);
  78. if (event->handled())
  79. return;
  80. views::Button::OnGestureEvent(event);
  81. // Prevent the menu controller from handling gesture events. The menu
  82. // controller may bring side-effects such as canceling the item selection.
  83. event->SetHandled();
  84. }
  85. void ClipboardHistoryMainButton::PaintButtonContents(gfx::Canvas* canvas) {
  86. if (!should_highlight_)
  87. return;
  88. // Highlight the background when the menu item is selected or pressed.
  89. cc::PaintFlags flags;
  90. flags.setAntiAlias(true);
  91. // Use the color in light mode when dark/light mode is not enabled. As the
  92. // background color of the context menu is from NativeTheme when the feature
  93. // is not enabled, and light mode is the default color of NativeTheme. If
  94. // dark/light mode is enabled, the background color of the context menus
  95. // inside SystemUI will be overridden to align with current system color mode.
  96. const SkColor color =
  97. features::IsDarkLightModeEnabled()
  98. ? GetColorProvider()->GetColor(kColorAshInkDrop)
  99. : SkColorSetA(SK_ColorBLACK,
  100. StyleUtil::kLightInkDropOpacity * SK_AlphaOPAQUE);
  101. flags.setColor(color);
  102. flags.setStyle(cc::PaintFlags::kFill_Style);
  103. canvas->DrawRect(GetLocalBounds(), flags);
  104. }
  105. } // namespace ash