From 59354c403d74bf3fb826fafd6d131840a5867ef1 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 24 Jul 2020 09:57:09 +0200 Subject: [PATCH] Integrated the tray icon with the hide on close setting --- .../main/java/envoy/client/data/Settings.java | 8 ++-- .../java/envoy/client/ui/StatusTrayIcon.java | 43 +++++++++++-------- .../client/ui/controller/LoginScene.java | 22 +++++++--- .../ui/settings/GeneralSettingsPane.java | 2 +- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/client/src/main/java/envoy/client/data/Settings.java b/client/src/main/java/envoy/client/data/Settings.java index fc16c26..239175d 100644 --- a/client/src/main/java/envoy/client/data/Settings.java +++ b/client/src/main/java/envoy/client/data/Settings.java @@ -75,7 +75,7 @@ public class Settings { private void supplementDefaults() { items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key.")); - items.putIfAbsent("onCloseMode", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed.")); + items.putIfAbsent("hideOnClose", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed.")); items.putIfAbsent("currentTheme", new SettingsItem<>("dark", "Current Theme Name", "The name of the currently selected theme.")); } @@ -124,15 +124,15 @@ public class Settings { * @return the current on close mode. * @since Envoy Client v0.3-alpha */ - public Boolean getCurrentOnCloseMode() { return (Boolean) items.get("onCloseMode").get(); } + public Boolean isHideOnClose() { return (Boolean) items.get("hideOnClose").get(); } /** * Sets the current on close mode. * - * @param currentOnCloseMode the on close mode that should be set. + * @param hideOnClose the on close mode that should be set. * @since Envoy Client v0.3-alpha */ - public void setCurrentOnCloseMode(boolean currentOnCloseMode) { ((SettingsItem) items.get("onCloseMode")).set(currentOnCloseMode); } + public void setHideOnClose(boolean hideOnClose) { ((SettingsItem) items.get("hideOnClose")).set(hideOnClose); } /** * @return the items diff --git a/client/src/main/java/envoy/client/ui/StatusTrayIcon.java b/client/src/main/java/envoy/client/ui/StatusTrayIcon.java index 80a83db..9555117 100644 --- a/client/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/client/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -9,7 +9,6 @@ import javafx.stage.Stage; import envoy.client.event.MessageCreationEvent; import envoy.data.Message; import envoy.event.EventBus; -import envoy.exception.EnvoyException; /** * Project: envoy-client
@@ -34,19 +33,21 @@ public class StatusTrayIcon { */ private boolean displayMessages = false; + /** + * @return true if the status tray icon is supported on this platform + * @since Envoy Client v0.2-beta + */ + public static boolean isSupported() { return SystemTray.isSupported(); } + /** * Creates a {@link StatusTrayIcon} with the Envoy logo, a tool tip and a pop-up * menu. * * @param stage the stage which focus determines if message * notifications are displayed - * @throws EnvoyException if the currently used OS does not support the System - * Tray API * @since Envoy Client v0.2-beta */ - public StatusTrayIcon(Stage stage) throws EnvoyException { - if (!SystemTray.isSupported()) throw new EnvoyException("The Envoy tray icon is not supported."); - + public StatusTrayIcon(Stage stage) { trayIcon = new TrayIcon(IconUtil.loadAWTCompatible("/icons/envoy_logo.png"), "Envoy"); trayIcon.setImageAutoSize(true); trayIcon.setToolTip("You are notified if you have unread messages."); @@ -61,30 +62,36 @@ public class StatusTrayIcon { // Only display messages if the stage is not focused stage.focusedProperty().addListener((ov, onHidden, onShown) -> displayMessages = ov.getValue() == Boolean.FALSE); - // Show the window if the user clicks on the icon trayIcon.addActionListener(evt -> Platform.runLater(() -> { stage.setIconified(false); stage.toFront(); stage.requestFocus(); })); // Start processing message events - // TODO: Handle other message types - EventBus.getInstance() - .register(MessageCreationEvent.class, - evt -> { if (displayMessages) trayIcon.displayMessage("New message received", evt.get().getText(), MessageType.INFO); }); + EventBus.getInstance().register(MessageCreationEvent.class, evt -> { + if (displayMessages) trayIcon + .displayMessage( + evt.get().hasAttachment() ? "New " + evt.get().getAttachment().getType().toString().toLowerCase() + " message received" + : "New message received", + evt.get().getText(), + MessageType.INFO); + }); } /** - * Makes this {@link StatusTrayIcon} appear in the system tray. + * Makes the icon appear in the system tray. * - * @throws EnvoyException if the status icon could not be attaches to the system - * tray for system-internal reasons * @since Envoy Client v0.2-alpha */ - public void show() throws EnvoyException { + public void show() { try { SystemTray.getSystemTray().add(trayIcon); - } catch (final AWTException e) { - throw new EnvoyException("Could not attach Envoy tray icon to system tray.", e); - } + } catch (AWTException e) {} } + + /** + * Removes the icon from the system tray. + * + * @since Envoy Client v0.2-beta + */ + public void hide() { SystemTray.getSystemTray().remove(trayIcon); } } diff --git a/client/src/main/java/envoy/client/ui/controller/LoginScene.java b/client/src/main/java/envoy/client/ui/controller/LoginScene.java index 7d058cd..177524b 100644 --- a/client/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/client/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -62,6 +62,7 @@ public final class LoginScene { private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final EventBus eventBus = EventBus.getInstance(); private static final ClientConfig config = ClientConfig.getInstance(); + private static final Settings settings = Settings.getInstance(); @FXML private void initialize() { @@ -209,15 +210,22 @@ public final class LoginScene { sceneContext.load(SceneContext.SceneInfo.CHAT_SCENE); sceneContext.getController().initializeData(sceneContext, localDB, client, writeProxy); - try { - new StatusTrayIcon(sceneContext.getStage()).show(); + if (StatusTrayIcon.isSupported()) { + + // Configure hide on close sceneContext.getStage().setOnCloseRequest(e -> { - sceneContext.getStage().setIconified(true); - e.consume(); + if (settings.isHideOnClose()) { + sceneContext.getStage().setIconified(true); + e.consume(); + } + }); + + // Initialize status tray icon + final var trayIcon = new StatusTrayIcon(sceneContext.getStage()); + settings.getItems().get("hideOnClose").setChangeHandler(c -> { + if (((Boolean) c)) trayIcon.show(); + else trayIcon.hide(); }); - } catch (EnvoyException e) { - e.printStackTrace(); } - } } 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 e6d5761..c82cb18 100644 --- a/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java +++ b/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java @@ -31,7 +31,7 @@ public class GeneralSettingsPane extends SettingsPane { final var vbox = new VBox(); // TODO: Support other value types - List.of("onCloseMode", "enterToSend") + List.of("hideOnClose", "enterToSend") .stream() .map(settings.getItems()::get) .map(i -> new SettingsCheckbox((SettingsItem) i))