diff --git a/src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java b/src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java index 6b19429b2..bb5af478c 100644 --- a/src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java +++ b/src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java @@ -64,17 +64,6 @@ abstract class FxApplicationModule { return builder.build(); } - @Provides - @FxApplicationScoped - static MainWindowComponent provideMainWindowComponent(MainWindowComponent.Builder builder) { - return builder.build(); - } - - @Provides - @FxApplicationScoped - static PreferencesComponent providePreferencesComponent(PreferencesComponent.Builder builder) { - return builder.build(); - } @Provides @FxApplicationScoped @@ -88,10 +77,4 @@ abstract class FxApplicationModule { return factory.create(); } - @Provides - @FxApplicationScoped - static NotificationComponent provideNotificationComponent(NotificationComponent.Factory factory) { - return factory.create(); - } - } \ No newline at end of file diff --git a/src/main/java/org/cryptomator/ui/fxapp/FxApplicationWindows.java b/src/main/java/org/cryptomator/ui/fxapp/FxApplicationWindows.java index 94c4fe330..f80b6de0e 100644 --- a/src/main/java/org/cryptomator/ui/fxapp/FxApplicationWindows.java +++ b/src/main/java/org/cryptomator/ui/fxapp/FxApplicationWindows.java @@ -39,6 +39,7 @@ import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutorService; +import java.util.function.Supplier; @FxApplicationScoped public class FxApplicationWindows { @@ -47,15 +48,15 @@ public class FxApplicationWindows { private final Stage primaryStage; private final Optional trayIntegration; - private final Lazy mainWindow; - private final Lazy preferencesWindow; + private final CachedLazy mainWindow; + private final CachedLazy preferencesWindow; private final QuitComponent.Builder quitWindowBuilder; private final UnlockComponent.Factory unlockWorkflowFactory; private final UpdateReminderComponent.Factory updateReminderWindowFactory; private final LockComponent.Factory lockWorkflowFactory; private final ErrorComponent.Factory errorWindowFactory; - private final Lazy eventViewWindow; - private final Lazy notificationWindow; + private final CachedLazy eventViewWindow; + private final CachedLazy notificationWindow; private final ExecutorService executor; private final VaultOptionsComponent.Factory vaultOptionsWindow; private final ShareVaultComponent.Factory shareVaultWindow; @@ -65,8 +66,8 @@ public class FxApplicationWindows { @Inject public FxApplicationWindows(@PrimaryStage Stage primaryStage, // Optional trayIntegration, // - Lazy mainWindow, // - Lazy preferencesWindow, // + MainWindowComponent.Builder mainWindowBuilder, // + PreferencesComponent.Builder preferencesWindowBuilder, // QuitComponent.Builder quitWindowBuilder, // UnlockComponent.Factory unlockWorkflowFactory, // UpdateReminderComponent.Factory updateReminderWindowFactory, // @@ -74,21 +75,21 @@ public class FxApplicationWindows { ErrorComponent.Factory errorWindowFactory, // VaultOptionsComponent.Factory vaultOptionsWindow, // ShareVaultComponent.Factory shareVaultWindow, // - Lazy eventViewWindow, // - Lazy notificationWindow, + EventViewComponent.Factory eventViewWindowFactory, // + NotificationComponent.Factory notificationWindowFactory, // ExecutorService executor, // Dialogs dialogs) { this.primaryStage = primaryStage; this.trayIntegration = trayIntegration; - this.mainWindow = mainWindow; - this.preferencesWindow = preferencesWindow; + this.mainWindow = new CachedLazy<>(mainWindowBuilder::build); + this.preferencesWindow = new CachedLazy<>(preferencesWindowBuilder::build); this.quitWindowBuilder = quitWindowBuilder; this.unlockWorkflowFactory = unlockWorkflowFactory; this.updateReminderWindowFactory = updateReminderWindowFactory; this.lockWorkflowFactory = lockWorkflowFactory; this.errorWindowFactory = errorWindowFactory; - this.eventViewWindow = eventViewWindow; - this.notificationWindow = notificationWindow; + this.eventViewWindow = new CachedLazy<>(eventViewWindowFactory::create); + this.notificationWindow = new CachedLazy<>(notificationWindowFactory::create); this.executor = executor; this.vaultOptionsWindow = vaultOptionsWindow; this.shareVaultWindow = shareVaultWindow; @@ -218,4 +219,29 @@ public class FxApplicationWindows { LOG.error("Failed to display stage", error); } } + + private static class CachedLazy implements Lazy { + + private final Supplier supplier; + private volatile T instance = null; + + public CachedLazy(Supplier supplier) { + this.supplier = supplier; + } + + @Override + public T get() { + T value = instance; + if (value == null) { + synchronized (this) { + value = instance; + if (value == null) { + value = supplier.get(); + instance = value; + } + } + } + return instance; + } + } }