From 08563fc589c8d004568765eea1f17076151456bd Mon Sep 17 00:00:00 2001 From: Marco Rebhan Date: Tue, 6 Apr 2021 15:30:20 +0200 Subject: Fix visual inconsistencies after changing theme settings. Closes #376. --- .../src/main/java/cuchaz/enigma/gui/Gui.java | 8 +------- .../src/main/java/cuchaz/enigma/gui/Main.java | 4 ++++ .../main/java/cuchaz/enigma/gui/config/Themes.java | 9 +++++--- .../java/cuchaz/enigma/gui/config/UiConfig.java | 24 ++++++++++++++-------- 4 files changed, 27 insertions(+), 18 deletions(-) (limited to 'enigma-swing/src/main/java/cuchaz/enigma') 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 007af64..ce4823e 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java @@ -117,8 +117,6 @@ public class Gui implements LanguageChangeListener { private final HashBiMap editors = HashBiMap.create(); public Gui(EnigmaProfile profile) { - UiConfig.getActiveLookAndFeel().setGlobalLAF(); - // init frame this.frame = new JFrame(Enigma.NAME); final Container pane = this.frame.getContentPane(); @@ -135,14 +133,10 @@ public class Gui implements LanguageChangeListener { }); } - System.setProperty("apple.laf.useScreenMenuBar", "true"); + Themes.addListener((lookAndFeel, boxHighlightPainters) -> SwingUtilities.updateComponentTreeUI(this.getFrame())); this.controller = new GuiController(this, profile); - Themes.addListener((lookAndFeel, boxHighlightPainters) -> SwingUtilities.updateComponentTreeUI(getFrame())); - - Themes.updateTheme(); - // init file choosers this.jarFileChooser = new FileDialog(getFrame(), I18n.translate("menu.file.jar.open"), FileDialog.LOAD); diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/Main.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/Main.java index 0589f36..b655bd4 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/Main.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/Main.java @@ -20,6 +20,7 @@ import com.google.common.io.MoreFiles; import joptsimple.*; import cuchaz.enigma.EnigmaProfile; +import cuchaz.enigma.gui.config.Themes; import cuchaz.enigma.gui.config.UiConfig; import cuchaz.enigma.translation.mapping.serde.MappingFormat; import cuchaz.enigma.utils.I18n; @@ -54,6 +55,9 @@ public class Main { EnigmaProfile parsedProfile = EnigmaProfile.read(options.valueOf(profile)); I18n.setLanguage(UiConfig.getLanguage()); + System.setProperty("apple.laf.useScreenMenuBar", "true"); + Themes.setupTheme(); + Gui gui = new Gui(parsedProfile); GuiController controller = gui.getController(); 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 b45236f..839a5cb 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 @@ -19,11 +19,14 @@ public class Themes { private static final Set listeners = new HashSet<>(); - public static void updateTheme() { + // Calling this after the UI is initialized (e.g. when the user changes + // theme settings) is currently not functional. + public static void setupTheme() { LookAndFeel laf = UiConfig.getActiveLookAndFeel(); laf.setGlobalLAF(); - setFonts(); - UiConfig.setLookAndFeelDefaults(laf, LookAndFeel.isDarkLaf()); + UiConfig.setLookAndFeelDefaults(UiConfig.getLookAndFeel(), LookAndFeel.isDarkLaf()); + UiConfig.snapshotConfig(); + Themes.setFonts(); UIManager.put("ScrollBar.showButtons", true); EnigmaSyntaxKit.invalidate(); DefaultSyntaxKit.initKit(); 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 9191485..8ec8720 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 @@ -25,13 +25,21 @@ public final class UiConfig { // 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; + private static ConfigSection runningSwing; static { if (!swing.existsOnDisk() && !ui.existsOnDisk()) { OldConfigImporter.doImport(); } + UiConfig.snapshotConfig(); + } + + // Saves the current configuration state so a consistent user interface can + // be provided for parts of the interface that don't support changing the + // configuration at runtime. Calling this after any UI elements are + // displayed can lead to visual glitches! + public static void snapshotConfig() { runningSwing = swing.data().copy(); } @@ -107,12 +115,12 @@ public final class UiConfig { } private static Color getThemeColorRgba(String colorName) { - ConfigSection s = runningSwing.section("Themes").section(getLookAndFeel().name()).section("Colors"); + ConfigSection s = runningSwing.section("Themes").section(getActiveLookAndFeel().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 = runningSwing.section("Themes").section(getLookAndFeel().name()).section("Colors"); + ConfigSection s = runningSwing.section("Themes").section(getActiveLookAndFeel().name()).section("Colors"); return new Color(s.getRgbColor(colorName).orElse(0)); } @@ -197,24 +205,24 @@ public final class UiConfig { } public static boolean useCustomFonts() { - return swing.data().section("Themes").section(getLookAndFeel().name()).section("Fonts").setIfAbsentBool("Use Custom", false); + return swing.data().section("Themes").section(getActiveLookAndFeel().name()).section("Fonts").setIfAbsentBool("Use Custom", false); } public static boolean activeUseCustomFonts() { - return runningSwing.section("Themes").section(getLookAndFeel().name()).section("Fonts").setIfAbsentBool("Use Custom", false); + return runningSwing.section("Themes").section(getActiveLookAndFeel().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); + swing.data().section("Themes").section(getActiveLookAndFeel().name()).section("Fonts").setBool("Use Custom", b); } public static Optional getFont(String name) { - Optional spec = swing.data().section("Themes").section(getLookAndFeel().name()).section("Fonts").getString(name); + Optional spec = swing.data().section("Themes").section(getActiveLookAndFeel().name()).section("Fonts").getString(name); return spec.map(Font::decode); } public static Optional getActiveFont(String name) { - Optional spec = runningSwing.section("Themes").section(getLookAndFeel().name()).section("Fonts").getString(name); + Optional spec = runningSwing.section("Themes").section(getActiveLookAndFeel().name()).section("Fonts").getString(name); return spec.map(Font::decode); } -- cgit v1.2.3