Skip to content

Commit

Permalink
Add "Quit Chunky" confirmation dialog (#1703)
Browse files Browse the repository at this point in the history
* move Dialog utils into single class

* add close confirmation dialog (not yet tracking unsaved changes)

* also confirm closing when using the OS close window button `(x)`

* Change wording until #1709 is fixed

Co-authored-by: Maik Marschner <[email protected]>

---------

Co-authored-by: Maik Marschner <[email protected]>
  • Loading branch information
Maximilian Stiede and leMaik authored Mar 15, 2024
1 parent b84665d commit a646ef5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 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,11 +685,6 @@ public File getSceneFile(String fileName) {
mapView.setYMax(256);
}

menuExit.setOnAction(event -> {
Platform.exit();
System.exit(0);
});

canvas = new RenderCanvasFx(this, chunky.getSceneManager().getScene(),
chunky.getRenderController().getRenderManager());
canvas.setRenderListener(renderTracker);
Expand Down Expand Up @@ -734,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 may 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 Expand Up @@ -820,7 +837,7 @@ private void requestRenderReset() {
ButtonType.CANCEL
);
confirmReset.setTitle("Reset render to apply setting changes?");
DialogUtils.setupDialogDesign(confirmReset, mapCanvas.getScene());
Dialogs.setupDialogDesign(confirmReset, mapCanvas.getScene());

ButtonType resultAction = confirmReset
.showAndWait()
Expand Down
24 changes: 0 additions & 24 deletions chunky/src/java/se/llbit/chunky/ui/dialogs/DialogUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import se.llbit.chunky.renderer.scene.PlayerModel;
import se.llbit.chunky.renderer.scene.Scene;
import se.llbit.chunky.ui.DoubleTextField;
import se.llbit.chunky.ui.dialogs.DialogUtils;
import se.llbit.chunky.ui.dialogs.ValidatingTextInputDialog;
import se.llbit.chunky.ui.elements.AngleAdjuster;
import se.llbit.chunky.ui.DoubleAdjuster;
Expand All @@ -55,6 +54,7 @@
import se.llbit.chunky.ui.render.RenderControlsTab;
import se.llbit.chunky.world.material.BeaconBeamMaterial;
import se.llbit.fx.LuxColorPicker;
import se.llbit.fxutil.Dialogs;
import se.llbit.json.Json;
import se.llbit.json.JsonArray;
import se.llbit.json.JsonObject;
Expand Down Expand Up @@ -239,7 +239,7 @@ private void updateEntity(Entity entity) {
playerIdentifierInput.setTitle("Input player identifier");
playerIdentifierInput.setHeaderText("Please enter the UUID or name of the player.");
playerIdentifierInput.setContentText("UUID / player name:");
DialogUtils.setupDialogDesign(playerIdentifierInput, getScene());
Dialogs.setupDialogDesign(playerIdentifierInput, getScene());
playerIdentifierInput.showAndWait().map(playerIdentifier -> {
try {
// TODO: refactor this (deduplicate code, check UUID format, trim input, better error handling)
Expand Down
27 changes: 27 additions & 0 deletions chunky/src/java/se/llbit/fxutil/Dialogs.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package se.llbit.fxutil;

import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Dialog;
Expand Down Expand Up @@ -42,6 +44,31 @@ public static Dialog<ButtonType> createSpecialApprovalConfirmation(
);
}

/**
* Init design of the dialog to the design of the main window.
* This sets the icon and color scheme.
*/
public static void setupDialogDesign(Dialog<?> dialog, Scene mainScene) {
Window mainWindow = mainScene.getWindow();
if(mainWindow instanceof Stage) {
Stage mainWindowStage = (Stage) mainWindow;
Stage dialogStage = (Stage) dialog.getDialogPane().getScene().getWindow();

dialogStage.getIcons().addAll(mainWindowStage.getIcons());
}
dialog.initOwner(mainWindow);
}

/**
* Sets the default button to the one matching the <code>targetButtonType</code>.
*/
public static void setDefaultButton(Alert alert, ButtonType targetButtonType) {
alert.getButtonTypes().forEach(buttonType -> {
Button button = (Button) alert.getDialogPane().lookupButton(buttonType);
button.setDefaultButton(buttonType == targetButtonType);
});
}

/**
* Makes the given dialog always stay on top of its parent window.
* @param dialog A dialog
Expand Down

0 comments on commit a646ef5

Please sign in to comment.