14
14
15
15
#include < SystemConfiguration/SCNetworkReachability.h>
16
16
17
+ static void * const PBEffectiveAppearanceContext = @" PBEffectiveAppearanceContext" ;
18
+
17
19
@interface PBWebController () <WebUIDelegate , WebFrameLoadDelegate , WebResourceLoadDelegate >
18
20
- (void )preferencesChangedWithNotification : (NSNotification *)theNotification ;
19
21
@end
@@ -44,6 +46,10 @@ - (void) awakeFromNib
44
46
selector: @selector (windowDidEndLiveResitzeWithNotification: )
45
47
name: NSWindowDidEndLiveResizeNotification
46
48
object: self .view.window];
49
+
50
+ if (@available (macOS 10.14 , *)) {
51
+ [[NSApplication sharedApplication ] addObserver: self forKeyPath: @" effectiveAppearance" options: (NSKeyValueObservingOptions )0 context: PBEffectiveAppearanceContext];
52
+ }
47
53
48
54
finishedLoading = NO ;
49
55
@@ -56,18 +62,48 @@ - (void) awakeFromNib
56
62
[self .view.mainFrame loadRequest: request];
57
63
}
58
64
65
+ - (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context
66
+ {
67
+ if (context != PBEffectiveAppearanceContext) {
68
+ return [super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
69
+ }
70
+
71
+ if (@available (macOS 10.14 , *)) {
72
+ [self setWebAppearance: ((NSApplication *)object).effectiveAppearance];
73
+ }
74
+ }
75
+
59
76
- (WebScriptObject *) script
60
77
{
61
78
return self.view .windowScriptObject ;
62
79
}
63
80
81
+ - (void ) setWebAppearance : (NSAppearance *)appearance
82
+ {
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
+ }
96
+ }
97
+
64
98
- (void )closeView
65
99
{
66
100
if (self.view ) {
67
101
[[self script ] setValue: nil forKey: @" Controller" ];
68
102
[self .view close ];
69
103
}
70
104
105
+ [[NSApplication sharedApplication ] removeObserver: self forKeyPath: @" effectiveAppearance" context: PBEffectiveAppearanceContext];
106
+
71
107
[[NSNotificationCenter defaultCenter ] removeObserver: self ];
72
108
}
73
109
@@ -88,6 +124,7 @@ - (void) webView:(id) v didFinishLoadForFrame:(id) frame
88
124
finishedLoading = YES ;
89
125
if ([self respondsToSelector: @selector (didLoad )])
90
126
[self performSelector: @selector (didLoad )];
127
+ [self setWebAppearance: nil ];
91
128
}
92
129
93
130
- (void )webView : (WebView *)webView addMessageToConsole : (NSDictionary *)dictionary
0 commit comments