border.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (c) 2012 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_BORDER_H_
  5. #define UI_VIEWS_BORDER_H_
  6. #include <memory>
  7. #include "third_party/skia/include/core/SkColor.h"
  8. #include "ui/color/color_id.h"
  9. #include "ui/gfx/color_palette.h"
  10. #include "ui/gfx/geometry/insets.h"
  11. #include "ui/views/views_export.h"
  12. namespace gfx {
  13. class Canvas;
  14. class Size;
  15. } // namespace gfx
  16. namespace views {
  17. class Painter;
  18. class View;
  19. ////////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Border class.
  22. //
  23. // The border class is used to display a border around a view.
  24. // To set a border on a view, call SetBorder on the view, for example:
  25. // view->SetBorder(
  26. // CreateSolidBorder(1, view->GetColorProvider()->GetColor(
  27. // ui::kColorFocusableBorderUnfocused)));
  28. // Make sure the border color is updated on theme changes.
  29. // Once set on a view, the border is owned by the view.
  30. //
  31. // IMPORTANT NOTE: not all views support borders at this point. In order to
  32. // support the border, views should make sure to use bounds excluding the
  33. // border (by calling View::GetLocalBoundsExcludingBorder) when doing layout and
  34. // painting.
  35. //
  36. ////////////////////////////////////////////////////////////////////////////////
  37. class VIEWS_EXPORT Border {
  38. public:
  39. Border();
  40. explicit Border(SkColor color);
  41. Border(const Border&) = delete;
  42. Border& operator=(const Border&) = delete;
  43. virtual ~Border();
  44. // Renders the border for the specified view.
  45. virtual void Paint(const View& view, gfx::Canvas* canvas) = 0;
  46. // Returns the border insets.
  47. virtual gfx::Insets GetInsets() const = 0;
  48. // Returns the minimum size this border requires. Note that this may not be
  49. // the same as the insets. For example, a Border may paint images to draw
  50. // some graphical border around a view, and this would return the minimum size
  51. // such that these images would not be clipped or overlapping -- but the
  52. // insets may be larger or smaller, depending on how the view wanted its
  53. // content laid out relative to these images.
  54. virtual gfx::Size GetMinimumSize() const = 0;
  55. // This is called by the View on which it is attached. This is overridden for
  56. // subclasses that depend on theme colors.
  57. virtual void OnViewThemeChanged(View* view);
  58. SkColor color() const { return color_; }
  59. // Sets the border color.
  60. void set_color(SkColor color) { color_ = color; }
  61. private:
  62. SkColor color_ = gfx::kPlaceholderColor;
  63. };
  64. // Convenience for creating a scoped_ptr with no Border.
  65. VIEWS_EXPORT std::unique_ptr<Border> NullBorder();
  66. // Creates a border that is a simple line of the specified thickness and color.
  67. VIEWS_EXPORT std::unique_ptr<Border> CreateSolidBorder(int thickness,
  68. SkColor color);
  69. // Creates a border that is a simple line of the specified thickness and color,
  70. // which updates on theme changes.
  71. VIEWS_EXPORT std::unique_ptr<Border> CreateThemedSolidBorder(int thickness,
  72. ui::ColorId color);
  73. // Creates a border that is a rounded rectangle of the specified thickness and
  74. // color.
  75. // NOTE: `corner_radius` is an OUTER EDGE RADIUS, not a stroke radius!
  76. VIEWS_EXPORT std::unique_ptr<Border>
  77. CreateRoundedRectBorder(int thickness, float corner_radius, SkColor color);
  78. VIEWS_EXPORT std::unique_ptr<Border> CreateRoundedRectBorder(
  79. int thickness,
  80. float corner_radius,
  81. const gfx::Insets& paint_insets,
  82. SkColor color);
  83. // Same as above except the color updates with theme changes.
  84. VIEWS_EXPORT std::unique_ptr<Border> CreateThemedRoundedRectBorder(
  85. int thickness,
  86. float corner_radius,
  87. ui::ColorId color_id);
  88. VIEWS_EXPORT std::unique_ptr<Border> CreateThemedRoundedRectBorder(
  89. int thickness,
  90. float corner_radius,
  91. const gfx::Insets& paint_insets,
  92. ui::ColorId color_id);
  93. // Creates a border for reserving space. The returned border does not paint
  94. // anything.
  95. VIEWS_EXPORT std::unique_ptr<Border> CreateEmptyBorder(
  96. const gfx::Insets& insets);
  97. // A simpler version of the above for a border with uniform thickness.
  98. VIEWS_EXPORT std::unique_ptr<Border> CreateEmptyBorder(int thickness);
  99. // Creates a border of the specified color, and thickness on each side specified
  100. // in |insets|.
  101. VIEWS_EXPORT std::unique_ptr<Border> CreateSolidSidedBorder(
  102. const gfx::Insets& insets,
  103. SkColor color);
  104. // Creates a new border that draws |border| and adds additional padding. This is
  105. // equivalent to changing the insets of |border| without changing how or what it
  106. // paints. Example:
  107. //
  108. // view->SetBorder(CreatePaddedBorder(
  109. // CreateSolidBorder(1, view->GetColorProvider()->GetColor(
  110. // ui::kColorFocusableBorderUnfocused)),
  111. // gfx::Insets::TLBR(2, 0, 0, 0)));
  112. //
  113. // yields a single dip red border and an additional 2dip of unpainted padding
  114. // above the view content (below the border).
  115. VIEWS_EXPORT std::unique_ptr<Border> CreatePaddedBorder(
  116. std::unique_ptr<Border> border,
  117. const gfx::Insets& insets);
  118. // Creates a Border from the specified Painter. |insets| define size of an area
  119. // allocated for a Border.
  120. VIEWS_EXPORT std::unique_ptr<Border> CreateBorderPainter(
  121. std::unique_ptr<Painter> painter,
  122. const gfx::Insets& insets);
  123. } // namespace views
  124. #endif // UI_VIEWS_BORDER_H_