Skip to content

Commit

Permalink
Changed api for FontSizeRule.
Browse files Browse the repository at this point in the history
Added provider for theme preferences on windows.
  • Loading branch information
weisJ committed Mar 31, 2020
1 parent f866e50 commit 3ee6d18
Show file tree
Hide file tree
Showing 44 changed files with 566 additions and 124 deletions.
16 changes: 10 additions & 6 deletions core/src/main/java/com/github/weisj/darklaf/LafManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
*/
package com.github.weisj.darklaf;

import com.github.weisj.darklaf.task.DefaultsInitTask;
import com.github.weisj.darklaf.theme.*;
import com.github.weisj.darklaf.task.DefaultsAdjustmentTask;
import com.github.weisj.darklaf.theme.DarculaTheme;
import com.github.weisj.darklaf.theme.Theme;
import com.github.weisj.darklaf.theme.info.DefaultThemeProvider;
import com.github.weisj.darklaf.theme.info.PreferredThemeStyle;
import com.github.weisj.darklaf.theme.info.ThemeProvider;

import javax.swing.*;
import java.awt.*;
Expand All @@ -48,7 +52,7 @@ public final class LafManager {
private static Theme theme;
private static boolean logEnabled = false;
private static boolean decorationsOverwrite = true;
private static final Collection<DefaultsInitTask> uiDefaultsTasks = new HashSet<>();
private static final Collection<DefaultsAdjustmentTask> uiDefaultsTasks = new HashSet<>();

static {
enableLogging(true);
Expand Down Expand Up @@ -241,7 +245,7 @@ private static void updateLafRecursively(final Window window) {
*
* @param task the defaults init task.
*/
public static void registerDefaultsInitTask(final DefaultsInitTask task) {
public static void registerDefaultsAdjustmentTask(final DefaultsAdjustmentTask task) {
uiDefaultsTasks.add(task);
}

Expand All @@ -250,7 +254,7 @@ public static void registerDefaultsInitTask(final DefaultsInitTask task) {
*
* @param task the defaults init task.
*/
public static void removeDefaultsInitTask(final DefaultsInitTask task) {
public static void removeDefaultsAdjustmentTask(final DefaultsAdjustmentTask task) {
uiDefaultsTasks.remove(task);
}

Expand All @@ -259,7 +263,7 @@ public static void removeDefaultsInitTask(final DefaultsInitTask task) {
*
* @return collection of init tasks.
*/
public static Collection<DefaultsInitTask> getUserInitTasks() {
public static Collection<DefaultsAdjustmentTask> getUserAdjustmentTasks() {
return uiDefaultsTasks;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,16 @@ public static Border createWidgetLineBorder(final int top, final int left, final
return createBorder(top, left, bottom, right, lineWidgetBorderMap, "borderSecondary");
}

public static void update(final Map<Object, Object> defaults) {
Color borderColor = getColor(defaults, "border");
public static void update(final UIDefaults defaults) {
Color borderColor = defaults.getColor("border");
for (WeakReference<WeakLineBorder> border : lineBorderMap.values()) {
WeakLineBorder b = border.get();
if (b != null) b.setColor(borderColor);
}
Color borderSecondaryColor = getColor(defaults, "borderSecondary");
Color borderSecondaryColor = defaults.getColor("borderSecondary");
for (WeakReference<WeakLineBorder> border : lineWidgetBorderMap.values()) {
WeakLineBorder b = border.get();
if (b != null) b.setColor(borderSecondaryColor);
}
}

private static Color getColor(final Map<Object, Object> defaults, final String key) {
Object color = defaults.get(key);
return color instanceof Color ? (Color) color : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.Properties;

public final class Decorations {
Expand Down Expand Up @@ -84,7 +83,7 @@ public static void initialize() {
decorationsProvider.initialize();
}

public static void loadDecorationProperties(final Properties uiProps, final Map<Object, Object> defaults) {
public static void loadDecorationProperties(final Properties uiProps, final UIDefaults defaults) {
decorationsProvider.loadDecorationProperties(uiProps, defaults);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.Properties;

public class DefaultDecorationsProvider implements DecorationsProvider {
Expand Down Expand Up @@ -60,6 +59,6 @@ public void initialize() {
}

@Override
public void loadDecorationProperties(final Properties properties, final Map<Object, Object> currentDefaults) {
public void loadDecorationProperties(final Properties properties, final UIDefaults currentDefaults) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,19 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.weisj.darklaf.theme;
package com.github.weisj.darklaf.task;

public enum FontSizeRule {
DEFAULT("default"),
TINY("tiny"),
SMALLER("smaller"),
SMALL("small"),
MEDIUM("medium"),
LARGE("large"),
LARGER("larger"),
HUGE("huge");
import com.github.weisj.darklaf.theme.Theme;

private final String propertyKey;
import java.util.Properties;

FontSizeRule(final String propertyKey) {
this.propertyKey = propertyKey;
}
public interface DefaultsAdjustmentTask {

public String getPropertyKey() {
return "fontSize." + propertyKey;
}
/**
* Execute the task.
*
* @param currentTheme the current theme.
* @param properties the properties.
*/
void run(final Theme currentTheme, final Properties properties);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

import com.github.weisj.darklaf.theme.Theme;

import java.util.Map;
import javax.swing.*;

@FunctionalInterface
public interface DefaultsInitTask {

/**
Expand All @@ -36,7 +35,7 @@ public interface DefaultsInitTask {
* @param currentTheme the current theme being initialized.
* @param defaults the current defaults to work with.
*/
void run(final Theme currentTheme, final Map<Object, Object> defaults);
void run(final Theme currentTheme, final UIDefaults defaults);

/**
* Indicated that this task should only be run if the laf is actually installed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

import com.github.weisj.darklaf.DarkLaf;
import com.github.weisj.darklaf.PropertyLoader;
import com.github.weisj.darklaf.theme.FontMapper;
import com.github.weisj.darklaf.theme.FontSizeRule;
import com.github.weisj.darklaf.theme.Theme;
import com.github.weisj.darklaf.theme.info.FontSizeRule;
import com.github.weisj.darklaf.util.SystemInfo;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.UIResource;
import java.awt.*;
Expand All @@ -38,27 +38,28 @@
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

public class FontDefaultsInitTask implements DefaultsInitTask {

private static final Logger LOGGER = Logger.getLogger(FontDefaultsInitTask.class.getName());
private static final String FONT_PROPERTY_PATH = "properties/";
private static final String FONT_SIZE_DEFAULTS_NAME = "font_sizes";
private static final String FONT_DEFAULTS_NAME = "font_sizes";
private static final String FONT_DEFAULTS_NAME = "font";

private static final String MAC_OS_CATALINA_FONT_NAME = ".AppleSystemUIFont";
private static final String MAC_OS_FONT_NAME = ".SF NS Text";
private final PropertyFontMapper fontMapper = new PropertyFontMapper();

@Override
public void run(final Theme currentTheme, final Map<Object, Object> defaults) {
public void run(final Theme currentTheme, final UIDefaults defaults) {
loadFontProperties(defaults);
if (SystemInfo.isMac) {
patchMacOSFonts(defaults);
}
applyFontRule(currentTheme, defaults);
}

private void loadFontProperties(final Map<Object, Object> defaults) {
private void loadFontProperties(final UIDefaults defaults) {
Properties fontSizeProps = PropertyLoader.loadProperties(DarkLaf.class,
FONT_SIZE_DEFAULTS_NAME,
FONT_PROPERTY_PATH);
Expand All @@ -69,7 +70,7 @@ private void loadFontProperties(final Map<Object, Object> defaults) {
PropertyLoader.putProperties(fontProps, defaults);
}

private void patchMacOSFonts(final Map<Object, Object> defaults) {
private void patchMacOSFonts(final UIDefaults defaults) {
for (Map.Entry<Object, Object> entry : defaults.entrySet()) {
if (entry.getValue() instanceof Font) {
Font font = (Font) entry.getValue();
Expand All @@ -89,27 +90,30 @@ private Font macOSFontFromFont(final Font font) {
return macFont == null ? font : macFont;
}

private void applyFontRule(final Theme currentTheme, final Map<Object, Object> defaults) {
private void applyFontRule(final Theme currentTheme, final UIDefaults defaults) {
FontSizeRule rule = currentTheme.getFontSizeRule();
if (rule == null || rule == FontSizeRule.DEFAULT) return;
if (rule == null || rule.getType() == FontSizeRule.AdjustmentType.NO_ADJUSTMENT) return;
for (Map.Entry<Object, Object> entry : defaults.entrySet()) {
if (entry != null && entry.getValue() instanceof Font) {
entry.setValue(fontWithRule((Font) entry.getValue(), rule, defaults));
}
}
}

private Font fontWithRule(final Font font, final FontSizeRule rule, final Map<Object, Object> defaults) {
Font withRule = getFontMapper(rule).map(font, defaults);
private Font fontWithRule(final Font font, final FontSizeRule rule, final UIDefaults defaults) {
if (font == null || defaults == null) return font;
float size = font.getSize2D();
float newSize = rule.adjustFontSize(size, defaults);
if (newSize == size) return font;
if (newSize <= 0) {
LOGGER.warning("Font " + font + " would be invisible after applying " + rule + ". Font won't be changed!");
return font;
}
Font withRule = font.deriveFont(newSize);
if (font instanceof UIResource
&& !(withRule instanceof UIResource)) {
withRule = new FontUIResource(withRule);
}
return withRule;
}

private FontMapper getFontMapper(final FontSizeRule rule) {
fontMapper.setPropertyKey(rule == null ? null : rule.getPropertyKey());
return fontMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.weisj.darklaf.theme;
package com.github.weisj.darklaf.task;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

public interface FontMapper {

Font map(final Font font, final Map<Object, Object> defaults);
Font map(final Font font, final UIDefaults defaults);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@
import com.github.weisj.darklaf.theme.Theme;

import javax.swing.*;
import java.util.Map;

public class IdeaDefaultsInitTask implements DefaultsInitTask {
@Override
public void run(final Theme currentTheme, final Map<Object, Object> defaults) {
public void run(final Theme currentTheme, final UIDefaults defaults) {
initIdeaDefaults(defaults);
}

@SuppressWarnings({"HardCodedStringLiteral"})
private void initIdeaDefaults(final Map<Object, Object> defaults) {
private void initIdeaDefaults(final UIDefaults defaults) {
defaults.put("Table.ancestorInputMap", new UIDefaults.LazyInputMap(
new Object[]{
"ctrl C", "copy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@
import javax.swing.text.DefaultEditorKit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Map;

public class InputDefaultsInitTask implements DefaultsInitTask {

@Override
public void run(final Theme currentTheme, final Map<Object, Object> defaults) {
public void run(final Theme currentTheme, final UIDefaults defaults) {
initInputMapDefaults(defaults);
patchComboBox(new MetalLookAndFeel().getDefaults(), defaults);
}

private void initInputMapDefaults(final Map<Object, Object> defaults) {
private void initInputMapDefaults(final UIDefaults defaults) {
// Make ENTER work in JTrees
final InputMap treeInputMap = (InputMap) defaults.get("Tree.focusInputMap");
if (treeInputMap != null) {
// it's really possible. For example, GTK+ doesn't have such map
// it's really possible. For example, GTK+ doesn't have such a map.
treeInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "toggle");
}
// Cut/Copy/Paste in JTextAreas
Expand Down Expand Up @@ -99,7 +98,7 @@ private void installCutCopyPasteShortcuts(final InputMap inputMap,
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, mask), DefaultEditorKit.cutAction);
}

private static void patchComboBox(final UIDefaults metalDefaults, final Map<Object, Object> defaults) {
private static void patchComboBox(final UIDefaults metalDefaults, final UIDefaults defaults) {
defaults.remove("ComboBox.ancestorInputMap");
defaults.remove("ComboBox.actionMap");
defaults.put("ComboBox.ancestorInputMap", metalDefaults.get("ComboBox.ancestorInputMap"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
import com.github.weisj.darklaf.util.SystemInfo;

import javax.swing.*;
import java.util.Map;

public class PlatformDefaultsInitTask implements DefaultsInitTask {
@Override
public void run(final Theme currentTheme, final Map<Object, Object> defaults) {
public void run(final Theme currentTheme, final UIDefaults defaults) {
String key = DarkPopupMenuUI.KEY_DEFAULT_LIGHTWEIGHT_POPUPS;
if (SystemInfo.isWindows10 && Decorations.isCustomDecorationSupported()) {
JPopupMenu.setDefaultLightWeightPopupEnabled(Boolean.TRUE.equals(defaults.get(key + ".windows")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
package com.github.weisj.darklaf.task;

import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.theme.FontMapper;
import com.github.weisj.darklaf.theme.Theme;

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.logging.Logger;

public class PropertyFontMapper implements FontMapper {
Expand All @@ -52,7 +51,7 @@ public void setPropertyKey(final String propertyKey) {
}

@Override
public Font map(final Font font, final Map<Object, Object> defaults) {
public Font map(final Font font, final UIDefaults defaults) {
if (propertyKey == null) return font;
adjustment = getSize(defaults);
// No need to create a new font.
Expand All @@ -67,7 +66,7 @@ public Font map(final Font font, final Map<Object, Object> defaults) {
return font.deriveFont(font.getSize2D() + adjustment);
}

private int getSize(final Map<Object, Object> defaults) {
private int getSize(final UIDefaults defaults) {
// Use cached value if already queried.
if (lastTheme == LafManager.getTheme()) return adjustment;
lastTheme = LafManager.getTheme();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.github.weisj.darklaf.PropertyLoader;
import com.github.weisj.darklaf.theme.Theme;

import java.util.Map;
import javax.swing.*;
import java.util.Properties;

public class SystemDefaultsInitTask implements DefaultsInitTask {
Expand All @@ -36,11 +36,11 @@ public class SystemDefaultsInitTask implements DefaultsInitTask {
private static final String OVERWRITES_NAME = "overwrites";

@Override
public void run(final Theme currentTheme, final Map<Object, Object> defaults) {
public void run(final Theme currentTheme, final UIDefaults defaults) {
loadSystemOverwrites(defaults);
}

private void loadSystemOverwrites(final Map<Object, Object> defaults) {
private void loadSystemOverwrites(final UIDefaults defaults) {
Properties overwrites = PropertyLoader.loadProperties(DarkLaf.class, OVERWRITES_NAME, OVERWRITES_PATH);
overwrites.values().removeIf(v -> System.getProperty(DarkLaf.SYSTEM_PROPERTY_PREFIX + v.toString()) == null);
overwrites.entrySet().forEach(
Expand Down
Loading

0 comments on commit 3ee6d18

Please sign in to comment.