Browse Source

Make platform.preferences usable as a standalone module

jnf_util_static
Jannis Weis 3 years ago
parent
commit
99833fcd04
No known key found for this signature in database
GPG Key ID: 7C9D8D4B558049AB
  1. 48
      core/src/main/java/com/github/weisj/darklaf/nativelaf/ThemePreferencesHandler.java
  2. 32
      macos/src/main/java/com/github/weisj/darklaf/platform/macos/MacOSPreferenceMonitor.java
  3. 14
      macos/src/main/java/com/github/weisj/darklaf/platform/macos/ui/MacOSTitlePane.java
  4. 52
      platform-preferences/src/main/java/com/github/weisj/darklaf/platform/preferences/NativeThemePreferenceProvider.java
  5. 28
      platform-preferences/src/main/java/com/github/weisj/darklaf/platform/preferences/SystemPreferenceChangeListener.java
  6. 97
      platform-preferences/src/main/java/com/github/weisj/darklaf/platform/preferences/SystemPreferencesManager.java
  7. 30
      windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsPreferenceMonitor.java

48
core/src/main/java/com/github/weisj/darklaf/nativelaf/ThemePreferencesHandler.java

@ -23,12 +23,11 @@ package com.github.weisj.darklaf.nativelaf;
import javax.swing.*;
import com.github.weisj.darklaf.DarkLaf;
import com.github.weisj.darklaf.platform.preferences.NativeThemePreferenceProvider;
import com.github.weisj.darklaf.platform.preferences.SystemPreferencesManager;
import com.github.weisj.darklaf.theme.event.ThemeEventSupport;
import com.github.weisj.darklaf.theme.event.ThemePreferenceChangeEvent;
import com.github.weisj.darklaf.theme.event.ThemePreferenceListener;
import com.github.weisj.darklaf.theme.spec.PreferredThemeStyle;
import com.github.weisj.darklaf.theme.spec.ThemePreferenceProvider;
import com.github.weisj.darklaf.util.PropertyUtil;
public class ThemePreferencesHandler {
@ -38,7 +37,7 @@ public class ThemePreferencesHandler {
private static ThemePreferencesHandler sharedInstance;
private final ThemeEventSupport<ThemePreferenceChangeEvent, ThemePreferenceListener> changeSupport =
new ThemeEventSupport<>();
private final ThemePreferenceProvider preferenceProvider;
private final SystemPreferencesManager systemPreferencesManager;
public static ThemePreferencesHandler getSharedInstance() {
if (sharedInstance == null) setSharedInstance(new ThemePreferencesHandler());
@ -50,20 +49,12 @@ public class ThemePreferencesHandler {
}
protected ThemePreferencesHandler() {
if (isNativePreferencesEnabled()) {
preferenceProvider = NativeThemePreferenceProvider.create();
} else {
preferenceProvider = NativeThemePreferenceProvider.createNullProvider();
}
preferenceProvider.initialize();
preferenceProvider.setCallback(this::onChange);
systemPreferencesManager = new SystemPreferencesManager(isNativePreferencesEnabled());
systemPreferencesManager.addListener(this::onChange);
}
private void onChange(final PreferredThemeStyle style) {
SwingUtilities.invokeLater(() -> {
changeSupport.dispatchEvent(new ThemePreferenceChangeEvent(style));
});
SwingUtilities.invokeLater(() -> changeSupport.dispatchEvent(new ThemePreferenceChangeEvent(style)));
}
public void addThemePreferenceChangeListener(final ThemePreferenceListener listener) {
@ -75,7 +66,7 @@ public class ThemePreferencesHandler {
}
public void enablePreferenceChangeReporting(final boolean enabled) {
preferenceProvider.setReporting(enabled);
systemPreferencesManager.enableReporting(enabled);
}
private boolean isNativePreferencesEnabled() {
@ -84,31 +75,30 @@ public class ThemePreferencesHandler {
}
public boolean isPreferenceChangeReportingEnabled() {
return preferenceProvider.canReport() && preferenceProvider.isReporting()
&& PropertyUtil.getSystemFlag(PREFERENCE_REPORTING_FLAG);
return systemPreferencesManager.isReportingEnabled() && PropertyUtil.getSystemFlag(PREFERENCE_REPORTING_FLAG);
}
public boolean supportsNativeAccentColor() {
return preferenceProvider.supportsNativeAccentColor();
public boolean canReport() {
return systemPreferencesManager.provider().canReport();
}
public boolean supportsNativeSelectionColor() {
return preferenceProvider.supportsNativeSelectionColor();
public boolean supportsNativeFontSize() {
return systemPreferencesManager.provider().supportsNativeFontSize();
}
public boolean supportsNativeFontSize() {
return preferenceProvider.supportsNativeFontSize();
public boolean supportsNativeAccentColor() {
return systemPreferencesManager.provider().supportsNativeAccentColor();
}
public boolean supportsNativeTheme() {
return preferenceProvider.supportsNativeTheme();
public boolean supportsNativeSelectionColor() {
return systemPreferencesManager.provider().supportsNativeSelectionColor();
}
public PreferredThemeStyle getPreferredThemeStyle() {
return preferenceProvider.getPreference();
public boolean supportsNativeTheme() {
return systemPreferencesManager.provider().supportsNativeTheme();
}
public boolean canReport() {
return preferenceProvider.canReport();
public PreferredThemeStyle getPreferredThemeStyle() {
return systemPreferencesManager.getPreferredThemeStyle();
}
}

32
macos/src/main/java/com/github/weisj/darklaf/platform/macos/MacOSPreferenceMonitor.java

@ -24,9 +24,13 @@ import java.awt.*;
import java.util.Objects;
import java.util.logging.Logger;
import javax.swing.*;
import com.github.weisj.darklaf.util.LogUtil;
public class MacOSPreferenceMonitor {
private static final Logger LOGGER = Logger.getLogger(MacOSThemePreferenceProvider.class.getName());
private static final Logger LOGGER = LogUtil.getLogger(MacOSThemePreferenceProvider.class);
private final MacOSThemePreferenceProvider preferenceProvider;
@ -42,18 +46,20 @@ public class MacOSPreferenceMonitor {
}
private void onNotification() {
boolean newDark = JNIThemeInfoMacOS.isDarkThemeEnabled();
boolean newHighContrast = JNIThemeInfoMacOS.isHighContrastEnabled();
Color newAccentColor = JNIThemeInfoMacOS.getAccentColor();
Color newSelectionColor = JNIThemeInfoMacOS.getSelectionColor();
if (darkMode != newDark || highContrast != newHighContrast || !Objects.equals(accentColor, newAccentColor)
|| !Objects.equals(selectionColor, newSelectionColor)) {
darkMode = newDark;
accentColor = newAccentColor;
selectionColor = newSelectionColor;
highContrast = newHighContrast;
preferenceProvider.reportPreferenceChange(darkMode, highContrast, accentColor, selectionColor);
}
SwingUtilities.invokeLater(() -> {
boolean newDark = JNIThemeInfoMacOS.isDarkThemeEnabled();
boolean newHighContrast = JNIThemeInfoMacOS.isHighContrastEnabled();
Color newAccentColor = JNIThemeInfoMacOS.getAccentColor();
Color newSelectionColor = JNIThemeInfoMacOS.getSelectionColor();
if (darkMode != newDark || highContrast != newHighContrast || !Objects.equals(accentColor, newAccentColor)
|| !Objects.equals(selectionColor, newSelectionColor)) {
darkMode = newDark;
accentColor = newAccentColor;
selectionColor = newSelectionColor;
highContrast = newHighContrast;
preferenceProvider.reportPreferenceChange(darkMode, highContrast, accentColor, selectionColor);
}
});
}
private void start() {

14
macos/src/main/java/com/github/weisj/darklaf/platform/macos/ui/MacOSTitlePane.java

@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020-2022 Jannis Weis
* Copyright (c) 2020-2021 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,
@ -32,10 +32,13 @@ import javax.swing.*;
import com.github.weisj.darklaf.platform.decorations.CustomTitlePane;
import com.github.weisj.darklaf.platform.macos.JNIDecorationsMacOS;
import com.github.weisj.darklaf.util.LogUtil;
import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil;
public class MacOSTitlePane extends CustomTitlePane {
private static final Logger LOGGER = Logger.getLogger(MacOSTitlePane.class.getName());
private static final Logger LOGGER = LogUtil.getLogger(MacOSTitlePane.class);
private final JRootPane rootPane;
private final Window window;
@ -134,7 +137,7 @@ public class MacOSTitlePane extends CustomTitlePane {
}
private boolean isUseColoredTitleBar(final JRootPane rootPane) {
return MacOSDecorationsUtil.getBooleanProperty(rootPane, "JRootPane.coloredTitleBar", true);
return PropertyUtil.getBooleanProperty(rootPane, "JRootPane.coloredTitleBar", true);
}
private String getTitle() {
@ -241,8 +244,9 @@ public class MacOSTitlePane extends CustomTitlePane {
|| getDecorationStyle() == JRootPane.NONE;
}
private void updateTitleBarVisibility() {
titleBarHidden = MacOSDecorationsUtil.getBooleanProperty(rootPane, "JRootPane.hideTitleBar", false);
titleBarHidden = PropertyUtil.getBooleanProperty(rootPane, "JRootPane.hideTitleBar");
rootPane.doLayout();
rootPane.repaint();
}
@ -284,7 +288,7 @@ public class MacOSTitlePane extends CustomTitlePane {
@Override
public void propertyChange(final PropertyChangeEvent pce) {
String name = pce.getPropertyName();
if ("title".equals(name)) {
if (PropertyKey.TITLE.equals(name)) {
titleLabel.setText(pce.getNewValue() == null ? "" : pce.getNewValue().toString());
repaint();
}

52
platform-preferences/src/main/java/com/github/weisj/darklaf/platform/preferences/NativeThemePreferenceProvider.java

@ -1,52 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2022 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.preferences;
import com.github.weisj.darklaf.platform.SystemInfo;
import com.github.weisj.darklaf.platform.macos.MacOSThemePreferenceProvider;
import com.github.weisj.darklaf.platform.preferences.impl.DefaultThemePreferenceProvider;
import com.github.weisj.darklaf.platform.windows.WindowsThemePreferenceProvider;
import com.github.weisj.darklaf.theme.spec.ThemePreferenceProvider;
public final class NativeThemePreferenceProvider {
public static ThemePreferenceProvider create() {
ThemePreferenceProvider nativeProvider;
try {
// Extend for different platforms.
if (SystemInfo.isWindows10OrGreater) {
nativeProvider = new WindowsThemePreferenceProvider();
} else if (SystemInfo.isMac) {
nativeProvider = new MacOSThemePreferenceProvider();
} else {
nativeProvider = createNullProvider();
}
} catch (final Throwable e) {
// If native modules are not available disable them.
nativeProvider = createNullProvider();
}
return nativeProvider;
}
public static ThemePreferenceProvider createNullProvider() {
return new DefaultThemePreferenceProvider();
}
}

28
platform-preferences/src/main/java/com/github/weisj/darklaf/platform/preferences/SystemPreferenceChangeListener.java

@ -0,0 +1,28 @@
/*
* MIT License
*
* Copyright (c) 2022 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.preferences;
import com.github.weisj.darklaf.theme.spec.PreferredThemeStyle;
public interface SystemPreferenceChangeListener {
void onSystemPreferencesChanged(final PreferredThemeStyle style);
}

97
platform-preferences/src/main/java/com/github/weisj/darklaf/platform/preferences/SystemPreferencesManager.java

@ -0,0 +1,97 @@
/*
* MIT License
*
* Copyright (c) 2022 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.preferences;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.github.weisj.darklaf.platform.SystemInfo;
import com.github.weisj.darklaf.platform.macos.MacOSThemePreferenceProvider;
import com.github.weisj.darklaf.platform.preferences.impl.DefaultThemePreferenceProvider;
import com.github.weisj.darklaf.platform.windows.WindowsThemePreferenceProvider;
import com.github.weisj.darklaf.theme.spec.PreferredThemeStyle;
import com.github.weisj.darklaf.theme.spec.ThemePreferenceProvider;
public class SystemPreferencesManager {
private final List<SystemPreferenceChangeListener> listenerList = Collections.synchronizedList(new ArrayList<>());
private final ThemePreferenceProvider preferenceProvider;
public SystemPreferencesManager(final boolean allowNativeReporting) {
preferenceProvider = createProvider(allowNativeReporting);
preferenceProvider.setCallback(this::onPreferenceChange);
}
public SystemPreferencesManager() {
this(true);
}
public ThemePreferenceProvider provider() {
return preferenceProvider;
}
private void onPreferenceChange(final PreferredThemeStyle preferredThemeStyle) {
synchronized (listenerList) {
new ArrayList<>(listenerList).forEach(listener -> {
if (listener != null) listener.onSystemPreferencesChanged(preferredThemeStyle);
});
}
}
public void addListener(final SystemPreferenceChangeListener listener) {
listenerList.add(listener);
}
public void removeListener(final SystemPreferenceChangeListener listener) {
listenerList.add(listener);
}
private static ThemePreferenceProvider createProvider(final boolean allowNativeReporting) {
ThemePreferenceProvider nativeProvider;
try {
// Extend for different platforms.
if (SystemInfo.isWindows10OrGreater && allowNativeReporting) {
nativeProvider = new WindowsThemePreferenceProvider();
} else if (SystemInfo.isMac && allowNativeReporting) {
nativeProvider = new MacOSThemePreferenceProvider();
} else {
nativeProvider = new DefaultThemePreferenceProvider();
}
} catch (final Throwable e) {
// If native modules are not available disable them.
nativeProvider = new DefaultThemePreferenceProvider();
}
return nativeProvider;
}
public void enableReporting(final boolean reportingEnabled) {
preferenceProvider.setReporting(reportingEnabled);
}
public boolean isReportingEnabled() {
return preferenceProvider.canReport() && preferenceProvider.isReporting();
}
public PreferredThemeStyle getPreferredThemeStyle() {
return preferenceProvider.getPreference();
}
}

30
windows/src/main/java/com/github/weisj/darklaf/platform/windows/WindowsPreferenceMonitor.java

@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020-2021 Jannis Weis
* Copyright (c) 2020-2022 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,
@ -22,6 +22,8 @@ package com.github.weisj.darklaf.platform.windows;
import java.util.logging.Logger;
import javax.swing.*;
import com.github.weisj.darklaf.util.LogUtil;
public class WindowsPreferenceMonitor {
@ -42,18 +44,20 @@ public class WindowsPreferenceMonitor {
}
private void onNotification() {
boolean newDark = JNIThemeInfoWindows.isDarkThemeEnabled();
boolean newHighContrast = JNIThemeInfoWindows.isHighContrastEnabled();
long newFotScale = JNIThemeInfoWindows.getFontScaleFactor();
int newColor = JNIThemeInfoWindows.getAccentColor();
if (darkMode != newDark || color != newColor || fontScaleFactor != newFotScale
|| highContrast != newHighContrast) {
darkMode = newDark;
fontScaleFactor = newFotScale;
highContrast = newHighContrast;
color = newColor;
preferenceProvider.reportPreferenceChange(highContrast, darkMode, fontScaleFactor, color);
}
SwingUtilities.invokeLater(() -> {
boolean newDark = JNIThemeInfoWindows.isDarkThemeEnabled();
boolean newHighContrast = JNIThemeInfoWindows.isHighContrastEnabled();
long newFotScale = JNIThemeInfoWindows.getFontScaleFactor();
int newColor = JNIThemeInfoWindows.getAccentColor();
if (darkMode != newDark || color != newColor || fontScaleFactor != newFotScale
|| highContrast != newHighContrast) {
darkMode = newDark;
fontScaleFactor = newFotScale;
highContrast = newHighContrast;
color = newColor;
preferenceProvider.reportPreferenceChange(highContrast, darkMode, fontScaleFactor, color);
}
});
}
private void start() {

Loading…
Cancel
Save