Procházet zdrojové kódy

Removed @Named("trayMenuSupported") from FxApplication component

Sebastian Stenzel před 4 roky
rodič
revize
984b7a2d0e

+ 0 - 6
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();
 	}
 

+ 4 - 5
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<FxApplicationComponent> fxAppComponent;
 	private final ExecutorService executor;
 	private final AtomicBoolean started;
 	private final CompletableFuture<FxApplication> future;
-	private final boolean hasTrayIcon;
 
 	@Inject
-	public FxApplicationStarter(FxApplicationComponent.Builder fxAppComponent, ExecutorService executor, Settings settings) {
+	public FxApplicationStarter(Lazy<FxApplicationComponent> 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<FxApplication> 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);
 			});

+ 5 - 4
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<Vault> vaults;
-	private final TrayMenuComponent.Builder trayComponent;
+	private final Lazy<TrayMenuComponent> trayMenu;
 	private final FxApplicationStarter fxApplicationStarter;
 	private final AppLaunchEventHandler launchEventHandler;
 	private final Optional<TrayIntegrationProvider> trayIntegration;
 
 	@Inject
-	public UiLauncher(Settings settings, ObservableList<Vault> vaults, TrayMenuComponent.Builder trayComponent, FxApplicationStarter fxApplicationStarter, AppLaunchEventHandler launchEventHandler, Optional<TrayIntegrationProvider> trayIntegration) {
+	public UiLauncher(Settings settings, ObservableList<Vault> vaults, Lazy<TrayMenuComponent> trayMenu, FxApplicationStarter fxApplicationStarter, AppLaunchEventHandler launchEventHandler, Optional<TrayIntegrationProvider> 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);

+ 12 - 0
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<UiAppearanceProvider> provideAppearanceProvider() {

+ 11 - 11
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();
 	}
 }

+ 14 - 8
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> autoStartProvider;
 	private final ObjectProperty<SelectedPreferencesTab> 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> autoStartProvider, Set<KeychainAccessProvider> keychainAccessProviders, ObjectProperty<SelectedPreferencesTab> selectedTabProperty, LicenseHolder licenseHolder, ExecutorService executor, ResourceBundle resourceBundle, Application application, Environment environment, ErrorComponent.Builder errorComponent) {
+	GeneralPreferencesController(@PreferencesWindow Stage window, Settings settings, TrayMenuComponent trayMenu, Optional<AutoStartProvider> autoStartProvider, Set<KeychainAccessProvider> keychainAccessProviders, ObjectProperty<SelectedPreferencesTab> 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<KeychainBackend> {
@@ -196,6 +202,6 @@ public class GeneralPreferencesController implements FxController {
 		public KeychainBackend fromString(String string) {
 			throw new UnsupportedOperationException();
 		}
-	}
 
+	}
 }

+ 11 - 1
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<UiAppearanceProvider> appearanceProvider;
 	private final TrayMenuController trayMenuController;
 	private final TrayIcon trayIcon;
+	private boolean initialized;
+
 
 	@Inject
 	TrayIconController(TrayImageFactory imageFactory, TrayMenuController trayMenuController, Optional<UiAppearanceProvider> 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;
+	}
 }

+ 21 - 2
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 <code>true</code> if a tray icon can be installed
+	 */
+	default boolean isSupported() {
+		return SystemTray.isSupported();
+	}
+
+	/**
+	 * @return <code>true</code> 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();
 	}