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 { ...@@ -70,6 +70,9 @@ class NATIVE_THEME_EXPORT NativeThemeMac : public NativeThemeBase {
bool round_right, bool round_right,
bool focus); bool focus);
// Updates cached dark mode status and notifies observers if it has changed.
void UpdateDarkModeStatus();
protected: protected:
friend class NativeTheme; friend class NativeTheme;
friend class base::NoDestructor<NativeThemeMac>; friend class base::NoDestructor<NativeThemeMac>;
...@@ -88,6 +91,8 @@ class NATIVE_THEME_EXPORT NativeThemeMac : public NativeThemeBase { ...@@ -88,6 +91,8 @@ class NATIVE_THEME_EXPORT NativeThemeMac : public NativeThemeBase {
appearance_observer_; appearance_observer_;
id high_contrast_notification_token_; id high_contrast_notification_token_;
bool is_dark_mode_ = false;
DISALLOW_COPY_AND_ASSIGN(NativeThemeMac); DISALLOW_COPY_AND_ASSIGN(NativeThemeMac);
}; };
......
...@@ -19,6 +19,21 @@ ...@@ -19,6 +19,21 @@
#include "ui/gfx/skia_util.h" #include "ui/gfx/skia_util.h"
#include "ui/native_theme/common_theme.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) @interface NSWorkspace (Redeclarations)
@property(readonly) BOOL accessibilityDisplayShouldIncreaseContrast; @property(readonly) BOOL accessibilityDisplayShouldIncreaseContrast;
...@@ -29,11 +44,14 @@ ...@@ -29,11 +44,14 @@
@interface NativeThemeEffectiveAppearanceObserver : NSObject @interface NativeThemeEffectiveAppearanceObserver : NSObject
@end @end
@implementation NativeThemeEffectiveAppearanceObserver @implementation NativeThemeEffectiveAppearanceObserver {
ui::NativeThemeMac* owner_;
}
- (instancetype)init { - (instancetype)initWithOwner:(ui::NativeThemeMac*)owner {
self = [super init]; self = [super init];
if (self) { if (self) {
owner_ = owner;
if (@available(macOS 10.14, *)) { if (@available(macOS 10.14, *)) {
[NSApp addObserver:self [NSApp addObserver:self
forKeyPath:@"effectiveAppearance" forKeyPath:@"effectiveAppearance"
...@@ -55,7 +73,7 @@ ...@@ -55,7 +73,7 @@
ofObject:(id)object ofObject:(id)object
change:(NSDictionary*)change change:(NSDictionary*)change
context:(void*)context { context:(void*)context {
ui::NativeTheme::GetInstanceForNativeUi()->NotifyObservers(); owner_->UpdateDarkModeStatus();
} }
@end @end
...@@ -262,19 +280,18 @@ bool NativeThemeMac::UsesHighContrastColors() const { ...@@ -262,19 +280,18 @@ bool NativeThemeMac::UsesHighContrastColors() const {
bool NativeThemeMac::SystemDarkModeEnabled() const { bool NativeThemeMac::SystemDarkModeEnabled() const {
if (@available(macOS 10.14, *)) { if (@available(macOS 10.14, *)) {
NSAppearanceName appearance = return is_dark_mode_;
[[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[ } else {
NSAppearanceNameAqua, NSAppearanceNameDarkAqua // Support "--force-dark-mode" in macOS < 10.14.
]]; return NativeThemeBase::SystemDarkModeEnabled();
return [appearance isEqual:NSAppearanceNameDarkAqua];
} }
return NativeThemeBase::SystemDarkModeEnabled();
} }
NativeThemeMac::NativeThemeMac() { NativeThemeMac::NativeThemeMac() {
if (base::FeatureList::IsEnabled(features::kDarkMode)) { if (base::FeatureList::IsEnabled(features::kDarkMode)) {
is_dark_mode_ = IsDarkMode();
appearance_observer_.reset( appearance_observer_.reset(
[[NativeThemeEffectiveAppearanceObserver alloc] init]); [[NativeThemeEffectiveAppearanceObserver alloc] initWithOwner:this]);
} }
if (@available(macOS 10.10, *)) { if (@available(macOS 10.10, *)) {
high_contrast_notification_token_ = [[[NSWorkspace sharedWorkspace] high_contrast_notification_token_ = [[[NSWorkspace sharedWorkspace]
...@@ -302,4 +319,11 @@ void NativeThemeMac::PaintSelectedMenuItem(cc::PaintCanvas* canvas, ...@@ -302,4 +319,11 @@ void NativeThemeMac::PaintSelectedMenuItem(cc::PaintCanvas* canvas,
canvas->drawRect(gfx::RectToSkRect(rect), flags); 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 } // 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