Skip to content

Commit

Permalink
Theme preference support on macOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
weisJ committed Apr 2, 2020
1 parent 401a47b commit fed32f2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public class JNIThemeInfoMacOS {
public static native boolean isHighContrastEnabled();

public static native long getFontScaleFactor();

public static native long createPreferenceChangeListener();

public static native void deletePreferenceChangeListener(final long listenerPtr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ private static void loadLibrary() {
NativeUtil.loadLibraryFromJar(
"/com/github/weisj/darklaf/platform/darklaf-macos/macos-x86-64/libdarklaf-macos.dylib");
loaded = true;
LOGGER.info("Loaded libdarklaf-macos.dylib. Decorations are enabled.");
LOGGER.info("Loaded libdarklaf-macos.dylib. Native features are enabled.");
} else {
LOGGER.warning("JRE model '"
+ SystemInfo.jreArchitecture
+ "' not supported. Decorations will be disabled");
+ "' not supported. Native features will be disabled");
}
} catch (Throwable e) {
//Library not found, SecurityManager prevents library loading etc.
LOGGER.log(Level.SEVERE, "Could not load decorations library libdarklaf-macos.dylib." +
" Decorations will be disabled", e);
" Native features will be disabled", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.github.weisj.darklaf.theme.info.PreferredThemeStyle;
import com.github.weisj.darklaf.theme.info.ThemePreferenceProvider;

import java.util.function.Consumer;

public class MacOSThemePreferenceProvider implements ThemePreferenceProvider {

private final PreferredThemeStyle fallbackStyle = new PreferredThemeStyle(ContrastRule.STANDARD,
Expand All @@ -44,4 +46,19 @@ public void initialize() {
MacOSLibrary.updateLibrary();
}

@Override
public void setCallback(final Consumer<PreferredThemeStyle> callback) {

}

@Override
public void setReporting(final boolean reporting) {

}

@Override
public boolean isReporting() {
return false;
}

}
87 changes: 84 additions & 3 deletions macos/src/main/objectiveCpp/ThemeInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,105 @@
* SOFTWARE.
*/
#import "com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS.h"
#import <JavaNativeFoundation/JavaNativeFoundation.h>
#import <AppKit/AppKit.h>

#define OBJC(jl) ((id)jlong_to_ptr(jl))

#define KEY_APPLE_INTERFACE_STYLE @"AppleInterfaceStyle"
#define KEY_SWITCHES_AUTOMATICALLY @"AppleInterfaceStyleSwitchesAutomatically"
#define VALUE_DARK @"Dark"

@interface PreferenceChangeListener:NSObject {
}
@end

@implementation PreferenceChangeListener
- (id)init {
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[self listenToKey:KEY_APPLE_INTERFACE_STYLE onCenter:center];
[self listenToKey:KEY_SWITCHES_AUTOMATICALLY onCenter:center];
NSLog(@"Listener created");
return self;
}

- (void)dealloc {
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center removeObserver:self]; // Removes all registered notifications.
NSLog(@"Listener destroyed");
[super dealloc];
}

-(void)listenToKey:(NSString *)key onCenter:(NSDistributedNotificationCenter *)center {
[center addObserver:self
selector:@selector(notificationEvent:)
name:key
object:nil];
}

-(void)notificationEvent:(NSNotification *)notification {
NSLog(@"Notification: %@", notification);
}
@end

JNIEXPORT jboolean JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_isDarkThemeEnabled(JNIEnv *env, jclass obj) {
JNF_COCOA_ENTER(env);
if(@available(macOS 10.14, *)) {
NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
return (jboolean)[@"Dark" caseInsensitiveCompare:osxMode] == NSOrderedSame;
NSString *interfaceStyle = [[NSUserDefaults standardUserDefaults] stringForKey:KEY_APPLE_INTERFACE_STYLE];
// interfaceStyle can be nil (light mode) or "Dark" (dark mode).
BOOL isDark = [VALUE_DARK caseInsensitiveCompare:interfaceStyle] == NSOrderedSame;
if (@available(macOS 10.15, *)) {
// Catalina
BOOL switchesAutomatically = [[NSUserDefaults standardUserDefaults] boolForKey:KEY_SWITCHES_AUTOMATICALLY];
if (switchesAutomatically) {
// If switchesAutomatically == true the roles of "Dark" and nil are changed.
return (jboolean)!isDark;
}
}
// Mojave or switchesAutomatically == false.
return (jboolean)isDark;
} else {
return (jboolean)false;
}
JNF_COCOA_EXIT(env);
return false;
}

JNIEXPORT jboolean JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_isHighContrastEnabled(JNIEnv *env, jclass obj) {
return NSWorkspace.sharedWorkspace.accessibilityDisplayShouldIncreaseContrast;
JNF_COCOA_ENTER(env);
return (jboolean) NSWorkspace.sharedWorkspace.accessibilityDisplayShouldIncreaseContrast;
JNF_COCOA_EXIT(env);
return (jboolean) false;
}

JNIEXPORT jlong JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_getFontScaleFactor(JNIEnv *env, jclass obj) {
JNF_COCOA_ENTER(env);
// Currently not implemented.
return 100;
JNF_COCOA_EXIT(env);
return 100;
}

JNIEXPORT jlong JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_createPreferenceChangeListener(JNIEnv *env, jclass obj) {
JNF_COCOA_DURING(env); // We dont want an auto release pool.
PreferenceChangeListener *listener = [[PreferenceChangeListener alloc] init];
[listener retain];
return (jlong) listener;
JNF_COCOA_HANDLE(env);
return (jlong) 0;
}

JNIEXPORT void JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_deletePreferenceChangeListener(JNIEnv *env, jclass obj, jlong listenerPtr) {
JNF_COCOA_ENTER(env);
PreferenceChangeListener *listener = OBJC(listenerPtr);
if (listener) {
[listener release];
[listener dealloc];
}
JNF_COCOA_EXIT(env);
}

0 comments on commit fed32f2

Please sign in to comment.