From a4e9474b975702e88acfe6f16862d53e1b877883 Mon Sep 17 00:00:00 2001 From: delvh Date: Mon, 28 Sep 2020 15:58:42 +0200 Subject: [PATCH] Fixed Saving When Using Alt F4 and Disabled Hiding If StatusTrayIcon is not supported(#65) Fixed potentially not saving when using alt f4 and disabled hiding if StatusTrayIcon is not supported Reviewed-on: https://git.kske.dev/zdm/envoy/pulls/65 Reviewed-by: kske Reviewed-by: DieGurke --- .../main/java/envoy/client/data/LocalDB.java | 2 +- .../envoy/client/helper/ShutdownHelper.java | 5 ++-- .../java/envoy/client/ui/SceneContext.java | 28 +++++++++++-------- .../main/java/envoy/client/ui/Startup.java | 7 +++-- .../ui/settings/GeneralSettingsPane.java | 18 +++++++----- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index 0e0da55..b5c90ab 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -235,7 +235,7 @@ public final class LocalDB implements EventListener { @Event(priority = 150) private void onUserStatusChange(UserStatusChange evt) { - this.getChat(evt.getID()).map(Chat::getRecipient).map(User.class::cast).ifPresent(u -> u.setStatus(evt.get())); + getChat(evt.getID()).map(Chat::getRecipient).map(User.class::cast).ifPresent(u -> u.setStatus(evt.get())); } @Event(priority = 150) diff --git a/client/src/main/java/envoy/client/helper/ShutdownHelper.java b/client/src/main/java/envoy/client/helper/ShutdownHelper.java index 7f6dd7e..18164b8 100644 --- a/client/src/main/java/envoy/client/helper/ShutdownHelper.java +++ b/client/src/main/java/envoy/client/helper/ShutdownHelper.java @@ -8,6 +8,7 @@ import javafx.scene.control.Alert.AlertType; import envoy.client.data.*; import envoy.client.event.*; import envoy.client.ui.SceneContext.SceneInfo; +import envoy.client.ui.StatusTrayIcon; import envoy.util.EnvoyLog; import dev.kske.eventbus.EventBus; @@ -24,12 +25,12 @@ public final class ShutdownHelper { /** * Exits Envoy or minimizes it, depending on the current state of - * {@link Settings#isHideOnClose()}. + * {@link Settings#isHideOnClose()} and {@link StatusTrayIcon#isSupported()}. * * @since Envoy Client v0.2-beta */ public static void exit() { - if (Settings.getInstance().isHideOnClose()) Context.getInstance().getStage().setIconified(true); + if (Settings.getInstance().isHideOnClose() && StatusTrayIcon.isSupported()) Context.getInstance().getStage().setIconified(true); else { EventBus.getInstance().dispatch(new EnvoyCloseEvent()); System.exit(0); diff --git a/client/src/main/java/envoy/client/ui/SceneContext.java b/client/src/main/java/envoy/client/ui/SceneContext.java index 30a02cb..e458407 100644 --- a/client/src/main/java/envoy/client/ui/SceneContext.java +++ b/client/src/main/java/envoy/client/ui/SceneContext.java @@ -105,17 +105,7 @@ public final class SceneContext implements EventListener { sceneStack.push(scene); stage.setScene(scene); - // Add the option to exit Linux-like with "Control" + "Q" - scene.getAccelerators().put(new KeyCodeCombination(KeyCode.Q, KeyCombination.CONTROL_DOWN), ShutdownHelper::exit); - - // Add the option to logout using "Control"+"Shift"+"L" if not in login scene - if (sceneInfo != SceneInfo.LOGIN_SCENE) scene.getAccelerators() - .put(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), ShutdownHelper::logout); - - // Add the option to open the settings scene with "Control"+"S", if being in - // chat scene - if (sceneInfo.equals(SceneInfo.CHAT_SCENE)) - scene.getAccelerators().put(new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN), () -> load(SceneInfo.SETTINGS_SCENE)); + supplyKeyboardShortcuts(sceneInfo, scene); // The LoginScene is the only scene not intended to be resized // As strange as it seems, this is needed as otherwise the LoginScene won't be @@ -130,6 +120,22 @@ public final class SceneContext implements EventListener { } } + private void supplyKeyboardShortcuts(SceneInfo sceneInfo, final Scene scene) { + final var accelerators = scene.getAccelerators(); + + // Add the option to exit Linux-like with "Control" + "Q" or "Alt" + "F4" + accelerators.put(new KeyCodeCombination(KeyCode.Q, KeyCombination.CONTROL_DOWN), ShutdownHelper::exit); + + // Add the option to logout using "Control"+"Shift"+"L" if not in login scene + if (sceneInfo != SceneInfo.LOGIN_SCENE) + accelerators.put(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), ShutdownHelper::logout); + + // Add the option to open the settings scene with "Control"+"S", if being in + // chat scene + if (sceneInfo == SceneInfo.CHAT_SCENE) + accelerators.put(new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN), () -> load(SceneInfo.SETTINGS_SCENE)); + } + /** * Removes the current scene and displays the previous one. * diff --git a/client/src/main/java/envoy/client/ui/Startup.java b/client/src/main/java/envoy/client/ui/Startup.java index 2801161..811ba61 100644 --- a/client/src/main/java/envoy/client/ui/Startup.java +++ b/client/src/main/java/envoy/client/ui/Startup.java @@ -205,10 +205,11 @@ public final class Startup extends Application { context.getSceneContext().load(SceneContext.SceneInfo.CHAT_SCENE); stage.centerOnScreen(); - if (StatusTrayIcon.isSupported()) { + // Exit or minimize the stage when a close request occurs + stage.setOnCloseRequest( + e -> { ShutdownHelper.exit(); if (Settings.getInstance().isHideOnClose() && StatusTrayIcon.isSupported()) e.consume(); }); - // Exit or minimize the stage when a close request occurs - stage.setOnCloseRequest(e -> { ShutdownHelper.exit(); if (Settings.getInstance().isHideOnClose()) e.consume(); }); + if (StatusTrayIcon.isSupported()) { // Initialize status tray icon final var trayIcon = new StatusTrayIcon(stage); diff --git a/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java b/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java index bc8cba6..2e3786c 100644 --- a/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java +++ b/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java @@ -5,6 +5,7 @@ import javafx.scene.control.*; import envoy.client.data.SettingsItem; import envoy.client.event.ThemeChangeEvent; import envoy.client.helper.ShutdownHelper; +import envoy.client.ui.StatusTrayIcon; import envoy.data.User.UserStatus; import dev.kske.eventbus.EventBus; @@ -22,13 +23,16 @@ public final class GeneralSettingsPane extends SettingsPane { super("General"); setSpacing(10); - // TODO: Support other value types - final var settingsItems = settings.getItems(); - final var hideOnCloseCheckbox = new SettingsCheckbox((SettingsItem) settingsItems.get("hideOnClose")); - final var hideOnCloseTooltip = new Tooltip("If selected, Envoy will still be present in the task bar when closed."); - hideOnCloseTooltip.setWrapText(true); - hideOnCloseCheckbox.setTooltip(hideOnCloseTooltip); - getChildren().add(hideOnCloseCheckbox); + final var settingsItems = settings.getItems(); + + // Add hide on close if supported + if (StatusTrayIcon.isSupported()) { + final var hideOnCloseCheckbox = new SettingsCheckbox((SettingsItem) settingsItems.get("hideOnClose")); + final var hideOnCloseTooltip = new Tooltip("If selected, Envoy will still be present in the task bar when closed."); + hideOnCloseTooltip.setWrapText(true); + hideOnCloseCheckbox.setTooltip(hideOnCloseTooltip); + getChildren().add(hideOnCloseCheckbox); + } final var enterToSendCheckbox = new SettingsCheckbox((SettingsItem) settingsItems.get("enterToSend")); final var enterToSendTooltip = new Tooltip(