-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSAppearance+PBDarkMode.m
52 lines (43 loc) · 1.17 KB
/
NSAppearance+PBDarkMode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// NSAppearance+PBDarkMode.m
// GitX
//
// Created by Etienne on 18/11/2018.
//
#import "NSAppearance+PBDarkMode.h"
NSString *const PBEffectiveAppearanceChanged = @"PBEffectiveAppearanceChanged";
@implementation NSAppearance (PBDarkMode)
- (BOOL)isDarkMode
{
if (@available(macOS 10.14, *)) {
if ([self bestMatchFromAppearancesWithNames:@[ NSAppearanceNameDarkAqua, NSAppearanceNameAqua ]] == NSAppearanceNameDarkAqua)
return YES;
return NO;
} else {
return NO;
}
}
@end
@implementation NSApplication (PBDarkMode)
- (BOOL)isDarkMode
{
if (@available(macOS 10.14, *)) {
return self.effectiveAppearance.isDarkMode;
} else {
return NO;
}
}
- (void)registerObserverForAppearanceChanges:(id)observer
{
if (@available(macOS 10.14, *)) {
/* This leaks the observation, but since it's tied to the life of NSApp
* it doesn't matter ;-) */
[[NSApplication sharedApplication] addObserver:observer
keyPath:@"effectiveAppearance"
options:0
block:^(MAKVONotification *notification) {
[[NSNotificationCenter defaultCenter] postNotificationName:PBEffectiveAppearanceChanged object:observer];
}];
}
}
@end