highlight_border.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2022 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 UI_VIEWS_HIGHLIGHT_BORDER_H_
  5. #define UI_VIEWS_HIGHLIGHT_BORDER_H_
  6. #include "ui/views/border.h"
  7. #include "ui/views/views_export.h"
  8. namespace gfx {
  9. class Rect;
  10. class RoundedCornersF;
  11. } // namespace gfx
  12. namespace views {
  13. constexpr int kHighlightBorderThickness = 1;
  14. // A rounded rectangle border that has inner (highlight) and outer color.
  15. // Useful when go/cros-launcher-spec mentions "BorderHighlight".
  16. class VIEWS_EXPORT HighlightBorder : public views::Border {
  17. public:
  18. // TODO(crbug/1319944): Change these type names to something more descriptive.
  19. enum class Type {
  20. // A higher contrast highlight border than the `kHighlightBorder2` used
  21. // for floating components that do not have a shield below.
  22. kHighlightBorder1,
  23. // A less contrast highlight border for components that float above a
  24. // shield.
  25. kHighlightBorder2,
  26. // Has same inner border color as `kHighlightBorder1`. The outer color is
  27. // same in dark and light mode with low opacity. This type is mainly used
  28. // with `HighlightBorderLayerOverlay` whose outer border is outside the
  29. // window contents. For more information, refer to the comment of
  30. // `HighlightBorderOverlay`.
  31. kHighlightBorder3,
  32. };
  33. // The type of insets created by this highlight border. The insets shrink the
  34. // content view.
  35. enum class InsetsType {
  36. // The border does not add insets that shrink the content, so the content
  37. // around the edge of the view can be overlapped with the border.
  38. kNoInsets,
  39. // Insetting by half of the border size pushes the content inside the outer
  40. // color of the border so the content bounds is surrounded by outer color
  41. // of the border.
  42. kHalfInsets,
  43. // Insetting by full border size makes sure that contents in the view
  44. // start inside the inner color of the highlight border, so there is no
  45. // overlap between content.
  46. kFullInsets,
  47. };
  48. // Paints the highlight border onto `canvas` for the specified `view`. The
  49. // color of the border will be determined using `view`'s color provider. Note
  50. // that directly using this function won't set the insets on any view so it
  51. // acts like setting kNoInsets when using HighlightBorder class.
  52. static void PaintBorderToCanvas(gfx::Canvas* canvas,
  53. const views::View& view,
  54. const gfx::Rect& bounds,
  55. const gfx::RoundedCornersF& corner_radii,
  56. Type type,
  57. bool use_light_colors);
  58. // Returns the inner highlight color used to paint highlight border.
  59. static SkColor GetHighlightColor(const views::View& view,
  60. HighlightBorder::Type type,
  61. bool use_light_colors);
  62. // Returns the outer border color used to paint highlight border.
  63. static SkColor GetBorderColor(const views::View& view,
  64. HighlightBorder::Type type,
  65. bool use_light_colors);
  66. HighlightBorder(int corner_radius,
  67. Type type,
  68. bool use_light_colors,
  69. InsetsType insets_type = InsetsType::kNoInsets);
  70. HighlightBorder(const HighlightBorder&) = delete;
  71. HighlightBorder& operator=(const HighlightBorder&) = delete;
  72. ~HighlightBorder() override = default;
  73. // views::Border:
  74. void Paint(const views::View& view, gfx::Canvas* canvas) override;
  75. gfx::Insets GetInsets() const override;
  76. gfx::Size GetMinimumSize() const override;
  77. private:
  78. const int corner_radius_;
  79. const Type type_;
  80. // True if the border should use light colors when the D/L mode feature is
  81. // not enabled.
  82. const bool use_light_colors_;
  83. // Used by `GetInsets()` to calculate the insets.
  84. const InsetsType insets_type_;
  85. };
  86. } // namespace views
  87. #endif // UI_VIEWS_HIGHLIGHT_BORDER_H_