Skip to content

Commit f610974

Browse files
implemented UiAppearanceProvider for macOS
1 parent 3467b36 commit f610974

18 files changed

+469
-5
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3030

3131
<!-- runtime dependencies -->
32-
<api.version>0.1.4</api.version>
32+
<api.version>0.1.5</api.version>
3333
<slf4j.version>1.7.30</slf4j.version>
3434
<guava.version>30.0-jre</guava.version>
3535
<gson.version>2.8.6</gson.version>

src/main/headers/org_cryptomator_macos_uiappearance_AppAppearance_Native.h

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/headers/org_cryptomator_macos_uiappearance_MacSystemAppearance_Native.h

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.cryptomator.macos.uiappearance;
2+
3+
import org.cryptomator.macos.common.NativeLibLoader;
4+
5+
class AppAppearance {
6+
7+
void setToAqua() {
8+
Native.INSTANCE.setToAqua();
9+
}
10+
11+
void setToDarkAqua() {
12+
Native.INSTANCE.setToDarkAqua();
13+
}
14+
15+
private static class Native {
16+
static final Native INSTANCE = new Native();
17+
18+
private Native() {
19+
NativeLibLoader.loadLib();
20+
}
21+
22+
public native void setToAqua();
23+
24+
public native void setToDarkAqua();
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.cryptomator.macos.uiappearance;
2+
3+
import org.cryptomator.macos.common.NativeLibLoader;
4+
5+
class MacSystemAppearance {
6+
7+
public String getCurrentInterfaceStyle() {
8+
return Native.INSTANCE.getCurrentInterfaceStyle();
9+
}
10+
11+
/**
12+
* @param listener
13+
* @return 0 in case of errors or a pointer to the observer
14+
*/
15+
long registerObserverWithListener(MacSystemAppearanceListener listener) {
16+
return Native.INSTANCE.registerObserverWithListener(listener);
17+
}
18+
19+
void deregisterObserver(long observer) {
20+
Native.INSTANCE.deregisterObserver(observer);
21+
}
22+
23+
private static class Native {
24+
static final Native INSTANCE = new Native();
25+
26+
private Native() {
27+
NativeLibLoader.loadLib();
28+
}
29+
30+
public native String getCurrentInterfaceStyle();
31+
32+
public native long registerObserverWithListener(MacSystemAppearanceListener listener);
33+
34+
public native void deregisterObserver(long observer);
35+
}
36+
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.cryptomator.macos.uiappearance;
2+
3+
@FunctionalInterface
4+
interface MacSystemAppearanceListener {
5+
6+
void systemAppearanceChanged();
7+
8+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.cryptomator.macos.uiappearance;
2+
3+
import org.cryptomator.integrations.uiappearance.Theme;
4+
import org.cryptomator.integrations.uiappearance.UiAppearanceException;
5+
import org.cryptomator.integrations.uiappearance.UiAppearanceListener;
6+
import org.cryptomator.integrations.uiappearance.UiAppearanceProvider;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class MacUiAppearanceProvider implements UiAppearanceProvider {
12+
13+
private final MacSystemAppearance systemAppearance;
14+
private final AppAppearance appAppearance;
15+
private final Map<UiAppearanceListener, Long> registeredObservers;
16+
17+
public MacUiAppearanceProvider() {
18+
this.systemAppearance = new MacSystemAppearance();
19+
this.appAppearance = new AppAppearance();
20+
this.registeredObservers = new HashMap<>();
21+
}
22+
23+
@Override
24+
public Theme getSystemTheme() {
25+
return systemAppearance.getCurrentInterfaceStyle().equals("Dark") ? Theme.DARK : Theme.LIGHT;
26+
}
27+
28+
@Override
29+
public void adjustToTheme(Theme theme) {
30+
switch (theme) {
31+
case LIGHT:
32+
appAppearance.setToAqua();
33+
break;
34+
case DARK:
35+
appAppearance.setToDarkAqua();
36+
break;
37+
}
38+
}
39+
40+
@Override
41+
public void addListener(UiAppearanceListener listener) throws UiAppearanceException {
42+
var observer = systemAppearance.registerObserverWithListener(() -> {
43+
listener.systemAppearanceChanged(getSystemTheme());
44+
});
45+
if (observer == 0) {
46+
throw new UiAppearanceException("Failed to register system appearance observer.");
47+
} else {
48+
registeredObservers.put(listener, observer);
49+
}
50+
}
51+
52+
@Override
53+
public void removeListener(UiAppearanceListener listener) throws UiAppearanceException {
54+
var observer = registeredObservers.remove(listener);
55+
if (observer != null) {
56+
systemAppearance.deregisterObserver(observer);
57+
}
58+
}
59+
60+
}

src/main/native/Integrations.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@
99
/* Begin PBXBuildFile section */
1010
74CF2E7B254C295A006266D6 /* org_cryptomator_macos_autostart_MacLaunchServices_Native.m in Sources */ = {isa = PBXBuildFile; fileRef = 74CF2E7A254C295A006266D6 /* org_cryptomator_macos_autostart_MacLaunchServices_Native.m */; };
1111
9E0819C91D75CABC000C89E6 /* org_cryptomator_macos_keychain_MacKeychain_Native.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E0819C71D75CABC000C89E6 /* org_cryptomator_macos_keychain_MacKeychain_Native.m */; };
12+
9E21340025542735006EA872 /* org_cryptomator_macos_uiappearance_AppAppearance_Native.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E2133FF25542735006EA872 /* org_cryptomator_macos_uiappearance_AppAppearance_Native.m */; };
13+
9E2134032554275D006EA872 /* org_cryptomator_macos_uiappearance_MacSystemAppearance_Native.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E2134022554275D006EA872 /* org_cryptomator_macos_uiappearance_MacSystemAppearance_Native.m */; };
14+
9E21340B255429CA006EA872 /* SKYAppearanceObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = 9E213409255429CA006EA872 /* SKYAppearanceObserver.h */; };
15+
9E21340C255429CA006EA872 /* SKYAppearanceObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E21340A255429CA006EA872 /* SKYAppearanceObserver.m */; };
16+
9E21341125542ECE006EA872 /* SKYAppearanceNotifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 9E21340F25542ECE006EA872 /* SKYAppearanceNotifier.h */; };
17+
9E21341225542ECE006EA872 /* SKYAppearanceNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E21341025542ECE006EA872 /* SKYAppearanceNotifier.m */; };
1218
/* End PBXBuildFile section */
1319

