Browse Source

Decouple settings panel from ThemeSettings

Reduce amount of times ColoredRadioButton patches the underlying icon.
Ensure color returned by ForegroundColorGenerationTask is an UIResource.
Improve ForegroundColorGenerationTask to account for saturation of color.
pull/189/head
weisj 4 years ago
parent
commit
6ff0ffc799
  1. 123
      core/src/main/java/com/github/weisj/darklaf/components/ColoredRadioButton.java
  2. 2
      core/src/main/java/com/github/weisj/darklaf/graphics/StringPainter.java
  3. 39
      core/src/main/java/com/github/weisj/darklaf/settings/DefaultSettingsConfiguration.java
  4. 255
      core/src/main/java/com/github/weisj/darklaf/settings/SettingsConfiguration.java
  5. 133
      core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java
  6. 496
      core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettingsPanel.java
  7. 62
      core/src/main/java/com/github/weisj/darklaf/task/ForegroundColorGenerationTask.java
  8. 2
      core/src/main/java/javax/swing/text/DefaultHighlighterDark/DarkHighlightPainter.java
  9. 3
      core/src/test/java/ui/SettingsDemo.java
  10. 14
      utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java
  11. 14
      utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java

123
core/src/main/java/com/github/weisj/darklaf/components/ColoredRadioButton.java

