mirror of https://github.com/weisJ/darklaf.git
weisj
5 years ago
45 changed files with 594 additions and 152 deletions
@ -0,0 +1,39 @@
|
||||
/* |
||||
* 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.task; |
||||
|
||||
import com.github.weisj.darklaf.theme.Theme; |
||||
|
||||
import java.util.Properties; |
||||
|
||||
public interface DefaultsAdjustmentTask { |
||||
|
||||
/** |
||||
* Execute the task. |
||||
* |
||||
* @param currentTheme the current theme. |
||||
* @param properties the properties. |
||||
*/ |
||||
void run(final Theme currentTheme, final Properties properties); |
||||
} |
@ -1,45 +0,0 @@
|
||||
/* |
||||
* 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.theme; |
||||
|
||||
public enum FontSizeRule { |
||||
DEFAULT("default"), |
||||
TINY("tiny"), |
||||
SMALLER("smaller"), |
||||
SMALL("small"), |
||||
MEDIUM("medium"), |
||||
LARGE("large"), |
||||
LARGER("larger"), |
||||
HUGE("huge"); |
||||
|
||||
private final String propertyKey; |
||||
|
||||
FontSizeRule(final String propertyKey) { |
||||
this.propertyKey = propertyKey; |
||||
} |
||||
|
||||
public String getPropertyKey() { |
||||
return "fontSize." + propertyKey; |
||||
} |
||||
} |
@ -0,0 +1,74 @@
|
||||
/* |
||||
* 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.theme.info; |
||||
|
||||
import javax.swing.*; |
||||
import java.util.logging.Logger; |
||||
|
||||
public enum FontSizePreset { |
||||
TINY("tiny"), |
||||
SMALLER("smaller"), |
||||
SMALL("small"), |
||||
MEDIUM("medium"), |
||||
LARGE("large"), |
||||
LARGER("larger"), |
||||
HUGE("huge"); |
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(FontSizePreset.class.getName()); |
||||
private final String propertyName; |
||||
private final FontSizeRule.AdjustmentType type; |
||||
|
||||
FontSizePreset(final String propertyName) { |
||||
this(propertyName, FontSizeRule.AdjustmentType.ABSOLUTE_ADJUSTMENT); |
||||
} |
||||
|
||||
FontSizePreset(final String propertyName, final FontSizeRule.AdjustmentType type) { |
||||
this.propertyName = propertyName; |
||||
this.type = type; |
||||
} |
||||
|
||||
public float adjustFontSize(final float size, final UIDefaults defaults) { |
||||
int adjustment = getSize(defaults); |
||||
return type.adjustSize(size, adjustment, adjustment / 100f); |
||||
} |
||||
|
||||
private String getPropertyName() { |
||||
return "fontSize." + propertyName; |
||||
} |
||||
|
||||
public FontSizeRule.AdjustmentType getType() { |
||||
return type; |
||||
} |
||||
|
||||
private int getSize(final UIDefaults defaults) { |
||||
String key = getPropertyName(); |
||||
Object obj = defaults.get(getPropertyName()); |
||||
if (!(obj instanceof Integer)) { |
||||
LOGGER.warning("Font size property '" + key + "' not specified. Font will not be changed"); |
||||
return Integer.MAX_VALUE; |
||||
} else { |
||||
return (int) obj; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,130 @@
|
||||
/* |
||||
* 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.theme.info; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
public class FontSizeRule { |
||||
private static final FontSizeRule DEFAULT = new FontSizeRule(AdjustmentType.NO_ADJUSTMENT, 0, 1f); |
||||
|
||||
private final AdjustmentType type; |
||||
private final FontSizePreset preset; |
||||
private final float relativeAdjustment; |
||||
private final int absoluteAdjustment; |
||||
|
||||
protected FontSizeRule(final FontSizePreset preset) { |
||||
this.preset = preset; |
||||
type = preset.getType(); |
||||
relativeAdjustment = 1f; |
||||
absoluteAdjustment = 0; |
||||
} |
||||
|
||||
protected FontSizeRule(final AdjustmentType type, |
||||
final int absoluteAdjustment, final float relativeAdjustment) { |
||||
this.type = type; |
||||
this.absoluteAdjustment = absoluteAdjustment; |
||||
this.relativeAdjustment = relativeAdjustment; |
||||
preset = null; |
||||
} |
||||
|
||||
public static FontSizeRule getDefault() { |
||||
return DEFAULT; |
||||
} |
||||
|
||||
public static FontSizeRule fromPreset(final FontSizePreset preset) { |
||||
return new FontSizeRule(preset); |
||||
} |
||||
|
||||
public static FontSizeRule absoluteAdjustment(final int adjustment) { |
||||
return new FontSizeRule(AdjustmentType.ABSOLUTE_ADJUSTMENT, adjustment, 1f); |
||||
} |
||||
|
||||
public static FontSizeRule relativeAdjustment(final float percent) { |
||||
return new FontSizeRule(AdjustmentType.RELATIVE_ADJUSTMENT, 0, percent); |
||||
} |
||||
|
||||
public float adjustFontSize(final float size, final UIDefaults defaults) { |
||||
if (preset != null) return preset.adjustFontSize(size, defaults); |
||||
return type.adjustSize(size, absoluteAdjustment, relativeAdjustment); |
||||
} |
||||
|
||||
public AdjustmentType getType() { |
||||
if (preset == null) { |
||||
// Prevent unnecessary mapping of font.
|
||||
if (type == AdjustmentType.ABSOLUTE_ADJUSTMENT && absoluteAdjustment == 0) { |
||||
return AdjustmentType.NO_ADJUSTMENT; |
||||
} |
||||
if (type == AdjustmentType.RELATIVE_ADJUSTMENT && relativeAdjustment == 1f) { |
||||
return AdjustmentType.NO_ADJUSTMENT; |
||||
} |
||||
} |
||||
return type; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder builder = new StringBuilder("FontSizeRule{type=").append(type); |
||||
if (preset != null) { |
||||
builder.append(", preset=").append(preset); |
||||
} else { |
||||
switch (type) { |
||||
case ABSOLUTE_ADJUSTMENT: |
||||
builder.append(", absoluteAdjustment=").append(absoluteAdjustment); |
||||
break; |
||||
case RELATIVE_ADJUSTMENT: |
||||
builder.append(", relativeAdjustment=").append(relativeAdjustment); |
||||
break; |
||||
case NO_ADJUSTMENT: |
||||
default: |
||||
break; |
||||
} |
||||
} |
||||
builder.append("}"); |
||||
return builder.toString(); |
||||
} |
||||
|
||||
public enum AdjustmentType { |
||||
NO_ADJUSTMENT { |
||||
@Override |
||||
public float adjustSize(final float size, final int absolute, final float relative) { |
||||
return size; |
||||
} |
||||
}, |
||||
ABSOLUTE_ADJUSTMENT { |
||||
@Override |
||||
public float adjustSize(final float size, final int absolute, final float relative) { |
||||
return size + absolute; |
||||
} |
||||
}, |
||||
RELATIVE_ADJUSTMENT { |
||||
@Override |
||||
public float adjustSize(final float size, final int absolute, final float relative) { |
||||
return size * relative; |
||||
} |
||||
}; |
||||
|
||||
abstract public float adjustSize(final float size, final int absolute, final float relative); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,39 @@
|
||||
/* |
||||
* 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.theme.info; |
||||
|
||||
public interface ThemePreferenceProvider { |
||||
|
||||
/** |
||||
* Get the preferred theme style. |
||||
* |
||||
* @return the preferred theme style. |
||||
*/ |
||||
PreferredThemeStyle getPreference(); |
||||
|
||||
/** |
||||
* Initialize all necessary resources. |
||||
*/ |
||||
void initialize(); |
||||
} |
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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. |
||||
*/ |
||||
#include "com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows.h" |
||||
|
||||
#include <iostream> |
||||
#include <string> |
||||
#include <windows.h> |
||||
#include <winreg.h> |
||||
|
||||
#define DARK_MODE_PATH "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize" |
||||
#define DARK_MODE_KEY "AppsUseLightTheme" |
||||
#define FONT_SCALE_PATH "Software\\Microsoft\\Accessibility" |
||||
#define FONT_SCALE_KEY "TextScaleFactor" |
||||
|
||||
DWORD RegGetDword(HKEY hKey, const LPCSTR subKey, const LPCSTR value) |
||||
{ |
||||
DWORD data{}; |
||||
DWORD dataSize = sizeof(data); |
||||
DWORD flags = RRF_RT_REG_DWORD; |
||||
#ifdef _WIN64 |
||||
flags |= RRF_SUBKEY_WOW6464KEY; |
||||
#else |
||||
flags |= RRF_SUBKEY_WOW6432KEY; |
||||
#endif |
||||
LONG retCode = ::RegGetValue(hKey, subKey, value, flags, nullptr, &data, &dataSize); |
||||
if (retCode != ERROR_SUCCESS) |
||||
{ |
||||
throw retCode; |
||||
} |
||||
return data; |
||||
} |
||||
|
||||
bool IsDarkMode() |
||||
{ |
||||
try |
||||
{ |
||||
DWORD data = RegGetDword(HKEY_CURRENT_USER, DARK_MODE_PATH, DARK_MODE_KEY); |
||||
return data == 0; |
||||
} |
||||
catch (LONG e) |
||||
{ |
||||
// Error. Simply return false.
|
||||
return false; |
||||
} |
||||
} |
||||
|
||||
bool IsHighContrastMode() |
||||
{ |
||||
HIGHCONTRAST info = { 0 }; |
||||
info.cbSize = sizeof(HIGHCONTRAST); |
||||
BOOL ok = SystemParametersInfoW(SPI_GETHIGHCONTRAST, 0, &info, 0); |
||||
if (ok) |
||||
{ |
||||
return info.dwFlags & HCF_HIGHCONTRASTON; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
unsigned int GetTextScaleFactor() |
||||
{ |
||||
try |
||||
{ |
||||
return RegGetDword(HKEY_CURRENT_USER, FONT_SCALE_PATH, FONT_SCALE_KEY); |
||||
} |
||||
catch (LONG e) |
||||
{ |
||||
return 100; |
||||
} |
||||
} |
||||
|
||||
JNIEXPORT jboolean JNICALL |
||||
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_isDarkThemeEnabled(JNIEnv *env, jclass obj) |
||||
{ |
||||
return (jboolean)IsDarkMode(); |
||||
} |
||||
|
||||
JNIEXPORT jboolean JNICALL |
||||
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_isHighContrastEnabled(JNIEnv *env, jclass obj) |
||||
{ |
||||
return (jboolean)IsHighContrastMode(); |
||||
} |
||||
|
||||
JNIEXPORT jlong JNICALL |
||||
Java_com_github_weisj_darklaf_platform_windows_JNIThemeInfoWindows_getFontScaleFactor(JNIEnv *env, jclass obj) |
||||
{ |
||||
return (jlong)GetTextScaleFactor(); |
||||
} |
||||
|
@ -0,0 +1,50 @@
|
||||
/* |
||||
* 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.windows; |
||||
|
||||
import com.github.weisj.darklaf.theme.info.*; |
||||
|
||||
public class WindowsThemePreferenceProvider implements ThemePreferenceProvider { |
||||
|
||||
private final PreferredThemeStyle fallbackStyle = new PreferredThemeStyle(ContrastRule.STANDARD, |
||||
ColorToneRule.LIGHT); |
||||
|
||||
@Override |
||||
public PreferredThemeStyle getPreference() { |
||||
if (!WindowsLibrary.isLoaded()) return fallbackStyle; |
||||
boolean darkMode = JNIThemeInfoWindows.isDarkThemeEnabled(); |
||||
boolean highContrast = JNIThemeInfoWindows.isHighContrastEnabled(); |
||||
long fontScaling = JNIThemeInfoWindows.getFontScaleFactor(); |
||||
ContrastRule contrastRule = highContrast ? ContrastRule.HIGH_CONTRAST : ContrastRule.STANDARD; |
||||
ColorToneRule toneRule = darkMode ? ColorToneRule.DARK : ColorToneRule.LIGHT; |
||||
FontSizeRule fontSizeRule = FontSizeRule.relativeAdjustment(fontScaling / 100f); |
||||
return new PreferredThemeStyle(contrastRule, toneRule, fontSizeRule); |
||||
} |
||||
|
||||
@Override |
||||
public void initialize() { |
||||
WindowsLibrary.updateLibrary(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue