close_button.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_CLOSE_BUTTON_H_
  5. #define ASH_STYLE_CLOSE_BUTTON_H_
  6. #include "ui/base/metadata/metadata_header_macros.h"
  7. #include "ui/views/controls/button/image_button.h"
  8. #include "ui/views/view_targeter_delegate.h"
  9. namespace gfx {
  10. struct VectorIcon;
  11. } // namespace gfx
  12. namespace ash {
  13. // A circular ImageButton with kCloseButtonIcon inside. It has small, medium and
  14. // large different sizes. The touch area of the small close button will be
  15. // expanded.
  16. class CloseButton : public views::ImageButton,
  17. public views::ViewTargeterDelegate {
  18. public:
  19. METADATA_HEADER(CloseButton);
  20. enum class Type {
  21. kSmall,
  22. kMedium,
  23. kLarge,
  24. kSmallFloating,
  25. kMediumFloating,
  26. kLargeFloating,
  27. };
  28. CloseButton(PressedCallback callback,
  29. Type type,
  30. bool use_light_colors = false);
  31. CloseButton(const CloseButton&) = delete;
  32. CloseButton& operator=(const CloseButton&) = delete;
  33. ~CloseButton() override;
  34. // Returns true if the `bounding_box_` of the touch events intersect with the
  35. // button's bounds. Used to help enlarge the hit area of the close button.
  36. // Note, only necessary for `kSmall` type of CloseButton.
  37. bool DoesIntersectScreenRect(const gfx::Rect& screen_rect) const;
  38. // Resets the listener so that the listener can go out of scope.
  39. void ResetListener();
  40. // Sets the vector icon of the button. Note, doing this only when the button
  41. // wants to have different icons as the default close icon. E.g, the delete
  42. // button inside desks template wants to have trash icon instead of close
  43. // icon.
  44. void SetVectorIcon(const gfx::VectorIcon& icon);
  45. // Sets the button's background color or icon's color. Note, do this only when
  46. // the button wants to have different colors from the default ones.
  47. void SetBackgroundColor(const SkColor background_color);
  48. void SetIconColor(const SkColor icon_color);
  49. private:
  50. // views::ImageButton:
  51. void OnThemeChanged() override;
  52. gfx::Size CalculatePreferredSize() const override;
  53. // views::ViewTargeterDelegate:
  54. bool DoesIntersectRect(const views::View* target,
  55. const gfx::Rect& rect) const override;
  56. void UpdateVectorIcon();
  57. const Type type_;
  58. const gfx::VectorIcon* icon_;
  59. // True if the button wants to use light colors when the D/L mode feature is
  60. // not enabled. Note, can be removed when D/L mode feature is fully launched.
  61. const bool use_light_colors_;
  62. // Customized value for the button's background color and icon's color.
  63. absl::optional<SkColor> background_color_;
  64. absl::optional<SkColor> icon_color_;
  65. };
  66. } // namespace ash
  67. #endif // ASH_STYLE_CLOSE_BUTTON_H_