Commit eaa894c8 authored by Leonard Grey's avatar Leonard Grey Committed by Commit Bot

Mac: Cache SystemDarkModeEnabled()

This gets called enough while painting that I think it's worth it.

Bug: 950063
Change-Id: I11c84087b9bb04fb0d4ba26b4eec4ada050e65e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1555040Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Commit-Queue: Leonard Grey <lgrey@chromium.org>
Cr-Commit-Position: refs/heads/master@{#648680}
parent 052c819e
......@@ -70,6 +70,9 @@ class NATIVE_THEME_EXPORT NativeThemeMac : public NativeThemeBase {
bool round_right,
bool focus);
// Updates cached dark mode status and notifies observers if it has changed.
void UpdateDarkModeStatus();
protected:
friend class NativeTheme;
friend class base::NoDestructor<NativeThemeMac>;
......@@ -88,6 +91,8 @@ class NATIVE_THEME_EXPORT NativeThemeMac : public NativeThemeBase {
appearance_observer_;
id high_contrast_notification_token_;
bool is_dark_mode_ = false;
DISALLOW_COPY_AND_ASSIGN(NativeThemeMac);
};
......
......@@ -19,6 +19,21 @@
#include "ui/gfx/skia_util.h"
#include "ui/native_theme/common_theme.h"
namespace {
bool IsDarkMode() {
if (@available(macOS 10.14, *)) {
NSAppearanceName appearance =
[[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAqua, NSAppearanceNameDarkAqua
]];
return [appearance isEqual:NSAppearanceNameDarkAqua];
}
return false;
}
} // namespace
@interface NSWorkspace (Redeclarations)
@property(readonly) BOOL accessibilityDisplayShouldIncreaseContrast;
......@@ -29,11 +44,14 @@
@interface NativeThemeEffectiveAppearanceObserver : NSObject
@end
@implementation NativeThemeEffectiveAppearanceObserver
@implementation NativeThemeEffectiveAppearanceObserver {
ui::NativeThemeMac* owner_;
}
- (instancetype)init {
- (instancetype)initWithOwner:(ui::NativeThemeMac*)owner {
self = [super init];
if (self) {
owner_ = owner;
if (@available(macOS 10.14, *)) {
[NSApp addObserver:self
forKeyPath:@"effectiveAppearance"
......@@ -55,7 +73,7 @@
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context {
ui::NativeTheme::GetInstanceForNativeUi()->NotifyObservers();
owner_->UpdateDarkModeStatus();
}
@end
......@@ -262,19 +280,18 @@ bool NativeThemeMac::UsesHighContrastColors() const {
bool NativeThemeMac::SystemDarkModeEnabled() const {
if (@available(macOS 10.14, *)) {
NSAppearanceName appearance =
[[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAqua, NSAppearanceNameDarkAqua
]];
return [appearance isEqual:NSAppearanceNameDarkAqua];
}
return is_dark_mode_;
} else {
// Support "--force-dark-mode" in macOS < 10.14.
return NativeThemeBase::SystemDarkModeEnabled();
}
}
NativeThemeMac::NativeThemeMac() {
if (base::FeatureList::IsEnabled(features::kDarkMode)) {
is_dark_mode_ = IsDarkMode();
appearance_observer_.reset(
[[NativeThemeEffectiveAppearanceObserver alloc] init]);
[[NativeThemeEffectiveAppearanceObserver alloc] initWithOwner:this]);
}
if (@available(macOS 10.10, *)) {
high_contrast_notification_token_ = [[[NSWorkspace sharedWorkspace]
......@@ -302,4 +319,11 @@ void NativeThemeMac::PaintSelectedMenuItem(cc::PaintCanvas* canvas,
canvas->drawRect(gfx::RectToSkRect(rect), flags);
}
void NativeThemeMac::UpdateDarkModeStatus() {
bool was_dark_mode = is_dark_mode_;
is_dark_mode_ = IsDarkMode();
if (was_dark_mode != is_dark_mode_)
NotifyObservers();
}
} // namespace ui
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment