From df925c935cdd287e7992da1475555d1831a125a1 Mon Sep 17 00:00:00 2001 From: Danil Sokolov Date: Sun, 22 Nov 2026 11:09:53 +0000 Subject: [PATCH] Keep preferred theme after look and feel change PlafManager.installPreferredTheme cleared UI_DEFAULTS, which also holds the preferred theme name, so the next look and feel change fell back to the system theme. Fixes #43 --- .../java/org/flexdock/plaf/PlafManager.java | 4 +++ .../test/view/ThemeConfigurationTest.java | 25 +++++++++++++++++++ release-notes.txt | 1 + 3 files changed, 30 insertions(+) diff --git a/flexdock-view/src/main/java/org/flexdock/plaf/PlafManager.java b/flexdock-view/src/main/java/org/flexdock/plaf/PlafManager.java index 712da819..d0884982 100644 --- a/flexdock-view/src/main/java/org/flexdock/plaf/PlafManager.java +++ b/flexdock-view/src/main/java/org/flexdock/plaf/PlafManager.java @@ -108,7 +108,11 @@ public static void setPreferredTheme(String themeName, boolean reload) { public static void installPreferredTheme(boolean update) { Theme theme = getPreferredTheme(); + // UI_DEFAULTS also holds the preferred theme name, which must survive + // the next look and feel change + String themeName = (String) UI_DEFAULTS.get(PREFERRED_THEME_KEY); UI_DEFAULTS.clear(); + setProperty(PREFERRED_THEME_KEY, themeName); setProperty(View.class, theme.getViewUI()); setProperty(Titlebar.class, theme.getTitlebarUI()); setProperty(Button.class, theme.getButtonUI()); diff --git a/flexdock-view/src/test/java/org/flexdock/test/view/ThemeConfigurationTest.java b/flexdock-view/src/test/java/org/flexdock/test/view/ThemeConfigurationTest.java index 0506f97b..96ac5b98 100644 --- a/flexdock-view/src/test/java/org/flexdock/test/view/ThemeConfigurationTest.java +++ b/flexdock-view/src/test/java/org/flexdock/test/view/ThemeConfigurationTest.java @@ -4,6 +4,8 @@ import org.flexdock.plaf.PlafManager; import org.flexdock.plaf.theme.Theme; import org.flexdock.plaf.theme.UIFactory; +import org.flexdock.plaf.theme.ViewUI; +import org.flexdock.view.View; import org.junit.jupiter.api.Test; import javax.swing.LookAndFeel; @@ -27,6 +29,12 @@ private static Properties createProperties() { return properties; } + private static String getInstalledViewUiName() { + ViewUI ui = (ViewUI) PlafManager.getUI(new View("theme.view", "View")); + + return ui.getCreationParameters().getName(); + } + @Test void testDefaultPrefsDefineNamedUis() { assertTrue(Configurator.getNamedElementsByTagName(UIFactory.VIEW_KEY).containsKey("default")); @@ -81,4 +89,21 @@ void testCustomThemeUnderUnmappedLookAndFeel() throws Exception { } } + @Test + void testPreferredThemeSurvivesLookAndFeelChange() throws Exception { + LookAndFeel original = UIManager.getLookAndFeel(); + try { + PlafManager.setPreferredTheme("officeXP"); + assertEquals("officeXP", getInstalledViewUiName()); + + // A new instance, so the look and feel change event fires and the theme is reinstalled + UIManager.setLookAndFeel(new MetalLookAndFeel()); + + assertEquals("officeXP", getInstalledViewUiName()); + } finally { + PlafManager.setPreferredTheme((String) null); + UIManager.setLookAndFeel(original); + } + } + } diff --git a/release-notes.txt b/release-notes.txt index a3d2bafa..11ac81fa 100644 --- a/release-notes.txt +++ b/release-notes.txt @@ -16,6 +16,7 @@ Fixes: * PerspectiveManager: ignore detached docking ports when finding the main one * DefaultDockingPort: clear() also clears the nested docking ports * DockingStateListener: validate pending layout before capturing docking state +* PlafManager: keep the preferred theme after a look and feel change * DockingSplitPane: track the divider percentage from the first drag * DockingSplitPane: apply the percentage consistently in setDividerLocation() * DockingSplitPane: keep the percentage set before the size is known