@ -28,7 +28,7 @@ import java.awt.*;
import java.util.Properties; import java.util.Properties;
import javax.swing.*; import javax.swing.*;
import javax.swing.plaf.ButtonUI; import javax.swing.plaf.ComponentUI;
import com.github.weisj.darklaf.DarkLaf; import com.github.weisj.darklaf.DarkLaf;
import com.github.weisj.darklaf.LafManager; import com.github.weisj.darklaf.LafManager;
@ -36,6 +36,7 @@ import com.github.weisj.darklaf.PropertyLoader;
import com.github.weisj.darklaf.icons.IconLoader; import com.github.weisj.darklaf.icons.IconLoader;
import com.github.weisj.darklaf.icons.StateIcon; import com.github.weisj.darklaf.icons.StateIcon;
import com.github.weisj.darklaf.task.AccentColorAdjustmentTask; import com.github.weisj.darklaf.task.AccentColorAdjustmentTask;
import com.github.weisj.darklaf.task.ForegroundColorGenerationTask;
import com.github.weisj.darklaf.theme.Theme; import com.github.weisj.darklaf.theme.Theme;
import com.github.weisj.darklaf.ui.togglebutton.radiobutton.DarkRadioButtonUI; import com.github.weisj.darklaf.ui.togglebutton.radiobutton.DarkRadioButtonUI;
import com.github.weisj.darklaf.util.DarkUIUtil; import com.github.weisj.darklaf.util.DarkUIUtil;
@ -44,39 +45,70 @@ public class ColoredRadioButton extends JRadioButton {
public static final Color DEFAULT_FILLED = new Color(0); public static final Color DEFAULT_FILLED = new Color(0);
private Color color; private Color color;
private Color focusColor;
public ColoredRadioButton(final String text, final Color color) { public ColoredRadioButton(final String text, final Color color) {
super(text, null, false); super(text, null, false);
setColor(color); setColors(color, color);
} }
public ColoredRadioButton(final String text, final boolean selected, final Color color) { public ColoredRadioButton(final String text, final boolean selected, final Color color) {
super(text, null, selected); super(text, null, selected);
setColor(color); setColors(color, color);
}
public ColoredRadioButton(final String text, final Color color, final Color focusColor) {
super(text, null, false);
setColors(color, focusColor);
}
public ColoredRadioButton(final String text, final boolean selected, final Color color, final Color focusColor) {
super(text, null, selected);
setColors(color, focusColor);
}
public void setColors(final Color color, final Color focusColor) {
this.color = color;
this.focusColor = focusColor != null ? focusColor : color;
updateColorUI();
} }
public void setColor(final Color color) { public void setColor(final Color color) {
this.color = color; this.color = color;
ButtonUI ui = getUI(); updateColorUI();
if (ui instanceof ColoredRadioButtonUI) { }
((ColoredRadioButtonUI) ui).setColor(color);
} public void setFocusColor(final Color color) {
SwingUtilities.invokeLater(this::updateUI); this.focusColor = color;
updateColorUI();
}
private void updateColorUI() {
ColoredRadioButtonUI ui = DarkUIUtil.getUIOfType(getUI(), ColoredRadioButtonUI.class);
if (ui != null) ui.setColors(color, focusColor);
repaint();
} }
public Color getColor() { public Color getColor() {
return color; return color;
} }
@Override
protected void setUI(final ComponentUI newUI) {
if (!(newUI instanceof ColoredRadioButtonUI)) {
throw new IllegalArgumentException("UI must be of type ColoredRadioButtonUI");
}
super.setUI(newUI);
}
@Override @Override
public void updateUI() { public void updateUI() {
setUI(new ColoredRadioButtonUI(color)); setUI(new ColoredRadioButtonUI(color, focusColor));
} }
protected static class ColoredRadioButtonUI extends DarkRadioButtonUI { protected static class ColoredRadioButtonUI extends DarkRadioButtonUI {
private static final String[] PROPERTIES = { private static final String[] PROPERTIES = {"Icons.RadioButton.activeFillColor",
"Icons.RadioButton.activeFillColor",
"Icons.RadioButton.activeBorderColor", "Icons.RadioButton.activeBorderColor",
"Icons.RadioButtonDisabled.inactiveFillColor", "Icons.RadioButtonDisabled.inactiveFillColor",
"Icons.RadioButtonDisabled.inactiveBorderColor", "Icons.RadioButtonDisabled.inactiveBorderColor",
@ -94,45 +126,73 @@ public class ColoredRadioButton extends JRadioButton {
"Icons.RadioButtonSelectedFocused.focusSelectedBorderColor", "Icons.RadioButtonSelectedFocused.focusSelectedBorderColor",
"Icons.RadioButtonSelectedFocused.selectionFocusSelectedColor", "Icons.RadioButtonSelectedFocused.selectionFocusSelectedColor",
"Icons.RadioButtonSelectedFocused.glowFocus", "Icons.RadioButtonSelectedFocused.glowFocus",
"Icons.RadioButtonSelectedFocused.glowOpacity" "Icons.RadioButtonSelectedFocused.glowOpacity"};
}; private static final String[] COLOR_PROPERTIES = {"Icons.RadioButton.activeFillColor",
private static final String[] COLOR_PROPERTIES = {
"Icons.RadioButton.activeFillColor",
"Icons.RadioButton.activeBorderColor", "Icons.RadioButton.activeBorderColor",
"Icons.RadioButtonFocused.activeFillColor", "Icons.RadioButtonFocused.activeFillColor",
"Icons.RadioButtonFocused.focusBorderColor", "Icons.RadioButtonFocused.focusBorderColor",
"Icons.RadioButtonSelected.selectedFillColor", "Icons.RadioButtonSelected.selectedFillColor",
"Icons.RadioButtonSelected.selectedBorderColor", "Icons.RadioButtonSelected.selectedBorderColor",
"Icons.RadioButtonSelectedFocused.selectedFillColor", "Icons.RadioButtonSelectedFocused.selectedFillColor",
"Icons.RadioButtonSelectedFocused.focusSelectedBorderColor" "Icons.RadioButtonSelectedFocused.focusSelectedBorderColor"};
}; private static final String[] FOCUS_COLOR_PROPERTIES = {"Icons.RadioButtonFocused.glowFocus",
"Icons.RadioButtonSelectedFocused.glowFocus"};
private static final String[] FOREGROUND_PROPERTIES = {"Icons.RadioButtonSelected.selectionSelectedColor",
"Icons.RadioButtonSelectedFocused.selectionFocusSelectedColor"};
private static final double MIN_FG_CONTRAST = 0.6;
private Properties propertyMap; private Properties propertyMap;
private Icon stateIcon; private Icon stateIcon;
private final Color color; private Color color;
private Color focusColor;
private Color patchedColor;
private Color patchedFocusColor;
private boolean patched;
private static final AccentColorAdjustmentTask adjustment = new AccentColorAdjustmentTask(); private static final AccentColorAdjustmentTask adjustment = new AccentColorAdjustmentTask();
public ColoredRadioButtonUI(final Color color) { public ColoredRadioButtonUI(final Color color, final Color focusColor) {
super(); super();
this.color = color; this.color = color;
this.focusColor = focusColor;
} }
@Override @Override
protected void installIcons() { protected void installIcons() {
super.installIcons(); super.installIcons();
setColor(color); patchColors(color, focusColor);
} }
public void setColor(final Color color) { public void setColors(final Color color, final Color focusColor) {
if (color == null) return; this.color = color;
this.focusColor = focusColor != null ? focusColor : color;
}
@Override
public void update(final Graphics g, final JComponent c) {
if (patchedColor != color || patchedFocusColor != focusColor) {
patchColors(color, focusColor);
}
super.update(g, c);
}
private void patchColors(final Color color, final Color focusColor) {
if (color == null || (patched && patchedColor == color && patchedFocusColor == focusColor)) {
return;
}
this.patchedColor = color;
this.patchedFocusColor = focusColor;
IconLoader loader = DarkUIUtil.ICON_LOADER; IconLoader loader = DarkUIUtil.ICON_LOADER;
Theme theme = LafManager.getTheme(); Theme theme = LafManager.getTheme();
Properties props = new Properties(); Properties props = new Properties();
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); UIDefaults defaults = UIManager.getLookAndFeelDefaults();
theme.loadDefaults(props, defaults); theme.loadDefaults(props, defaults);
Color c = color == DEFAULT_FILLED ? (Color) props.get("widgetFillDefault") : color; Color accentCol = color == DEFAULT_FILLED ? (Color) props.get("widgetFillDefault") : color;
adjustment.applyColors(LafManager.getTheme(), props, c, null); Color focusCol = color == DEFAULT_FILLED ? accentCol : focusColor;
PropertyLoader.putProperties(PropertyLoader.loadProperties(DarkLaf.class, "radioButton", "properties/ui/"), adjustment.applyColors(LafManager.getTheme(), props, accentCol, null);
PropertyLoader.putProperties(PropertyLoader.loadProperties(DarkLaf.class, "radioButton",
"properties/ui/"),
props, defaults); props, defaults);
PropertyLoader.putProperties(PropertyLoader.loadProperties(DarkLaf.class, "radioButton", PropertyLoader.putProperties(PropertyLoader.loadProperties(DarkLaf.class, "radioButton",
"properties/icons/"), "properties/icons/"),
@ -142,7 +202,15 @@ public class ColoredRadioButton extends JRadioButton {
propertyMap.put(prop, props.get(prop)); propertyMap.put(prop, props.get(prop));
} }
for (String prop : COLOR_PROPERTIES) { for (String prop : COLOR_PROPERTIES) {
propertyMap.put(prop, c); propertyMap.put(prop, accentCol);
}
for (String prop : FOCUS_COLOR_PROPERTIES) {
propertyMap.put(prop, focusCol);
}
for (String prop : FOREGROUND_PROPERTIES) {
Color fg = ForegroundColorGenerationTask.makeAdjustedForeground((Color) props.get(prop),
accentCol, MIN_FG_CONTRAST);
propertyMap.put(prop, fg);
} }
stateIcon = new StateIcon(new Icon[]{load(loader, "control/radio.svg"), stateIcon = new StateIcon(new Icon[]{load(loader, "control/radio.svg"),
@ -151,6 +219,7 @@ public class ColoredRadioButton extends JRadioButton {
load(loader, "control/radioSelected.svg"), load(loader, "control/radioSelected.svg"),
load(loader, "control/radioSelectedDisabled.svg"), load(loader, "control/radioSelectedDisabled.svg"),
load(loader, "control/radioSelectedFocused.svg")}); load(loader, "control/radioSelectedFocused.svg")});
patched = true;
} }
private Icon load(final IconLoader loader, final String name) { private Icon load(final IconLoader loader, final String name) {
@ -159,7 +228,7 @@ public class ColoredRadioButton extends JRadioButton {
@Override @Override
protected Icon getStateIcon(final AbstractButton b) { protected Icon getStateIcon(final AbstractButton b) {
return color != null ? stateIcon : super.getStateIcon(b); return patched ? stateIcon : super.getStateIcon(b);
} }
} }
} }

2
core/src/main/java/com/github/weisj/darklaf/graphics/StringPainter.java

@ -163,7 +163,7 @@ public class StringPainter {
* Ensure the background color has sufficient contrast to the foreground. * Ensure the background color has sufficient contrast to the foreground.
*/ */
Color fg = g.getColor(); Color fg = g.getColor();
double brightness = ColorUtil.getBrightness(fg); double brightness = ColorUtil.getPerceivedBrightness(fg);
bgColor = brightness > 127 ? Color.BLACK : Color.WHITE; bgColor = brightness > 127 ? Color.BLACK : Color.WHITE;
} }

39
core/src/main/java/com/github/weisj/darklaf/settings/DefaultSettingsConfiguration.java

@ -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.settings;
import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.theme.Theme;
public class DefaultSettingsConfiguration extends SettingsConfiguration {
public DefaultSettingsConfiguration() {
setEnabledSystemPreferences(LafManager.isPreferenceChangeReportingEnabled());
Theme theme = LafManager.getTheme();
setTheme(theme);
setFontSizeRule(theme.getFontSizeRule());
setAccentColorRule(theme.getAccentColorRule());
}
}

255
core/src/main/java/com/github/weisj/darklaf/settings/SettingsConfiguration.java

@ -0,0 +1,255 @@
/*
* 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.settings;
import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.platform.ThemePreferencesHandler;
import com.github.weisj.darklaf.theme.Theme;
import com.github.weisj.darklaf.theme.info.AccentColorRule;
import com.github.weisj.darklaf.theme.info.FontSizeRule;
public class SettingsConfiguration {
private boolean isSystemPreferencesEnabled;
private boolean isAccentColorFollowsSystem;
private boolean isFontSizeFollowsSystem;
private boolean isThemeFollowsSystem;
private boolean isSelectionColorFollowsSystem;
private AccentColorRule accentColorRule;
private FontSizeRule fontSizeRule;
private Theme theme;
public void load(final SettingsConfiguration config) {
setEnabledSystemPreferences(config.isSystemPreferencesEnabled());
setAccentColorFollowsSystem(config.isAccentColorFollowsSystem());
setSelectionColorFollowsSystem(config.isSelectionColorFollowsSystem());
setFontSizeFollowsSystem(config.isFontSizeFollowsSystem());
setThemeFollowsSystem(config.isThemeFollowsSystem());
setFontSizeRule(config.getFontSizeRule());
setAccentColorRule(config.getAccentColorRule());
setTheme(config.getTheme());
}
/**
* Returns whether the option to follow the system settings is enabled.
*
* @return true if enabled.
* @see #isSystemPreferencesEnabled() (boolean)
* @see #isThemeFollowsSystem()
* @see #isAccentColorFollowsSystem()
* @see #isSelectionColorFollowsSystem()
* @see #isFontSizeFollowsSystem()
*/
public boolean isSystemPreferencesEnabled() {
return isSystemPreferencesEnabled;
}
/**
* Returns whether the accent color follows the system settings.
*
* @return true if accent color follows system settings.
* @see #setAccentColorFollowsSystem(boolean)
*/
public boolean isAccentColorFollowsSystem() {
return isAccentColorFollowsSystem;
}
/**
* Returns whether the font size follows the system settings.
*
* @return true if font size follows system settings.
* @see #setFontSizeFollowsSystem(boolean)
*/
public boolean isFontSizeFollowsSystem() {
return isFontSizeFollowsSystem;
}
/**
* Returns whether the selection color follows the system settings.
*
* @return true if selection color follows system settings.
* @see #setSelectionColorFollowsSystem(boolean)
*/
public boolean isSelectionColorFollowsSystem() {
return isSelectionColorFollowsSystem;
}
/**
* Returns whether the theme follows the system settings.
*
* @return true if theme follows system settings.
* @see #setThemeFollowsSystem(boolean)
*/
public boolean isThemeFollowsSystem() {
return isThemeFollowsSystem;
}
/**
* Get the currently selected accent color rule. This is not the same as the rule of {@link LafManager#getTheme()}
* as the current settings might not have been applied.
*
* @return the current selected accent color rule.
* @see #setAccentColorRule(AccentColorRule)
*/
public AccentColorRule getAccentColorRule() {
return accentColorRule;
}
/**
* Get the currently selected font size rule. This is not the same as the rule of {@link LafManager#getTheme()} as
* the current settings might not have been applied.
*
* @return the current selected font size rule.
* @see #setFontSizeRule(FontSizeRule)
*/
public FontSizeRule getFontSizeRule() {
return fontSizeRule;
}
/**
* Get the currently selected theme. This is not the same as {@link LafManager#getTheme()} as the current settings
* might not have been applied.
*
* @return the current selected theme.
* @see #setTheme(Theme)
*/
public Theme getTheme() {
return theme;
}
/**
* Enables the option to follow system preferences.
*
* @param enabled true if enabled.
* @see #setAccentColorFollowsSystem(boolean)
* @see #setSelectionColorFollowsSystem(boolean)
* @see #setFontSizeFollowsSystem(boolean)
* @see #setThemeFollowsSystem(boolean)
*/
public void setEnabledSystemPreferences(final boolean enabled) {
isSystemPreferencesEnabled = enabled;
}
/**
* Sets whether the accent color should follow the system settings. This only works if {@link
* #isSelectionColorFollowsSystem()} is true.
*
* @param accentColorFollowsSystem true if accent color should follow system.
* @see #isAccentColorFollowsSystem()
*/
public void setAccentColorFollowsSystem(final boolean accentColorFollowsSystem) {
isAccentColorFollowsSystem = accentColorFollowsSystem;
}
/**
* Sets whether the font size should follow the system settings. This only works if {@link
* #isSelectionColorFollowsSystem()} is true.
*
* @param fontSizeFollowsSystem true if font size should follow system.
* @see #isFontSizeFollowsSystem()
*/
public void setFontSizeFollowsSystem(final boolean fontSizeFollowsSystem) {
isFontSizeFollowsSystem = fontSizeFollowsSystem;
}
/**
* Sets whether the selection color should follow the system settings. This only works if {@link
* #isSelectionColorFollowsSystem()} is true.
*
* @param selectionColorFollowsSystem true if selection color should follow system.
* @see #isSelectionColorFollowsSystem()
*/
public void setSelectionColorFollowsSystem(final boolean selectionColorFollowsSystem) {
isSelectionColorFollowsSystem = selectionColorFollowsSystem;
}
/**
* Sets whether the theme should follow the system settings. This only works if {@link
* #isSelectionColorFollowsSystem()} is true.
*
* @param themeFollowsSystem true if theme should follow system.
* @see #isThemeFollowsSystem()
*/
public void setThemeFollowsSystem(final boolean themeFollowsSystem) {
isThemeFollowsSystem = themeFollowsSystem;
}
/**
* Sets the font accent color rule.
*
* @param accentColorRule the accent color rule.
* @see #getAccentColorRule()
*/
public void setAccentColorRule(final AccentColorRule accentColorRule) {
this.accentColorRule = accentColorRule;
}
/**
* Sets the font size rule.
*
* @param fontSizeRule the font size rule.
* @see #getFontSizeRule()
*/
public void setFontSizeRule(final FontSizeRule fontSizeRule) {
this.fontSizeRule = fontSizeRule;
}
/**
* Sets the theme.
*
* @param theme the theme.
* @see #getTheme()
*/
public void setTheme(final Theme theme) {
this.theme = theme;
}
public boolean isSystemPreferencesSupported() {
return ThemePreferencesHandler.getSharedInstance().canReport();
}
public boolean isSystemAccentColorSupported() {
return isSystemPreferencesEnabled()
&& ThemePreferencesHandler.getSharedInstance().supportsNativeAccentColor();
}
public boolean isSystemFontSizeSupported() {
return isSystemPreferencesEnabled()
&& ThemePreferencesHandler.getSharedInstance().supportsNativeFontSize();
}
public boolean isSystemSelectionColorSupported() {
return isSystemPreferencesEnabled()
&& ThemePreferencesHandler.getSharedInstance().supportsNativeSelectionColor();
}
public boolean isSystemThemeSupported() {
return isSystemPreferencesEnabled()
&& ThemePreferencesHandler.getSharedInstance().supportsNativeTheme();
}
}

133
core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java

@ -38,6 +38,7 @@ import com.github.weisj.darklaf.theme.event.ThemePreferenceChangeEvent;
import com.github.weisj.darklaf.theme.event.ThemePreferenceListener; import com.github.weisj.darklaf.theme.event.ThemePreferenceListener;
import com.github.weisj.darklaf.theme.info.AccentColorRule; import com.github.weisj.darklaf.theme.info.AccentColorRule;
import com.github.weisj.darklaf.theme.info.FontSizeRule; import com.github.weisj.darklaf.theme.info.FontSizeRule;
import com.github.weisj.darklaf.theme.info.PreferredThemeStyle;
import com.github.weisj.darklaf.util.DarkUIUtil; import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.LazyValue; import com.github.weisj.darklaf.util.LazyValue;
import com.github.weisj.darklaf.util.ResourceUtil; import com.github.weisj.darklaf.util.ResourceUtil;
@ -46,11 +47,15 @@ public class ThemeSettings implements ThemePreferenceListener {
private static final LazyValue<ThemeSettings> instance = new LazyValue<>(ThemeSettings::new); private static final LazyValue<ThemeSettings> instance = new LazyValue<>(ThemeSettings::new);
private static final LazyValue<Icon> icon = new LazyValue<>(() -> UIManager.getIcon("ThemeSettings.icon")); private static final LazyValue<Icon> icon = new LazyValue<>(() -> UIManager.getIcon("ThemeSettings.icon"));
private final JPanel contentPane;
private final ThemeSettingsPanel settingsPanel;
private final ResourceBundle resourceBundle = ResourceUtil.getResourceBundle("theme_settings"); private final ResourceBundle resourceBundle = ResourceUtil.getResourceBundle("theme_settings");
private final LazyValue<ThemeSettingsPanel> settingsPanel;
private JDialog dialog; private JDialog dialog;
private final SettingsConfiguration savedConfiguration;
private final SettingsConfiguration currentConfiguration;
public static ThemeSettings getInstance() { public static ThemeSettings getInstance() {
return instance.get(); return instance.get();
} }
@ -81,19 +86,26 @@ public class ThemeSettings implements ThemePreferenceListener {
} }
protected ThemeSettings() { protected ThemeSettings() {
settingsPanel = new ThemeSettingsPanel(resourceBundle);
contentPane = new JPanel(new BorderLayout());
contentPane.add(settingsPanel, BorderLayout.CENTER);
contentPane.add(createButtonPanel(), BorderLayout.SOUTH);
settingsPanel.fetch(true);
LafManager.addThemePreferenceChangeListener(this); LafManager.addThemePreferenceChangeListener(this);
currentConfiguration = new DefaultSettingsConfiguration();
savedConfiguration = new DefaultSettingsConfiguration();
settingsPanel = new LazyValue<>(() -> {
ThemeSettingsPanel panel = new ThemeSettingsPanel(resourceBundle);
panel.loadConfiguration(currentConfiguration);
return panel;
});
} }
protected JDialog createDialog(final Window parent) { protected JDialog createDialog(final Window parent) {
JDialog dialog = new JDialog(parent); JDialog dialog = new JDialog(parent);
dialog.setIconImage(IconLoader.createFrameIcon(getIcon(), dialog)); dialog.setIconImage(IconLoader.createFrameIcon(getIcon(), dialog));
dialog.setTitle(settingsPanel.getTitle()); dialog.setTitle(getTitle());
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(settingsPanel.get(), BorderLayout.CENTER);
contentPane.add(createButtonPanel(), BorderLayout.SOUTH);
dialog.setContentPane(contentPane); dialog.setContentPane(contentPane);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.pack(); dialog.pack();
dialog.setLocationByPlatform(true); dialog.setLocationByPlatform(true);
@ -133,7 +145,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @return the settings panel. * @return the settings panel.
*/ */
public JComponent getSettingsPanel() { public JComponent getSettingsPanel() {
return settingsPanel; return settingsPanel.get();
} }
/** /**
@ -147,7 +159,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #isFontSizeFollowsSystem() * @see #isFontSizeFollowsSystem()
*/ */
public boolean isSystemPreferencesEnabled() { public boolean isSystemPreferencesEnabled() {
return settingsPanel.isSystemPreferencesEnabled(); return currentConfiguration.isSystemPreferencesEnabled();
} }
/** /**
@ -157,7 +169,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #setAccentColorFollowsSystem(boolean) * @see #setAccentColorFollowsSystem(boolean)
*/ */
public boolean isAccentColorFollowsSystem() { public boolean isAccentColorFollowsSystem() {
return settingsPanel.isAccentColorFollowsSystem(); return currentConfiguration.isAccentColorFollowsSystem();
} }
/** /**
@ -167,7 +179,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #setFontSizeFollowsSystem(boolean) * @see #setFontSizeFollowsSystem(boolean)
*/ */
public boolean isFontSizeFollowsSystem() { public boolean isFontSizeFollowsSystem() {
return settingsPanel.isFontSizeFollowsSystem(); return currentConfiguration.isFontSizeFollowsSystem();
} }
/** /**
@ -177,7 +189,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #setSelectionColorFollowsSystem(boolean) * @see #setSelectionColorFollowsSystem(boolean)
*/ */
public boolean isSelectionColorFollowsSystem() { public boolean isSelectionColorFollowsSystem() {
return settingsPanel.isSelectionColorFollowsSystem(); return currentConfiguration.isSelectionColorFollowsSystem();
} }
/** /**
@ -187,7 +199,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #setThemeFollowsSystem(boolean) * @see #setThemeFollowsSystem(boolean)
*/ */
public boolean isThemeFollowsSystem() { public boolean isThemeFollowsSystem() {
return settingsPanel.isThemeFollowsSystem(); return currentConfiguration.isThemeFollowsSystem();
} }
/** /**
@ -198,7 +210,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #setAccentColorRule(AccentColorRule) * @see #setAccentColorRule(AccentColorRule)
*/ */
public AccentColorRule getAccentColorRule() { public AccentColorRule getAccentColorRule() {
return settingsPanel.getAccentColorRule(); return currentConfiguration.getAccentColorRule();
} }
/** /**
@ -209,7 +221,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #setFontSizeRule(FontSizeRule) * @see #setFontSizeRule(FontSizeRule)
*/ */
public FontSizeRule getFontSizeRule() { public FontSizeRule getFontSizeRule() {
return settingsPanel.getFontSizeRule(); return currentConfiguration.getFontSizeRule();
} }
/** /**
@ -220,7 +232,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #setTheme(Theme) * @see #setTheme(Theme)
*/ */
public Theme getTheme() { public Theme getTheme() {
return settingsPanel.getTheme(); return currentConfiguration.getTheme();
} }
/** /**
@ -233,7 +245,8 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #setThemeFollowsSystem(boolean) * @see #setThemeFollowsSystem(boolean)
*/ */
public void setEnabledSystemPreferences(final boolean enabled) { public void setEnabledSystemPreferences(final boolean enabled) {
settingsPanel.setEnabledSystemPreferences(enabled); currentConfiguration.setEnabledSystemPreferences(enabled);
updateSettingsPanel();
} }
/** /**
@ -244,7 +257,8 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #isAccentColorFollowsSystem() * @see #isAccentColorFollowsSystem()
*/ */
public void setAccentColorFollowsSystem(final boolean accentColorFollowsSystem) { public void setAccentColorFollowsSystem(final boolean accentColorFollowsSystem) {
settingsPanel.setAccentColorFollowsSystem(accentColorFollowsSystem); currentConfiguration.setAccentColorFollowsSystem(accentColorFollowsSystem);
updateSettingsPanel();
} }
/** /**
@ -255,7 +269,8 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #isFontSizeFollowsSystem() * @see #isFontSizeFollowsSystem()
*/ */
public void setFontSizeFollowsSystem(final boolean fontSizeFollowsSystem) { public void setFontSizeFollowsSystem(final boolean fontSizeFollowsSystem) {
settingsPanel.setFontSizeFollowsSystem(fontSizeFollowsSystem); currentConfiguration.setFontSizeFollowsSystem(fontSizeFollowsSystem);
updateSettingsPanel();
} }
/** /**
@ -266,7 +281,8 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #isSelectionColorFollowsSystem() * @see #isSelectionColorFollowsSystem()
*/ */
public void setSelectionColorFollowsSystem(final boolean selectionColorFollowsSystem) { public void setSelectionColorFollowsSystem(final boolean selectionColorFollowsSystem) {
settingsPanel.setSelectionColorFollowsSystem(selectionColorFollowsSystem); currentConfiguration.setSelectionColorFollowsSystem(selectionColorFollowsSystem);
updateSettingsPanel();
} }
/** /**
@ -277,7 +293,8 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #isThemeFollowsSystem() * @see #isThemeFollowsSystem()
*/ */
public void setThemeFollowsSystem(final boolean themeFollowsSystem) { public void setThemeFollowsSystem(final boolean themeFollowsSystem) {
settingsPanel.setThemeFollowsSystem(themeFollowsSystem); currentConfiguration.setThemeFollowsSystem(themeFollowsSystem);
updateSettingsPanel();
} }
/** /**
@ -287,7 +304,8 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #getAccentColorRule() * @see #getAccentColorRule()
*/ */
public void setAccentColorRule(final AccentColorRule accentColorRule) { public void setAccentColorRule(final AccentColorRule accentColorRule) {
settingsPanel.setAccentColorRule(accentColorRule); currentConfiguration.setAccentColorRule(accentColorRule);
updateSettingsPanel();
} }
/** /**
@ -297,7 +315,9 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #getFontSizeRule() * @see #getFontSizeRule()
*/ */
public void setFontSizeRule(final FontSizeRule fontSizeRule) { public void setFontSizeRule(final FontSizeRule fontSizeRule) {
settingsPanel.setFontSizeRule(fontSizeRule); currentConfiguration.setFontSizeRule(fontSizeRule);
updateSettingsPanel();
} }
/** /**
@ -307,7 +327,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #getTheme() * @see #getTheme()
*/ */
public void setTheme(final Theme theme) { public void setTheme(final Theme theme) {
settingsPanel.setTheme(theme); currentConfiguration.setTheme(theme);
} }
protected Component createButtonPanel() { protected Component createButtonPanel() {
@ -333,7 +353,7 @@ public class ThemeSettings implements ThemePreferenceListener {
box.add(ok); box.add(ok);
box.add(cancel); box.add(cancel);
box.add(apply); box.add(apply);
box.setBorder(settingsPanel.getBorder()); box.setBorder(settingsPanel.get().getBorder());
return box; return box;
} }
@ -341,14 +361,29 @@ public class ThemeSettings implements ThemePreferenceListener {
* Updates all values according to the current settings. * Updates all values according to the current settings.
*/ */
public void refresh() { public void refresh() {
settingsPanel.fetch(true); currentConfiguration.setEnabledSystemPreferences(LafManager.isPreferenceChangeReportingEnabled());
updateSettingsPanel();
}
private void updateSettingsPanel() {
settingsPanel.ifPresent(p -> p.loadConfiguration(currentConfiguration));
}
private void fetchFromSettingsPanel() {
settingsPanel.ifPresent(ThemeSettingsPanel::updateConfiguration);
} }
/** /**
* Saves the settings. * Saves the settings.
*/ */
public void save() { public void save() {
settingsPanel.saveSettings(); fetchFromSettingsPanel();
LafManager.enabledPreferenceChangeReporting(currentConfiguration.isSystemPreferencesEnabled());
savedConfiguration.load(currentConfiguration);
}
public void setConfiguration(final SettingsConfiguration configuration) {
this.currentConfiguration.load(configuration);
} }
/** /**
@ -363,7 +398,37 @@ public class ThemeSettings implements ThemePreferenceListener {
* Sets the theme according to the selected options. Does not save the settings. * Sets the theme according to the selected options. Does not save the settings.
*/ */
public void peek() { public void peek() {
applyTheme(settingsPanel.getEffectiveTheme()); applyTheme(getEffectiveTheme());
}
private Theme getEffectiveTheme() {
return getEffectiveTheme(LafManager.getPreferredThemeStyle());
}
private Theme getEffectiveTheme(final PreferredThemeStyle themeStyle) {
Theme baseTheme = getEffectiveBaseTheme(themeStyle);
if (baseTheme == null) return null;
FontSizeRule fontSizeRule = getEffectiveFontSizeRule(baseTheme, themeStyle);
AccentColorRule accentColorRule = getEffectiveAccentColorRule(baseTheme);
return baseTheme.derive(fontSizeRule, accentColorRule);
}
private Theme getEffectiveBaseTheme(final PreferredThemeStyle preferredThemeStyle) {
return isThemeFollowsSystem()
? LafManager.themeForPreferredStyle(preferredThemeStyle)
: getTheme();
}
private FontSizeRule getEffectiveFontSizeRule(final Theme theme, final PreferredThemeStyle preferredThemeStyle) {
if (theme == null) return FontSizeRule.getDefault();
return isFontSizeFollowsSystem()
? preferredThemeStyle.getFontSizeRule()
: getFontSizeRule();
}
private AccentColorRule getEffectiveAccentColorRule(final Theme theme) {
if (theme == null) return AccentColorRule.getDefault();
return currentConfiguration.getAccentColorRule();
} }
/** /**
@ -372,7 +437,7 @@ public class ThemeSettings implements ThemePreferenceListener {
* @see #save() * @see #save()
*/ */
public void revert() { public void revert() {
settingsPanel.discardChanges(); currentConfiguration.load(savedConfiguration);
refresh(); refresh();
} }
@ -381,7 +446,7 @@ public class ThemeSettings implements ThemePreferenceListener {
if (LafManager.getTheme().appearsEqualTo(theme)) return; if (LafManager.getTheme().appearsEqualTo(theme)) return;
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
LafManager.installTheme(theme); LafManager.installTheme(theme);
settingsPanel.fetch(true); refresh();
}); });
} }
@ -400,12 +465,12 @@ public class ThemeSettings implements ThemePreferenceListener {
* @return the title * @return the title
*/ */
public String getTitle() { public String getTitle() {
return settingsPanel.getTitle(); return resourceBundle.getString("title");
} }
@Override @Override
public void themePreferenceChanged(final ThemePreferenceChangeEvent e) { public void themePreferenceChanged(final ThemePreferenceChangeEvent e) {
settingsPanel.fetch(e.getPreferredThemeStyle()); refresh();
applyTheme(settingsPanel.getEffectiveTheme(e.getPreferredThemeStyle())); applyTheme(getEffectiveTheme(e.getPreferredThemeStyle()));
} }
} }

