1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/background.h"
#include <optional>
#include <utility>
#include "base/check.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "cc/paint/paint_flags.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_variant.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/views/painter.h"
#include "ui/views/view.h"
#if BUILDFLAG(IS_WIN)
#include "skia/ext/skia_utils_win.h"
#endif
namespace views {
// SolidBackground is a trivial Background implementation that fills the
// background in a solid color.
class SolidBackground : public Background {
public:
explicit SolidBackground(ui::ColorVariant color) { SetColor(color); }
SolidBackground(const SolidBackground&) = delete;
SolidBackground& operator=(const SolidBackground&) = delete;
void Paint(gfx::Canvas* canvas, View* view) const override {
// Fill the background. Note that we don't constrain to the bounds as
// canvas is already clipped for us.
canvas->DrawColor(color().ResolveToSkColor(view->GetColorProvider()));
}
void OnViewThemeChanged(View* view) override {
if (color().IsSemantic()) {
view->SchedulePaint();
}
}
};
// Shared class for RoundedRectBackground and ThemedRoundedRectBackground.
class RoundedRectBackground : public Background {
public:
RoundedRectBackground(ui::ColorVariant color,
const gfx::RoundedCornersF& radii,
const gfx::Insets& insets)
: radii_(radii), insets_(insets) {
SetColor(color);
}
RoundedRectBackground(const RoundedRectBackground&) = delete;
RoundedRectBackground& operator=(const RoundedRectBackground&) = delete;
void Paint(gfx::Canvas* canvas, View* view) const override {
gfx::Rect rect(view->GetLocalBounds());
rect.Inset(insets_);
SkPath path;
SkScalar radii[8] = {radii_.upper_left(), radii_.upper_left(),
radii_.upper_right(), radii_.upper_right(),
radii_.lower_right(), radii_.lower_right(),
radii_.lower_left(), radii_.lower_left()};
path.addRoundRect(gfx::RectToSkRect(rect), radii);
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(color().ResolveToSkColor(view->GetColorProvider()));
canvas->DrawPath(path, flags);
}
std::optional<gfx::RoundedCornersF> GetRoundedCornerRadii() const override {
return radii_;
}
void OnViewThemeChanged(View* view) override {
if (color().IsSemantic()) {
view->SchedulePaint();
}
}
private:
const gfx::RoundedCornersF radii_;
const gfx::Insets insets_;
};
// ThemedVectorIconBackground is an image drawn on the view's background using
// ThemedVectorIcon to react to theme changes.
class ThemedVectorIconBackground : public Background {
public:
explicit ThemedVectorIconBackground(const ui::ThemedVectorIcon& icon)
: icon_(icon) {
DCHECK(!icon_.empty());
}
ThemedVectorIconBackground(const ThemedVectorIconBackground&) = delete;
ThemedVectorIconBackground& operator=(const ThemedVectorIconBackground&) =
delete;
void OnViewThemeChanged(View* view) override { view->SchedulePaint(); }
void Paint(gfx::Canvas* canvas, View* view) const override {
canvas->DrawImageInt(icon_.GetImageSkia(view->GetColorProvider()), 0, 0);
}
private:
const ui::ThemedVectorIcon icon_;
};
class BackgroundPainter : public Background {
public:
explicit BackgroundPainter(std::unique_ptr<Painter> painter)
: painter_(std::move(painter)) {
DCHECK(painter_);
}
BackgroundPainter(const BackgroundPainter&) = delete;
BackgroundPainter& operator=(const BackgroundPainter&) = delete;
~BackgroundPainter() override = default;
void Paint(gfx::Canvas* canvas, View* view) const override {
Painter::PaintPainterAt(canvas, painter_.get(), view->GetLocalBounds());
}
void SetColor(ui::ColorVariant color) override {
NOTREACHED() << "It does not make sense to `SetColor()` for a painter "
"based background.";
}
private:
std::unique_ptr<Painter> painter_;
};
Background::Background() = default;
Background::~Background() = default;
void Background::SetColor(ui::ColorVariant color) {
color_ = color;
}
void Background::OnViewThemeChanged(View* view) {}
std::optional<gfx::RoundedCornersF> Background::GetRoundedCornerRadii() const {
return std::nullopt;
}
/////////////////////////////////////////////////////////////////////////////
// Factory methods implementations:
/////////////////////////////////////////////////////////////////////////////
std::unique_ptr<Background> CreateSolidBackground(ui::ColorVariant color) {
return std::make_unique<SolidBackground>(color);
}
std::unique_ptr<Background> CreateRoundedRectBackground(
ui::ColorVariant color,
float radius,
int for_border_thickness) {
return CreateRoundedRectBackground(color, gfx::RoundedCornersF{radius},
for_border_thickness);
}
std::unique_ptr<Background> CreateRoundedRectBackground(
ui::ColorVariant color,
float top_radius,
float bottom_radius,
int for_border_thickness) {
return CreateRoundedRectBackground(
color,
gfx::RoundedCornersF{top_radius, top_radius, bottom_radius,
bottom_radius},
for_border_thickness);
}
std::unique_ptr<Background> CreateRoundedRectBackground(
ui::ColorVariant color,
const gfx::RoundedCornersF& radii,
int for_border_thickness) {
return CreateRoundedRectBackground(color, radii,
gfx::Insets(for_border_thickness / 2.0f));
}
std::unique_ptr<Background> CreateRoundedRectBackground(
ui::ColorVariant color,
const gfx::RoundedCornersF& radii,
const gfx::Insets& insets) {
// If the radii is not set, fallback to SolidBackground since it results in
// more efficient tiling by cc. See crbug.com/1464128.
if (radii.IsEmpty() && insets.IsEmpty()) {
return CreateSolidBackground(color);
}
return std::make_unique<RoundedRectBackground>(color, radii, insets);
}
std::unique_ptr<Background> CreateThemedVectorIconBackground(
const ui::ThemedVectorIcon& icon) {
return std::make_unique<ThemedVectorIconBackground>(icon);
}
std::unique_ptr<Background> CreateBackgroundFromPainter(
std::unique_ptr<Painter> painter) {
return std::make_unique<BackgroundPainter>(std::move(painter));
}
} // namespace views
|