Skip to content

Commit

Permalink
also confirm closing when using the OS close window button (x)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirleyNekoDev committed Mar 10, 2024
1 parent 1909575 commit 4b990e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
16 changes: 10 additions & 6 deletions chunky/src/java/se/llbit/chunky/ui/ChunkyFx.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import javafx.application.Application;
import javafx.application.HostServices;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -44,10 +43,12 @@ public class ChunkyFx extends Application {

private static HostServices hostServices = null;
private static Chunky chunkyInstance;
private static Stage mainStage = null;

@Override public void start(Stage stage) {
try {
ChunkyFx.hostServices = this.getHostServices();
mainStage = stage;

FXMLLoader loader = new FXMLLoader(getClass().getResource("Chunky.fxml"));
ChunkyFxController controller = new ChunkyFxController(stage, chunkyInstance);
Expand All @@ -56,11 +57,6 @@ public class ChunkyFx extends Application {
Scene scene = new Scene(root);
stage.setScene(scene);
stage.getIcons().add(Icons.CHUNKY_ICON);
stage.setOnCloseRequest(event -> {
PersistentSettings.setWindowPosition(new WindowPosition(stage));
Platform.exit();
System.exit(0);
});
File stylesheet = new File(SettingsDirectory.getSettingsDirectory(), "style.css");
if (stylesheet.isFile()) {
scene.getStylesheets().add(stylesheet.toURI().toURL().toExternalForm());
Expand All @@ -81,6 +77,14 @@ public class ChunkyFx extends Application {
}
}

@Override
public void stop() throws Exception {
if(mainStage != null) {
PersistentSettings.setWindowPosition(new WindowPosition(mainStage));
}
System.exit(0);
}

public static void startChunkyUI(Chunky chunkyInstance) {
ChunkyFx.chunkyInstance = chunkyInstance;
launch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
Expand Down Expand Up @@ -322,6 +323,9 @@ public void exportMapView() {
}

@Override public void initialize(URL fxmlUrl, ResourceBundle resources) {
stage.setOnCloseRequest(this::confirmAndClose);
menuExit.setOnAction(this::confirmAndClose);

scene = chunky.getSceneManager().getScene();
renderController = chunky.getRenderController();
renderManager = renderController.getRenderManager();
Expand Down Expand Up @@ -681,24 +685,6 @@ public File getSceneFile(String fileName) {
mapView.setYMax(256);
}

menuExit.setOnAction(event -> {
Alert confirmQuit = Dialogs.createAlert(Alert.AlertType.CONFIRMATION);
confirmQuit.setTitle("Quit Chunky");
confirmQuit.setHeaderText("You have unsaved changes in your scene.");
confirmQuit.setContentText("Do you want to quit chunky without saving?\nAll unsaved changes will be lost.");
confirmQuit.getButtonTypes().setAll(
new ButtonType("Quit Chunky", ButtonBar.ButtonData.YES),
ButtonType.CANCEL
);
Dialogs.setDefaultButton(confirmQuit, ButtonType.CANCEL);
confirmQuit.showAndWait()
.filter((bt) -> bt.getButtonData() == ButtonBar.ButtonData.YES)
.ifPresent((bt) -> {
Platform.exit();
System.exit(0);
});
});

canvas = new RenderCanvasFx(this, chunky.getSceneManager().getScene(),
chunky.getRenderController().getRenderManager());
canvas.setRenderListener(renderTracker);
Expand Down Expand Up @@ -747,6 +733,24 @@ public File getSceneFile(String fileName) {
PersistentSettings.setSppTargetDefault(scene.getTargetSpp()));
}

public void confirmAndClose(Event event) {
Alert confirmQuit = Dialogs.createAlert(Alert.AlertType.CONFIRMATION);
confirmQuit.setTitle("Quit Chunky");
confirmQuit.setHeaderText("You have unsaved changes in your scene.");
confirmQuit.setContentText("Do you want to quit chunky without saving?\nAll unsaved changes will be lost.");
confirmQuit.getButtonTypes().setAll(
new ButtonType("Quit Chunky", ButtonBar.ButtonData.YES),
ButtonType.CANCEL
);
Dialogs.setDefaultButton(confirmQuit, ButtonType.CANCEL);
ButtonType result = confirmQuit.showAndWait().orElse(ButtonType.CANCEL);
if(result.getButtonData() == ButtonBar.ButtonData.YES) {
Platform.exit();
} else {
event.consume();
}
}

public void openSceneChooser() {
try {
if (this.sceneChooser == null) {
Expand Down

0 comments on commit 4b990e4

Please sign in to comment.