From db3f3efc5289ca20d752c9e6035b70b3d6f519ba Mon Sep 17 00:00:00 2001 From: Sam Carlberg Date: Tue, 21 Nov 2017 17:57:53 -0500 Subject: [PATCH 1/4] Ask to save the current layout when closing shuffleboard Fixes #173 --- .../edu/wpi/first/shuffleboard/api/base.css | 8 +++++++ .../first/shuffleboard/app/Shuffleboard.java | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/api/src/main/resources/edu/wpi/first/shuffleboard/api/base.css b/api/src/main/resources/edu/wpi/first/shuffleboard/api/base.css index 01f9de10f..49e62febe 100644 --- a/api/src/main/resources/edu/wpi/first/shuffleboard/api/base.css +++ b/api/src/main/resources/edu/wpi/first/shuffleboard/api/base.css @@ -235,3 +235,11 @@ /* Bring the content to all edges */ -fx-padding: 0; /* 10 0 0 10 */ } + +.dialog-pane .header-panel { + -fx-background-color: -swatch-500; +} + +.dialog-pane .header-panel .label { + -fx-text-fill: white; +} diff --git a/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java b/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java index 769ce47e5..d1a477617 100644 --- a/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java +++ b/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java @@ -28,6 +28,8 @@ import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; import javafx.scene.layout.Pane; import javafx.stage.Screen; import javafx.stage.Stage; @@ -90,6 +92,25 @@ public void start(Stage primaryStage) throws IOException { primaryStage.setMinHeight(480); primaryStage.setWidth(Screen.getPrimary().getVisualBounds().getWidth()); primaryStage.setHeight(Screen.getPrimary().getVisualBounds().getHeight()); + primaryStage.setOnCloseRequest(closeEvent -> { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.getDialogPane().getScene().getStylesheets().setAll(mainPane.getStylesheets()); + alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO, ButtonType.CANCEL); + alert.getDialogPane().setHeaderText("Save the current layout?"); + alert.showAndWait().ifPresent(bt -> { + if (bt == ButtonType.YES) { + try { + mainWindowController.save(); + } catch (IOException ex) { + logger.log(Level.WARNING, "Could not save the layout", ex); + } + } else if (bt == ButtonType.CANCEL) { + // cancel the close request by consuming the event + closeEvent.consume(); + } + // Don't need to check for NO because it just lets the window close normally + }); + }); primaryStage.show(); Time.setStartTime(Time.now()); } From 93a559f1186d684b57ead43b6020b14666f4e135 Mon Sep 17 00:00:00 2001 From: Sam Carlberg Date: Tue, 21 Nov 2017 18:08:35 -0500 Subject: [PATCH 2/4] Set title, improve message text --- .../main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java b/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java index d1a477617..5bb871a3b 100644 --- a/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java +++ b/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java @@ -94,9 +94,10 @@ public void start(Stage primaryStage) throws IOException { primaryStage.setHeight(Screen.getPrimary().getVisualBounds().getHeight()); primaryStage.setOnCloseRequest(closeEvent -> { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Save layout"); alert.getDialogPane().getScene().getStylesheets().setAll(mainPane.getStylesheets()); alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO, ButtonType.CANCEL); - alert.getDialogPane().setHeaderText("Save the current layout?"); + alert.getDialogPane().setHeaderText("Save the current layout before closing?"); alert.showAndWait().ifPresent(bt -> { if (bt == ButtonType.YES) { try { From 72e490d6428dc9392b3a22896ab5420ab331f85b Mon Sep 17 00:00:00 2001 From: Sam Carlberg Date: Wed, 22 Nov 2017 14:07:32 -0500 Subject: [PATCH 3/4] Add option to not show exit confirmation dialog --- .../first/shuffleboard/app/Shuffleboard.java | 4 ++++ .../app/prefs/AppPreferences.java | 22 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java b/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java index 5bb871a3b..1d0c8c2d2 100644 --- a/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java +++ b/app/src/main/java/edu/wpi/first/shuffleboard/app/Shuffleboard.java @@ -93,6 +93,10 @@ public void start(Stage primaryStage) throws IOException { primaryStage.setWidth(Screen.getPrimary().getVisualBounds().getWidth()); primaryStage.setHeight(Screen.getPrimary().getVisualBounds().getHeight()); primaryStage.setOnCloseRequest(closeEvent -> { + if (!AppPreferences.getInstance().isConfirmExit()) { + // Don't show the confirmation dialog, just exit + return; + } Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Save layout"); alert.getDialogPane().getScene().getStylesheets().setAll(mainPane.getStylesheets()); diff --git a/app/src/main/java/edu/wpi/first/shuffleboard/app/prefs/AppPreferences.java b/app/src/main/java/edu/wpi/first/shuffleboard/app/prefs/AppPreferences.java index 5d3162289..d1fd122f6 100644 --- a/app/src/main/java/edu/wpi/first/shuffleboard/app/prefs/AppPreferences.java +++ b/app/src/main/java/edu/wpi/first/shuffleboard/app/prefs/AppPreferences.java @@ -18,8 +18,7 @@ import javafx.beans.property.SimpleObjectProperty; /** - * Contains the user preferences for the app. These preferences are user-specific and are saved - * to the users home directory and are not contained in save files. + * Contains the user preferences for the app. These preferences are user-specific and are not contained in save files. */ public final class AppPreferences { @@ -28,6 +27,8 @@ public final class AppPreferences { private final Property saveFile = new SimpleObjectProperty<>(this, "saveFile", null); private final BooleanProperty autoLoadLastSaveFile = new SimpleBooleanProperty(this, "automaticallyLoadLastSaveFile", true); + private final BooleanProperty confirmExit = + new SimpleBooleanProperty(this, "showConfirmationDialogWhenExiting", true); @VisibleForTesting static AppPreferences instance = new AppPreferences(); @@ -41,11 +42,13 @@ public AppPreferences() { PreferencesUtils.read(defaultTileSize, preferences); PreferencesUtils.read(saveFile, preferences, File::new); PreferencesUtils.read(autoLoadLastSaveFile, preferences); + PreferencesUtils.read(confirmExit, preferences); theme.addListener(__ -> PreferencesUtils.save(theme, preferences, Theme::getName)); defaultTileSize.addListener(__ -> PreferencesUtils.save(defaultTileSize, preferences)); saveFile.addListener(__ -> PreferencesUtils.save(saveFile, preferences, File::getAbsolutePath)); autoLoadLastSaveFile.addListener(__ -> PreferencesUtils.save(autoLoadLastSaveFile, preferences)); + confirmExit.addListener(__ -> PreferencesUtils.save(confirmExit, preferences)); } public static AppPreferences getInstance() { @@ -59,7 +62,8 @@ public ImmutableList> getProperties() { return ImmutableList.of( theme, defaultTileSize, - autoLoadLastSaveFile + autoLoadLastSaveFile, + confirmExit ); } @@ -110,4 +114,16 @@ public BooleanProperty autoLoadLastSaveFileProperty() { public void setAutoLoadLastSaveFile(boolean autoLoadLastSaveFile) { this.autoLoadLastSaveFile.set(autoLoadLastSaveFile); } + + public boolean isConfirmExit() { + return confirmExit.get(); + } + + public BooleanProperty confirmExitProperty() { + return confirmExit; + } + + public void setConfirmExit(boolean confirmExit) { + this.confirmExit.set(confirmExit); + } } From 0d0b5f3833db5e8e63e6005578c8e0ba62b7ea59 Mon Sep 17 00:00:00 2001 From: Sam Carlberg Date: Wed, 22 Nov 2017 15:43:36 -0500 Subject: [PATCH 4/4] Show confirmation dialog on File -> Close Changes the MainWindowController.close() method to attempt to close the window rather than terminating the program --- .../app/MainWindowController.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/edu/wpi/first/shuffleboard/app/MainWindowController.java b/app/src/main/java/edu/wpi/first/shuffleboard/app/MainWindowController.java index 5874ab941..2aa06e5f2 100644 --- a/app/src/main/java/edu/wpi/first/shuffleboard/app/MainWindowController.java +++ b/app/src/main/java/edu/wpi/first/shuffleboard/app/MainWindowController.java @@ -1,13 +1,11 @@ package edu.wpi.first.shuffleboard.app; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; -import com.google.common.io.Files; - import edu.wpi.first.shuffleboard.api.DashboardMode; import edu.wpi.first.shuffleboard.api.components.SourceTreeTable; +import edu.wpi.first.shuffleboard.api.components.WidgetPropertySheet; import edu.wpi.first.shuffleboard.api.dnd.DataFormats; import edu.wpi.first.shuffleboard.api.plugin.Plugin; +import edu.wpi.first.shuffleboard.api.prefs.FlushableProperty; import edu.wpi.first.shuffleboard.api.sources.DataSource; import edu.wpi.first.shuffleboard.api.sources.SourceEntry; import edu.wpi.first.shuffleboard.api.sources.recording.Recorder; @@ -19,13 +17,15 @@ import edu.wpi.first.shuffleboard.app.components.DashboardTab; import edu.wpi.first.shuffleboard.app.components.DashboardTabPane; import edu.wpi.first.shuffleboard.app.components.WidgetGallery; -import edu.wpi.first.shuffleboard.api.components.WidgetPropertySheet; import edu.wpi.first.shuffleboard.app.json.JsonBuilder; import edu.wpi.first.shuffleboard.app.plugin.PluginLoader; import edu.wpi.first.shuffleboard.app.prefs.AppPreferences; -import edu.wpi.first.shuffleboard.api.prefs.FlushableProperty; import edu.wpi.first.shuffleboard.app.sources.recording.Playback; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import com.google.common.io.Files; + import org.fxmisc.easybind.EasyBind; import java.io.File; @@ -68,6 +68,8 @@ import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.StageStyle; +import javafx.stage.Window; +import javafx.stage.WindowEvent; import static edu.wpi.first.shuffleboard.api.components.SourceTreeTable.alphabetical; import static edu.wpi.first.shuffleboard.api.components.SourceTreeTable.branchesFirst; @@ -292,10 +294,17 @@ public void setDashboard(DashboardTabPane dashboard) { centerSplitPane.getItems().add(dashboard); } + /** + * Closes from interacting with the "Close" menu item. + */ @FXML public void close() { log.info("Exiting app"); - System.exit(0); + + // Attempt to close the main window. This lets window closing handlers run. Calling System.exit() or Platform.exit() + // will more-or-less immediately terminate the application without calling these handlers. + Window window = root.getScene().getWindow(); + window.fireEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST)); } /**