496
core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettingsPanel.java

@ -25,7 +25,6 @@
package com.github.weisj.darklaf.settings; package com.github.weisj.darklaf.settings;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.util.*; import java.util.*;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -56,7 +55,8 @@ import com.github.weisj.darklaf.util.Alignment;
public class ThemeSettingsPanel extends JPanel { public class ThemeSettingsPanel extends JPanel {
private final ResourceBundle resourceBundle; private final ResourceBundle resourceBundle;
private Icon icon;
private final SettingsPanelConfiguration settingsConfiguration;
private JCheckBox fontSizeFollowsSystem; private JCheckBox fontSizeFollowsSystem;
private JCheckBox accentColorFollowsSystem; private JCheckBox accentColorFollowsSystem;
@ -69,11 +69,6 @@ public class ThemeSettingsPanel extends JPanel {
private ButtonGroup bgSelection; private ButtonGroup bgSelection;
private ButtonGroup bgAccent; private ButtonGroup bgAccent;
private boolean followFontSize;
private boolean followAccentColor;
private boolean followSelectionColor;
private boolean followTheme;
private boolean systemPreferences;
private Color defaultAccentColor; private Color defaultAccentColor;
private Color defaultSelectionColor; private Color defaultSelectionColor;
private ColoredRadioButton customAccent; private ColoredRadioButton customAccent;
@ -83,6 +78,7 @@ public class ThemeSettingsPanel extends JPanel {
public ThemeSettingsPanel(final ResourceBundle resourceBundle) { public ThemeSettingsPanel(final ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle; this.resourceBundle = resourceBundle;
this.settingsConfiguration = new SettingsPanelConfiguration();
init(); init();
} }
@ -91,93 +87,38 @@ public class ThemeSettingsPanel extends JPanel {
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(createGeneralSettings(), BorderLayout.CENTER); add(createGeneralSettings(), BorderLayout.CENTER);
add(createMonitorSettings(), BorderLayout.SOUTH); add(createMonitorSettings(), BorderLayout.SOUTH);
fetch(true);
update();
} }
public void saveSettings() { public void loadConfiguration(final SettingsConfiguration configuration) {
systemPreferences = !enabledSystemPreferences.getTristateModel().isDeselected(); themeComboBox.setModel(LafManager.getThemeComboBoxModel());
followTheme = fontSizeFollowsSystem.isSelected(); settingsConfiguration.load(configuration);
followAccentColor = accentColorFollowsSystem.isSelected(); settingsConfiguration.setAccentColorRule(settingsConfiguration.getAccentColorRule());
followSelectionColor = selectionColorFollowsSystem.isSelected();
followFontSize = fontSizeFollowsSystem.isSelected();
LafManager.enabledPreferenceChangeReporting(!enabledSystemPreferences.getTristateModel().isDeselected());
}
public void discardChanges() { update();
setFontSizeFollowsSystem(followFontSize);
setThemeFollowsSystem(followTheme);
setAccentColorFollowsSystem(followAccentColor);
setSelectionColorFollowsSystem(followSelectionColor);
setEnabledSystemPreferences(systemPreferences);
} }
private void update() { private void update() {
Theme selectedTheme = (Theme) themeComboBox.getSelectedItem();
if (selectedTheme == null) selectedTheme = LafManager.getTheme();
boolean enabled = !enabledSystemPreferences.getTristateModel().isDeselected(); boolean enabled = !enabledSystemPreferences.getTristateModel().isDeselected();
themeFollowsSystem.setEnabled(enabled);
accentColorFollowsSystem.setEnabled(enabled);
selectionColorFollowsSystem.setEnabled(enabled);
fontSizeFollowsSystem.setEnabled(enabled);
if (enabledSystemPreferences.isSelected()) {
themeFollowsSystem.setSelected(themeFollowsSystem.isSelected() && themeFollowsSystem.isEnabled());
accentColorFollowsSystem.setSelected(accentColorFollowsSystem.isSelected()
&& accentColorFollowsSystem.isEnabled());
selectionColorFollowsSystem.setSelected(selectionColorFollowsSystem.isSelected()
&& selectionColorFollowsSystem.isEnabled());
fontSizeFollowsSystem.setSelected(fontSizeFollowsSystem.isSelected() && fontSizeFollowsSystem.isEnabled());
}
enabledSystemPreferences.setEnabled(ThemePreferencesHandler.getSharedInstance().canReport());
themeComboBox.setEnabled(!(isThemeFollowsSystem() && isSystemPreferencesEnabled()));
setEnabledSystemPreferences(isSystemPreferencesEnabled());
enableButtonGroup(bgAccent, selectedTheme.supportsCustomAccentColor()
&& !accentColorFollowsSystem.isSelected());
enableButtonGroup(bgSelection, selectedTheme.supportsCustomSelectionColor()
&& !selectionColorFollowsSystem.isSelected());
fontSlider.setEnabled(!(isFontSizeFollowsSystem() && isSystemPreferencesEnabled()));
}
protected void fetch(final PreferredThemeStyle themeStyle, final boolean ignoreSettings) { enabledSystemPreferences.setEnabled(settingsConfiguration.isSystemPreferencesSupported());
Theme theme = LafManager.themeForPreferredStyle(themeStyle);
fetch(theme, ignoreSettings);
}
protected void fetch(final Theme theme, final boolean ignoreSettings) { themeFollowsSystem.setEnabled(enabled && settingsConfiguration.isSystemThemeSupported());
Theme selectedTheme = (Theme) themeComboBox.getSelectedItem(); accentColorFollowsSystem.setEnabled(enabled && settingsConfiguration.isSystemAccentColorSupported());
Theme currentTheme = LafManager.getTheme(); selectionColorFollowsSystem.setEnabled(enabled && settingsConfiguration.isSystemSelectionColorSupported());
themeComboBox.setModel(LafManager.getThemeComboBoxModel()); fontSizeFollowsSystem.setEnabled(enabled && settingsConfiguration.isSystemFontSizeSupported());
if (ignoreSettings || isThemeFollowsSystem()) {
setTheme(isThemeFollowsSystem() ? theme : currentTheme);
} else {
themeComboBox.setSelectedItem(selectedTheme);
}
if (ignoreSettings || isFontSizeFollowsSystem()) {
setFontSizeRule(isFontSizeFollowsSystem()
? theme.getFontSizeRule()
: currentTheme.getFontSizeRule());
}
if (ignoreSettings || isAccentColorFollowsSystem()) {
setAccentColor(getAccentColor(isAccentColorFollowsSystem() ? theme : currentTheme, true));
}
if (ignoreSettings || isSelectionColorFollowsSystem()) {
setSelectionColor(getSelectionColor(isSelectionColorFollowsSystem() ? theme : currentTheme, true));
}
if (!ignoreSettings) {
setEnabledSystemPreferences(LafManager.isPreferenceChangeReportingEnabled());
}
update();
}
public void fetch(final PreferredThemeStyle themeStyle) { settingsConfiguration.setEnabledSystemPreferences(settingsConfiguration.isSystemPreferencesEnabled());
fetch(themeStyle, false);
enableButtonGroup(bgAccent, !settingsConfiguration.isAccentColorFollowsSystem()
&& settingsConfiguration.getSelectedTheme().supportsCustomAccentColor());
enableButtonGroup(bgSelection, !settingsConfiguration.isSelectionColorFollowsSystem()
&& settingsConfiguration.getSelectedTheme().supportsCustomSelectionColor());
themeComboBox.setEnabled(!settingsConfiguration.isThemeFollowsSystem());
fontSlider.setEnabled(!settingsConfiguration.isFontSizeFollowsSystem());
} }
public void fetch(final boolean ignoreSettings) { public void updateConfiguration() {
fetch(LafManager.getPreferredThemeStyle(), ignoreSettings); ThemeSettings.getInstance().setConfiguration(settingsConfiguration);
} }
private boolean updateButtonGroup(final ButtonGroup bg, final Color currentColor, private boolean updateButtonGroup(final ButtonGroup bg, final Color currentColor,
@ -194,18 +135,6 @@ public class ThemeSettingsPanel extends JPanel {
return false; return false;
} }
public Theme getEffectiveTheme(final PreferredThemeStyle preferredThemeStyle) {
Theme theme = getTheme(preferredThemeStyle);
if (theme == null) return null;
FontSizeRule fontSizeRule = getFontSizeRule(theme, preferredThemeStyle);
AccentColorRule accentColorRule = getAccentColorRule(theme);
return theme.derive(fontSizeRule, accentColorRule);
}
public Theme getEffectiveTheme() {
return getEffectiveTheme(LafManager.getPreferredThemeStyle());
}
protected Color getSelectedColor(final ButtonGroup bg, final AbstractButton defaultButton) { protected Color getSelectedColor(final ButtonGroup bg, final AbstractButton defaultButton) {
Enumeration<AbstractButton> enumeration = bg.getElements(); Enumeration<AbstractButton> enumeration = bg.getElements();
while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
@ -244,21 +173,29 @@ public class ThemeSettingsPanel extends JPanel {
defaultSelectionColor = createDefaultColor("themeSelectionColor"); defaultSelectionColor = createDefaultColor("themeSelectionColor");
defaultSelection = addColoredButton(bgSelection, selectionBox, defaultSelectionColor, defaultSelection = addColoredButton(bgSelection, selectionBox, defaultSelectionColor,
resourceBundle.getString("color_default")); resourceBundle.getString("color_default"));
AbstractButton selectionBlue = addColoredButton(bgSelection, selectionBox, MacOSColors.SELECTION_BLUE, AbstractButton selectionBlue = addColoredButton(bgSelection, selectionBox,
MacOSColors.SELECTION_BLUE, MacOSColors.ACCENT_BLUE,
resourceBundle.getString("color_blue")); resourceBundle.getString("color_blue"));
AbstractButton selectionPurple = addColoredButton(bgSelection, selectionBox, MacOSColors.SELECTION_PURPLE, AbstractButton selectionPurple = addColoredButton(bgSelection, selectionBox,
MacOSColors.SELECTION_PURPLE, MacOSColors.ACCENT_LILAC,
resourceBundle.getString("color_purple")); resourceBundle.getString("color_purple"));
AbstractButton selectionPink = addColoredButton(bgSelection, selectionBox, MacOSColors.SELECTION_PINK, AbstractButton selectionPink = addColoredButton(bgSelection, selectionBox,
MacOSColors.SELECTION_PINK, MacOSColors.ACCENT_ROSE,
resourceBundle.getString("color_pink")); resourceBundle.getString("color_pink"));
AbstractButton selectionRed = addColoredButton(bgSelection, selectionBox, MacOSColors.SELECTION_RED, AbstractButton selectionRed = addColoredButton(bgSelection, selectionBox,
MacOSColors.SELECTION_RED, MacOSColors.ACCENT_RED,
resourceBundle.getString("color_red")); resourceBundle.getString("color_red"));
AbstractButton selectionOrange = addColoredButton(bgSelection, selectionBox, MacOSColors.SELECTION_ORANGE, AbstractButton selectionOrange = addColoredButton(bgSelection, selectionBox,
MacOSColors.SELECTION_ORANGE, MacOSColors.ACCENT_ORANGE,
resourceBundle.getString("color_orange")); resourceBundle.getString("color_orange"));
AbstractButton selectionYellow = addColoredButton(bgSelection, selectionBox, MacOSColors.SELECTION_YELLOW, AbstractButton selectionYellow = addColoredButton(bgSelection, selectionBox,
MacOSColors.SELECTION_YELLOW, MacOSColors.ACCENT_YELLOW,
resourceBundle.getString("color_yellow")); resourceBundle.getString("color_yellow"));
AbstractButton selectionGreen = addColoredButton(bgSelection, selectionBox, MacOSColors.SELECTION_GREEN, AbstractButton selectionGreen = addColoredButton(bgSelection, selectionBox,
MacOSColors.SELECTION_GREEN, MacOSColors.ACCENT_GREEN,
resourceBundle.getString("color_green")); resourceBundle.getString("color_green"));
AbstractButton selectionGraphite = addColoredButton(bgSelection, selectionBox, MacOSColors.SELECTION_GRAPHITE, AbstractButton selectionGraphite = addColoredButton(bgSelection, selectionBox,
MacOSColors.SELECTION_GRAPHITE, MacOSColors.ACCENT_GRAPHITE,
resourceBundle.getString("color_gray")); resourceBundle.getString("color_gray"));
customSelection = addCustomButton(bgSelection, selectionBox, currentSelectionColor, defaultSelectionColor, customSelection = addCustomButton(bgSelection, selectionBox, currentSelectionColor, defaultSelectionColor,
resourceBundle.getString("color_custom")); resourceBundle.getString("color_custom"));
@ -324,116 +261,6 @@ public class ThemeSettingsPanel extends JPanel {
return new ThemedColor(key); return new ThemedColor(key);
} }
public boolean isSystemPreferencesEnabled() {
return !enabledSystemPreferences.getTristateModel().isDeselected() && enabledSystemPreferences.isEnabled();
}
public boolean isAccentColorFollowsSystem() {
return accentColorFollowsSystem.isSelected() && accentColorFollowsSystem.isEnabled();
}
public boolean isFontSizeFollowsSystem() {
return fontSizeFollowsSystem.isSelected() && fontSizeFollowsSystem.isEnabled();
}
public boolean isSelectionColorFollowsSystem() {
return selectionColorFollowsSystem.isSelected() && selectionColorFollowsSystem.isEnabled();
}
public boolean isThemeFollowsSystem() {
return themeFollowsSystem.isSelected() && themeFollowsSystem.isEnabled();
}
public Theme getTheme() {
return getTheme(LafManager.getPreferredThemeStyle());
}
protected Theme getTheme(final PreferredThemeStyle preferredThemeStyle) {
return isThemeFollowsSystem()
? LafManager.themeForPreferredStyle(preferredThemeStyle)
: (Theme) themeComboBox.getSelectedItem();
}
public FontSizeRule getFontSizeRule() {
PreferredThemeStyle preferredThemeStyle = LafManager.getPreferredThemeStyle();
return getFontSizeRule(getTheme(preferredThemeStyle), preferredThemeStyle);
}
protected FontSizeRule getFontSizeRule(final Theme theme, final PreferredThemeStyle preferredThemeStyle) {
if (theme == null) return FontSizeRule.getDefault();
return isFontSizeFollowsSystem()
? preferredThemeStyle.getFontSizeRule()
: FontSizeRule.relativeAdjustment(fontSlider.getValue());
}
public AccentColorRule getAccentColorRule() {
PreferredThemeStyle preferredThemeStyle = LafManager.getPreferredThemeStyle();
return getAccentColorRule(getTheme(preferredThemeStyle));
}
protected AccentColorRule getAccentColorRule(final Theme theme) {
if (theme == null) return AccentColorRule.getDefault();
Color accentColor = getAccentColor(theme, isAccentColorFollowsSystem());
Color selectionColor = getSelectionColor(theme, isSelectionColorFollowsSystem());
return AccentColorRule.fromColor(accentColor, selectionColor);
}
protected Color getAccentColor(final Theme theme, final boolean useThemeColor) {
return theme.supportsCustomAccentColor() ? useThemeColor ? theme.getAccentColorRule().getAccentColor()
: getSelectedColor(bgAccent, defaultAccent)
: null;
}
protected Color getSelectionColor(final Theme theme, final boolean useThemeColor) {
return theme.supportsCustomSelectionColor() ? useThemeColor ? theme.getAccentColorRule().getSelectionColor()
: getSelectedColor(bgSelection, defaultSelection)
: null;
}
public void setEnabledSystemPreferences(final boolean enabled) {
TristateState state = TristateState.DESELECTED;
if (enabled
&& (isFontSizeFollowsSystem() || !fontSizeFollowsSystem.isEnabled())
&& (isThemeFollowsSystem() || !themeFollowsSystem.isEnabled())
&& (isAccentColorFollowsSystem() || !accentColorFollowsSystem.isEnabled())
&& (isSelectionColorFollowsSystem() || !selectionColorFollowsSystem.isEnabled())) {
state = TristateState.SELECTED;
} else if (enabled) {
enabledSystemPreferences.getTristateModel().setIndeterminate();
return;
}
enabledSystemPreferences.getTristateModel().setState(state);
}
public void setAccentColorFollowsSystem(final boolean accentColorFollowsSystem) {
this.accentColorFollowsSystem.setSelected(accentColorFollowsSystem);
}
public void setFontSizeFollowsSystem(final boolean fontSizeFollowsSystem) {
this.fontSizeFollowsSystem.setSelected(fontSizeFollowsSystem);
}
public void setSelectionColorFollowsSystem(final boolean selectionColorFollowsSystem) {
this.selectionColorFollowsSystem.setSelected(selectionColorFollowsSystem);
}
public void setThemeFollowsSystem(final boolean themeFollowsSystem) {
this.themeFollowsSystem.setSelected(themeFollowsSystem);
}
public void setTheme(final Theme theme) {
themeComboBox.setSelectedItem(LafManager.getClosestMatchForTheme(theme));
}
public void setAccentColorRule(final AccentColorRule accentColorRule) {
setAccentColorRule(accentColorRule.getAccentColor(), accentColorRule.getSelectionColor());
}
protected void setAccentColorRule(final Color accentColor, final Color selectionColor) {
setAccentColor(accentColor);
setSelectionColor(selectionColor);
}
protected void setAccentColor(final Color accentColor) { protected void setAccentColor(final Color accentColor) {
setXColor(accentColor, bgAccent, customAccent, defaultAccent, defaultAccentColor); setXColor(accentColor, bgAccent, customAccent, defaultAccent, defaultAccentColor);
} }
@ -456,14 +283,6 @@ public class ThemeSettingsPanel extends JPanel {
} }
} }
public void setFontSizeRule(final FontSizeRule fontSizeRule) {
if (fontSizeRule == null) {
fontSlider.setValue(FontSizePreset.NORMAL.getPercentage());
} else {
fontSlider.setValue(fontSizeRule.getPercentage());
}
}
private JSlider createFontSlider() { private JSlider createFontSlider() {
JSlider fontSlider = new JSlider() { JSlider fontSlider = new JSlider() {
@Override @Override
@ -572,10 +391,10 @@ public class ThemeSettingsPanel extends JPanel {
enabledSystemPreferences = new TristateCheckBox(resourceBundle.getString("check_system_preferences")); enabledSystemPreferences = new TristateCheckBox(resourceBundle.getString("check_system_preferences"));
ActionListener actionListener = e -> SwingUtilities.invokeLater(this::update);
enabledSystemPreferences.addChangeListener(e -> { enabledSystemPreferences.addChangeListener(e -> {
if (!enabledSystemPreferences.getTristateModel().isIndeterminate()) { if (!enabledSystemPreferences.getTristateModel().isIndeterminate()) {
boolean selected = enabledSystemPreferences.getTristateModel().isSelected(); boolean selected = enabledSystemPreferences.getTristateModel().isSelected();
if (themeFollowsSystem.isEnabled()) themeFollowsSystem.setSelected(selected); if (themeFollowsSystem.isEnabled()) themeFollowsSystem.setSelected(selected);
if (accentColorFollowsSystem.isEnabled()) accentColorFollowsSystem.setSelected(selected); if (accentColorFollowsSystem.isEnabled()) accentColorFollowsSystem.setSelected(selected);
if (selectionColorFollowsSystem.isEnabled()) selectionColorFollowsSystem.setSelected(selected); if (selectionColorFollowsSystem.isEnabled()) selectionColorFollowsSystem.setSelected(selected);
@ -583,10 +402,11 @@ public class ThemeSettingsPanel extends JPanel {
} }
update(); update();
}); });
themeFollowsSystem.addActionListener(actionListener); themeFollowsSystem.addActionListener(e -> update());
accentColorFollowsSystem.addActionListener(actionListener);
selectionColorFollowsSystem.addActionListener(actionListener); accentColorFollowsSystem.addActionListener(e -> update());
fontSizeFollowsSystem.addActionListener(actionListener); selectionColorFollowsSystem.addActionListener(e -> update());
fontSizeFollowsSystem.addActionListener(e -> update());
enabledSystemPreferences.setSelected(LafManager.isPreferenceChangeReportingEnabled()); enabledSystemPreferences.setSelected(LafManager.isPreferenceChangeReportingEnabled());
@ -656,34 +476,49 @@ public class ThemeSettingsPanel extends JPanel {
} }
} }
public ColoredRadioButton addColoredButton(final ColoredRadioButton button, private ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent,
final ButtonGroup bg, final JComponent parent, final Color color, final String tipText) {
final String tipText) { return addColoredButton(bg, parent, color, color, tipText);
setupButton(button, bg, tipText);
parent.add(button);
return button;
} }
public ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent, private ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent,
final Color color, final AbstractButton peer, final Color color, final AbstractButton peer,
final String tipText) { final String tipText) {
ColoredRadioButton button = addColoredButton(new ColoredRadioButton(null, color), bg, parent, tipText); return addColoredButton(bg, parent, color, color, peer, tipText);
button.addActionListener(e -> { }
if (button.isSelected()) {
peer.setSelected(true); private ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent,
} final Color color, final Color focusColor,
}); final String tipText) {
return addColoredButton(bg, parent, color, focusColor, null, tipText);
}
private ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent,
final Color color, final Color focusColor,
final AbstractButton peer,
final String tipText) {
ColoredRadioButton button = addColoredButtonImpl(new ColoredRadioButton(null, color, focusColor),
bg, parent, tipText);
if (peer != null) {
button.addActionListener(e -> {
if (button.isSelected()) {
peer.setSelected(true);
}
});
}
return button; return button;
} }
public ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent, private ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent,
final Color color, final String tipText) { final Supplier<Color> colorSupplier, final String tipText) {
return addColoredButton(new ColoredRadioButton(null, color), bg, parent, tipText); return addColoredButton(bg, parent, colorSupplier, colorSupplier, tipText);
} }
public ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent, private ColoredRadioButton addColoredButton(final ButtonGroup bg, final JComponent parent,
final Supplier<Color> colorSupplier, final String tipText) { final Supplier<Color> colorSupplier,
return addColoredButton(new ColoredRadioButton(null, null) { final Supplier<Color> focusColorSupplier,
final String tipText) {
return addColoredButtonImpl(new ColoredRadioButton(null, null) {
{ {
addActionListener(e -> getColor()); addActionListener(e -> getColor());
} }
@ -692,13 +527,21 @@ public class ThemeSettingsPanel extends JPanel {
public Color getColor() { public Color getColor() {
Color c = super.getColor(); Color c = super.getColor();
if (c == null) { if (c == null) {
setColor(colorSupplier.get()); setColors(colorSupplier.get(), focusColorSupplier.get());
} }
return super.getColor(); return super.getColor();
} }
}, bg, parent, tipText); }, bg, parent, tipText);
} }
private ColoredRadioButton addColoredButtonImpl(final ColoredRadioButton button,
final ButtonGroup bg, final JComponent parent,
final String tipText) {
setupButton(button, bg, tipText);
parent.add(button);
return button;
}
private void setupButton(final ColoredRadioButton button, final ButtonGroup bg, final String tipText) { private void setupButton(final ColoredRadioButton button, final ButtonGroup bg, final String tipText) {
bg.add(button); bg.add(button);
button.setName(tipText); button.setName(tipText);
@ -709,7 +552,162 @@ public class ThemeSettingsPanel extends JPanel {
button.putClientProperty(ToolTipConstants.KEY_CONTEXT, context); button.putClientProperty(ToolTipConstants.KEY_CONTEXT, context);
} }
public String getTitle() { private class SettingsPanelConfiguration extends SettingsConfiguration {
return resourceBundle.getString("title");
@Override
public boolean isSystemPreferencesEnabled() {
return !enabledSystemPreferences.getTristateModel().isDeselected() && isSystemPreferencesSupported();
}
@Override
public boolean isAccentColorFollowsSystem() {
return accentColorFollowsSystem.isSelected() && isSystemAccentColorSupported();
}
@Override
public boolean isFontSizeFollowsSystem() {
return fontSizeFollowsSystem.isSelected() && isSystemFontSizeSupported();
}
@Override
public boolean isSelectionColorFollowsSystem() {
return selectionColorFollowsSystem.isSelected() && isSystemSelectionColorSupported();
}
@Override
public boolean isThemeFollowsSystem() {
return themeFollowsSystem.isSelected() && isSystemThemeSupported();
}
@Override
public boolean isSystemAccentColorSupported() {
return super.isSystemAccentColorSupported() && getSelectedTheme().supportsCustomAccentColor();
}
@Override
public boolean isSystemSelectionColorSupported() {
return super.isSystemSelectionColorSupported() && getSelectedTheme().supportsCustomSelectionColor();
}
@Override
public AccentColorRule getAccentColorRule() {
PreferredThemeStyle preferredThemeStyle = LafManager.getPreferredThemeStyle();
return getAccentColorRule(getTheme(preferredThemeStyle));
}
private AccentColorRule getAccentColorRule(final Theme theme) {
if (theme == null) return AccentColorRule.getDefault();
Color accentColor = getAccentColor(theme, isAccentColorFollowsSystem());
Color selectionColor = getSelectionColor(theme, isSelectionColorFollowsSystem());
return AccentColorRule.fromColor(accentColor, selectionColor);
}
private Color getAccentColor(final Theme theme, final boolean useThemeColor) {
return theme.supportsCustomAccentColor()
? useThemeColor
? theme.getAccentColorRule().getAccentColor()
: getSelectedColor(bgAccent, defaultAccent)
: null;
}
private Color getSelectionColor(final Theme theme, final boolean useThemeColor) {
return theme.supportsCustomSelectionColor()
? useThemeColor
? theme.getAccentColorRule().getSelectionColor()
: getSelectedColor(bgSelection, defaultSelection)
: null;
}
@Override
public FontSizeRule getFontSizeRule() {
PreferredThemeStyle preferredThemeStyle = LafManager.getPreferredThemeStyle();
return getFontSizeRule(getTheme(preferredThemeStyle), preferredThemeStyle);
}
private FontSizeRule getFontSizeRule(final Theme theme, final PreferredThemeStyle preferredThemeStyle) {
if (theme == null) return FontSizeRule.getDefault();
return isFontSizeFollowsSystem()
? preferredThemeStyle.getFontSizeRule()
: FontSizeRule.relativeAdjustment(fontSlider.getValue()); // Todo
}
@Override
public Theme getTheme() {
return getTheme(LafManager.getPreferredThemeStyle());
}
private Theme getTheme(final PreferredThemeStyle preferredThemeStyle) {
return isThemeFollowsSystem()
? LafManager.themeForPreferredStyle(preferredThemeStyle)
: getSelectedTheme();
}
private Theme getSelectedTheme() {
return (Theme) themeComboBox.getSelectedItem();
}
@Override
public void setEnabledSystemPreferences(final boolean enabled) {
TristateState state = TristateState.DESELECTED;
if (enabled
&& (isFontSizeFollowsSystem() || !fontSizeFollowsSystem.isEnabled())
&& (isThemeFollowsSystem() || !themeFollowsSystem.isEnabled())
&& (isAccentColorFollowsSystem() || !accentColorFollowsSystem.isEnabled())
&& (isSelectionColorFollowsSystem() || !selectionColorFollowsSystem.isEnabled())) {
state = TristateState.SELECTED;
} else if (enabled) {
enabledSystemPreferences.getTristateModel().setIndeterminate();
return;
}
enabledSystemPreferences.getTristateModel().setState(state);
}
@Override
public void setAccentColorFollowsSystem(final boolean accentColorFollowsSystem) {
ThemeSettingsPanel.this.accentColorFollowsSystem.setSelected(accentColorFollowsSystem);
}
@Override
public void setFontSizeFollowsSystem(final boolean fontSizeFollowsSystem) {
ThemeSettingsPanel.this.fontSizeFollowsSystem.setSelected(fontSizeFollowsSystem);
}
@Override
public void setSelectionColorFollowsSystem(final boolean selectionColorFollowsSystem) {
ThemeSettingsPanel.this.selectionColorFollowsSystem.setSelected(selectionColorFollowsSystem);
}
@Override
public void setThemeFollowsSystem(final boolean themeFollowsSystem) {
ThemeSettingsPanel.this.themeFollowsSystem.setSelected(themeFollowsSystem);
}
@Override
public void setAccentColorRule(final AccentColorRule accentColorRule) {
if (accentColorRule == null) {
setAccentColorRule(null, null);
} else {
setAccentColorRule(accentColorRule.getAccentColor(), accentColorRule.getSelectionColor());
}
}
protected void setAccentColorRule(final Color accentColor, final Color selectionColor) {
setAccentColor(accentColor);
setSelectionColor(selectionColor);
}
@Override
public void setFontSizeRule(final FontSizeRule fontSizeRule) {
if (fontSizeRule == null) {
fontSlider.setValue(FontSizePreset.NORMAL.getPercentage());
} else {
fontSlider.setValue(fontSizeRule.getPercentage());
}
}
@Override
public void setTheme(final Theme theme) {
themeComboBox.setSelectedItem(LafManager.getClosestMatchForTheme(theme));
}
} }
} }

