hover_highlight_view.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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
  3. // found in the LICENSE file.
  4. #ifndef ASH_SYSTEM_TRAY_HOVER_HIGHLIGHT_VIEW_H_
  5. #define ASH_SYSTEM_TRAY_HOVER_HIGHLIGHT_VIEW_H_
  6. #include <memory>
  7. #include "ash/system/accessibility/tray_accessibility.h"
  8. #include "ash/system/tray/actionable_view.h"
  9. #include "base/bind.h"
  10. #include "ui/gfx/font.h"
  11. #include "ui/gfx/text_constants.h"
  12. namespace views {
  13. class Border;
  14. class Label;
  15. }
  16. namespace ash {
  17. class TriView;
  18. class ViewClickListener;
  19. // A view that changes background color on hover, and triggers a callback in the
  20. // associated ViewClickListener on click. The view can also be forced to
  21. // maintain a fixed height.
  22. class ASH_EXPORT HoverHighlightView : public ActionableView {
  23. public:
  24. enum class AccessibilityState {
  25. // The default accessibility view.
  26. DEFAULT,
  27. // This view is a checked checkbox.
  28. CHECKED_CHECKBOX,
  29. // This view is an unchecked checkbox.
  30. UNCHECKED_CHECKBOX
  31. };
  32. // If |listener| is null then no action is taken on click.
  33. explicit HoverHighlightView(ViewClickListener* listener);
  34. HoverHighlightView(const HoverHighlightView&) = delete;
  35. HoverHighlightView& operator=(const HoverHighlightView&) = delete;
  36. ~HoverHighlightView() override;
  37. // Convenience function for populating the view with an icon and a label. This
  38. // also sets the accessible name. Primarily used for scrollable rows in
  39. // detailed views.
  40. void AddIconAndLabel(const gfx::ImageSkia& image, const std::u16string& text);
  41. // Convenience function for populating the view with an arbitrary view and a
  42. // label. This also sets the accessible name.
  43. void AddViewAndLabel(std::unique_ptr<views::View> view,
  44. const std::u16string& text);
  45. // Populates the view with a text label, inset on the left by the horizontal
  46. // space that would normally be occupied by an icon.
  47. void AddLabelRow(const std::u16string& text);
  48. // Populates the view with a text label with custom start inset.
  49. void AddLabelRow(const std::u16string& text, int start_inset);
  50. // Adds an optional right icon to an already populated view. |icon_size| is
  51. // the size of the icon in DP.
  52. void AddRightIcon(const gfx::ImageSkia& image, int icon_size);
  53. // Adds an optional right view to an already populated view.
  54. void AddRightView(views::View* view,
  55. std::unique_ptr<views::Border> border = nullptr);
  56. // Hides or shows the right view for an already populated view.
  57. void SetRightViewVisible(bool visible);
  58. // Sets the text of the sub label for an already populated view. |sub_text|
  59. // must not be empty and prior to calling this function, |text_label_| must
  60. // not be null.
  61. void SetSubText(const std::u16string& sub_text);
  62. // Allows view to expand its height. Size of unexapandable view is fixed and
  63. // equals to kTrayPopupItemHeight.
  64. void SetExpandable(bool expandable);
  65. // Changes the view's current accessibility state. This will fire an
  66. // accessibility event if needed.
  67. void SetAccessibilityState(AccessibilityState accessibility_state);
  68. // Removes current children of the view so that it can be re-populated.
  69. void Reset();
  70. bool is_populated() const { return is_populated_; }
  71. views::Label* text_label() { return text_label_; }
  72. views::Label* sub_text_label() { return sub_text_label_; }
  73. views::View* left_view() { return left_view_; }
  74. views::View* right_view() { return right_view_; }
  75. views::View* sub_row() { return sub_row_; }
  76. protected:
  77. // Override from Button to also set the tooltip for all child elements.
  78. void OnSetTooltipText(const std::u16string& tooltip_text) override;
  79. // views::View:
  80. void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  81. const char* GetClassName() const override;
  82. TriView* tri_view() { return tri_view_; }
  83. private:
  84. friend class TrayAccessibilityTest;
  85. // ActionableView:
  86. bool PerformAction(const ui::Event& event) override;
  87. // views::View:
  88. gfx::Size CalculatePreferredSize() const override;
  89. int GetHeightForWidth(int width) const override;
  90. void OnFocus() override;
  91. // Adds a view that acts as a container for all views that are added into the
  92. // sub-row, i.e. the row below the label.
  93. void AddSubRowContainer();
  94. void OnEnabledChanged();
  95. // Determines whether the view is populated or not. If it is, Reset() should
  96. // be called before re-populating the view.
  97. bool is_populated_ = false;
  98. ViewClickListener* const listener_ = nullptr;
  99. views::Label* text_label_ = nullptr;
  100. views::Label* sub_text_label_ = nullptr;
  101. views::View* left_view_ = nullptr;
  102. views::View* right_view_ = nullptr;
  103. views::View* sub_row_ = nullptr;
  104. TriView* tri_view_ = nullptr;
  105. bool expandable_ = false;
  106. AccessibilityState accessibility_state_ = AccessibilityState::DEFAULT;
  107. base::CallbackListSubscription enabled_changed_subscription_ =
  108. AddEnabledChangedCallback(
  109. base::BindRepeating(&HoverHighlightView::OnEnabledChanged,
  110. base::Unretained(this)));
  111. };
  112. } // namespace ash
  113. #endif // ASH_SYSTEM_TRAY_HOVER_HIGHLIGHT_VIEW_H_