mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-05-21 04:01:27 +00:00
Refactor FxApplicationStyle class
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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<? extends UiTheme> observable, @SuppressWarnings("unused") UiTheme oldValue, UiTheme newValue) {
|
||||
if (appearanceProvider.isPresent() && oldValue == UiTheme.AUTOMATIC && newValue != UiTheme.AUTOMATIC) {
|
||||
private void appThemeChanged(@SuppressWarnings("unused") ObservableValue<? extends UiTheme> 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<Theme> appliedThemeProperty() {
|
||||
public ObjectProperty<Theme> appliedAppThemeProperty() {
|
||||
return appliedTheme;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user