icon_button.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2021 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_STYLE_ICON_BUTTON_H_
  5. #define ASH_STYLE_ICON_BUTTON_H_
  6. #include "ash/ash_export.h"
  7. #include "ui/base/metadata/metadata_header_macros.h"
  8. #include "ui/views/controls/button/image_button.h"
  9. namespace gfx {
  10. class ImageSkia;
  11. struct VectorIcon;
  12. } // namespace gfx
  13. namespace ui {
  14. class Event;
  15. } // namespace ui
  16. namespace ash {
  17. // A circular ImageButton that can have small/medium/large different sizes. Each
  18. // of them has the floating version, which does not have the background. The
  19. // button can be togglable if `is_togglable` is set to true, the icon inside
  20. // might change on different toggle states. A fixed size of EmptyBorder will be
  21. // applied to the button if `has_border` is true, this is done to help
  22. // differentiating focus ring from the content of the button.
  23. class ASH_EXPORT IconButton : public views::ImageButton {
  24. public:
  25. METADATA_HEADER(IconButton);
  26. enum class Type {
  27. kTiny,
  28. kSmall,
  29. kMedium,
  30. kLarge,
  31. kTinyFloating,
  32. kSmallFloating,
  33. kMediumFloating,
  34. kLargeFloating
  35. };
  36. // Used to determine how the button will behave when disabled.
  37. enum class DisabledButtonBehavior {
  38. // The button will display toggle button as off.
  39. kNone = 0,
  40. // The button will display on/off status of toggle.
  41. kCanDisplayDisabledToggleValue = 1,
  42. };
  43. IconButton(PressedCallback callback,
  44. Type type,
  45. const gfx::VectorIcon* icon,
  46. int accessible_name_id);
  47. IconButton(PressedCallback callback,
  48. Type type,
  49. const gfx::VectorIcon* icon,
  50. bool is_togglable,
  51. bool has_border);
  52. IconButton(PressedCallback callback,
  53. Type type,
  54. const gfx::VectorIcon* icon,
  55. const std::u16string& accessible_name,
  56. bool is_togglable,
  57. bool has_border);
  58. IconButton(PressedCallback callback,
  59. Type type,
  60. const gfx::VectorIcon* icon,
  61. int accessible_name_id,
  62. bool is_togglable,
  63. bool has_border);
  64. IconButton(const IconButton&) = delete;
  65. IconButton& operator=(const IconButton&) = delete;
  66. ~IconButton() override;
  67. bool toggled() const { return toggled_; }
  68. void set_button_behavior(DisabledButtonBehavior button_behavior) {
  69. button_behavior_ = button_behavior;
  70. }
  71. // Sets the vector icon of the button, it might change on different `toggled_`
  72. // states.
  73. void SetVectorIcon(const gfx::VectorIcon& icon);
  74. // Sets the button's background color. Note, do this only when the button
  75. // wants to have different color from the default one.
  76. void SetBackgroundColor(const SkColor background_color);
  77. // Sets the button's background image. The |background_image| is resized to
  78. // fit the button. Note, if set, |background_image| is painted on top of
  79. // the button's existing background color.
  80. void SetBackgroundImage(const gfx::ImageSkia& background_image);
  81. // Sets the icon's color. If the button is togglable, this will be the color
  82. // when it's not toggled.
  83. void SetIconColor(const SkColor icon_color);
  84. // Sets the size to use for the vector icon in DIPs.
  85. void SetIconSize(int size);
  86. // Updates the `toggled_` state of the button.
  87. void SetToggled(bool toggled);
  88. // views::ImageButton:
  89. void PaintButtonContents(gfx::Canvas* canvas) override;
  90. void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  91. void OnThemeChanged() override;
  92. void NotifyClick(const ui::Event& event) override;
  93. protected:
  94. void UpdateVectorIcon();
  95. private:
  96. // For unit tests.
  97. friend class BluetoothFeaturePodControllerTest;
  98. const Type type_;
  99. const gfx::VectorIcon* icon_ = nullptr;
  100. // True if this button is togglable.
  101. bool is_togglable_ = false;
  102. // True if the button is currently toggled.
  103. bool toggled_ = false;
  104. // Customized value for button's background color or icon's color.
  105. absl::optional<SkColor> background_color_;
  106. absl::optional<SkColor> icon_color_;
  107. // Custom value for icon size (usually used to make the icon smaller).
  108. absl::optional<int> icon_size_;
  109. DisabledButtonBehavior button_behavior_ = DisabledButtonBehavior::kNone;
  110. };
  111. } // namespace ash
  112. #endif // ASH_STYLE_ICON_BUTTON_H_