From 984b7a2d0eb79d2457d9f08c9a0373b3ea3d69d6 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Thu, 17 Dec 2020 14:46:52 +0100 Subject: [PATCH] Removed @Named("trayMenuSupported") from FxApplication component --- .../ui/fxapp/FxApplicationComponent.java | 6 ----- .../ui/launcher/FxApplicationStarter.java | 9 ++++---- .../cryptomator/ui/launcher/UiLauncher.java | 9 ++++---- .../ui/launcher/UiLauncherModule.java | 12 ++++++++++ .../mainwindow/MainWindowTitleController.java | 22 +++++++++--------- .../GeneralPreferencesController.java | 22 +++++++++++------- .../ui/traymenu/TrayIconController.java | 12 +++++++++- .../ui/traymenu/TrayMenuComponent.java | 23 +++++++++++++++++-- 8 files changed, 78 insertions(+), 37 deletions(-) diff --git a/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplicationComponent.java b/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplicationComponent.java index 0fe031617..7d5fd55bf 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplicationComponent.java +++ b/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplicationComponent.java @@ -5,11 +5,8 @@ *******************************************************************************/ package org.cryptomator.ui.fxapp; -import dagger.BindsInstance; import dagger.Subcomponent; -import javax.inject.Named; - @FxApplicationScoped @Subcomponent(modules = FxApplicationModule.class) public interface FxApplicationComponent { @@ -19,9 +16,6 @@ public interface FxApplicationComponent { @Subcomponent.Builder interface Builder { - @BindsInstance - Builder trayMenuSupported(@Named("trayMenuSupported") boolean trayMenuSupported); - FxApplicationComponent build(); } diff --git a/main/ui/src/main/java/org/cryptomator/ui/launcher/FxApplicationStarter.java b/main/ui/src/main/java/org/cryptomator/ui/launcher/FxApplicationStarter.java index cc2122069..1632b7fc3 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/launcher/FxApplicationStarter.java +++ b/main/ui/src/main/java/org/cryptomator/ui/launcher/FxApplicationStarter.java @@ -1,5 +1,6 @@ package org.cryptomator.ui.launcher; +import dagger.Lazy; import org.cryptomator.common.settings.Settings; import org.cryptomator.ui.fxapp.FxApplication; import org.cryptomator.ui.fxapp.FxApplicationComponent; @@ -20,19 +21,17 @@ public class FxApplicationStarter { private static final Logger LOG = LoggerFactory.getLogger(FxApplicationStarter.class); - private final FxApplicationComponent.Builder fxAppComponent; + private final Lazy fxAppComponent; private final ExecutorService executor; private final AtomicBoolean started; private final CompletableFuture future; - private final boolean hasTrayIcon; @Inject - public FxApplicationStarter(FxApplicationComponent.Builder fxAppComponent, ExecutorService executor, Settings settings) { + public FxApplicationStarter(Lazy fxAppComponent, ExecutorService executor, Settings settings) { this.fxAppComponent = fxAppComponent; this.executor = executor; this.started = new AtomicBoolean(); this.future = new CompletableFuture<>(); - this.hasTrayIcon = SystemTray.isSupported() && settings.showTrayIcon().get(); } public CompletionStage get() { @@ -48,7 +47,7 @@ public class FxApplicationStarter { Platform.startup(() -> { assert Platform.isFxApplicationThread(); LOG.info("JavaFX Runtime started."); - FxApplication app = fxAppComponent.trayMenuSupported(hasTrayIcon).build().application(); + FxApplication app = fxAppComponent.get().application(); app.start(); future.complete(app); }); diff --git a/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncher.java b/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncher.java index 78bfd0a97..62e6b03f1 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncher.java +++ b/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncher.java @@ -1,5 +1,6 @@ package org.cryptomator.ui.launcher; +import dagger.Lazy; import org.cryptomator.common.settings.Settings; import org.cryptomator.common.vaults.Vault; import org.cryptomator.integrations.tray.TrayIntegrationProvider; @@ -24,16 +25,16 @@ public class UiLauncher { private final Settings settings; private final ObservableList vaults; - private final TrayMenuComponent.Builder trayComponent; + private final Lazy trayMenu; private final FxApplicationStarter fxApplicationStarter; private final AppLaunchEventHandler launchEventHandler; private final Optional trayIntegration; @Inject - public UiLauncher(Settings settings, ObservableList vaults, TrayMenuComponent.Builder trayComponent, FxApplicationStarter fxApplicationStarter, AppLaunchEventHandler launchEventHandler, Optional trayIntegration) { + public UiLauncher(Settings settings, ObservableList vaults, Lazy trayMenu, FxApplicationStarter fxApplicationStarter, AppLaunchEventHandler launchEventHandler, Optional trayIntegration) { this.settings = settings; this.vaults = vaults; - this.trayComponent = trayComponent; + this.trayMenu = trayMenu; this.fxApplicationStarter = fxApplicationStarter; this.launchEventHandler = launchEventHandler; this.trayIntegration = trayIntegration; @@ -42,7 +43,7 @@ public class UiLauncher { public void launch() { boolean hidden = settings.startHidden().get(); if (SystemTray.isSupported() && settings.showTrayIcon().get()) { - trayComponent.build().addIconToSystemTray(); + trayMenu.get().addIconToSystemTray(); launch(true, hidden); } else { launch(false, hidden); diff --git a/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncherModule.java b/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncherModule.java index f4bed076b..f85659c53 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncherModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncherModule.java @@ -19,6 +19,18 @@ import java.util.concurrent.BlockingQueue; @Module(subcomponents = {TrayMenuComponent.class, FxApplicationComponent.class}) public abstract class UiLauncherModule { + @Provides + @Singleton + static TrayMenuComponent provideTrayMenuComponent(TrayMenuComponent.Builder builder) { + return builder.build(); + } + + @Provides + @Singleton + static FxApplicationComponent provideFxApplicationComponent(FxApplicationComponent.Builder builder) { + return builder.build(); + } + @Provides @Singleton static Optional provideAppearanceProvider() { diff --git a/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowTitleController.java b/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowTitleController.java index 29b2990ce..15553cb52 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowTitleController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowTitleController.java @@ -7,13 +7,14 @@ import org.cryptomator.ui.fxapp.FxApplication; import org.cryptomator.ui.fxapp.UpdateChecker; import org.cryptomator.ui.launcher.AppLifecycleListener; import org.cryptomator.ui.preferences.SelectedPreferencesTab; +import org.cryptomator.ui.traymenu.TrayMenuComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; -import javax.inject.Named; import javafx.beans.binding.Bindings; import javafx.beans.binding.BooleanBinding; +import javafx.beans.property.ReadOnlyBooleanProperty; import javafx.fxml.FXML; import javafx.scene.layout.HBox; import javafx.stage.Stage; @@ -28,27 +29,26 @@ public class MainWindowTitleController implements FxController { private final AppLifecycleListener appLifecycle; private final Stage window; private final FxApplication application; - private final boolean isTrayIconPresent; + private final boolean trayMenuInitialized; private final UpdateChecker updateChecker; private final BooleanBinding updateAvailable; private final LicenseHolder licenseHolder; private final Settings settings; - private final BooleanBinding debugModeEnabled; private double xOffset; private double yOffset; + @Inject - MainWindowTitleController(AppLifecycleListener appLifecycle, @MainWindow Stage window, FxApplication application, @Named("trayMenuSupported") boolean isTrayIconPresent, UpdateChecker updateChecker, LicenseHolder licenseHolder, Settings settings) { + MainWindowTitleController(AppLifecycleListener appLifecycle, @MainWindow Stage window, FxApplication application, TrayMenuComponent trayMenu, UpdateChecker updateChecker, LicenseHolder licenseHolder, Settings settings) { this.appLifecycle = appLifecycle; this.window = window; this.application = application; - this.isTrayIconPresent = isTrayIconPresent; + this.trayMenuInitialized = trayMenu.isInitialized(); this.updateChecker = updateChecker; this.updateAvailable = updateChecker.latestVersionProperty().isNotNull(); this.licenseHolder = licenseHolder; this.settings = settings; - this.debugModeEnabled = Bindings.createBooleanBinding(this::isDebugModeEnabled, settings.debugMode()); } @FXML @@ -71,7 +71,7 @@ public class MainWindowTitleController implements FxController { @FXML public void close() { - if (isTrayIconPresent) { + if (trayMenuInitialized) { window.close(); } else { appLifecycle.quit(); @@ -113,14 +113,14 @@ public class MainWindowTitleController implements FxController { } public boolean isTrayIconPresent() { - return isTrayIconPresent; + return trayMenuInitialized; } - public BooleanBinding debugModeEnabledProperty() { - return debugModeEnabled; + public ReadOnlyBooleanProperty debugModeEnabledProperty() { + return settings.debugMode(); } public boolean isDebugModeEnabled() { - return settings.debugMode().get(); + return debugModeEnabledProperty().get(); } } diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java index e6445ac9c..2588f940c 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/GeneralPreferencesController.java @@ -10,11 +10,11 @@ import org.cryptomator.integrations.autostart.ToggleAutoStartFailedException; import org.cryptomator.integrations.keychain.KeychainAccessProvider; import org.cryptomator.ui.common.ErrorComponent; import org.cryptomator.ui.common.FxController; +import org.cryptomator.ui.traymenu.TrayMenuComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; -import javax.inject.Named; import javafx.application.Application; import javafx.beans.property.ObjectProperty; import javafx.beans.value.ObservableValue; @@ -27,12 +27,10 @@ import javafx.scene.control.Toggle; import javafx.scene.control.ToggleGroup; import javafx.stage.Stage; import javafx.util.StringConverter; -import java.awt.SystemTray; import java.util.Arrays; import java.util.Optional; import java.util.ResourceBundle; import java.util.Set; -import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; @PreferencesScoped @@ -42,10 +40,11 @@ public class GeneralPreferencesController implements FxController { private final Stage window; private final Settings settings; + private final boolean trayMenuInitialized; + private final boolean trayMenuSupported; private final Optional autoStartProvider; private final ObjectProperty selectedTabProperty; private final LicenseHolder licenseHolder; - private final ExecutorService executor; private final ResourceBundle resourceBundle; private final Application application; private final Environment environment; @@ -61,15 +60,17 @@ public class GeneralPreferencesController implements FxController { public RadioButton nodeOrientationLtr; public RadioButton nodeOrientationRtl; + @Inject - GeneralPreferencesController(@PreferencesWindow Stage window, Settings settings, Optional autoStartProvider, Set keychainAccessProviders, ObjectProperty selectedTabProperty, LicenseHolder licenseHolder, ExecutorService executor, ResourceBundle resourceBundle, Application application, Environment environment, ErrorComponent.Builder errorComponent) { + GeneralPreferencesController(@PreferencesWindow Stage window, Settings settings, TrayMenuComponent trayMenu, Optional autoStartProvider, Set keychainAccessProviders, ObjectProperty selectedTabProperty, LicenseHolder licenseHolder, ResourceBundle resourceBundle, Application application, Environment environment, ErrorComponent.Builder errorComponent) { this.window = window; this.settings = settings; + this.trayMenuInitialized = trayMenu.isInitialized(); + this.trayMenuSupported = trayMenu.isSupported(); this.autoStartProvider = autoStartProvider; this.keychainAccessProviders = keychainAccessProviders; this.selectedTabProperty = selectedTabProperty; this.licenseHolder = licenseHolder; - this.executor = executor; this.resourceBundle = resourceBundle; this.application = application; this.environment = environment; @@ -107,8 +108,12 @@ public class GeneralPreferencesController implements FxController { return Arrays.stream(KeychainBackend.values()).filter(value -> namesOfAvailableProviders.contains(value.getProviderClass())).toArray(KeychainBackend[]::new); } + public boolean isTrayMenuInitialized() { + return trayMenuInitialized; + } + public boolean isTrayMenuSupported() { - return SystemTray.isSupported(); + return trayMenuSupported; } public boolean isAutoStartSupported() { @@ -177,6 +182,7 @@ public class GeneralPreferencesController implements FxController { public UiTheme fromString(String string) { throw new UnsupportedOperationException(); } + } private static class KeychainBackendConverter extends StringConverter { @@ -196,6 +202,6 @@ public class GeneralPreferencesController implements FxController { public KeychainBackend fromString(String string) { throw new UnsupportedOperationException(); } - } + } } diff --git a/main/ui/src/main/java/org/cryptomator/ui/traymenu/TrayIconController.java b/main/ui/src/main/java/org/cryptomator/ui/traymenu/TrayIconController.java index 6c790ea06..0d176ebde 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/traymenu/TrayIconController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/traymenu/TrayIconController.java @@ -1,5 +1,6 @@ package org.cryptomator.ui.traymenu; +import com.google.common.base.Preconditions; import org.apache.commons.lang3.SystemUtils; import org.cryptomator.integrations.uiappearance.Theme; import org.cryptomator.integrations.uiappearance.UiAppearanceException; @@ -22,6 +23,8 @@ public class TrayIconController { private final Optional appearanceProvider; private final TrayMenuController trayMenuController; private final TrayIcon trayIcon; + private boolean initialized; + @Inject TrayIconController(TrayImageFactory imageFactory, TrayMenuController trayMenuController, Optional appearanceProvider) { @@ -31,7 +34,9 @@ public class TrayIconController { this.trayIcon = new TrayIcon(imageFactory.loadImage(), "Cryptomator", trayMenuController.getMenu()); } - public void initializeTrayIcon() { + public synchronized void initializeTrayIcon() throws IllegalStateException { + Preconditions.checkState(!initialized); + appearanceProvider.ifPresent(appearanceProvider -> { try { appearanceProvider.addListener(this::systemInterfaceThemeChanged); @@ -53,10 +58,15 @@ public class TrayIconController { } trayMenuController.initTrayMenu(); + + this.initialized = true; } private void systemInterfaceThemeChanged(Theme theme) { trayIcon.setImage(imageFactory.loadImage()); // TODO refactor "theme" is re-queried in loadImage() } + public boolean isInitialized() { + return initialized; + } } diff --git a/main/ui/src/main/java/org/cryptomator/ui/traymenu/TrayMenuComponent.java b/main/ui/src/main/java/org/cryptomator/ui/traymenu/TrayMenuComponent.java index ce94487f1..28ec8f35f 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/traymenu/TrayMenuComponent.java +++ b/main/ui/src/main/java/org/cryptomator/ui/traymenu/TrayMenuComponent.java @@ -15,8 +15,27 @@ public interface TrayMenuComponent { TrayIconController trayIconController(); - default void addIconToSystemTray() { - assert SystemTray.isSupported(); + /** + * @return true if a tray icon can be installed + */ + default boolean isSupported() { + return SystemTray.isSupported(); + } + + /** + * @return true if a tray icon has been installed + */ + default boolean isInitialized() { + return trayIconController().isInitialized(); + } + + /** + * Installs a tray icon to the system tray. + * + * @throws IllegalStateException If already added + */ + default void addIconToSystemTray() throws IllegalStateException { + assert isSupported(); trayIconController().initializeTrayIcon(); }