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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
// Copyright 2022 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/native_theme/native_theme_fluent.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "cc/paint/paint_canvas.h"
#include "cc/paint/paint_flags.h"
#include "skia/ext/font_utils.h"
#include "third_party/skia/include/core/SkFont.h"
#include "third_party/skia/include/core/SkFontMgr.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkTextBlob.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/rrect_f.h"
#include "ui/native_theme/features/native_theme_features.h"
#include "ui/native_theme/native_theme_constants_fluent.h"
namespace ui {
NativeThemeFluent::NativeThemeFluent(bool should_only_use_dark_colors)
: NativeThemeBase(should_only_use_dark_colors) {
set_use_overlay_scrollbar(CalculateUseOverlayScrollbar());
scrollbar_width_ = kFluentScrollbarThickness;
}
NativeThemeFluent::~NativeThemeFluent() = default;
// static
NativeThemeFluent* NativeThemeFluent::web_instance() {
static base::NoDestructor<NativeThemeFluent> s_native_theme_for_web(
/*should_only_use_dark_colors=*/false);
return s_native_theme_for_web.get();
}
void NativeThemeFluent::PaintArrowButton(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
const gfx::Rect& rect,
Part direction,
State state,
ColorScheme color_scheme,
bool in_forced_colors,
const ScrollbarArrowExtraParams& extra_params) const {
PaintButton(canvas, color_provider, rect, direction, color_scheme,
in_forced_colors, extra_params);
PaintArrow(canvas, color_provider, rect, direction, state, color_scheme,
extra_params);
}
void NativeThemeFluent::PaintScrollbarTrack(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
Part part,
State state,
const ScrollbarTrackExtraParams& extra_params,
const gfx::Rect& rect,
ColorScheme color_scheme,
bool in_forced_colors) const {
gfx::Rect track_fill_rect = rect;
if (in_forced_colors) {
gfx::Insets edge_insets;
if (part == NativeTheme::Part::kScrollbarHorizontalTrack) {
edge_insets.set_left_right(-kFluentScrollbarTrackOutlineWidth,
-kFluentScrollbarTrackOutlineWidth);
} else {
edge_insets.set_top_bottom(-kFluentScrollbarTrackOutlineWidth,
-kFluentScrollbarTrackOutlineWidth);
}
const gfx::InsetsF outline_insets(kFluentScrollbarTrackOutlineWidth / 2.0f);
gfx::RectF outline_rect(rect);
outline_rect.Inset(outline_insets + gfx::InsetsF(edge_insets));
const SkColor track_outline_color =
color_provider->GetColor(kColorWebNativeControlScrollbarThumb);
cc::PaintFlags outline_flags;
outline_flags.setColor(track_outline_color);
outline_flags.setStyle(cc::PaintFlags::kStroke_Style);
outline_flags.setStrokeWidth(kFluentScrollbarTrackOutlineWidth);
canvas->drawRect(gfx::RectFToSkRect(outline_rect), outline_flags);
// Adjust fill rect to not overlap with the outline stroke rect.
constexpr gfx::Insets fill_insets(kFluentScrollbarTrackOutlineWidth);
track_fill_rect.Inset(fill_insets + edge_insets);
}
const SkColor track_color =
extra_params.track_color.has_value()
? extra_params.track_color.value()
: color_provider->GetColor(kColorWebNativeControlScrollbarTrack);
cc::PaintFlags flags;
flags.setColor(track_color);
canvas->drawIRect(gfx::RectToSkIRect(track_fill_rect), flags);
}
void NativeThemeFluent::PaintScrollbarThumb(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
Part part,
State state,
const gfx::Rect& rect,
const ScrollbarThumbExtraParams& extra_params,
ColorScheme color_scheme) const {
DCHECK_NE(state, NativeTheme::kDisabled);
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setColor(GetScrollbarThumbColor(*color_provider, state, extra_params));
const SkRect sk_rect = gfx::RectToSkRect(rect);
if (extra_params.is_web_test) {
// Web tests draw the thumb as a square to avoid issues that come with the
// differences in calculation of anti-aliasing and rounding in different
// platforms.
canvas->drawRect(sk_rect, flags);
} else {
canvas->drawRRect(SkRRect::MakeRectXY(sk_rect, kFluentScrollbarPartsRadius,
kFluentScrollbarPartsRadius),
flags);
}
}
gfx::Insets NativeThemeFluent::GetScrollbarSolidColorThumbInsets(
Part part) const {
// TODO(crbug.com/40213017): We should probably move the thumb rect insetting
// logic from blink::ScrollbarThemeFluent::ThumbRect() to here, to make sure
// the web UI and the native UI use the same thumb insetting logic.
return gfx::Insets();
}
SkColor4f NativeThemeFluent::GetScrollbarThumbColor(
const ui::ColorProvider& color_provider,
State state,
const ScrollbarThumbExtraParams& extra_params) const {
auto get_color_id = [&] {
if (state == NativeTheme::kPressed) {
return kColorWebNativeControlScrollbarThumbPressed;
} else if (state == NativeTheme::kHovered) {
return kColorWebNativeControlScrollbarThumbHovered;
} else if (extra_params.is_thumb_minimal_mode) {
return kColorWebNativeControlScrollbarThumbOverlayMinimalMode;
}
return kColorWebNativeControlScrollbarThumb;
};
return SkColor4f::FromColor(
GetContrastingPressedOrHoveredColor(
extra_params.thumb_color,
extra_params.track_color.value_or(
color_provider.GetColor(kColorWebNativeControlScrollbarTrack)),
state, /*part=*/Part::kScrollbarVerticalThumb)
.value_or(color_provider.GetColor(get_color_id())));
}
void NativeThemeFluent::PaintScrollbarCorner(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
State state,
const gfx::Rect& rect,
const ScrollbarTrackExtraParams& extra_params,
ColorScheme color_scheme) const {
cc::PaintFlags flags;
const SkColor corner_color =
extra_params.track_color.has_value()
? extra_params.track_color.value()
: color_provider->GetColor(kColorWebNativeControlScrollbarCorner);
flags.setColor(corner_color);
canvas->drawIRect(RectToSkIRect(rect), flags);
}
gfx::Size NativeThemeFluent::GetPartSize(Part part,
State state,
const ExtraParams& extra) const {
switch (part) {
case kScrollbarHorizontalThumb:
return gfx::Size(kFluentScrollbarMinimalThumbLength,
kFluentScrollbarThumbThickness);
case kScrollbarVerticalThumb:
return gfx::Size(kFluentScrollbarThumbThickness,
kFluentScrollbarMinimalThumbLength);
case kScrollbarHorizontalTrack:
return gfx::Size(0, scrollbar_width_);
case kScrollbarVerticalTrack:
return gfx::Size(scrollbar_width_, 0);
case kScrollbarUpArrow:
case kScrollbarDownArrow:
return gfx::Size(scrollbar_width_, kFluentScrollbarButtonSideLength);
case kScrollbarLeftArrow:
case kScrollbarRightArrow:
return gfx::Size(kFluentScrollbarButtonSideLength, scrollbar_width_);
default:
break;
}
return NativeThemeBase::GetPartSize(part, state, extra);
}
void NativeThemeFluent::PaintButton(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
const gfx::Rect& rect,
Part direction,
ColorScheme color_scheme,
bool in_forced_colors,
const ScrollbarArrowExtraParams& extra_params) const {
cc::PaintFlags flags;
const SkColor button_color = extra_params.track_color.value_or(
color_provider->GetColor(kColorWebNativeControlScrollbarTrack));
flags.setColor(button_color);
gfx::Rect button_fill_rect = rect;
if (in_forced_colors) {
const gfx::InsetsF outline_insets(kFluentScrollbarTrackOutlineWidth / 2.0f);
gfx::Insets edge_insets;
if (direction == NativeTheme::Part::kScrollbarUpArrow) {
edge_insets.set_bottom(-kFluentScrollbarTrackOutlineWidth);
} else if (direction == NativeTheme::Part::kScrollbarDownArrow) {
edge_insets.set_top(-kFluentScrollbarTrackOutlineWidth);
} else if (direction == NativeTheme::Part::kScrollbarLeftArrow) {
edge_insets.set_right(-kFluentScrollbarTrackOutlineWidth);
} else if (direction == NativeTheme::Part::kScrollbarRightArrow) {
edge_insets.set_left(-kFluentScrollbarTrackOutlineWidth);
}
gfx::RectF outline_rect(rect);
outline_rect.Inset(outline_insets + gfx::InsetsF(edge_insets));
const SkColor arrow_outline_color =
color_provider->GetColor(kColorWebNativeControlScrollbarThumb);
cc::PaintFlags outline_flags;
outline_flags.setColor(arrow_outline_color);
outline_flags.setStyle(cc::PaintFlags::kStroke_Style);
outline_flags.setStrokeWidth(kFluentScrollbarTrackOutlineWidth);
if (IsFluentOverlayScrollbarEnabled()) {
PaintRoundedButton(canvas, gfx::RectFToSkRect(outline_rect),
outline_flags, direction);
} else {
canvas->drawRect(gfx::RectFToSkRect(outline_rect), outline_flags);
}
// Adjust the fill rect to not overlap with the outline stroke rect.
constexpr gfx::Insets fill_insets(kFluentScrollbarTrackOutlineWidth);
button_fill_rect.Inset(fill_insets + edge_insets);
}
if (IsFluentOverlayScrollbarEnabled()) {
PaintRoundedButton(canvas, gfx::RectToSkRect(button_fill_rect), flags,
direction);
} else {
canvas->drawIRect(gfx::RectToSkIRect(button_fill_rect), flags);
}
}
void NativeThemeFluent::PaintArrow(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
const gfx::Rect& rect,
Part part,
State state,
ColorScheme color_scheme,
const ScrollbarArrowExtraParams& extra_params) const {
const ColorId arrow_color_id =
state == NativeTheme::kPressed || state == NativeTheme::kHovered
? kColorWebNativeControlScrollbarArrowForegroundPressed
: kColorWebNativeControlScrollbarArrowForeground;
const SkColor arrow_color =
GetContrastingPressedOrHoveredColor(
extra_params.thumb_color,
extra_params.track_color.value_or(
color_provider->GetColor(kColorWebNativeControlScrollbarTrack)),
state, part)
.value_or(color_provider->GetColor(arrow_color_id));
cc::PaintFlags flags;
flags.setColor(arrow_color);
if (!typeface_.has_value()) {
const sk_sp<SkFontMgr> font_manager(skia::DefaultFontMgr());
typeface_ = sk_sp<SkTypeface>(
font_manager->matchFamilyStyle(kFluentScrollbarFont, SkFontStyle()));
}
if (!ArrowIconsAvailable()) {
// Paint regular triangular arrows if the font with arrow icons is not
// available. GetArrowRect() returns the float rect but it is expected to be
// the integer rect in this case.
const SkPath path =
PathForArrow(ToNearestRect(GetArrowRect(rect, part, state)), part);
canvas->drawPath(path, flags);
return;
}
const gfx::RectF bounding_rect = GetArrowRect(rect, part, state);
// The bounding rect for an arrow is a square, so that we can use the width
// despite the arrow direction.
CHECK(typeface_.has_value());
SkFont font(typeface_.value(), bounding_rect.width());
font.setEdging(SkFont::Edging::kAntiAlias);
font.setSubpixel(true);
flags.setAntiAlias(true);
const char* arrow_code_point = GetArrowCodePointForScrollbarPart(part);
canvas->drawTextBlob(SkTextBlob::MakeFromString(arrow_code_point, font),
bounding_rect.x(), bounding_rect.bottom(), flags);
}
gfx::RectF NativeThemeFluent::GetArrowRect(const gfx::Rect& rect,
Part part,
State state) const {
int min_rect_side, max_rect_side;
std::tie(min_rect_side, max_rect_side) =
std::minmax(rect.width(), rect.height());
const int arrow_side = GetArrowSideLength(state);
// Calculates the scaling ratio used to determine the arrow rect side length.
const float arrow_to_button_side_scale_ratio =
arrow_side / static_cast<float>(kFluentScrollbarButtonSideLength);
int side_length =
base::ClampCeil(max_rect_side * arrow_to_button_side_scale_ratio);
gfx::RectF arrow_rect(rect);
if (ArrowIconsAvailable()) {
arrow_rect.ClampToCenteredSize(gfx::SizeF(side_length, side_length));
} else {
// Add 1px to the side length if the difference between smaller button rect
// and arrow side length is odd to keep the arrow rect in the center as well
// as use int coordinates. This avoids the usage of anti-aliasing.
side_length += (min_rect_side - side_length) % 2;
arrow_rect.ClampToCenteredSize(gfx::SizeF(side_length, side_length));
arrow_rect.set_origin(
gfx::PointF(std::floor(arrow_rect.x()), std::floor(arrow_rect.y())));
}
// The end result is a centered arrow rect within the button rect with the
// applied offset.
OffsetArrowRect(arrow_rect, part, max_rect_side);
return arrow_rect;
}
int NativeThemeFluent::GetArrowSideLength(State state) const {
if (state == NativeTheme::kPressed) {
return ArrowIconsAvailable()
? kFluentScrollbarPressedArrowRectLength
: kFluentScrollbarPressedArrowRectFallbackLength;
}
return kFluentScrollbarArrowRectLength;
}
void NativeThemeFluent::OffsetArrowRect(gfx::RectF& arrow_rect,
Part part,
int max_rect_side) const {
const float scaled_offset =
std::round(kFluentScrollbarArrowOffset * max_rect_side /
static_cast<float>(kFluentScrollbarButtonSideLength));
switch (part) {
case kScrollbarUpArrow:
arrow_rect.Offset(0, -scaled_offset);
break;
case kScrollbarDownArrow:
arrow_rect.Offset(0, scaled_offset);
break;
case kScrollbarLeftArrow:
arrow_rect.Offset(-scaled_offset, 0);
break;
case kScrollbarRightArrow:
arrow_rect.Offset(scaled_offset, 0);
break;
default:
NOTREACHED();
}
}
const char* NativeThemeFluent::GetArrowCodePointForScrollbarPart(
Part part) const {
switch (part) {
case Part::kScrollbarUpArrow:
return kFluentScrollbarUpArrow;
case Part::kScrollbarDownArrow:
return kFluentScrollbarDownArrow;
case Part::kScrollbarLeftArrow:
return kFluentScrollbarLeftArrow;
case Part::kScrollbarRightArrow:
return kFluentScrollbarRightArrow;
default:
NOTREACHED();
}
}
int NativeThemeFluent::GetPaintedScrollbarTrackInset() const {
return kFluentPaintedScrollbarTrackInset;
}
float NativeThemeFluent::GetContrastRatioForState(State state,
Part part) const {
CHECK(SupportedPartsForContrastingColor(part));
// Calculated by taking the contrast ratio between the foreground and
// background colors.
static constexpr float kFluentScrollbarForegroundContrastRatio = 1.8f;
return kFluentScrollbarForegroundContrastRatio;
}
void NativeThemeFluent::PaintRoundedButton(cc::PaintCanvas* canvas,
SkRect rect,
cc::PaintFlags paint_flags,
NativeTheme::Part direction) const {
paint_flags.setAntiAlias(true);
SkScalar upper_left_radius = 0;
SkScalar lower_left_radius = 0;
SkScalar upper_right_radius = 0;
SkScalar lower_right_radius = 0;
if (direction == NativeTheme::kScrollbarUpArrow) {
upper_left_radius = kFluentScrollbarPartsRadius;
upper_right_radius = kFluentScrollbarPartsRadius;
} else if (direction == NativeTheme::kScrollbarDownArrow) {
lower_left_radius = kFluentScrollbarPartsRadius;
lower_right_radius = kFluentScrollbarPartsRadius;
} else if (direction == NativeTheme::kScrollbarLeftArrow) {
lower_left_radius = kFluentScrollbarPartsRadius;
upper_left_radius = kFluentScrollbarPartsRadius;
} else if (direction == NativeTheme::kScrollbarRightArrow) {
lower_right_radius = kFluentScrollbarPartsRadius;
upper_right_radius = kFluentScrollbarPartsRadius;
}
gfx::RRectF rounded_rect(
gfx::SkRectToRectF(rect), upper_left_radius, upper_left_radius,
upper_right_radius, upper_right_radius, lower_right_radius,
lower_right_radius, lower_left_radius, lower_left_radius);
canvas->drawRRect(static_cast<SkRRect>(rounded_rect), paint_flags);
}
} // namespace ui
|