62
core/src/main/java/com/github/weisj/darklaf/task/ForegroundColorGenerationTask.java

@ -28,9 +28,13 @@ import java.awt.*;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import javax.swing.plaf.ColorUIResource;
import com.github.weisj.darklaf.color.DarkColorModelHSB; import com.github.weisj.darklaf.color.DarkColorModelHSB;
import com.github.weisj.darklaf.theme.Theme; import com.github.weisj.darklaf.theme.Theme;
import com.github.weisj.darklaf.theme.info.AccentColorRule; import com.github.weisj.darklaf.theme.info.AccentColorRule;
import com.github.weisj.darklaf.uiresource.DarkColorUIResource;
import com.github.weisj.darklaf.util.ColorUtil;
import com.github.weisj.darklaf.util.Pair; import com.github.weisj.darklaf.util.Pair;
public class ForegroundColorGenerationTask extends ColorAdjustmentTask { public class ForegroundColorGenerationTask extends ColorAdjustmentTask {
@ -62,18 +66,56 @@ public class ForegroundColorGenerationTask extends ColorAdjustmentTask {
.filter(o -> o instanceof Pair<?, ?>) .filter(o -> o instanceof Pair<?, ?>)
.map(Pair.class::cast) .map(Pair.class::cast)
.filter(p -> p.getFirst() instanceof Color) .filter(p -> p.getFirst() instanceof Color)
.forEach(p -> properties.put(p.getSecond(), makeForeground((Color) p.getFirst()))); .forEach(p -> properties.put(p.getSecond(), makeForeground((Color) p.getFirst(),
MIN_FOREGROUND_DIFFERENCE)));
}
public static ColorUIResource makeForeground(final Color c, final double minimumBrightnessDifference) {
double[] hsbBG = DarkColorModelHSB.RGBtoHSBValues(c.getRed(), c.getGreen(), c.getBlue());
double[] hsbFG = new double[]{hsbBG[0], 0, 1 - hsbBG[2]};
return makeAdjustedForeground(hsbFG, hsbBG, Bias.BACKGROUND, minimumBrightnessDifference);
}
public static ColorUIResource makeAdjustedForeground(final Color fg, final Color bg,
final double minimumBrightnessDifference) {
return makeAdjustedForeground(fg, bg, Bias.BACKGROUND, minimumBrightnessDifference);
}
public static ColorUIResource makeAdjustedForeground(final Color fg, final Color bg, final Bias bias,
final double minimumBrightnessDifference) {
double[] hsbBG = DarkColorModelHSB.RGBtoHSBValues(bg.getRed(), bg.getGreen(), bg.getBlue());
double[] hsbFG = DarkColorModelHSB.RGBtoHSBValues(fg.getRed(), fg.getGreen(), fg.getBlue());
return makeAdjustedForeground(hsbFG, hsbBG, bias, minimumBrightnessDifference);
} }
private Color makeForeground(final Color c) { private static ColorUIResource makeAdjustedForeground(final double[] hsbFG, final double[] hsbBG, final Bias bias,
double[] hsb = DarkColorModelHSB.RGBtoHSBValues(c.getRed(), c.getGreen(), c.getBlue()); final double minimumBrightnessDifference) {
double b = hsb[2]; double bgBrightness = hsbBG[2];
double brightness = 1 - b; double fgBrightness = hsbFG[2];
double difference = Math.abs(brightness - b);
if (difference < MIN_FOREGROUND_DIFFERENCE) { Bias b = bias != null ? bias : Bias.BACKGROUND;
int bias = b < 0.5 ? 1 : -1; if (b == Bias.BACKGROUND) {
brightness += bias * (Math.abs(MIN_FOREGROUND_DIFFERENCE - difference)); Color c = DarkColorModelHSB.getColorFromHSBValues(hsbBG[0], hsbBG[1], hsbBG[2]);
double bgBright = (1 - hsbBG[1]) * ColorUtil.getPerceivedBrightness(c);
b = bgBright <= 127.5f ? Bias.WHITE : Bias.BLACK;
}
double difference = Math.abs(fgBrightness - bgBrightness);
if (difference < minimumBrightnessDifference) {
fgBrightness += b.direction * (Math.abs(minimumBrightnessDifference - difference));
}
return new DarkColorUIResource(DarkColorModelHSB.getColorFromHSBValues(hsbFG[0], hsbFG[1], fgBrightness));
}
public enum Bias {
BACKGROUND(0),
WHITE(1),
BLACK(-1);
private final int direction;
Bias(final int direction) {
this.direction = direction;
} }
return DarkColorModelHSB.getColorFromHSBValues(hsb[0], 0, brightness);
} }
} }

