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 Mar 31, 2020
1 parent 6f3f1ab commit 89b3c86
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 49 deletions.
6 changes: 5 additions & 1 deletion macos/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fun DependencyHandlerScope.javaImplementation(dep: Any) {
}

dependencies {
javaImplementation(project(":darklaf-theme"))
javaImplementation(project(":darklaf-native-utils"))
javaImplementation(project(":darklaf-utils"))
javaImplementation(project(":darklaf-platform-base"))
Expand All @@ -34,7 +35,10 @@ library {
compilerArgs.addAll("-x", "objective-c++")
compilerArgs.addAll("-mmacosx-version-min=10.10")
compilerArgs.addJavaFrameworks()
source.from(file("src/main/objectiveCpp/JNIDecorations.mm"))
source.from(
file("src/main/objectiveCpp/Decorations.mm"),
file("src/main/objectiveCpp/ThemeInfo.mm")
)
}
}
binaries.whenElementFinalized(CppSharedLibrary::class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,10 @@
*/
package com.github.weisj.darklaf.platform.macos;

import com.github.weisj.darklaf.platform.NativeUtil;
import com.github.weisj.darklaf.util.SystemInfo;

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JNIDecorationsMacOS {

private static final Logger LOGGER = Logger.getLogger(JNIDecorationsMacOS.class.getName());
private static boolean loaded;
private static boolean attemptedLoad;

public static native long getComponentPointer(final Window window);

public static native void retainWindow(final long hwnd);
Expand All @@ -55,41 +46,4 @@ public class JNIDecorationsMacOS {
public static native boolean isFullscreen(final long hwnd);

public static native double getTitleFontSize(final long hwnd);


/**
* Load the decorations-library if necessary.
*/
public static void updateLibrary() {
if (!loaded && !attemptedLoad) {
loadLibrary();
}
}

private static void loadLibrary() {
attemptedLoad = true;
if (!SystemInfo.isMac || loaded) {
return;
}
try {
if (SystemInfo.isX64) {
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.");
} else {
LOGGER.warning("JRE model '"
+ SystemInfo.jreArchitecture
+ "' not supported. Decorations 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);
}
}

public static boolean isCustomDecorationSupported() {
return loaded;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.weisj.darklaf.platform.macos;

public class JNIThemeInfoMacOS {

public static native boolean isDarkThemeEnabled();

public static native boolean isHighContrastEnabled();

public static native long getFontScaleFactor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public CustomTitlePane createTitlePane(final JRootPane rootPane, final int decor

@Override
public boolean isCustomDecorationSupported() {
return JNIDecorationsMacOS.isCustomDecorationSupported();
return MacOSLibrary.isLoaded();
}

@Override
public void initialize() {
JNIDecorationsMacOS.updateLibrary();
MacOSLibrary.updateLibrary();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.weisj.darklaf.platform.macos;

import com.github.weisj.darklaf.platform.NativeUtil;
import com.github.weisj.darklaf.util.SystemInfo;

import java.util.logging.Level;
import java.util.logging.Logger;

public class MacOSLibrary {

private static final Logger LOGGER = Logger.getLogger(MacOSLibrary.class.getName());
private static boolean loaded;
private static boolean attemptedLoad;

/**
* Load the decorations-library if necessary.
*/
public static void updateLibrary() {
if (!loaded && !attemptedLoad) {
loadLibrary();
}
}

private static void loadLibrary() {
attemptedLoad = true;
if (!SystemInfo.isMac || loaded) {
return;
}
try {
if (SystemInfo.isX64) {
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.");
} else {
LOGGER.warning("JRE model '"
+ SystemInfo.jreArchitecture
+ "' not supported. Decorations 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);
}
}

public static boolean isLoaded() {
return loaded;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.weisj.darklaf.platform.macos;

import com.github.weisj.darklaf.theme.info.ColorToneRule;
import com.github.weisj.darklaf.theme.info.ContrastRule;
import com.github.weisj.darklaf.theme.info.PreferredThemeStyle;
import com.github.weisj.darklaf.theme.info.ThemePreferenceProvider;

public class MacOSThemePreferenceProvider implements ThemePreferenceProvider {

private final PreferredThemeStyle fallbackStyle = new PreferredThemeStyle(ContrastRule.STANDARD,
ColorToneRule.LIGHT);

@Override
public PreferredThemeStyle getPreference() {
if (!MacOSLibrary.isLoaded()) return fallbackStyle;
return new PreferredThemeStyle();
}

@Override
public void initialize() {
MacOSLibrary.updateLibrary();
}

}
File renamed without changes.
40 changes: 40 additions & 0 deletions macos/src/main/objectiveCpp/ThemeInfo.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#import "com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS.h"
#import <AppKit/AppKit.h>

JNIEXPORT jboolean JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_isDarkThemeEnabled(JNIEnv *env, jclass obj) {
return false;
}

JNIEXPORT jboolean JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_isHighContrastEnabled(JNIEnv *env, jclass obj) {
return [NSWorkspace accessibilityDisplayShouldIncreaseContrast];;
}

JNIEXPORT jlong JNICALL
Java_com_github_weisj_darklaf_platform_macos_JNIThemeInfoMacOS_getFontScaleFactor(JNIEnv *env, jclass obj) {
return 100;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class PreferredThemeStyle {
private final ColorToneRule colorToneRule;
private final FontSizeRule fontSizeRule;

public PreferredThemeStyle() {
this(ContrastRule.STANDARD, ColorToneRule.LIGHT);
}

public PreferredThemeStyle(final ContrastRule contrastRule,
final ColorToneRule colorToneRule) {
if (contrastRule == null) throw new IllegalArgumentException("null is not a valid contrast rule");
Expand Down

0 comments on commit 89b3c86

Please sign in to comment.