Skip to content

Commit cc4e621

Browse files
author
Mat Gadd
committed
Pass current appearance into JS on load and change
1 parent 17bd432 commit cc4e621

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Classes/Controllers/PBWebController.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include <SystemConfiguration/SCNetworkReachability.h>
1616

17+
static void * const PBEffectiveAppearanceContext = @"PBEffectiveAppearanceContext";
18+
1719
@interface PBWebController () <WebUIDelegate, WebFrameLoadDelegate, WebResourceLoadDelegate>
1820
- (void)preferencesChangedWithNotification:(NSNotification *)theNotification;
1921
@end
@@ -44,6 +46,10 @@ - (void) awakeFromNib
4446
selector:@selector(windowDidEndLiveResitzeWithNotification:)
4547
name:NSWindowDidEndLiveResizeNotification
4648
object:self.view.window];
49+
50+
if (@available(macOS 10.14, *)) {
51+
[[NSApplication sharedApplication] addObserver:self forKeyPath:@"effectiveAppearance" options:(NSKeyValueObservingOptions)0 context:PBEffectiveAppearanceContext];
52+
}
4753

4854
finishedLoading = NO;
4955

@@ -56,18 +62,48 @@ - (void) awakeFromNib
5662
[self.view.mainFrame loadRequest:request];
5763
}
5864

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+
5976
- (WebScriptObject *) script
6077
{
6178
return self.view.windowScriptObject;
6279
}
6380

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+
6498
- (void)closeView
6599
{
66100
if (self.view) {
67101
[[self script] setValue:nil forKey:@"Controller"];
68102
[self.view close];
69103
}
70104

105+
[[NSApplication sharedApplication] removeObserver:self forKeyPath:@"effectiveAppearance" context:PBEffectiveAppearanceContext];
106+
71107
[[NSNotificationCenter defaultCenter] removeObserver:self];
72108
}
73109

@@ -88,6 +124,7 @@ - (void) webView:(id) v didFinishLoadForFrame:(id) frame
88124
finishedLoading = YES;
89125
if ([self respondsToSelector:@selector(didLoad)])
90126
[self performSelector:@selector(didLoad)];
127+
[self setWebAppearance:nil];
91128
}
92129

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

Resources/html/lib/GitX.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,13 @@ var bindCommitSelectionLinks = function(el) {
6666
}, false);
6767
}
6868
};
69+
70+
var setAppearance = function (appearance) {
71+
if (appearance === "DARK") {
72+
document.body.classList.remove('body--light');
73+
document.body.classList.add('body--dark');
74+
} else {
75+
document.body.classList.remove('body--dark');
76+
document.body.classList.add('body--light');
77+
}
78+
};

0 commit comments

Comments
 (0)