From 3c8c544cbdd378f876c014cbfc140e548173774a Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 8 Oct 2020 17:09:09 +0200 Subject: [PATCH] Added shortcuts to change status and TrayIconPopupMenu items Fixes #14 --- .../java/envoy/client/helper/AlertHelper.java | 2 -- .../java/envoy/client/ui/SceneContext.java | 18 ++++++++++-- .../java/envoy/client/ui/StatusTrayIcon.java | 29 ++++++++++++++----- .../ui/chatscene/ChatSceneCommands.java | 15 +++++++++- .../ui/settings/GeneralSettingsPane.java | 22 +++++++------- .../main/java/envoy/client/util/UserUtil.java | 3 +- 6 files changed, 64 insertions(+), 25 deletions(-) diff --git a/client/src/main/java/envoy/client/helper/AlertHelper.java b/client/src/main/java/envoy/client/helper/AlertHelper.java index 4c6e880..6b053f7 100644 --- a/client/src/main/java/envoy/client/helper/AlertHelper.java +++ b/client/src/main/java/envoy/client/helper/AlertHelper.java @@ -27,8 +27,6 @@ public final class AlertHelper { * @since Envoy Client v0.2-beta */ public static void confirmAction(Alert alert, Runnable action) { - alert.setHeight(225); - alert.setWidth(400); alert.setHeaderText(""); if (Settings.getInstance().isAskForConfirmation()) alert.showAndWait().filter(ButtonType.OK::equals).ifPresent(bu -> action.run()); else action.run(); diff --git a/client/src/main/java/envoy/client/ui/SceneContext.java b/client/src/main/java/envoy/client/ui/SceneContext.java index 2e66023..970c737 100644 --- a/client/src/main/java/envoy/client/ui/SceneContext.java +++ b/client/src/main/java/envoy/client/ui/SceneContext.java @@ -14,6 +14,7 @@ import envoy.client.data.Settings; import envoy.client.event.*; import envoy.client.helper.ShutdownHelper; import envoy.client.util.UserUtil; +import envoy.data.User.UserStatus; import envoy.util.EnvoyLog; import dev.kske.eventbus.*; @@ -127,10 +128,23 @@ public final class SceneContext implements EventListener { // 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) + if (sceneInfo != SceneInfo.LOGIN_SCENE) { + + // Add the option to logout using "Control"+"Shift"+"L" accelerators.put(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), UserUtil::logout); + // Add the option to change status using "Control" + "Shift" + + // (o)F(fline)/ A(way)/ B(usy)/(o)N(line) + accelerators.put(new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), + () -> UserUtil.changeStatus(UserStatus.OFFLINE)); + accelerators.put(new KeyCodeCombination(KeyCode.A, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), + () -> UserUtil.changeStatus(UserStatus.AWAY)); + accelerators.put(new KeyCodeCombination(KeyCode.B, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), + () -> UserUtil.changeStatus(UserStatus.BUSY)); + accelerators.put(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), + () -> UserUtil.changeStatus(UserStatus.ONLINE)); + } + // Add the option to open the settings scene with "Control"+"S", if being in // chat scene if (sceneInfo == SceneInfo.CHAT_SCENE) diff --git a/client/src/main/java/envoy/client/ui/StatusTrayIcon.java b/client/src/main/java/envoy/client/ui/StatusTrayIcon.java index 7fbbe40..10d0a4b 100644 --- a/client/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/client/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -7,8 +7,9 @@ import javafx.application.Platform; import javafx.stage.Stage; import envoy.client.helper.ShutdownHelper; -import envoy.client.util.IconUtil; +import envoy.client.util.*; import envoy.data.Message; +import envoy.data.User.UserStatus; import dev.kske.eventbus.*; import dev.kske.eventbus.Event; @@ -51,12 +52,27 @@ public final class StatusTrayIcon implements EventListener { trayIcon.setImageAutoSize(true); trayIcon.setToolTip("You are notified if you have unread messages."); - final PopupMenu popup = new PopupMenu(); + final var popup = new PopupMenu(); - final MenuItem exitMenuItem = new MenuItem("Exit"); + // Adding the exit menu item + final var exitMenuItem = new MenuItem("Exit"); exitMenuItem.addActionListener(evt -> ShutdownHelper.exit()); popup.add(exitMenuItem); + // Adding the logout menu item + final var logoutMenuItem = new MenuItem("Logout"); + logoutMenuItem.addActionListener(evt -> { hide(); UserUtil.logout(); }); + popup.add(exitMenuItem); + + // Adding the status change items + final var statusSubMenu = new Menu("Change status"); + for (final var status : UserStatus.values()) { + final var statusMenuItem = new MenuItem(status.toString().toLowerCase()); + statusMenuItem.addActionListener(evt -> UserUtil.changeStatus(status)); + statusSubMenu.add(statusMenuItem); + } + popup.add(statusSubMenu); + trayIcon.setPopupMenu(popup); // Only display messages if the stage is not focused @@ -89,9 +105,8 @@ public final class StatusTrayIcon implements EventListener { @Event private void onMessage(Message message) { - if (displayMessages) trayIcon.displayMessage( - message.hasAttachment() ? "New " + message.getAttachment().getType().toString().toLowerCase() + " message received" : "New message received", - message.getText(), - MessageType.INFO); + if (displayMessages) trayIcon + .displayMessage(message.hasAttachment() ? "New " + message.getAttachment().getType().toString().toLowerCase() + " message received" + : "New message received", message.getText(), MessageType.INFO); } } diff --git a/client/src/main/java/envoy/client/ui/chatscene/ChatSceneCommands.java b/client/src/main/java/envoy/client/ui/chatscene/ChatSceneCommands.java index 3fa95c9..ea4a646 100644 --- a/client/src/main/java/envoy/client/ui/chatscene/ChatSceneCommands.java +++ b/client/src/main/java/envoy/client/ui/chatscene/ChatSceneCommands.java @@ -4,7 +4,8 @@ import java.util.Random; import java.util.function.*; import java.util.logging.Level; -import javafx.scene.control.ListView; +import javafx.scene.control.*; +import javafx.scene.control.Alert.AlertType; import javafx.scene.control.skin.VirtualFlow; import envoy.client.data.Context; @@ -14,6 +15,7 @@ import envoy.client.ui.SceneContext.SceneInfo; import envoy.client.ui.controller.ChatScene; import envoy.client.util.*; import envoy.data.Message; +import envoy.data.User.UserStatus; import envoy.util.EnvoyLog; /** @@ -63,6 +65,17 @@ public final class ChatSceneCommands { .setDescription("Opens the settings screen") .buildNoArg("settings"); + // Status change initialization + builder.setAction(text -> { + try { + UserUtil.changeStatus(Enum.valueOf(UserStatus.class, text.get(0).toUpperCase())); + } catch (final IllegalArgumentException e) { + final var alert = new Alert(AlertType.ERROR); + alert.setContentText("Please provide an existing status"); + alert.showAndWait(); + } + }).setDescription("Changes your status to the given status.").setNumberOfArguments(1).setDefaults("OFFLINE").build("status"); + // Selection of a new message initialization messageDependantAction("s", m -> { messageList.getSelectionModel().clearSelection(); messageList.getSelectionModel().select(m); }, 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 7178cef..4c0ce19 100644 --- a/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java +++ b/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java @@ -55,18 +55,16 @@ public final class GeneralSettingsPane extends SettingsPane { combobox.setOnAction(e -> { settings.setCurrentTheme(combobox.getValue()); EventBus.getInstance().dispatch(new ThemeChangeEvent()); }); getChildren().add(combobox); - if (context.getClient().isOnline()) { - final var statusComboBox = new ComboBox(); - statusComboBox.getItems().setAll(UserStatus.values()); - statusComboBox.setValue(context.getLocalDB().getUser().getStatus()); - statusComboBox.setTooltip(new Tooltip("Change your current status")); - statusComboBox.setOnAction(e -> { - final var status = statusComboBox.getValue(); - if (status == null) return; - else UserUtil.changeStatus(status); - }); - getChildren().add(statusComboBox); - } + final var statusComboBox = new ComboBox(); + statusComboBox.getItems().setAll(UserStatus.values()); + statusComboBox.setValue(context.getLocalDB().getUser().getStatus()); + statusComboBox.setTooltip(new Tooltip("Change your current status")); + statusComboBox.setOnAction(e -> { + final var status = statusComboBox.getValue(); + if (status == null) return; + else UserUtil.changeStatus(status); + }); + getChildren().add(statusComboBox); final var logoutButton = new Button("Logout"); logoutButton.setOnAction(e -> UserUtil.logout()); diff --git a/client/src/main/java/envoy/client/util/UserUtil.java b/client/src/main/java/envoy/client/util/UserUtil.java index 8862ef9..2a41fd1 100644 --- a/client/src/main/java/envoy/client/util/UserUtil.java +++ b/client/src/main/java/envoy/client/util/UserUtil.java @@ -53,6 +53,7 @@ public final class UserUtil { */ public static void changeStatus(UserStatus newStatus) { EventBus.getInstance().dispatch(new OwnStatusChange(newStatus)); - Context.getInstance().getClient().send(new UserStatusChange(Context.getInstance().getLocalDB().getUser().getID(), newStatus)); + if (Context.getInstance().getClient().isOnline()) + Context.getInstance().getClient().send(new UserStatusChange(Context.getInstance().getLocalDB().getUser().getID(), newStatus)); } }