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 <kai@kske.dev>
Reviewed-by: DieGurke <maxi@kske.dev>
This commit is contained in:
Leon Hofmeister 2020-09-28 15:58:42 +02:00
parent 3f0267624c
commit a4e9474b97
5 changed files with 36 additions and 24 deletions

View File

@ -235,7 +235,7 @@ public final class LocalDB implements EventListener {
@Event(priority = 150) @Event(priority = 150)
private void onUserStatusChange(UserStatusChange evt) { 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) @Event(priority = 150)

View File

@ -8,6 +8,7 @@ import javafx.scene.control.Alert.AlertType;
import envoy.client.data.*; import envoy.client.data.*;
import envoy.client.event.*; import envoy.client.event.*;
import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.SceneContext.SceneInfo;
import envoy.client.ui.StatusTrayIcon;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
import dev.kske.eventbus.EventBus; import dev.kske.eventbus.EventBus;
@ -24,12 +25,12 @@ public final class ShutdownHelper {
/** /**
* Exits Envoy or minimizes it, depending on the current state of * 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 * @since Envoy Client v0.2-beta
*/ */
public static void exit() { 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 { else {
EventBus.getInstance().dispatch(new EnvoyCloseEvent()); EventBus.getInstance().dispatch(new EnvoyCloseEvent());
System.exit(0); System.exit(0);

View File

@ -105,17 +105,7 @@ public final class SceneContext implements EventListener {
sceneStack.push(scene); sceneStack.push(scene);
stage.setScene(scene); stage.setScene(scene);
// Add the option to exit Linux-like with "Control" + "Q" supplyKeyboardShortcuts(sceneInfo, scene);
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));
// The LoginScene is the only scene not intended to be resized // 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 // 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. * Removes the current scene and displays the previous one.
* *

View File

@ -205,10 +205,11 @@ public final class Startup extends Application {
context.getSceneContext().load(SceneContext.SceneInfo.CHAT_SCENE); context.getSceneContext().load(SceneContext.SceneInfo.CHAT_SCENE);
stage.centerOnScreen(); stage.centerOnScreen();
if (StatusTrayIcon.isSupported()) {
// Exit or minimize the stage when a close request occurs // Exit or minimize the stage when a close request occurs
stage.setOnCloseRequest(e -> { ShutdownHelper.exit(); if (Settings.getInstance().isHideOnClose()) e.consume(); }); stage.setOnCloseRequest(
e -> { ShutdownHelper.exit(); if (Settings.getInstance().isHideOnClose() && StatusTrayIcon.isSupported()) e.consume(); });
if (StatusTrayIcon.isSupported()) {
// Initialize status tray icon // Initialize status tray icon
final var trayIcon = new StatusTrayIcon(stage); final var trayIcon = new StatusTrayIcon(stage);

View File

@ -5,6 +5,7 @@ import javafx.scene.control.*;
import envoy.client.data.SettingsItem; import envoy.client.data.SettingsItem;
import envoy.client.event.ThemeChangeEvent; import envoy.client.event.ThemeChangeEvent;
import envoy.client.helper.ShutdownHelper; import envoy.client.helper.ShutdownHelper;
import envoy.client.ui.StatusTrayIcon;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import dev.kske.eventbus.EventBus; import dev.kske.eventbus.EventBus;
@ -22,13 +23,16 @@ public final class GeneralSettingsPane extends SettingsPane {
super("General"); super("General");
setSpacing(10); setSpacing(10);
// TODO: Support other value types
final var settingsItems = settings.getItems(); final var settingsItems = settings.getItems();
// Add hide on close if supported
if (StatusTrayIcon.isSupported()) {
final var hideOnCloseCheckbox = new SettingsCheckbox((SettingsItem<Boolean>) settingsItems.get("hideOnClose")); final var hideOnCloseCheckbox = new SettingsCheckbox((SettingsItem<Boolean>) settingsItems.get("hideOnClose"));
final var hideOnCloseTooltip = new Tooltip("If selected, Envoy will still be present in the task bar when closed."); final var hideOnCloseTooltip = new Tooltip("If selected, Envoy will still be present in the task bar when closed.");
hideOnCloseTooltip.setWrapText(true); hideOnCloseTooltip.setWrapText(true);
hideOnCloseCheckbox.setTooltip(hideOnCloseTooltip); hideOnCloseCheckbox.setTooltip(hideOnCloseTooltip);
getChildren().add(hideOnCloseCheckbox); getChildren().add(hideOnCloseCheckbox);
}
final var enterToSendCheckbox = new SettingsCheckbox((SettingsItem<Boolean>) settingsItems.get("enterToSend")); final var enterToSendCheckbox = new SettingsCheckbox((SettingsItem<Boolean>) settingsItems.get("enterToSend"));
final var enterToSendTooltip = new Tooltip( final var enterToSendTooltip = new Tooltip(