From cd9671588014985792a694ff8e0fc3e217a6dfda Mon Sep 17 00:00:00 2001 From: Marco Rebhan Date: Thu, 25 Mar 2021 19:23:35 +0100 Subject: Fix using runtime changed configuration for parts of the UI that don't support it yet Closes #346. --- .../java/cuchaz/enigma/gui/EnigmaSyntaxKit.java | 6 ++-- .../src/main/java/cuchaz/enigma/gui/Gui.java | 12 +++++-- .../java/cuchaz/enigma/gui/config/LookAndFeel.java | 3 +- .../main/java/cuchaz/enigma/gui/config/Themes.java | 4 +-- .../java/cuchaz/enigma/gui/config/UiConfig.java | 41 ++++++++++++++++++---- .../java/cuchaz/enigma/gui/dialog/FontDialog.java | 2 +- .../cuchaz/enigma/gui/dialog/JavadocDialog.java | 2 +- .../java/cuchaz/enigma/gui/elements/MenuBar.java | 4 +-- .../java/cuchaz/enigma/gui/util/ScaleUtil.java | 25 ++++++------- 9 files changed, 64 insertions(+), 35 deletions(-) (limited to 'enigma-swing/src/main/java') diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/EnigmaSyntaxKit.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/EnigmaSyntaxKit.java index b500e4ba..115dfb4a 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/EnigmaSyntaxKit.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/EnigmaSyntaxKit.java @@ -1,5 +1,7 @@ package cuchaz.enigma.gui; +import java.awt.Font; + import de.sciss.syntaxpane.DefaultSyntaxKit; import de.sciss.syntaxpane.components.LineNumbersRuler; import de.sciss.syntaxpane.syntaxkits.JavaSyntaxKit; @@ -7,8 +9,6 @@ import de.sciss.syntaxpane.util.Configuration; import cuchaz.enigma.gui.config.UiConfig; -import java.awt.Font; - public class EnigmaSyntaxKit extends JavaSyntaxKit { private static Configuration configuration = null; @@ -55,7 +55,7 @@ public class EnigmaSyntaxKit extends JavaSyntaxKit { configuration.put("Action.quick-find", "cuchaz.enigma.gui.QuickFindAction, menu F"); - Font editorFont = UiConfig.shouldUseCustomFonts() ? UiConfig.getEditorFont() : UiConfig.getFallbackEditorFont(); + Font editorFont = UiConfig.activeUseCustomFonts() ? UiConfig.getEditorFont() : UiConfig.getFallbackEditorFont(); configuration.put("DefaultFont", UiConfig.encodeFont(editorFont)); } diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java index c5f827ef..007af645 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java @@ -23,7 +23,10 @@ import java.util.function.Function; import javax.annotation.Nullable; import javax.swing.*; -import javax.swing.tree.*; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreeNode; +import javax.swing.tree.TreePath; import com.google.common.collect.HashBiMap; import com.google.common.collect.Lists; @@ -55,7 +58,10 @@ import cuchaz.enigma.network.packet.RemoveMappingC2SPacket; import cuchaz.enigma.network.packet.RenameC2SPacket; import cuchaz.enigma.source.Token; import cuchaz.enigma.translation.mapping.EntryRemapper; -import cuchaz.enigma.translation.representation.entry.*; +import cuchaz.enigma.translation.representation.entry.ClassEntry; +import cuchaz.enigma.translation.representation.entry.Entry; +import cuchaz.enigma.translation.representation.entry.FieldEntry; +import cuchaz.enigma.translation.representation.entry.MethodEntry; import cuchaz.enigma.utils.I18n; import cuchaz.enigma.utils.validation.ParameterizedMessage; import cuchaz.enigma.utils.validation.ValidationContext; @@ -111,7 +117,7 @@ public class Gui implements LanguageChangeListener { private final HashBiMap editors = HashBiMap.create(); public Gui(EnigmaProfile profile) { - UiConfig.getLookAndFeel().setGlobalLAF(); + UiConfig.getActiveLookAndFeel().setGlobalLAF(); // init frame this.frame = new JFrame(Enigma.NAME); diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/config/LookAndFeel.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/config/LookAndFeel.java index 3edc87a2..ab9fa2a0 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/config/LookAndFeel.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/config/LookAndFeel.java @@ -11,7 +11,6 @@ import javax.swing.plaf.metal.MetalLookAndFeel; import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.FlatLightLaf; import com.formdev.flatlaf.FlatSystemProperties; -import cuchaz.enigma.gui.util.ScaleUtil; public enum LookAndFeel { DEFAULT(false), @@ -37,7 +36,7 @@ public enum LookAndFeel { public void setGlobalLAF() { // Configure FlatLaf's UI scale to be our scale factor. // This is also used for the SVG icons, so it applies even when some other LaF is active. - System.setProperty(FlatSystemProperties.UI_SCALE, Float.toString(ScaleUtil.getScaleFactor())); + System.setProperty(FlatSystemProperties.UI_SCALE, Float.toString(UiConfig.getActiveScaleFactor())); try { switch (this) { diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/config/Themes.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/config/Themes.java index 4905b1c5..fe8d863e 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/config/Themes.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/config/Themes.java @@ -20,7 +20,7 @@ public class Themes { private static final Set listeners = new HashSet<>(); public static void updateTheme() { - LookAndFeel laf = UiConfig.getLookAndFeel(); + LookAndFeel laf = UiConfig.getActiveLookAndFeel(); laf.setGlobalLAF(); setFonts(); UiConfig.setLookAndFeelDefaults(laf, LookAndFeel.isDarkLaf()); @@ -34,7 +34,7 @@ public class Themes { } private static void setFonts() { - if (UiConfig.shouldUseCustomFonts()) { + if (UiConfig.activeUseCustomFonts()) { Font small = UiConfig.getSmallFont(); Font bold = UiConfig.getDefaultFont(); Font normal = UiConfig.getDefault2Font(); diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java index e4adadf1..9191485a 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java @@ -19,10 +19,20 @@ public final class UiConfig { // Swing specific configuration such as theming private static final ConfigContainer swing = ConfigContainer.getOrCreate("enigma/enigmaswing"); + // These are used for getting stuff that needs to stay constant for the + // runtime of the program, e.g. the current theme, because changing of these + // settings without a restart isn't implemented correctly yet. + // Don't change the values in this container with the expectation that they + // get saved, this is purely a backup of the configuration that existed at + // startup. + private static final ConfigSection runningSwing; + static { if (!swing.existsOnDisk() && !ui.existsOnDisk()) { OldConfigImporter.doImport(); } + + runningSwing = swing.data().copy(); } public static void save() { @@ -42,6 +52,10 @@ public final class UiConfig { return (float) swing.data().section("General").setIfAbsentDouble("Scale Factor", 1.0); } + public static float getActiveScaleFactor() { + return (float) runningSwing.section("General").setIfAbsentDouble("Scale Factor", 1.0); + } + public static void setScaleFactor(float scale) { swing.data().section("General").setDouble("Scale Factor", scale); } @@ -71,6 +85,10 @@ public final class UiConfig { return swing.data().section("Themes").setIfAbsentEnum(LookAndFeel::valueOf, "Current", LookAndFeel.NONE); } + public static LookAndFeel getActiveLookAndFeel() { + return runningSwing.section("Themes").setIfAbsentEnum(LookAndFeel::valueOf, "Current", LookAndFeel.NONE); + } + public static void setLookAndFeel(LookAndFeel laf) { swing.data().section("Themes").setEnum("Current", laf); } @@ -89,12 +107,12 @@ public final class UiConfig { } private static Color getThemeColorRgba(String colorName) { - ConfigSection s = swing.data().section("Themes").section(getLookAndFeel().name()).section("Colors"); + ConfigSection s = runningSwing.section("Themes").section(getLookAndFeel().name()).section("Colors"); return fromComponents(s.getRgbColor(colorName).orElse(0), s.getDouble(String.format("%s Alpha", colorName)).orElse(0)); } private static Color getThemeColorRgb(String colorName) { - ConfigSection s = swing.data().section("Themes").section(getLookAndFeel().name()).section("Colors"); + ConfigSection s = runningSwing.section("Themes").section(getLookAndFeel().name()).section("Colors"); return new Color(s.getRgbColor(colorName).orElse(0)); } @@ -178,10 +196,14 @@ public final class UiConfig { return getThemeColorRgb("Line Numbers Selected"); } - public static boolean shouldUseCustomFonts() { + public static boolean useCustomFonts() { return swing.data().section("Themes").section(getLookAndFeel().name()).section("Fonts").setIfAbsentBool("Use Custom", false); } + public static boolean activeUseCustomFonts() { + return runningSwing.section("Themes").section(getLookAndFeel().name()).section("Fonts").setIfAbsentBool("Use Custom", false); + } + public static void setUseCustomFonts(boolean b) { swing.data().section("Themes").section(getLookAndFeel().name()).section("Fonts").setBool("Use Custom", b); } @@ -191,12 +213,17 @@ public final class UiConfig { return spec.map(Font::decode); } + public static Optional getActiveFont(String name) { + Optional spec = runningSwing.section("Themes").section(getLookAndFeel().name()).section("Fonts").getString(name); + return spec.map(Font::decode); + } + public static void setFont(String name, Font font) { swing.data().section("Themes").section(getLookAndFeel().name()).section("Fonts").setString(name, encodeFont(font)); } public static Font getDefaultFont() { - return getFont("Default").orElseGet(() -> ScaleUtil.scaleFont(Font.decode(Font.DIALOG).deriveFont(Font.BOLD))); + return getActiveFont("Default").orElseGet(() -> ScaleUtil.scaleFont(Font.decode(Font.DIALOG).deriveFont(Font.BOLD))); } public static void setDefaultFont(Font font) { @@ -204,7 +231,7 @@ public final class UiConfig { } public static Font getDefault2Font() { - return getFont("Default 2").orElseGet(() -> ScaleUtil.scaleFont(Font.decode(Font.DIALOG))); + return getActiveFont("Default 2").orElseGet(() -> ScaleUtil.scaleFont(Font.decode(Font.DIALOG))); } public static void setDefault2Font(Font font) { @@ -212,7 +239,7 @@ public final class UiConfig { } public static Font getSmallFont() { - return getFont("Small").orElseGet(() -> ScaleUtil.scaleFont(Font.decode(Font.DIALOG))); + return getActiveFont("Small").orElseGet(() -> ScaleUtil.scaleFont(Font.decode(Font.DIALOG))); } public static void setSmallFont(Font font) { @@ -220,7 +247,7 @@ public final class UiConfig { } public static Font getEditorFont() { - return getFont("Editor").orElseGet(UiConfig::getFallbackEditorFont); + return getActiveFont("Editor").orElseGet(UiConfig::getFallbackEditorFont); } public static void setEditorFont(Font font) { diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/FontDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/FontDialog.java index de019adb..1db8aea2 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/FontDialog.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/FontDialog.java @@ -43,7 +43,7 @@ public class FontDialog extends JDialog { public FontDialog(Frame owner) { super(owner, "Fonts", true); - this.customCheckBox.setSelected(UiConfig.shouldUseCustomFonts()); + this.customCheckBox.setSelected(UiConfig.useCustomFonts()); this.entries.setPreferredSize(ScaleUtil.getDimension(100, 0)); diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/JavadocDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/JavadocDialog.java index 8a5a3766..0e6d374c 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/JavadocDialog.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/JavadocDialog.java @@ -79,7 +79,7 @@ public class JavadocDialog { } } }); - this.text.setFont(UiConfig.shouldUseCustomFonts() ? UiConfig.getEditorFont() : UiConfig.getFallbackEditorFont()); + this.text.setFont(UiConfig.activeUseCustomFonts() ? UiConfig.getEditorFont() : UiConfig.getFallbackEditorFont()); // buttons panel JPanel buttonsPanel = new JPanel(); diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java index d5d657dc..f7f42a58 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/MenuBar.java @@ -270,7 +270,7 @@ public class MenuBar { private void onCustomScaleClicked() { String answer = (String) JOptionPane.showInputDialog(this.gui.getFrame(), I18n.translate("menu.view.scale.custom.title"), I18n.translate("menu.view.scale.custom.title"), - JOptionPane.QUESTION_MESSAGE, null, null, Float.toString(ScaleUtil.getScaleFactor() * 100)); + JOptionPane.QUESTION_MESSAGE, null, null, Float.toString(UiConfig.getScaleFactor() * 100)); if (answer == null) return; float newScale = 1.0f; try { @@ -455,7 +455,7 @@ public class MenuBar { }) .collect(Collectors.toMap(x -> x.a, x -> x.b)); - JRadioButtonMenuItem currentScaleButton = scaleButtons.get(ScaleUtil.getScaleFactor()); + JRadioButtonMenuItem currentScaleButton = scaleButtons.get(UiConfig.getScaleFactor()); if (currentScaleButton != null) { currentScaleButton.setSelected(true); } diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java index 78de07dc..28e37693 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java @@ -24,12 +24,8 @@ public class ScaleUtil { private static List listeners = new ArrayList<>(); - public static float getScaleFactor() { - return UiConfig.getScaleFactor(); - } - public static void setScaleFactor(float scaleFactor) { - float oldScale = getScaleFactor(); + float oldScale = UiConfig.getScaleFactor(); float clamped = Math.min(Math.max(0.25f, scaleFactor), 10.0f); UiConfig.setScaleFactor(clamped); rescaleFontInConfig("Default", oldScale); @@ -61,28 +57,29 @@ public class ScaleUtil { } public static Font scaleFont(Font font) { - return createTweakerForCurrentLook(getScaleFactor()).modifyFont("", font); + return createTweakerForCurrentLook(UiConfig.getActiveScaleFactor()).modifyFont("", font); } private static void rescaleFontInConfig(String name, float oldScale) { UiConfig.getFont(name).ifPresent(font -> UiConfig.setFont(name, rescaleFont(font, oldScale))); } - public static Font rescaleFont(Font font, float oldScale) { - float newSize = Math.round(font.getSize() / oldScale * getScaleFactor()); + // This does not use the font that's currently active in the UI! + private static Font rescaleFont(Font font, float oldScale) { + float newSize = Math.round(font.getSize() / oldScale * UiConfig.getScaleFactor()); return font.deriveFont(newSize); } public static float scale(float f) { - return f * getScaleFactor(); + return f * UiConfig.getActiveScaleFactor(); } public static float invert(float f) { - return f / getScaleFactor(); + return f / UiConfig.getActiveScaleFactor(); } public static int scale(int i) { - return (int) (i * getScaleFactor()); + return (int) (i * UiConfig.getActiveScaleFactor()); } public static Border createEmptyBorder(int top, int left, int bottom, int right) { @@ -90,13 +87,13 @@ public class ScaleUtil { } public static int invert(int i) { - return (int) (i / getScaleFactor()); + return (int) (i / UiConfig.getActiveScaleFactor()); } public static void applyScaling() { - float scale = getScaleFactor(); + float scale = UiConfig.getActiveScaleFactor(); - if (UiConfig.getLookAndFeel().needsScaling()) { + if (UiConfig.getActiveLookAndFeel().needsScaling()) { UiDefaultsScaler.updateAndApplyGlobalScaling((int) (100 * scale), true); } -- cgit v1.2.3