From f1eb9978048d09f1ae01735c9c3b01d682cd35a4 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Tue, 16 Jul 2024 17:25:47 +0200 Subject: [PATCH] make quickAccessService togglable & selectable in UI --- .../common/settings/SettingsProvider.java | 14 ++++- .../GeneralPreferencesController.java | 60 ++++++++++++++----- .../resources/fxml/preferences_general.fxml | 4 ++ 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/cryptomator/common/settings/SettingsProvider.java b/src/main/java/org/cryptomator/common/settings/SettingsProvider.java index a33b51027..dd31062fc 100644 --- a/src/main/java/org/cryptomator/common/settings/SettingsProvider.java +++ b/src/main/java/org/cryptomator/common/settings/SettingsProvider.java @@ -13,6 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.google.common.base.Suppliers; import org.cryptomator.common.Environment; +import org.cryptomator.integrations.quickaccess.QuickAccessService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,6 +27,7 @@ import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; +import java.util.List; import java.util.Optional; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; @@ -45,11 +47,13 @@ public class SettingsProvider implements Supplier { private final Supplier settings = Suppliers.memoize(this::load); private final Environment env; private final ScheduledExecutorService scheduler; + private final Optional quickAccessService; @Inject - public SettingsProvider(Environment env, ScheduledExecutorService scheduler) { + public SettingsProvider(Environment env, ScheduledExecutorService scheduler, List quickAccessServices) { this.env = env; this.scheduler = scheduler; + this.quickAccessService = quickAccessServices.stream().findFirst(); } @Override @@ -58,7 +62,13 @@ public class SettingsProvider implements Supplier { } private Settings load() { - Settings settings = env.getSettingsPath().flatMap(this::tryLoad).findFirst().orElseGet(() -> Settings.create(env)); + Settings settings = env.getSettingsPath() // + .flatMap(this::tryLoad) // + .findFirst() // + .orElseGet(() -> Settings.create(env)); + if (settings.quickAccessService.getValue() == null) { + quickAccessService.ifPresent(s -> settings.quickAccessService.setValue(s.getClass().getName())); + } settings.setSaveCmd(this::scheduleSave); return settings; } diff --git a/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java b/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java index 12fdeaab4..13e823fd0 100644 --- a/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java +++ b/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java @@ -4,7 +4,9 @@ import org.cryptomator.common.Environment; import org.cryptomator.common.settings.Settings; import org.cryptomator.integrations.autostart.AutoStartProvider; import org.cryptomator.integrations.autostart.ToggleAutoStartFailedException; +import org.cryptomator.integrations.common.NamedServiceProvider; import org.cryptomator.integrations.keychain.KeychainAccessProvider; +import org.cryptomator.integrations.quickaccess.QuickAccessService; import org.cryptomator.ui.common.FxController; import org.cryptomator.ui.fxapp.FxApplicationWindows; import org.slf4j.Logger; @@ -30,12 +32,15 @@ public class GeneralPreferencesController implements FxController { private final Stage window; private final Settings settings; private final Optional autoStartProvider; + private final List quickAccessServices; private final Application application; private final Environment environment; private final List keychainAccessProviders; private final FxApplicationWindows appWindows; public CheckBox useKeychainCheckbox; public ChoiceBox keychainBackendChoiceBox; + public CheckBox useQuickAccessCheckbox; + public ChoiceBox quickAccessServiceChoiceBox; public CheckBox startHiddenCheckbox; public CheckBox autoCloseVaultsCheckbox; public CheckBox debugModeCheckbox; @@ -43,11 +48,12 @@ public class GeneralPreferencesController implements FxController { public ToggleGroup nodeOrientation; @Inject - GeneralPreferencesController(@PreferencesWindow Stage window, Settings settings, Optional autoStartProvider, List keychainAccessProviders, Application application, Environment environment, FxApplicationWindows appWindows) { + GeneralPreferencesController(@PreferencesWindow Stage window, Settings settings, Optional autoStartProvider, List keychainAccessProviders, List quickAccessServices, Application application, Environment environment, FxApplicationWindows appWindows) { this.window = window; this.settings = settings; this.autoStartProvider = autoStartProvider; this.keychainAccessProviders = keychainAccessProviders; + this.quickAccessServices = quickAccessServices; this.application = application; this.environment = environment; this.appWindows = appWindows; @@ -60,13 +66,21 @@ public class GeneralPreferencesController implements FxController { debugModeCheckbox.selectedProperty().bindBidirectional(settings.debugMode); autoStartProvider.ifPresent(autoStart -> autoStartCheckbox.setSelected(autoStart.isEnabled())); - var keychainSettingsConverter = new KeychainProviderClassNameConverter(keychainAccessProviders); + var keychainSettingsConverter = new ServiceToSettingsConverter<>(keychainAccessProviders); keychainBackendChoiceBox.getItems().addAll(keychainAccessProviders); keychainBackendChoiceBox.setValue(keychainSettingsConverter.fromString(settings.keychainProvider.get())); keychainBackendChoiceBox.setConverter(new KeychainProviderDisplayNameConverter()); Bindings.bindBidirectional(settings.keychainProvider, keychainBackendChoiceBox.valueProperty(), keychainSettingsConverter); useKeychainCheckbox.selectedProperty().bindBidirectional(settings.useKeychain); keychainBackendChoiceBox.disableProperty().bind(useKeychainCheckbox.selectedProperty().not()); + + useQuickAccessCheckbox.selectedProperty().bindBidirectional(settings.useQuickAccess); + var quickAccessSettingsConverter = new ServiceToSettingsConverter<>(quickAccessServices); + quickAccessServiceChoiceBox.getItems().addAll(quickAccessServices); + quickAccessServiceChoiceBox.setValue(quickAccessSettingsConverter.fromString(settings.quickAccessService.get())); + quickAccessServiceChoiceBox.setConverter(new NamedServiceConverter<>()); + Bindings.bindBidirectional(settings.quickAccessService, quickAccessServiceChoiceBox.valueProperty(), quickAccessSettingsConverter); + quickAccessServiceChoiceBox.disableProperty().bind(useQuickAccessCheckbox.selectedProperty().not()); } public boolean isAutoStartSupported() { @@ -116,29 +130,47 @@ public class GeneralPreferencesController implements FxController { } - private static class KeychainProviderClassNameConverter extends StringConverter { - - private final List keychainAccessProviders; - - public KeychainProviderClassNameConverter(List keychainAccessProviders) { - this.keychainAccessProviders = keychainAccessProviders; - } + private static class NamedServiceConverter extends StringConverter { @Override - public String toString(KeychainAccessProvider provider) { - if (provider == null) { + public String toString(T namedService) { + if (namedService == null) { return null; } else { - return provider.getClass().getName(); + return namedService.getName(); } } @Override - public KeychainAccessProvider fromString(String string) { + public T fromString(String string) { + throw new UnsupportedOperationException(); + } + + } + + private static class ServiceToSettingsConverter extends StringConverter { + + private final List services; + + public ServiceToSettingsConverter(List services) { + this.services = services; + } + + @Override + public String toString(T service) { + if (service == null) { + return null; + } else { + return service.getClass().getName(); + } + } + + @Override + public T fromString(String string) { if (string == null) { return null; } else { - return keychainAccessProviders.stream().filter(provider -> provider.getClass().getName().equals(string)).findAny().orElse(null); + return services.stream().filter(provider -> provider.getClass().getName().equals(string)).findAny().orElse(null); } } } diff --git a/src/main/resources/fxml/preferences_general.fxml b/src/main/resources/fxml/preferences_general.fxml index 7f7954fc8..afc5ad35b 100644 --- a/src/main/resources/fxml/preferences_general.fxml +++ b/src/main/resources/fxml/preferences_general.fxml @@ -30,6 +30,10 @@ + + + +