From e3433cb312b5128d994132448ae6d2ba969f3236 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Tue, 10 Feb 2026 18:17:09 +0100 Subject: [PATCH] Refactor FxApplicationStyle class --- .../ChooseExistingVaultController.java | 2 +- .../ui/fxapp/FxApplicationStyle.java | 106 ++++++++---------- 2 files changed, 49 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/cryptomator/ui/addvaultwizard/ChooseExistingVaultController.java b/src/main/java/org/cryptomator/ui/addvaultwizard/ChooseExistingVaultController.java index be9ea15c7..7862c3b20 100644 --- a/src/main/java/org/cryptomator/ui/addvaultwizard/ChooseExistingVaultController.java +++ b/src/main/java/org/cryptomator/ui/addvaultwizard/ChooseExistingVaultController.java @@ -59,7 +59,7 @@ public class ChooseExistingVaultController implements FxController { this.vault = vault; this.vaultListManager = vaultListManager; this.resourceBundle = resourceBundle; - this.screenshot = applicationStyle.appliedThemeProperty().map(this::selectScreenshot); + this.screenshot = applicationStyle.appliedAppThemeProperty().map(this::selectScreenshot); } private Image selectScreenshot(Theme theme) { diff --git a/src/main/java/org/cryptomator/ui/fxapp/FxApplicationStyle.java b/src/main/java/org/cryptomator/ui/fxapp/FxApplicationStyle.java index d8b7f540d..cf524b449 100644 --- a/src/main/java/org/cryptomator/ui/fxapp/FxApplicationStyle.java +++ b/src/main/java/org/cryptomator/ui/fxapp/FxApplicationStyle.java @@ -38,86 +38,76 @@ public class FxApplicationStyle { public void initialize() { appearanceProvider.ifPresent(service -> { - if(service instanceof JfxUiAppearanceProvider fxService) { + if (service instanceof JfxUiAppearanceProvider fxService) { fxService.initialize(Platform.getPreferences()); } }); + applyTheme(settings.theme.get()); settings.theme.addListener(this::appThemeChanged); - loadSelectedStyleSheet(settings.theme.get()); } - private void appThemeChanged(@SuppressWarnings("unused") ObservableValue observable, @SuppressWarnings("unused") UiTheme oldValue, UiTheme newValue) { - if (appearanceProvider.isPresent() && oldValue == UiTheme.AUTOMATIC && newValue != UiTheme.AUTOMATIC) { + private void appThemeChanged(@SuppressWarnings("unused") ObservableValue observable, UiTheme oldValue, UiTheme newValue) { + if (oldValue == UiTheme.AUTOMATIC && newValue != UiTheme.AUTOMATIC) { + appearanceProvider.ifPresent(service -> { + try { + service.removeListener(systemInterfaceThemeListener); + } catch (UiAppearanceException e) { + LOG.warn("Failed to disable automatic theme switching."); + } + }); + } + applyTheme(newValue); + } + + private void applyTheme(UiTheme theme) { + if (!licenseHolder.isValidLicense()) { + loadAndApplyLightTheme(); + } else { + switch (theme) { + case AUTOMATIC -> registerAutomaticThemeChange(); + case LIGHT -> loadAndApplyLightTheme(); + case DARK -> loadAndApplyDarkTheme(); + } + } + } + + private void registerAutomaticThemeChange() { + appearanceProvider.ifPresent(provider -> { try { - appearanceProvider.get().removeListener(systemInterfaceThemeListener); + provider.addListener(systemInterfaceThemeListener); } catch (UiAppearanceException e) { - LOG.error("Failed to disable automatic theme switching."); + LOG.error("Failed to enable automatic theme switching."); } - } - loadSelectedStyleSheet(newValue); + systemInterfaceThemeChanged(provider.getSystemTheme()); + }); } - private void loadSelectedStyleSheet(UiTheme desiredTheme) { - UiTheme theme = licenseHolder.isValidLicense() ? desiredTheme : UiTheme.LIGHT; - switch (theme) { - case LIGHT -> applyLightTheme(); - case DARK -> applyDarkTheme(); - case AUTOMATIC -> { - appearanceProvider.ifPresent(provider -> { - try { - provider.addListener(systemInterfaceThemeListener); - } catch (UiAppearanceException e) { - LOG.error("Failed to enable automatic theme switching."); - } - }); - applySystemTheme(); - } + private void systemInterfaceThemeChanged(Theme osTheme) { + switch (osTheme) { + case LIGHT -> loadAndApplyLightTheme(); + case DARK -> loadAndApplyDarkTheme(); } } - private void systemInterfaceThemeChanged(Theme theme) { - switch (theme) { - case LIGHT -> applyLightTheme(); - case DARK -> applyDarkTheme(); - } + private void loadAndApplyLightTheme() { + loadAndApplyTheme(Theme.LIGHT, "/css/light_theme.css"); } - private void applySystemTheme() { - if (appearanceProvider.isPresent()) { - systemInterfaceThemeChanged(appearanceProvider.get().getSystemTheme()); - } else { - LOG.warn("No UiAppearanceProvider present, assuming LIGHT theme..."); - applyLightTheme(); - } + private void loadAndApplyDarkTheme() { + loadAndApplyTheme(Theme.DARK, "/css/dark_theme.css"); } - private void applyLightTheme() { - var stylesheet = Optional // - .ofNullable(getClass().getResource("/css/light_theme.bss")) // - .orElse(getClass().getResource("/css/light_theme.css")); + private void loadAndApplyTheme(Theme appTheme, String cssFile) { + var stylesheet = getClass().getResource(cssFile); if (stylesheet == null) { - LOG.warn("Failed to load light_theme stylesheet"); - } else { - Application.setUserAgentStylesheet(stylesheet.toString()); - appearanceProvider.ifPresent(provider -> provider.adjustToTheme(Theme.LIGHT)); - appliedTheme.set(Theme.LIGHT); + throw new IllegalStateException("Cannot find resource %s".formatted(cssFile)); } + Application.setUserAgentStylesheet(stylesheet.toString()); + appearanceProvider.ifPresent(provider -> provider.adjustToTheme(appTheme)); + appliedTheme.set(appTheme); } - private void applyDarkTheme() { - var stylesheet = Optional // - .ofNullable(getClass().getResource("/css/dark_theme.bss")) // - .orElse(getClass().getResource("/css/dark_theme.css")); - if (stylesheet == null) { - LOG.warn("Failed to load dark_theme stylesheet"); - } else { - Application.setUserAgentStylesheet(stylesheet.toString()); - appearanceProvider.ifPresent(provider -> provider.adjustToTheme(Theme.DARK)); - appliedTheme.set(Theme.DARK); - } - } - - public ObjectProperty appliedThemeProperty() { + public ObjectProperty appliedAppThemeProperty() { return appliedTheme; } }