painter.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_PAINTER_H_
  5. #define UI_VIEWS_PAINTER_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include "third_party/skia/include/core/SkBlendMode.h"
  9. #include "third_party/skia/include/core/SkColor.h"
  10. #include "ui/base/nine_image_painter_factory.h"
  11. #include "ui/gfx/geometry/insets.h"
  12. #include "ui/views/views_export.h"
  13. namespace gfx {
  14. class Canvas;
  15. class ImageSkia;
  16. class InsetsF;
  17. class Rect;
  18. class Size;
  19. } // namespace gfx
  20. namespace ui {
  21. class LayerOwner;
  22. }
  23. namespace views {
  24. class View;
  25. // Painter, as the name implies, is responsible for painting in a particular
  26. // region. Think of Painter as a Border or Background that can be painted
  27. // in any region of a View.
  28. class VIEWS_EXPORT Painter {
  29. public:
  30. Painter();
  31. Painter(const Painter&) = delete;
  32. Painter& operator=(const Painter&) = delete;
  33. virtual ~Painter();
  34. // A convenience method for painting a Painter in a particular region.
  35. // This translates the canvas to x/y and paints the painter.
  36. static void PaintPainterAt(gfx::Canvas* canvas,
  37. Painter* painter,
  38. const gfx::Rect& rect);
  39. // Convenience that paints |focus_painter| only if |view| HasFocus() and
  40. // |focus_painter| is non-NULL.
  41. static void PaintFocusPainter(View* view,
  42. gfx::Canvas* canvas,
  43. Painter* focus_painter);
  44. // Creates a painter that draws a RoundRect with a solid color and given
  45. // corner radius.
  46. static std::unique_ptr<Painter> CreateSolidRoundRectPainter(
  47. SkColor color,
  48. float radius,
  49. const gfx::Insets& insets = gfx::Insets(),
  50. SkBlendMode blend_mode = SkBlendMode::kSrcOver,
  51. bool antialias = true);
  52. // Creates a painter that draws a RoundRect with a solid color and a given
  53. // corner radius, and also adds a 1px border (inset) in the given color.
  54. static std::unique_ptr<Painter> CreateRoundRectWith1PxBorderPainter(
  55. SkColor bg_color,
  56. SkColor stroke_color,
  57. float radius,
  58. SkBlendMode blend_mode = SkBlendMode::kSrcOver,
  59. bool antialias = true);
  60. // Creates a painter that divides |image| into nine regions. The four corners
  61. // are rendered at the size specified in insets (eg. the upper-left corner is
  62. // rendered at 0 x 0 with a size of insets.left() x insets.top()). The center
  63. // and edge images are stretched to fill the painted area.
  64. static std::unique_ptr<Painter> CreateImagePainter(
  65. const gfx::ImageSkia& image,
  66. const gfx::Insets& insets);
  67. // Creates a painter that paints images in a scalable grid. The images must
  68. // share widths by column and heights by row. The corners are painted at full
  69. // size, while center and edge images are stretched to fill the painted area.
  70. // The center image may be zero (to be skipped). This ordering must be used:
  71. // Top-Left/Top/Top-Right/Left/[Center]/Right/Bottom-Left/Bottom/Bottom-Right.
  72. static std::unique_ptr<Painter> CreateImageGridPainter(const int image_ids[]);
  73. // Deprecated: used the InsetsF version below.
  74. static std::unique_ptr<Painter> CreateSolidFocusPainter(
  75. SkColor color,
  76. const gfx::Insets& insets);
  77. // |thickness| is in dip.
  78. static std::unique_ptr<Painter> CreateSolidFocusPainter(
  79. SkColor color,
  80. int thickness,
  81. const gfx::InsetsF& insets);
  82. // Creates and returns a texture layer that is painted by |painter|.
  83. static std::unique_ptr<ui::LayerOwner> CreatePaintedLayer(
  84. std::unique_ptr<Painter> painter);
  85. // Returns the minimum size this painter can paint without obvious graphical
  86. // problems (e.g. overlapping images).
  87. virtual gfx::Size GetMinimumSize() const = 0;
  88. // Paints the painter in the specified region.
  89. virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) = 0;
  90. };
  91. } // namespace views
  92. #endif // UI_VIEWS_PAINTER_H_