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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
|
// Copyright 2013 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_mac.h"
#import <Accessibility/Accessibility.h>
#import <Cocoa/Cocoa.h>
#include <MediaAccessibility/MediaAccessibility.h>
#include <stddef.h>
#include <variant>
#include <vector>
#include "base/command_line.h"
#include "base/mac/mac_util.h"
#include "base/no_destructor.h"
#include "cc/paint/paint_shader.h"
#include "ui/base/cocoa/defaults_utils.h"
#include "ui/base/ui_base_features.h"
#include "ui/base/ui_base_switches.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/native_theme_aura.h"
#include "ui/native_theme/native_theme_utils.h"
namespace {
bool IsDarkMode() {
NSAppearanceName appearance =
[NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAqua, NSAppearanceNameDarkAqua
]];
return [appearance isEqual:NSAppearanceNameDarkAqua];
}
bool PrefersReducedTransparency() {
return NSWorkspace.sharedWorkspace
.accessibilityDisplayShouldReduceTransparency;
}
bool IsHighContrast() {
return NSWorkspace.sharedWorkspace.accessibilityDisplayShouldIncreaseContrast;
}
bool InvertedColors() {
return NSWorkspace.sharedWorkspace.accessibilityDisplayShouldInvertColors;
}
} // namespace
// Helper object to respond to light mode/dark mode changeovers.
@interface NativeThemeEffectiveAppearanceObserver : NSObject
@end
@implementation NativeThemeEffectiveAppearanceObserver {
void (^_handler)() __strong;
}
- (instancetype)initWithHandler:(void (^)())handler {
self = [super init];
if (self) {
_handler = handler;
[NSApp addObserver:self
forKeyPath:@"effectiveAppearance"
options:0
context:nullptr];
}
return self;
}
- (void)dealloc {
[NSApp removeObserver:self forKeyPath:@"effectiveAppearance"];
}
- (void)observeValueForKeyPath:(NSString*)forKeyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context {
_handler();
}
@end
namespace {
// Helper to make indexing an array by an enum class easier.
template <class KEY, class VALUE>
struct EnumArray {
VALUE& operator[](const KEY& key) { return array[static_cast<size_t>(key)]; }
VALUE array[static_cast<size_t>(KEY::COUNT)];
};
} // namespace
namespace ui {
// static
NativeTheme* NativeTheme::GetInstanceForWeb() {
return NativeThemeMacWeb::instance();
}
// static
NativeTheme* NativeTheme::GetInstanceForNativeUi() {
return NativeThemeMac::instance();
}
NativeTheme* NativeTheme::GetInstanceForDarkUI() {
static base::NoDestructor<NativeThemeMac> s_native_theme(
/*configure_web_instance=*/false, /*should_only_use_dark_colors=*/true);
return s_native_theme.get();
}
// static
bool NativeTheme::SystemDarkModeSupported() {
return true;
}
// static
NativeThemeMac* NativeThemeMac::instance() {
static base::NoDestructor<NativeThemeMac> s_native_theme(
/*configure_web_instance=*/true, /*should_only_use_dark_colors=*/false);
return s_native_theme.get();
}
NativeThemeAura::PreferredContrast NativeThemeMac::CalculatePreferredContrast()
const {
return IsHighContrast() ? NativeThemeAura::PreferredContrast::kMore
: NativeThemeAura::PreferredContrast::kNoPreference;
}
void NativeThemeMac::Paint(cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra,
ColorScheme color_scheme,
bool in_forced_colors,
const std::optional<SkColor>& accent_color) const {
ColorScheme color_scheme_updated = color_scheme;
if (color_scheme_updated == ColorScheme::kDefault) {
color_scheme_updated = GetDefaultSystemColorScheme();
}
if (rect.IsEmpty()) {
return;
}
switch (part) {
case kScrollbarHorizontalThumb:
case kScrollbarVerticalThumb:
PaintMacScrollbarThumb(canvas, part, state, rect,
std::get<ScrollbarExtraParams>(extra),
color_scheme_updated);
break;
case kScrollbarHorizontalTrack:
case kScrollbarVerticalTrack:
PaintMacScrollBarTrackOrCorner(canvas, part, state,
std::get<ScrollbarExtraParams>(extra),
rect, color_scheme_updated, false);
break;
case kScrollbarCorner:
PaintMacScrollBarTrackOrCorner(canvas, part, state,
std::get<ScrollbarExtraParams>(extra),
rect, color_scheme_updated, true);
break;
default:
NativeThemeBase::Paint(canvas, color_provider, part, state, rect, extra,
color_scheme, in_forced_colors, accent_color);
break;
}
}
void ConstrainInsets(int old_width, int min_width, int* left, int* right) {
int requested_total_inset = *left + *right;
if (requested_total_inset == 0) {
return;
}
int max_total_inset = old_width - min_width;
if (requested_total_inset < max_total_inset) {
return;
}
if (max_total_inset < 0) {
*left = *right = 0;
return;
}
// Multiply the right/bottom inset by the ratio by which we need to shrink the
// total inset. This has the effect of rounding down the right/bottom inset,
// if the two sides are to be affected unevenly.
// This is done instead of using inset scale functions to maintain expected
// behavior and to map to how it looks like other scrollbars work on MacOS.
*right *= max_total_inset * 1.0f / requested_total_inset;
*left = max_total_inset - *right;
}
void ConstrainedInset(gfx::Rect* rect,
gfx::Size min_size,
gfx::Insets initial_insets) {
int inset_left = initial_insets.left();
int inset_right = initial_insets.right();
int inset_top = initial_insets.top();
int inset_bottom = initial_insets.bottom();
ConstrainInsets(rect->width(), min_size.width(), &inset_left, &inset_right);
ConstrainInsets(rect->height(), min_size.height(), &inset_top, &inset_bottom);
rect->Inset(
gfx::Insets::TLBR(inset_top, inset_left, inset_bottom, inset_right));
}
void NativeThemeMac::PaintMacScrollBarTrackOrCorner(
cc::PaintCanvas* canvas,
Part part,
State state,
const ScrollbarExtraParams& extra_params,
const gfx::Rect& rect,
ColorScheme color_scheme,
bool is_corner) const {
if (is_corner && extra_params.is_overlay) {
return;
}
PaintScrollBarTrackGradient(canvas, rect, extra_params, is_corner,
color_scheme);
PaintScrollbarTrackInnerBorder(canvas, rect, extra_params, is_corner,
color_scheme);
PaintScrollbarTrackOuterBorder(canvas, rect, extra_params, is_corner,
color_scheme);
}
void NativeThemeMac::PaintScrollBarTrackGradient(
cc::PaintCanvas* canvas,
const gfx::Rect& rect,
const ScrollbarExtraParams& extra_params,
bool is_corner,
ColorScheme color_scheme) const {
gfx::Canvas paint_canvas(canvas, 1.0f);
// Select colors.
std::vector<SkColor4f> gradient_colors;
bool dark_mode = color_scheme == ColorScheme::kDark;
if (extra_params.is_overlay) {
if (dark_mode) {
gradient_colors = {SkColor4f{0.847f, 0.847f, 0.847f, 0.157f},
SkColor4f{0.8f, 0.8f, 0.8f, 0.149f},
SkColor4f{0.8f, 0.8f, 0.8f, 0.149f},
SkColor4f{0.8f, 0.8f, 0.8f, 0.149f}};
} else {
gradient_colors = {SkColor4f{0.973f, 0.973f, 0.973f, 0.776f},
SkColor4f{0.973f, 0.973f, 0.973f, 0.761f},
SkColor4f{0.973f, 0.973f, 0.973f, 0.761f},
SkColor4f{0.973f, 0.973f, 0.973f, 0.761f}};
}
} else {
// Non-overlay scroller track colors are not transparent. On Safari, they
// are, but on all other macOS applications they are not.
if (dark_mode) {
gradient_colors = {SkColor4f{0.176f, 0.176f, 0.176f, 1.0f},
SkColor4f{0.169f, 0.169f, 0.169f, 1.0f}};
} else {
gradient_colors = {SkColor4f{0.98f, 0.98f, 0.98f, 1.0f},
SkColor4f{0.98f, 0.98f, 0.98f, 1.0f}};
}
}
// Set the gradient direction.
std::vector<SkPoint> gradient_bounds;
if (is_corner) {
if (extra_params.orientation == ScrollbarOrientation::kVerticalOnRight) {
gradient_bounds = {gfx::PointToSkPoint(rect.origin()),
gfx::PointToSkPoint(rect.bottom_right())};
} else {
gradient_bounds = {gfx::PointToSkPoint(rect.top_right()),
gfx::PointToSkPoint(rect.bottom_left())};
}
} else {
if (extra_params.orientation == ScrollbarOrientation::kHorizontal) {
gradient_bounds = {gfx::PointToSkPoint(rect.origin()),
gfx::PointToSkPoint(rect.top_right())};
} else {
gradient_bounds = {gfx::PointToSkPoint(rect.origin()),
gfx::PointToSkPoint(rect.bottom_left())};
}
}
// And draw.
cc::PaintFlags flags;
std::optional<SkColor> track_color =
GetScrollbarColor(ScrollbarPart::kTrack, color_scheme, extra_params);
if (track_color.has_value()) {
flags.setAntiAlias(true);
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(track_color.value());
} else {
flags.setShader(cc::PaintShader::MakeLinearGradient(
gradient_bounds.data(), gradient_colors.data(), nullptr,
gradient_colors.size(), SkTileMode::kClamp));
}
paint_canvas.DrawRect(rect, flags);
}
void NativeThemeMac::PaintScrollbarTrackInnerBorder(
cc::PaintCanvas* canvas,
const gfx::Rect& rect,
const ScrollbarExtraParams& extra_params,
bool is_corner,
ColorScheme color_scheme) const {
gfx::Canvas paint_canvas(canvas, 1.0f);
// Compute the rect for the border.
gfx::Rect inner_border(rect);
if (extra_params.orientation == ScrollbarOrientation::kVerticalOnLeft) {
inner_border.set_x(rect.right() -
ScrollbarTrackBorderWidth(extra_params.scale_from_dip));
}
if (is_corner ||
extra_params.orientation == ScrollbarOrientation::kHorizontal) {
inner_border.set_height(
ScrollbarTrackBorderWidth(extra_params.scale_from_dip));
}
if (is_corner ||
extra_params.orientation != ScrollbarOrientation::kHorizontal) {
inner_border.set_width(
ScrollbarTrackBorderWidth(extra_params.scale_from_dip));
}
// And draw.
cc::PaintFlags flags;
SkColor inner_border_color =
GetScrollbarColor(ScrollbarPart::kTrackInnerBorder, color_scheme,
extra_params)
.value();
flags.setColor(inner_border_color);
paint_canvas.DrawRect(inner_border, flags);
}
void NativeThemeMac::PaintScrollbarTrackOuterBorder(
cc::PaintCanvas* canvas,
const gfx::Rect& rect,
const ScrollbarExtraParams& extra_params,
bool is_corner,
ColorScheme color_scheme) const {
gfx::Canvas paint_canvas(canvas, 1.0f);
cc::PaintFlags flags;
SkColor outer_border_color =
GetScrollbarColor(ScrollbarPart::kTrackOuterBorder, color_scheme,
extra_params)
.value();
flags.setColor(outer_border_color);
// Draw the horizontal outer border.
if (is_corner ||
extra_params.orientation == ScrollbarOrientation::kHorizontal) {
gfx::Rect outer_border(rect);
outer_border.set_height(
ScrollbarTrackBorderWidth(extra_params.scale_from_dip));
outer_border.set_y(rect.bottom() -
ScrollbarTrackBorderWidth(extra_params.scale_from_dip));
paint_canvas.DrawRect(outer_border, flags);
}
// Draw the vertical outer border.
if (is_corner ||
extra_params.orientation != ScrollbarOrientation::kHorizontal) {
gfx::Rect outer_border(rect);
outer_border.set_width(
ScrollbarTrackBorderWidth(extra_params.scale_from_dip));
if (extra_params.orientation == ScrollbarOrientation::kVerticalOnRight) {
outer_border.set_x(rect.right() - ScrollbarTrackBorderWidth(
extra_params.scale_from_dip));
}
paint_canvas.DrawRect(outer_border, flags);
}
}
gfx::Size NativeThemeMac::GetThumbMinSize(bool vertical, float scale) {
const int kLength = 18 * scale;
const int kGirth = 6 * scale;
return vertical ? gfx::Size(kGirth, kLength) : gfx::Size(kLength, kGirth);
}
void NativeThemeMac::PaintMacScrollbarThumb(
cc::PaintCanvas* canvas,
Part part,
State state,
const gfx::Rect& rect,
const ScrollbarExtraParams& scroll_thumb,
ColorScheme color_scheme) const {
gfx::Canvas paint_canvas(canvas, 1.0f);
// Compute the bounds for the rounded rect for the thumb from the bounds of
// the thumb.
gfx::Rect bounds(rect);
{
// Shrink the thumb evenly in length and girth to fit within the track.
gfx::Insets thumb_insets(GetScrollbarThumbInset(
scroll_thumb.is_overlay, scroll_thumb.scale_from_dip));
// Also shrink the thumb in girth to not touch the border.
if (scroll_thumb.orientation == ScrollbarOrientation::kHorizontal) {
thumb_insets.set_top(
thumb_insets.top() +
ScrollbarTrackBorderWidth(scroll_thumb.scale_from_dip));
ConstrainedInset(&bounds,
GetThumbMinSize(false, scroll_thumb.scale_from_dip),
thumb_insets);
} else {
thumb_insets.set_left(
thumb_insets.left() +
ScrollbarTrackBorderWidth(scroll_thumb.scale_from_dip));
ConstrainedInset(&bounds,
GetThumbMinSize(true, scroll_thumb.scale_from_dip),
thumb_insets);
}
}
// Draw.
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setStyle(cc::PaintFlags::kFill_Style);
SkColor thumb_color =
GetScrollbarColor(ScrollbarPart::kThumb, color_scheme, scroll_thumb)
.value();
flags.setColor(thumb_color);
const SkScalar radius = std::min(bounds.width(), bounds.height());
paint_canvas.DrawRoundRect(bounds, radius, flags);
}
std::optional<SkColor> NativeThemeMac::GetScrollbarColor(
ScrollbarPart part,
ColorScheme color_scheme,
const ScrollbarExtraParams& extra_params) const {
// This function is called from the renderer process through the scrollbar
// drawing functions. Due to this, it cannot use any of the dynamic NS system
// colors.
bool dark_mode = color_scheme == ColorScheme::kDark;
if (part == ScrollbarPart::kThumb) {
if (extra_params.thumb_color.has_value()) {
return extra_params.thumb_color.value();
}
if (extra_params.is_overlay) {
return dark_mode ? SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF)
: SkColorSetARGB(0x80, 0, 0, 0);
}
if (dark_mode) {
return extra_params.is_hovering ? SkColorSetRGB(0x93, 0x93, 0x93)
: SkColorSetRGB(0x6B, 0x6B, 0x6B);
}
return extra_params.is_hovering ? SkColorSetARGB(0x80, 0, 0, 0)
: SkColorSetARGB(0x3A, 0, 0, 0);
} else if (part == ScrollbarPart::kTrackInnerBorder) {
if (extra_params.track_color.has_value()) {
return extra_params.track_color.value();
}
if (extra_params.is_overlay) {
return dark_mode ? SkColorSetARGB(0x33, 0xE5, 0xE5, 0xE5)
: SkColorSetARGB(0xF9, 0xDF, 0xDF, 0xDF);
}
return dark_mode ? SkColorSetRGB(0x3D, 0x3D, 0x3D)
: SkColorSetRGB(0xE8, 0xE8, 0xE8);
} else if (part == ScrollbarPart::kTrackOuterBorder) {
if (extra_params.track_color.has_value()) {
return extra_params.track_color.value();
}
if (extra_params.is_overlay) {
return dark_mode ? SkColorSetARGB(0x28, 0xD8, 0xD8, 0xD8)
: SkColorSetARGB(0xC6, 0xE8, 0xE8, 0xE8);
}
return dark_mode ? SkColorSetRGB(0x51, 0x51, 0x51)
: SkColorSetRGB(0xED, 0xED, 0xED);
} else if (part == ScrollbarPart::kTrack) {
if (extra_params.track_color.has_value()) {
return extra_params.track_color.value();
}
}
return std::nullopt;
}
SkColor NativeThemeMac::GetSystemButtonPressedColor(SkColor base_color) const {
// TODO crbug.com/1003612: This should probably be replaced with a color
// transform.
// Mac has a different "pressed button" styling because it doesn't use
// ripples.
return color_utils::GetResultingPaintColor(SkColorSetA(SK_ColorBLACK, 0x10),
base_color);
}
void NativeThemeMac::PaintMenuPopupBackground(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
const gfx::Size& size,
const MenuBackgroundExtraParams& menu_background,
ColorScheme color_scheme) const {
DCHECK(color_provider);
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setColor(color_provider->GetColor(kColorMenuBackground));
const SkScalar radius = SkIntToScalar(menu_background.corner_radius);
SkRect rect = gfx::RectToSkRect(gfx::Rect(size));
canvas->drawRoundRect(rect, radius, radius, flags);
}
void NativeThemeMac::PaintMenuItemBackground(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
State state,
const gfx::Rect& rect,
const MenuItemExtraParams& menu_item,
ColorScheme color_scheme) const {
switch (state) {
case NativeTheme::kNormal:
case NativeTheme::kDisabled:
// Draw nothing over the regular background.
break;
case NativeTheme::kHovered:
PaintSelectedMenuItem(canvas, color_provider, rect, menu_item);
break;
default:
NOTREACHED();
}
}
// static
static void CaptionSettingsChangedNotificationCallback(CFNotificationCenterRef,
void*,
CFStringRef,
const void*,
CFDictionaryRef) {
NativeTheme::GetInstanceForWeb()->NotifyOnCaptionStyleUpdated();
}
NativeThemeMac::NativeThemeMac(bool configure_web_instance,
bool should_only_use_dark_colors)
: NativeThemeBase(should_only_use_dark_colors) {
if (!should_only_use_dark_colors) {
InitializeDarkModeStateAndObserver();
}
set_prefers_reduced_transparency(PrefersReducedTransparency());
set_inverted_colors(InvertedColors());
if (!IsForcedHighContrast()) {
SetPreferredContrast(CalculatePreferredContrast());
}
__block auto theme = this;
display_accessibility_notification_token_ =
[NSWorkspace.sharedWorkspace.notificationCenter
addObserverForName:
NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification
object:nil
queue:nil
usingBlock:^(NSNotification* notification) {
if (!IsForcedHighContrast()) {
theme->SetPreferredContrast(CalculatePreferredContrast());
}
theme->set_prefers_reduced_transparency(
PrefersReducedTransparency());
theme->set_inverted_colors(InvertedColors());
theme->NotifyOnNativeThemeUpdated();
}];
if (@available(macOS 15.0, *)) {
non_blinking_cursor_token_ = [[NSNotificationCenter defaultCenter]
addObserverForName:
AXPrefersNonBlinkingTextInsertionIndicatorDidChangeNotification
object:nil
queue:nil
usingBlock:^(NSNotification* notification) {
theme->NotifyOnNativeThemeUpdated();
}];
}
if (configure_web_instance) {
ConfigureWebInstance();
}
}
NativeThemeMac::~NativeThemeMac() {
[NSNotificationCenter.defaultCenter
removeObserver:display_accessibility_notification_token_];
if (@available(macOS 15.0, *)) {
[NSNotificationCenter.defaultCenter
removeObserver:non_blinking_cursor_token_];
}
}
bool NativeThemeMac::PrefersNonBlinkingCursor() const {
if (prefers_non_blinking_cursor_for_testing_) {
return true;
}
if (@available(macOS 15.0, *)) {
return AXPrefersNonBlinkingTextInsertionIndicator();
}
return false;
}
std::optional<base::TimeDelta> NativeThemeMac::GetPlatformCaretBlinkInterval()
const {
// MacOS 15 introduces a new setting that allows users to enable a
// non-blinking cursor. When this setting is enabled, we always show the
// cursor. In Blink/Views this is signaled by a blink period of 0.
if (PrefersNonBlinkingCursor()) {
return base::TimeDelta();
}
// If there's insertion point flash rate info in NSUserDefaults, use the
// blink period derived from that.
return ui::TextInsertionCaretBlinkPeriodFromDefaults();
}
void NativeThemeMac::PaintSelectedMenuItem(
cc::PaintCanvas* canvas,
const ColorProvider* color_provider,
const gfx::Rect& rect,
const MenuItemExtraParams& extra_params) const {
DCHECK(color_provider);
// Draw the background.
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setColor(color_provider->GetColor(kColorMenuItemBackgroundSelected));
const SkScalar radius = SkIntToScalar(extra_params.corner_radius);
canvas->drawRoundRect(gfx::RectToSkRect(rect), radius, radius, flags);
}
void NativeThemeMac::InitializeDarkModeStateAndObserver() {
__block auto theme = this;
set_use_dark_colors(IsDarkMode());
set_preferred_color_scheme(CalculatePreferredColorScheme());
appearance_observer_ =
[[NativeThemeEffectiveAppearanceObserver alloc] initWithHandler:^{
theme->set_use_dark_colors(IsDarkMode());
theme->set_preferred_color_scheme(CalculatePreferredColorScheme());
theme->NotifyOnNativeThemeUpdated();
}];
}
void NativeThemeMac::ConfigureWebInstance() {
// NativeThemeAura is used as web instance so we need to initialize its state.
NativeTheme* web_instance = NativeTheme::GetInstanceForWeb();
web_instance->set_use_dark_colors(IsDarkMode());
web_instance->set_preferred_color_scheme(CalculatePreferredColorScheme());
web_instance->SetPreferredContrast(CalculatePreferredContrast());
web_instance->set_prefers_reduced_transparency(PrefersReducedTransparency());
web_instance->set_inverted_colors(InvertedColors());
// Add the web native theme as an observer to stay in sync with color scheme
// changes.
color_scheme_observer_ =
std::make_unique<NativeTheme::ColorSchemeNativeThemeObserver>(
NativeTheme::GetInstanceForWeb());
AddObserver(color_scheme_observer_.get());
// Observe caption style changes.
CFNotificationCenterAddObserver(
CFNotificationCenterGetLocalCenter(), this,
CaptionSettingsChangedNotificationCallback,
kMACaptionAppearanceSettingsChangedNotification, nullptr,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
NativeThemeMacWeb::NativeThemeMacWeb()
: NativeThemeAura(
/*use_overlay_scrollbars=*/CalculateUseOverlayScrollbar(),
/*should_only_use_dark_colors=*/false) {}
// static
NativeThemeMacWeb* NativeThemeMacWeb::instance() {
static base::NoDestructor<NativeThemeMacWeb> s_native_theme;
return s_native_theme.get();
}
} // namespace ui
|