From 4779bbf415e849b5fb3cf72197e827575678c0b1 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Tue, 24 Nov 2020 15:04:09 +0100 Subject: [PATCH] Don't bother FxApplication with stuff that is meant to be dealt with internally within QuitComponent --- .../cryptomator/ui/fxapp/FxApplication.java | 8 ++---- .../ui/fxapp/FxApplicationModule.java | 8 ------ .../cryptomator/ui/quit/QuitComponent.java | 18 +++++-------- .../cryptomator/ui/quit/QuitController.java | 27 ++++++++++++++----- .../org/cryptomator/ui/quit/QuitModule.java | 6 ----- 5 files changed, 28 insertions(+), 39 deletions(-) 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 b05a6687f..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 @@ -31,7 +31,6 @@ import javafx.collections.ObservableSet; import javafx.stage.Stage; import java.awt.desktop.QuitResponse; import java.util.Optional; -import java.util.concurrent.atomic.AtomicReference; @FxApplicationScoped public class FxApplication extends Application { @@ -49,10 +48,9 @@ public class FxApplication extends Application { private final LicenseHolder licenseHolder; private final BooleanBinding hasVisibleStages; private final UiAppearanceListener systemInterfaceThemeListener = this::systemInterfaceThemeChanged; - private final AtomicReference quitResponse; @Inject - FxApplication(Settings settings, Lazy mainWindow, Lazy preferencesWindow, Provider unlockWindowBuilderProvider, Lazy quitWindow, Optional trayIntegration, Optional appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, ObservableSet visibleStages, AtomicReference quitResponse) { + 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; @@ -63,7 +61,6 @@ public class FxApplication extends Application { this.vaultService = vaultService; this.licenseHolder = licenseHolder; this.hasVisibleStages = Bindings.isNotEmpty(visibleStages); - this.quitResponse = quitResponse; } public void start() { @@ -111,9 +108,8 @@ public class FxApplication extends Application { } public void showQuitWindow(QuitResponse response) { - quitResponse.set(response); Platform.runLater(() -> { - quitWindow.get().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 549ea4536..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 @@ -22,23 +22,15 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableSet; import javafx.scene.image.Image; import javafx.stage.Stage; -import java.awt.desktop.QuitResponse; import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; import java.util.Collections; import java.util.List; -import java.util.concurrent.atomic.AtomicReference; @Module(includes = {UpdateCheckerModule.class}, subcomponents = {MainWindowComponent.class, PreferencesComponent.class, UnlockComponent.class, QuitComponent.class, ErrorComponent.class}) abstract class FxApplicationModule { - @Provides - @FxApplicationScoped - static AtomicReference provideQuitResponse() { - return new AtomicReference<>(); - } - @Provides @FxApplicationScoped static ObservableSet provideVisibleStages() { 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 eadd32052..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 @@ -10,9 +10,9 @@ import dagger.Subcomponent; import org.cryptomator.ui.common.FxmlFile; import org.cryptomator.ui.common.FxmlScene; -import javafx.beans.property.ObjectProperty; import javafx.scene.Scene; import javafx.stage.Stage; +import java.awt.desktop.QuitResponse; @QuitScoped @Subcomponent(modules = {QuitModule.class}) @@ -24,18 +24,12 @@ public interface QuitComponent { @FxmlScene(FxmlFile.QUIT) Lazy scene(); - ObjectProperty provideStageProperty(); + QuitController controller(); - default Stage showQuitWindow() { - var stageProperty = provideStageProperty(); - Stage stage; - if (stageProperty.isNull().get()) { - stage = window(); - stage.setScene(scene().get()); - stageProperty.set(stage); - } else { - stage = stageProperty.get(); - } + default Stage showQuitWindow(QuitResponse response) { + controller().updateQuitRequest(response); + Stage stage = window(); + stage.setScene(scene().get()); stage.show(); stage.requestFocus(); return stage; 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 583e9c010..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 @@ -17,6 +17,7 @@ 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 @@ -28,25 +29,37 @@ public class QuitController implements FxController { private final ObservableList unlockedVaults; private final ExecutorService executorService; private final VaultService vaultService; - private final AtomicReference quitResponse; + private final AtomicReference quitResponse = new AtomicReference<>(); public Button lockAndQuitButton; @Inject - QuitController(@QuitWindow Stage window, ObservableList vaults, ExecutorService executorService, VaultService vaultService, AtomicReference quitResponse) { + QuitController(@QuitWindow Stage window, ObservableList vaults, ExecutorService executorService, VaultService vaultService) { this.window = window; this.unlockedVaults = vaults.filtered(Vault::isUnlocked); this.executorService = executorService; this.vaultService = vaultService; - this.quitResponse = quitResponse; 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(); - quitResponse.get().cancelQuit(); - quitResponse.set(null); + respondToQuitRequest(QuitResponse::cancelQuit); } @FXML @@ -59,7 +72,7 @@ public class QuitController implements FxController { LOG.info("Locked {}", lockAllTask.getValue().stream().map(Vault::getDisplayName).collect(Collectors.joining(", "))); if (unlockedVaults.isEmpty()) { window.close(); - quitResponse.getAndSet(null).performQuit(); + respondToQuitRequest(QuitResponse::performQuit); } }); lockAllTask.setOnFailed(evt -> { @@ -68,7 +81,7 @@ public class QuitController implements FxController { lockAndQuitButton.setContentDisplay(ContentDisplay.TEXT_ONLY); // TODO: show force lock or force quit scene (and DO NOT cancelQuit() here!) (see https://github.com/cryptomator/cryptomator/pull/1416) window.close(); - quitResponse.getAndSet(null).cancelQuit(); + respondToQuitRequest(QuitResponse::cancelQuit); }); executorService.execute(lockAllTask); } diff --git a/main/ui/src/main/java/org/cryptomator/ui/quit/QuitModule.java b/main/ui/src/main/java/org/cryptomator/ui/quit/QuitModule.java index 75408165e..5047427af 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/quit/QuitModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/quit/QuitModule.java @@ -24,12 +24,6 @@ import java.util.ResourceBundle; @Module abstract class QuitModule { - @Provides - @QuitScoped - static ObjectProperty provideStageProperty() { - return new SimpleObjectProperty<>(); - } - @Provides @QuitWindow @QuitScoped