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
|
// Copyright 2019 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/controls/highlight_path_generator.h"
#include <algorithm>
#include <utility>
#include "third_party/skia/include/core/SkRect.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/geometry/rrect_f.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
namespace views {
HighlightPathGenerator::HighlightPathGenerator()
: HighlightPathGenerator(gfx::Insets()) {}
HighlightPathGenerator::HighlightPathGenerator(const gfx::Insets& insets)
: insets_(insets) {}
HighlightPathGenerator::~HighlightPathGenerator() = default;
// static
void HighlightPathGenerator::Install(
View* host,
std::unique_ptr<HighlightPathGenerator> generator) {
host->SetProperty(kHighlightPathGeneratorKey, std::move(generator));
}
// static
std::optional<gfx::RRectF> HighlightPathGenerator::GetRoundRectForView(
const View* view) {
HighlightPathGenerator* path_generator =
view->GetProperty(kHighlightPathGeneratorKey);
return path_generator ? path_generator->GetRoundRect(view) : std::nullopt;
}
SkPath HighlightPathGenerator::GetHighlightPath(const View* view) {
// A rounded rectangle must be supplied if using this default implementation.
std::optional<gfx::RRectF> round_rect = GetRoundRect(view);
DCHECK(round_rect);
return SkPath().addRRect(SkRRect{*round_rect});
}
std::optional<gfx::RRectF> HighlightPathGenerator::GetRoundRect(
const gfx::RectF& rect) {
return std::nullopt;
}
std::optional<gfx::RRectF> HighlightPathGenerator::GetRoundRect(
const View* view) {
gfx::Rect bounds =
use_contents_bounds_ ? view->GetContentsBounds() : view->GetLocalBounds();
bounds.Inset(insets_);
if (use_mirrored_rect_) {
bounds = view->GetMirroredRect(bounds);
}
return GetRoundRect(gfx::RectF(bounds));
}
std::optional<gfx::RRectF> EmptyHighlightPathGenerator::GetRoundRect(
const gfx::RectF& rect) {
return gfx::RRectF();
}
void InstallEmptyHighlightPathGenerator(View* view) {
HighlightPathGenerator::Install(
view, std::make_unique<EmptyHighlightPathGenerator>());
}
std::optional<gfx::RRectF> RectHighlightPathGenerator::GetRoundRect(
const gfx::RectF& rect) {
return gfx::RRectF(rect);
}
void InstallRectHighlightPathGenerator(View* view) {
HighlightPathGenerator::Install(
view, std::make_unique<RectHighlightPathGenerator>());
}
CircleHighlightPathGenerator::CircleHighlightPathGenerator(
const gfx::Insets& insets)
: HighlightPathGenerator(insets) {}
std::optional<gfx::RRectF> CircleHighlightPathGenerator::GetRoundRect(
const gfx::RectF& rect) {
gfx::RectF bounds = rect;
const float corner_radius = std::min(bounds.width(), bounds.height()) / 2.f;
bounds.ClampToCenteredSize(
gfx::SizeF(corner_radius * 2.f, corner_radius * 2.f));
return gfx::RRectF(bounds, corner_radius);
}
void InstallCircleHighlightPathGenerator(View* view) {
InstallCircleHighlightPathGenerator(view, gfx::Insets());
}
void InstallCircleHighlightPathGenerator(View* view,
const gfx::Insets& insets) {
HighlightPathGenerator::Install(
view, std::make_unique<CircleHighlightPathGenerator>(insets));
}
std::optional<gfx::RRectF> PillHighlightPathGenerator::GetRoundRect(
const gfx::RectF& rect) {
gfx::RectF bounds = rect;
const float corner_radius = std::min(bounds.width(), bounds.height()) / 2.f;
return gfx::RRectF(bounds, corner_radius);
}
void InstallPillHighlightPathGenerator(View* view) {
HighlightPathGenerator::Install(
view, std::make_unique<PillHighlightPathGenerator>());
}
FixedSizeCircleHighlightPathGenerator::FixedSizeCircleHighlightPathGenerator(
int radius)
: radius_(radius) {}
std::optional<gfx::RRectF> FixedSizeCircleHighlightPathGenerator::GetRoundRect(
const gfx::RectF& rect) {
gfx::RectF bounds = rect;
bounds.ClampToCenteredSize(gfx::SizeF(radius_ * 2, radius_ * 2));
return gfx::RRectF(bounds, radius_);
}
void InstallFixedSizeCircleHighlightPathGenerator(View* view, int radius) {
HighlightPathGenerator::Install(
view, std::make_unique<FixedSizeCircleHighlightPathGenerator>(radius));
}
RoundRectHighlightPathGenerator::RoundRectHighlightPathGenerator(
const gfx::Insets& insets,
int corner_radius)
: RoundRectHighlightPathGenerator(insets,
gfx::RoundedCornersF(corner_radius)) {}
RoundRectHighlightPathGenerator::RoundRectHighlightPathGenerator(
const gfx::Insets& insets,
const gfx::RoundedCornersF& rounded_corners)
: HighlightPathGenerator(insets), rounded_corners_(rounded_corners) {}
std::optional<gfx::RRectF> RoundRectHighlightPathGenerator::GetRoundRect(
const gfx::RectF& rect) {
return gfx::RRectF(rect, rounded_corners_);
}
void InstallRoundRectHighlightPathGenerator(View* view,
const gfx::Insets& insets,
int corner_radius) {
HighlightPathGenerator::Install(
view,
std::make_unique<RoundRectHighlightPathGenerator>(insets, corner_radius));
}
void InstallRoundRectHighlightPathGenerator(
View* view,
const gfx::Insets& insets,
const gfx::RoundedCornersF& corner_radii) {
HighlightPathGenerator::Install(
view,
std::make_unique<RoundRectHighlightPathGenerator>(insets, corner_radii));
}
} // namespace views
|