diff --git a/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplication.java b/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplication.java index 650254649..5c6b6e368 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplication.java +++ b/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplication.java @@ -40,8 +40,8 @@ public class FxApplication extends Application { private final Settings settings; private final Lazy mainWindow; private final Lazy preferencesWindow; + private final Lazy quitWindow; private final Provider unlockWindowBuilderProvider; - private final Provider quitWindowBuilderProvider; private final Optional trayIntegration; private final Optional appearanceProvider; private final VaultService vaultService; @@ -50,12 +50,12 @@ public class FxApplication extends Application { private final UiAppearanceListener systemInterfaceThemeListener = this::systemInterfaceThemeChanged; @Inject - FxApplication(Settings settings, Lazy mainWindow, Lazy preferencesWindow, Provider unlockWindowBuilderProvider, Provider quitWindowBuilderProvider, Optional trayIntegration, Optional appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, ObservableSet visibleStages) { + FxApplication(Settings settings, Lazy mainWindow, Lazy preferencesWindow, Provider unlockWindowBuilderProvider, Lazy quitWindow, Optional trayIntegration, Optional appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, ObservableSet visibleStages) { this.settings = settings; this.mainWindow = mainWindow; this.preferencesWindow = preferencesWindow; this.unlockWindowBuilderProvider = unlockWindowBuilderProvider; - this.quitWindowBuilderProvider = quitWindowBuilderProvider; + this.quitWindow = quitWindow; this.trayIntegration = trayIntegration; this.appearanceProvider = appearanceProvider; this.vaultService = vaultService; @@ -109,7 +109,7 @@ public class FxApplication extends Application { public void showQuitWindow(QuitResponse response) { Platform.runLater(() -> { - quitWindowBuilderProvider.get().quitResponse(response).build().showQuitWindow(); + quitWindow.get().showQuitWindow(response); LOG.debug("Showing QuitWindow"); }); } diff --git a/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java b/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java index a6297ce4a..5b4e16ffb 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java @@ -88,4 +88,8 @@ abstract class FxApplicationModule { return builder.build(); } + @Provides + static QuitComponent provideQuitComponent(QuitComponent.Builder builder) { + return builder.build(); + } } diff --git a/main/ui/src/main/java/org/cryptomator/ui/quit/QuitComponent.java b/main/ui/src/main/java/org/cryptomator/ui/quit/QuitComponent.java index 23ddf311a..e100c52e9 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/quit/QuitComponent.java +++ b/main/ui/src/main/java/org/cryptomator/ui/quit/QuitComponent.java @@ -5,7 +5,6 @@ *******************************************************************************/ package org.cryptomator.ui.quit; -import dagger.BindsInstance; import dagger.Lazy; import dagger.Subcomponent; import org.cryptomator.ui.common.FxmlFile; @@ -25,7 +24,10 @@ public interface QuitComponent { @FxmlScene(FxmlFile.QUIT) Lazy scene(); - default Stage showQuitWindow() { + QuitController controller(); + + default Stage showQuitWindow(QuitResponse response) { + controller().updateQuitRequest(response); Stage stage = window(); stage.setScene(scene().get()); stage.show(); @@ -36,9 +38,6 @@ public interface QuitComponent { @Subcomponent.Builder interface Builder { - @BindsInstance - Builder quitResponse(QuitResponse response); - QuitComponent build(); } diff --git a/main/ui/src/main/java/org/cryptomator/ui/quit/QuitController.java b/main/ui/src/main/java/org/cryptomator/ui/quit/QuitController.java index 130556ef2..12ddbbca3 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/quit/QuitController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/quit/QuitController.java @@ -16,6 +16,8 @@ import javafx.stage.Stage; import java.awt.desktop.QuitResponse; import java.util.Collection; import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; import java.util.stream.Collectors; @QuitScoped @@ -24,26 +26,40 @@ public class QuitController implements FxController { private static final Logger LOG = LoggerFactory.getLogger(QuitController.class); private final Stage window; - private final QuitResponse response; private final ObservableList unlockedVaults; private final ExecutorService executorService; private final VaultService vaultService; + private final AtomicReference quitResponse = new AtomicReference<>(); public Button lockAndQuitButton; @Inject - QuitController(@QuitWindow Stage window, QuitResponse response, ObservableList vaults, ExecutorService executorService, VaultService vaultService) { + QuitController(@QuitWindow Stage window, ObservableList vaults, ExecutorService executorService, VaultService vaultService) { this.window = window; - this.response = response; this.unlockedVaults = vaults.filtered(Vault::isUnlocked); this.executorService = executorService; this.vaultService = vaultService; + window.setOnCloseRequest(windowEvent -> cancel()); + } + + public void updateQuitRequest(QuitResponse newResponse) { + var oldResponse = quitResponse.getAndSet(newResponse); + if (oldResponse != null) { + oldResponse.cancelQuit(); + } + } + + private void respondToQuitRequest(Consumer action) { + var response = quitResponse.getAndSet(null); + if (response != null) { + action.accept(response); + } } @FXML public void cancel() { LOG.info("Quitting application canceled by user."); window.close(); - response.cancelQuit(); + respondToQuitRequest(QuitResponse::cancelQuit); } @FXML @@ -56,16 +72,16 @@ public class QuitController implements FxController { LOG.info("Locked {}", lockAllTask.getValue().stream().map(Vault::getDisplayName).collect(Collectors.joining(", "))); if (unlockedVaults.isEmpty()) { window.close(); - response.performQuit(); + respondToQuitRequest(QuitResponse::performQuit); } }); lockAllTask.setOnFailed(evt -> { LOG.warn("Locking failed", lockAllTask.getException()); lockAndQuitButton.setDisable(false); lockAndQuitButton.setContentDisplay(ContentDisplay.TEXT_ONLY); - // TODO: show force lock or force quit scene (and DO NOT cancelQuit() here!) - // see https://github.com/cryptomator/cryptomator/blob/1.4.16/main/ui/src/main/java/org/cryptomator/ui/model/Vault.java#L151-L163 - response.cancelQuit(); + // TODO: show force lock or force quit scene (and DO NOT cancelQuit() here!) (see https://github.com/cryptomator/cryptomator/pull/1416) + window.close(); + respondToQuitRequest(QuitResponse::cancelQuit); }); executorService.execute(lockAllTask); }