Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't rely on AppleInterfaceStyle for theme switching #8615

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions src/gui/osutils/macutils/AppKitImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ - (id) initWithObject:(AppKit*)appkit
name:NSWorkspaceSessionDidResignActiveNotification
object:nil];

[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceThemeChanged:)
name:@"AppleInterfaceThemeChangedNotification"
object:nil];
[NSApp addObserver:self forKeyPath:@"effectiveAppearance" options:NSKeyValueObservingOptionNew context:nil];

// Unfortunately, there is no notification for a wallpaper change, which affects
// the status bar colour on macOS Big Sur, but we can at least subscribe to this.
Expand All @@ -67,14 +64,37 @@ - (void) didDeactivateApplicationObserver:(NSNotification*) notification
}
}

//
// Light / dark theme toggled
//
- (void) interfaceThemeChanged:(NSNotification*) notification
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context
{
Q_UNUSED(notification);
if (m_appkit) {
emit m_appkit->interfaceThemeChanged();
Q_UNUSED(object);
Q_UNUSED(change);
Q_UNUSED(context);
opa334 marked this conversation as resolved.
Show resolved Hide resolved
if ([keyPath isEqualToString:@"effectiveAppearance"]) {
//
// Light / dark theme toggled
//
opa334 marked this conversation as resolved.
Show resolved Hide resolved
if (m_appkit) {

void (^emitBlock)(void) = ^
{
emit m_appkit->interfaceThemeChanged();
};

#if __clang_major__ >= 9 && MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
opa334 marked this conversation as resolved.
Show resolved Hide resolved
if(@available(macOS 11.0, *))
{
// Not sure why exactly this call is needed, but Apple sample code uses it so it's best to use it here too
[NSApp.effectiveAppearance performAsCurrentDrawingAppearance:emitBlock];
}
else
#endif
{
emitBlock();
}
opa334 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down Expand Up @@ -127,10 +147,7 @@ - (bool) isHidden:(pid_t) pid
//
- (bool) isDarkMode
{
NSDictionary* dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
id style = [dict objectForKey:@"AppleInterfaceStyle"];
return ( style && [style isKindOfClass:[NSString class]]
&& NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );
return [NSApp.effectiveAppearance.name isEqualToString:NSAppearanceNameDarkAqua];
}


Expand Down