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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
// 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/painter.h"
#include <utility>
#include "base/check.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_delegate.h"
#include "ui/compositor/layer_owner.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/insets_f.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/nine_image_painter.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/views/view.h"
namespace views {
namespace {
// SolidRoundRectPainter -------------------------------------------------------
// Creates a round rect painter with a 1 pixel border. The border paints on top
// of the background.
class SolidRoundRectPainter : public Painter {
public:
SolidRoundRectPainter(SkColor bg_color,
SkColor stroke_color,
gfx::RoundedCornersF radii,
const gfx::Insets& insets,
SkBlendMode blend_mode,
bool antialias,
bool should_border_scale);
SolidRoundRectPainter(const SolidRoundRectPainter&) = delete;
SolidRoundRectPainter& operator=(const SolidRoundRectPainter&) = delete;
~SolidRoundRectPainter() override;
// Painter:
gfx::Size GetMinimumSize() const override;
void Paint(gfx::Canvas* canvas, const gfx::Size& size) override;
private:
const SkColor bg_color_;
const SkColor stroke_color_;
const gfx::RoundedCornersF radii_;
const gfx::Insets insets_;
const SkBlendMode blend_mode_;
const bool antialias_;
const bool should_border_scale_;
};
SolidRoundRectPainter::SolidRoundRectPainter(SkColor bg_color,
SkColor stroke_color,
gfx::RoundedCornersF radii,
const gfx::Insets& insets,
SkBlendMode blend_mode,
bool antialias,
bool should_border_scale)
: bg_color_(bg_color),
stroke_color_(stroke_color),
radii_(radii),
insets_(insets),
blend_mode_(blend_mode),
antialias_(antialias),
should_border_scale_(should_border_scale) {}
SolidRoundRectPainter::~SolidRoundRectPainter() = default;
gfx::Size SolidRoundRectPainter::GetMinimumSize() const {
return gfx::Size();
}
void SolidRoundRectPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
gfx::ScopedCanvas scoped_canvas(canvas);
const float scale = canvas->UndoDeviceScaleFactor();
gfx::Rect inset_rect(size);
inset_rect.Inset(insets_);
gfx::RectF fill_rect(gfx::ScaleToEnclosedRect(inset_rect, scale));
gfx::RectF stroke_rect = fill_rect;
cc::PaintFlags flags;
flags.setBlendMode(blend_mode_);
if (antialias_) {
flags.setAntiAlias(true);
}
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(bg_color_);
SkPath fill_path;
const std::array<SkScalar, 8> scaled_radii = {
{radii_.upper_left() * scale, radii_.upper_left() * scale,
radii_.upper_right() * scale, radii_.upper_right() * scale,
radii_.lower_right() * scale, radii_.lower_right() * scale,
radii_.lower_left() * scale, radii_.lower_left() * scale}};
UNSAFE_TODO(fill_path.addRoundRect(gfx::RectFToSkRect(fill_rect),
scaled_radii.data()));
canvas->DrawPath(fill_path, flags);
if (stroke_color_ != SK_ColorTRANSPARENT) {
const float stroke_width = should_border_scale_ ? scale : 1.0f;
const float stroke_inset = stroke_width / 2;
stroke_rect.Inset(gfx::InsetsF(stroke_inset));
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(stroke_width);
flags.setColor(stroke_color_);
SkPath stroke_path;
std::array<SkScalar, 8> stroke_radii;
for (size_t i = 0; i < 8; i++) {
stroke_radii[i] = scaled_radii[i] - stroke_width / 2;
}
UNSAFE_TODO(stroke_path.addRoundRect(gfx::RectFToSkRect(stroke_rect),
stroke_radii.data()));
canvas->DrawPath(stroke_path, flags);
}
}
// SolidFocusPainter -----------------------------------------------------------
class SolidFocusPainter : public Painter {
public:
SolidFocusPainter(SkColor color, int thickness, const gfx::InsetsF& insets);
SolidFocusPainter(const SolidFocusPainter&) = delete;
SolidFocusPainter& operator=(const SolidFocusPainter&) = delete;
~SolidFocusPainter() override;
// Painter:
gfx::Size GetMinimumSize() const override;
void Paint(gfx::Canvas* canvas, const gfx::Size& size) override;
private:
const SkColor color_;
const int thickness_;
const gfx::InsetsF insets_;
};
SolidFocusPainter::SolidFocusPainter(SkColor color,
int thickness,
const gfx::InsetsF& insets)
: color_(color), thickness_(thickness), insets_(insets) {}
SolidFocusPainter::~SolidFocusPainter() = default;
gfx::Size SolidFocusPainter::GetMinimumSize() const {
return gfx::Size();
}
void SolidFocusPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
gfx::RectF rect((gfx::Rect(size)));
rect.Inset(insets_);
canvas->DrawSolidFocusRect(rect, color_, thickness_);
}
// ImagePainter ---------------------------------------------------------------
// ImagePainter stores and paints nine images as a scalable grid.
class ImagePainter : public Painter {
public:
// Constructs an ImagePainter with the specified image resource ids.
// See CreateImageGridPainter()'s comment regarding image ID count and order.
explicit ImagePainter(const int image_ids[]);
// Constructs an ImagePainter with the specified image and insets.
ImagePainter(const gfx::ImageSkia& image, const gfx::Insets& insets);
ImagePainter(const ImagePainter&) = delete;
ImagePainter& operator=(const ImagePainter&) = delete;
~ImagePainter() override;
// Painter:
gfx::Size GetMinimumSize() const override;
void Paint(gfx::Canvas* canvas, const gfx::Size& size) override;
private:
std::unique_ptr<gfx::NineImagePainter> nine_painter_;
};
ImagePainter::ImagePainter(const int image_ids[])
: nine_painter_(ui::CreateNineImagePainter(image_ids)) {}
ImagePainter::ImagePainter(const gfx::ImageSkia& image,
const gfx::Insets& insets)
: nine_painter_(new gfx::NineImagePainter(image, insets)) {}
ImagePainter::~ImagePainter() = default;
gfx::Size ImagePainter::GetMinimumSize() const {
return nine_painter_->GetMinimumSize();
}
void ImagePainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
nine_painter_->Paint(canvas, gfx::Rect(size));
}
class PaintedLayer : public ui::LayerOwner, public ui::LayerDelegate {
public:
explicit PaintedLayer(std::unique_ptr<Painter> painter);
PaintedLayer(const PaintedLayer&) = delete;
PaintedLayer& operator=(const PaintedLayer&) = delete;
~PaintedLayer() override;
// LayerDelegate:
void OnPaintLayer(const ui::PaintContext& context) override;
void OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) override;
private:
std::unique_ptr<Painter> painter_;
};
PaintedLayer::PaintedLayer(std::unique_ptr<Painter> painter)
: painter_(std::move(painter)) {
SetLayer(std::make_unique<ui::Layer>(ui::LAYER_TEXTURED));
layer()->set_delegate(this);
}
PaintedLayer::~PaintedLayer() = default;
void PaintedLayer::OnPaintLayer(const ui::PaintContext& context) {
ui::PaintRecorder recorder(context, layer()->size());
painter_->Paint(recorder.canvas(), layer()->size());
}
void PaintedLayer::OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) {}
} // namespace
// Painter --------------------------------------------------------------------
Painter::Painter() = default;
Painter::~Painter() = default;
// static
void Painter::PaintPainterAt(gfx::Canvas* canvas,
Painter* painter,
const gfx::Rect& rect) {
DCHECK(canvas);
DCHECK(painter);
canvas->Save();
canvas->Translate(rect.OffsetFromOrigin());
painter->Paint(canvas, rect.size());
canvas->Restore();
}
// static
void Painter::PaintFocusPainter(View* view,
gfx::Canvas* canvas,
Painter* focus_painter) {
if (focus_painter && view->HasFocus()) {
PaintPainterAt(canvas, focus_painter, view->GetLocalBounds());
}
}
// static
std::unique_ptr<Painter> Painter::CreateSolidRoundRectPainter(
SkColor color,
float radius,
const gfx::Insets& insets,
SkBlendMode blend_mode,
bool antialias) {
return std::make_unique<SolidRoundRectPainter>(
color, SK_ColorTRANSPARENT, gfx::RoundedCornersF(radius), insets,
blend_mode, antialias, false);
}
// static
std::unique_ptr<Painter> Painter::CreateSolidRoundRectPainterWithVariableRadius(
SkColor color,
gfx::RoundedCornersF radii,
const gfx::Insets& insets,
SkBlendMode blend_mode,
bool antialias) {
return std::make_unique<SolidRoundRectPainter>(
color, SK_ColorTRANSPARENT, radii, insets, blend_mode, antialias, false);
}
// static
std::unique_ptr<Painter> Painter::CreateRoundRectWith1PxBorderPainter(
SkColor bg_color,
SkColor stroke_color,
float radius,
SkBlendMode blend_mode,
bool antialias,
bool should_border_scale) {
return std::make_unique<SolidRoundRectPainter>(
bg_color, stroke_color, gfx::RoundedCornersF(radius), gfx::Insets(),
blend_mode, antialias, should_border_scale);
}
// static
std::unique_ptr<Painter> Painter::CreateRoundRectWith1PxBorderPainter(
SkColor bg_color,
SkColor stroke_color,
gfx::RoundedCornersF radii,
SkBlendMode blend_mode,
bool antialias,
bool should_border_scale) {
return std::make_unique<SolidRoundRectPainter>(
bg_color, stroke_color, radii, gfx::Insets(), blend_mode, antialias,
should_border_scale);
}
// static
std::unique_ptr<Painter> Painter::CreateImagePainter(
const gfx::ImageSkia& image,
const gfx::Insets& insets) {
return std::make_unique<ImagePainter>(image, insets);
}
// static
std::unique_ptr<Painter> Painter::CreateImageGridPainter(
const int image_ids[]) {
return std::make_unique<ImagePainter>(image_ids);
}
// static
std::unique_ptr<Painter> Painter::CreateSolidFocusPainter(
SkColor color,
const gfx::Insets& insets) {
// Before Canvas::DrawSolidFocusRect correctly inset the rect's bounds based
// on the thickness, callers had to add 1 to the bottom and right insets.
// Subtract that here so it works the same way with the new
// Canvas::DrawSolidFocusRect.
const gfx::InsetsF corrected_insets(insets - gfx::Insets::TLBR(0, 0, 1, 1));
return std::make_unique<SolidFocusPainter>(color, 1, corrected_insets);
}
// static
std::unique_ptr<Painter> Painter::CreateSolidFocusPainter(
SkColor color,
int thickness,
const gfx::InsetsF& insets) {
return std::make_unique<SolidFocusPainter>(color, thickness, insets);
}
// static
std::unique_ptr<ui::LayerOwner> Painter::CreatePaintedLayer(
std::unique_ptr<Painter> painter) {
return std::make_unique<PaintedLayer>(std::move(painter));
}
} // namespace views
|