pill_button.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_PILL_BUTTON_H_
  5. #define ASH_STYLE_PILL_BUTTON_H_
  6. #include "ash/ash_export.h"
  7. #include "ui/base/metadata/metadata_header_macros.h"
  8. #include "ui/views/controls/button/label_button.h"
  9. #include "ui/views/metadata/view_factory.h"
  10. namespace ash {
  11. // A label button with a rounded rectangle background. It can have an icon
  12. // inside as well, and its text and background colors will be different based on
  13. // the type of the button.
  14. class ASH_EXPORT PillButton : public views::LabelButton {
  15. public:
  16. METADATA_HEADER(PillButton);
  17. static constexpr int kPillButtonHorizontalSpacing = 16;
  18. // Types of the PillButton.
  19. enum class Type {
  20. // PillButton with an icon, default text and background colors.
  21. kIcon,
  22. // PillButton with an icon, default text and background colors,
  23. // `kPillButtonLargeHeight` as the button height.
  24. kIconLarge,
  25. // PillButton without an icon, default text and background colors.
  26. kIconless,
  27. // PillButton without an icon, `kButtonLabelColorPrimary` as the text color
  28. // and `kControlBackgroundColorAlert` as the background color.
  29. kIconlessAlert,
  30. // PillButton without an icon, `kButtonLabelColorBlue` as the text color and
  31. // default background color.
  32. kIconlessAccent,
  33. // PillButton without an icon, default text color and
  34. // `kControlBackgroundColorActive` as the background color.
  35. kIconlessProminent,
  36. // `kIconless` button without background.
  37. kIconlessFloating,
  38. // `kIconlessAccent` button without background.
  39. kIconlessAccentFloating,
  40. };
  41. // Keeps the button in light mode if `use_light_colors` is true.
  42. // InstallRoundRectHighlightPathGenerator for the button only if
  43. // `rounded_highlight_path` is true. This is special handlings for buttons
  44. // inside the old notifications UI, might can be removed once
  45. // `kNotificationsRefresh` is fully launched.
  46. explicit PillButton(PressedCallback callback = PressedCallback(),
  47. const std::u16string& text = std::u16string(),
  48. Type type = Type::kIconless,
  49. const gfx::VectorIcon* icon = nullptr,
  50. int horizontal_spacing = kPillButtonHorizontalSpacing,
  51. bool use_light_colors = false,
  52. bool rounded_highlight_path = true);
  53. PillButton(const PillButton&) = delete;
  54. PillButton& operator=(const PillButton&) = delete;
  55. ~PillButton() override;
  56. // views::LabelButton:
  57. gfx::Size CalculatePreferredSize() const override;
  58. int GetHeightForWidth(int width) const override;
  59. void OnThemeChanged() override;
  60. // Sets the button's background color, text's color or icon's color. Note, do
  61. // this only when the button wants to have different colors from the default
  62. // ones.
  63. void SetBackgroundColor(const SkColor background_color);
  64. void SetButtonTextColor(const SkColor text_color);
  65. void SetIconColor(const SkColor icon_color);
  66. void SetPillButtonType(Type type);
  67. // Sets the button's label to use the default label font, which is smaller
  68. // and less heavily weighted.
  69. void SetUseDefaultLabelFont();
  70. private:
  71. // Initialize the button layout according to the button type.
  72. void InitializeButtonLayout();
  73. // Returns the spacing on the side where the icon locates. The value is set
  74. // smaller to make the spacing on two sides visually look the same.
  75. int GetHorizontalSpacingWithIcon() const;
  76. Type type_;
  77. const gfx::VectorIcon* const icon_;
  78. // True if the button wants to use light colors when the D/L mode feature is
  79. // not enabled. Note, can be removed when D/L mode feature is fully launched.
  80. bool use_light_colors_;
  81. // Horizontal spacing of this button. `kPillButtonHorizontalSpacing` will be
  82. // set as the default value.
  83. int horizontal_spacing_;
  84. // The flag that indicates if highlight path is used for focus ring.
  85. const bool rounded_highlight_path_;
  86. // Customized value for the button's background color, text's color and icon's
  87. // color.
  88. absl::optional<SkColor> background_color_;
  89. absl::optional<SkColor> text_color_;
  90. absl::optional<SkColor> icon_color_;
  91. };
  92. BEGIN_VIEW_BUILDER(ASH_EXPORT, PillButton, views::LabelButton)
  93. VIEW_BUILDER_PROPERTY(const SkColor, BackgroundColor)
  94. VIEW_BUILDER_PROPERTY(const SkColor, TextColor)
  95. VIEW_BUILDER_PROPERTY(const SkColor, IconColor)
  96. VIEW_BUILDER_PROPERTY(PillButton::Type, PillButtonType)
  97. END_VIEW_BUILDER
  98. } // namespace ash
  99. DEFINE_VIEW_BUILDER(ASH_EXPORT, ash::PillButton)
  100. #endif // ASH_STYLE_PILL_BUTTON_H_