1420
/* Begin PBXFileReference section */
1521
74CF2E7A254C295A006266D6 /* org_cryptomator_macos_autostart_MacLaunchServices_Native.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = org_cryptomator_macos_autostart_MacLaunchServices_Native.m; sourceTree = "<group>"; };
1622
9E0819B71D748ECC000C89E6 /* libIntegrations.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libIntegrations.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
1723
9E0819C71D75CABC000C89E6 /* org_cryptomator_macos_keychain_MacKeychain_Native.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = org_cryptomator_macos_keychain_MacKeychain_Native.m; sourceTree = "<group>"; };
24+
9E2133FF25542735006EA872 /* org_cryptomator_macos_uiappearance_AppAppearance_Native.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = org_cryptomator_macos_uiappearance_AppAppearance_Native.m; sourceTree = "<group>"; };
25+
9E2134022554275D006EA872 /* org_cryptomator_macos_uiappearance_MacSystemAppearance_Native.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = org_cryptomator_macos_uiappearance_MacSystemAppearance_Native.m; sourceTree = "<group>"; };
26+
9E213409255429CA006EA872 /* SKYAppearanceObserver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKYAppearanceObserver.h; sourceTree = "<group>"; };
27+
9E21340A255429CA006EA872 /* SKYAppearanceObserver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SKYAppearanceObserver.m; sourceTree = "<group>"; };
28+
9E21340F25542ECE006EA872 /* SKYAppearanceNotifier.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKYAppearanceNotifier.h; sourceTree = "<group>"; };
29+
9E21341025542ECE006EA872 /* SKYAppearanceNotifier.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SKYAppearanceNotifier.m; sourceTree = "<group>"; };
1830
/* End PBXFileReference section */
1931