2
core/src/main/java/javax/swing/text/DefaultHighlighterDark/DarkHighlightPainter.java

@ -86,7 +86,7 @@ public class DarkHighlightPainter extends DefaultHighlighter.DefaultHighlightPai
public DarkHighlightPainter(final Paint paint, final boolean rounded, final float alpha) { public DarkHighlightPainter(final Paint paint, final boolean rounded, final float alpha) {
super(null); super(null);
wrapper = new HighlighterColor(color); wrapper = new HighlighterColor(null);
arcSize = UIManager.getInt("Highlight.arc"); arcSize = UIManager.getInt("Highlight.arc");
setPaint(paint); setPaint(paint);
setRoundedEdges(rounded); setRoundedEdges(rounded);

3
core/src/test/java/ui/SettingsDemo.java

@ -57,12 +57,13 @@ public class SettingsDemo implements ComponentDemo {
} }
}); });
content.add(box, BorderLayout.SOUTH); content.add(box, BorderLayout.SOUTH);
return content; return content;
} }
@Override @Override
public Icon getFrameIcon() { public Icon getFrameIcon() {
return ThemeSettings.getInstance().getIcon(); return ThemeSettings.getIcon();
} }
@Override @Override

14
utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java

@ -95,9 +95,15 @@ public final class ColorUtil {
return toAlpha(color, 255); return toAlpha(color, 255);
} }
public static double getBrightness(final Color c) { /**
return Math.sqrt(0.241 * c.getRed() * c.getRed() * Calculate the perceived brightness of a color.
+ 0.691 * c.getGreen() * c.getGreen() *
+ 0.068 * c.getBlue() * c.getBlue()); * @param c the color.
* @return the brightness from 0 to 255.
*/
public static double getPerceivedBrightness(final Color c) {
return Math.sqrt(0.299 * c.getRed() * c.getRed()
+ 0.587 * c.getGreen() * c.getGreen()
+ 0.114 * c.getBlue() * c.getBlue());
} }
} }

14
utils/src/main/java/com/github/weisj/darklaf/util/LazyValue.java

@ -24,6 +24,8 @@
*/ */
package com.github.weisj.darklaf.util; package com.github.weisj.darklaf.util;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
public class LazyValue<T> { public class LazyValue<T> {
@ -52,6 +54,18 @@ public class LazyValue<T> {
return value; return value;
} }
public void ifPresent(final Consumer<T> action) {
if (isInitialized() && value != null) {
action.accept(value);
}
}
public void ifPresentNullable(final Consumer<Optional<T>> action) {
if (isInitialized()) {
action.accept(Optional.ofNullable(value));
}
}
public T get() { public T get() {
if (value == null) value = load(); if (value == null) value = load();
return value; return value;

Loading…
Cancel
Save