background.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_BACKGROUND_H_
  5. #define UI_VIEWS_BACKGROUND_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include "build/build_config.h"
  9. #include "third_party/skia/include/core/SkColor.h"
  10. #include "ui/base/themed_vector_icon.h"
  11. #include "ui/color/color_id.h"
  12. #include "ui/gfx/color_palette.h"
  13. #include "ui/views/views_export.h"
  14. namespace gfx {
  15. class Canvas;
  16. }
  17. namespace ui {
  18. class ThemedVectorIcon;
  19. }
  20. namespace views {
  21. class Painter;
  22. class View;
  23. /////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Background class
  26. //
  27. // A background implements a way for views to paint a background. The
  28. // background can be either solid or based on a gradient. Of course,
  29. // Background can be subclassed to implement various effects.
  30. //
  31. // Any View can have a background. See View::SetBackground() and
  32. // View::OnPaintBackground()
  33. //
  34. /////////////////////////////////////////////////////////////////////////////
  35. class VIEWS_EXPORT Background {
  36. public:
  37. Background();
  38. Background(const Background&) = delete;
  39. Background& operator=(const Background&) = delete;
  40. virtual ~Background();
  41. // Render the background for the provided view
  42. virtual void Paint(gfx::Canvas* canvas, View* view) const = 0;
  43. // Set a solid, opaque color to be used when drawing backgrounds of native
  44. // controls. Unfortunately alpha=0 is not an option.
  45. void SetNativeControlColor(SkColor color);
  46. // This is called by the View on which it is attached. This is overridden for
  47. // subclasses that depend on theme colors.
  48. virtual void OnViewThemeChanged(View* view);
  49. // Returns the "background color". This is equivalent to the color set in
  50. // SetNativeControlColor(). For solid backgrounds, this is the color; for
  51. // gradient backgrounds, it's the midpoint of the gradient; for painter
  52. // backgrounds, this is not useful (returns a default color).
  53. SkColor get_color() const { return color_; }
  54. private:
  55. SkColor color_ = gfx::kPlaceholderColor;
  56. };
  57. // Creates a background that fills the canvas in the specified color.
  58. VIEWS_EXPORT std::unique_ptr<Background> CreateSolidBackground(SkColor color);
  59. // Creates a background that fills the canvas with rounded corners.
  60. // If using a rounded rect border as well, pass its radius as `radius` and its
  61. // thickness as `for_border_thickness`. This will inset the background properly
  62. // so it doesn't bleed through the border.
  63. VIEWS_EXPORT std::unique_ptr<Background> CreateRoundedRectBackground(
  64. SkColor color,
  65. float radius,
  66. int for_border_thickness = 0);
  67. // Same as above except it uses the color specified by the views's ColorProvider
  68. // and the given color identifier.
  69. VIEWS_EXPORT std::unique_ptr<Background> CreateThemedRoundedRectBackground(
  70. ui::ColorId color_id,
  71. float radius,
  72. int for_border_thickness = 0);
  73. // Creates a background that fills the canvas in the color specified by the
  74. // view's ColorProvider and the given color identifier.
  75. VIEWS_EXPORT std::unique_ptr<Background> CreateThemedSolidBackground(
  76. ui::ColorId color_id);
  77. // Creates a background from the specified Painter.
  78. VIEWS_EXPORT std::unique_ptr<Background> CreateBackgroundFromPainter(
  79. std::unique_ptr<Painter> painter);
  80. // Creates a background from the specified ThemedVectorIcon.
  81. VIEWS_EXPORT std::unique_ptr<Background> CreateThemedVectorIconBackground(
  82. const ui::ThemedVectorIcon& icon);
  83. } // namespace views
  84. #endif // UI_VIEWS_BACKGROUND_H_