2032
/* Begin PBXFrameworksBuildPhase section */
@@ -33,6 +45,12 @@
3345
children = (
3446
9E0819C71D75CABC000C89E6 /* org_cryptomator_macos_keychain_MacKeychain_Native.m */,
3547
74CF2E7A254C295A006266D6 /* org_cryptomator_macos_autostart_MacLaunchServices_Native.m */,
48+
9E2133FF25542735006EA872 /* org_cryptomator_macos_uiappearance_AppAppearance_Native.m */,
49+
9E2134022554275D006EA872 /* org_cryptomator_macos_uiappearance_MacSystemAppearance_Native.m */,
50+
9E21340F25542ECE006EA872 /* SKYAppearanceNotifier.h */,
51+
9E21341025542ECE006EA872 /* SKYAppearanceNotifier.m */,
52+
9E213409255429CA006EA872 /* SKYAppearanceObserver.h */,
53+
9E21340A255429CA006EA872 /* SKYAppearanceObserver.m */,
3654
9E0819B81D748ECC000C89E6 /* Products */,
3755
);
3856
sourceTree = "<group>";
@@ -52,6 +70,8 @@
5270
isa = PBXHeadersBuildPhase;
5371
buildActionMask = 2147483647;
5472
files = (
73+
9E21341125542ECE006EA872 /* SKYAppearanceNotifier.h in Headers */,
74+
9E21340B255429CA006EA872 /* SKYAppearanceObserver.h in Headers */,
5575
);
5676
runOnlyForDeploymentPostprocessing = 0;
5777
};
@@ -113,7 +133,11 @@
113133
buildActionMask = 2147483647;
114134
files = (
115135
74CF2E7B254C295A006266D6 /* org_cryptomator_macos_autostart_MacLaunchServices_Native.m in Sources */,
136+
9E21340C255429CA006EA872 /* SKYAppearanceObserver.m in Sources */,
137+
9E21340025542735006EA872 /* org_cryptomator_macos_uiappearance_AppAppearance_Native.m in Sources */,
138+
9E21341225542ECE006EA872 /* SKYAppearanceNotifier.m in Sources */,
116139
9E0819C91D75CABC000C89E6 /* org_cryptomator_macos_keychain_MacKeychain_Native.m in Sources */,
140+
9E2134032554275D006EA872 /* org_cryptomator_macos_uiappearance_MacSystemAppearance_Native.m in Sources */,
117141
);
118142
runOnlyForDeploymentPostprocessing = 0;
119143
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// SKYAppearanceNotifier.h
3+
// Integrations
4+
//
5+
// Created by Sebastian Stenzel on 05.11.20.
6+
// Copyright © 2020 Cryptomator. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@class SKYAppearanceObserver;
12+
13+
@interface SKYAppearanceNotifier : NSObject
14+
15+
+ (instancetype)sharedInstance;
16+
- (void)addObserver:(SKYAppearanceObserver *)observer;
17+
- (void)removeObserver:(SKYAppearanceObserver *)observer;
18+
19+
@end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// SKYAppearanceNotifier.m
3+
// Integrations
4+
//
5+
// Created by Sebastian Stenzel on 05.11.20.
6+
// Copyright © 2020 Cryptomator. All rights reserved.
7+
//
8+
9+
#import "SKYAppearanceNotifier.h"
10+
#import "SKYAppearanceObserver.h"
11+
12+
@interface SKYAppearanceNotifier ()
13+
@property (nonatomic, strong) NSMutableArray<SKYAppearanceObserver *> *observers;
14+
@end
15+
16+
@implementation SKYAppearanceNotifier
17+
18+
+ (instancetype)sharedInstance {
19+
static SKYAppearanceNotifier *sharedInstance;
20+
static dispatch_once_t onceToken;
21+
dispatch_once(&onceToken, ^{
22+
sharedInstance = [[SKYAppearanceNotifier alloc] init];
23+
});
24+
return sharedInstance;
25+
}
26+
27+
- (instancetype)init {
28+
if (self = [super init]) {
29+
self.observers = [NSMutableArray array];
30+
}
31+
return self;
32+
}
33+
34+
- (void)addObserver:(SKYAppearanceObserver *)observer {
35+
[self.observers addObject:observer];
36+
[[NSDistributedNotificationCenter defaultCenter] addObserver:observer selector:@selector(interfaceThemeChangedNotification:) name:@"AppleInterfaceThemeChangedNotification" object:nil];
37+
}
38+
39+
- (void)removeObserver:(SKYAppearanceObserver *)observer {
40+
[[NSDistributedNotificationCenter defaultCenter] removeObserver:observer];
41+
[self.observers removeObject:observer];
42+
}
43+
44+
@end

0 commit comments

Comments
 (0)