Skip to content

Commit ba2b2ca

Browse files
committed
Consolidate the Dark mode low-level helpers
1 parent e183a5f commit ba2b2ca

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

Classes/Controllers/ApplicationController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification
121121
setenv( "SSH_ASKPASS", [[[NSBundle mainBundle] pathForResource: @"gitx_askpasswd" ofType: @""] UTF8String], 1 );
122122
setenv( "DISPLAY", "localhost:0", 1 );
123123

124+
[NSApp registerObserverForAppearanceChanges:self];
124125
[self registerServices];
125126
started = YES;
126127
}

Classes/Controllers/PBWebController.m

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ - (void) awakeFromNib
4747
name:NSWindowDidEndLiveResizeNotification
4848
object:self.view.window];
4949

50-
if (@available(macOS 10.14, *)) {
51-
[[NSApplication sharedApplication] addObserver:self forKeyPath:@"effectiveAppearance" options:(NSKeyValueObservingOptions)0 context:PBEffectiveAppearanceContext];
52-
}
53-
50+
[nc addObserver:self
51+
selector:@selector(effectiveAppearanceDidChange:)
52+
name:PBEffectiveAppearanceChanged
53+
object:nil];
54+
5455
finishedLoading = NO;
5556

5657
[self.view setDrawsBackground:NO];
@@ -78,21 +79,10 @@ - (WebScriptObject *) script
7879
return self.view.windowScriptObject;
7980
}
8081

81-
- (void) setWebAppearance:(NSAppearance *)appearance
82+
- (void)effectiveAppearanceDidChange:(NSNotification *)notif
8283
{
83-
if (@available(macOS 10.14, *)) {
84-
if (appearance == nil) {
85-
appearance = [NSApplication sharedApplication].effectiveAppearance;
86-
}
87-
88-
NSAppearanceName appearanceName = [appearance bestMatchFromAppearancesWithNames:@[NSAppearanceNameDarkAqua, NSAppearanceNameAqua]];
89-
90-
if ([appearanceName isEqualToString:NSAppearanceNameDarkAqua]) {
91-
[self.script callWebScriptMethod:@"setAppearance" withArguments:@[@"DARK"]];
92-
} else {
93-
[self.script callWebScriptMethod:@"setAppearance" withArguments:@[@"LIGHT"]];
94-
}
95-
}
84+
NSString *mode = [NSApp isDarkMode] ? @"DARK" : @"LIGHT";
85+
[self.script callWebScriptMethod:@"setAppearance" withArguments:@[mode]];
9686
}
9787

9888
- (void)closeView
@@ -124,7 +114,7 @@ - (void) webView:(id) v didFinishLoadForFrame:(id) frame
124114
finishedLoading = YES;
125115
if ([self respondsToSelector:@selector(didLoad)])
126116
[self performSelector:@selector(didLoad)];
127-
[self setWebAppearance:nil];
117+
[self effectiveAppearanceDidChange:nil];
128118
}
129119

130120
- (void)webView:(WebView *)webView addMessageToConsole:(NSDictionary *)dictionary

Classes/NSAppearance+PBDarkMode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
NS_ASSUME_NONNULL_BEGIN
1111

12+
extern NSString *const PBEffectiveAppearanceChanged;
13+
1214
@interface NSAppearance (PBDarkMode)
1315
- (BOOL)isDarkMode;
1416
@end
1517

1618
@interface NSApplication (PBDarkMode)
1719
- (BOOL)isDarkMode;
20+
- (void)registerObserverForAppearanceChanges:(id)observer;
1821
@end
1922

2023
NS_ASSUME_NONNULL_END

Classes/NSAppearance+PBDarkMode.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#import "NSAppearance+PBDarkMode.h"
99

10+
NSString *const PBEffectiveAppearanceChanged = @"PBEffectiveAppearanceChanged";
11+
1012
@implementation NSAppearance (PBDarkMode)
1113

1214
- (BOOL)isDarkMode
@@ -33,4 +35,15 @@ - (BOOL)isDarkMode
3335
}
3436
}
3537

38+
- (void)registerObserverForAppearanceChanges:(id)observer
39+
{
40+
if (@available(macOS 10.14, *)) {
41+
/* This leaks the observation, but since it's tied to the life of NSApp
42+
* it doesn't matter ;-) */
43+
[[NSApplication sharedApplication] addObserver:observer keyPath:@"effectiveAppearance" options:0 block:^(MAKVONotification *notification) {
44+
[[NSNotificationCenter defaultCenter] postNotificationName:PBEffectiveAppearanceChanged object:observer];
45+
}];
46+
}
47+
}
48+
3649
@end

0 commit comments

Comments
 (0)