From 1a4e05b02fa1873396849de3ef4ff391d533fb09 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 9 Oct 2020 11:09:59 +0200 Subject: [PATCH] Add enhanced keyboard shortcut mechanism --- .../data/shortcuts/EnvoyShortcutConfig.java | 42 +++++++ .../data/shortcuts/GlobalKeyShortcuts.java | 118 ++++++++++++++++++ .../data/shortcuts/KeyboardMapping.java | 25 ++++ .../client/data/shortcuts/package-info.java | 7 ++ .../java/envoy/client/ui/SceneContext.java | 29 ++--- .../main/java/envoy/client/ui/Startup.java | 4 + .../client/ui/controller/SettingsScene.java | 13 +- 7 files changed, 217 insertions(+), 21 deletions(-) create mode 100644 client/src/main/java/envoy/client/data/shortcuts/EnvoyShortcutConfig.java create mode 100644 client/src/main/java/envoy/client/data/shortcuts/GlobalKeyShortcuts.java create mode 100644 client/src/main/java/envoy/client/data/shortcuts/KeyboardMapping.java create mode 100644 client/src/main/java/envoy/client/data/shortcuts/package-info.java diff --git a/client/src/main/java/envoy/client/data/shortcuts/EnvoyShortcutConfig.java b/client/src/main/java/envoy/client/data/shortcuts/EnvoyShortcutConfig.java new file mode 100644 index 0000000..d829597 --- /dev/null +++ b/client/src/main/java/envoy/client/data/shortcuts/EnvoyShortcutConfig.java @@ -0,0 +1,42 @@ +package envoy.client.data.shortcuts; + +import javafx.scene.input.*; + +import envoy.client.data.Context; +import envoy.client.helper.ShutdownHelper; +import envoy.client.ui.SceneContext.SceneInfo; + +/** + * Envoy-specific implementation of the keyboard-shortcut interaction offered by + * {@link GlobalKeyShortcuts}. + * + * @author Leon Hofmeister + * @since Envoy Client v0.3-beta + */ +public class EnvoyShortcutConfig { + + private EnvoyShortcutConfig() {} + + /** + * Supplies the default shortcuts for {@link GlobalKeyShortcuts}. + * + * @since Envoy Client v0.3-beta + */ + public static void initializeEnvoyShortcuts() { + final var instance = GlobalKeyShortcuts.getInstance(); + + // Add the option to exit Linux-like with "Control" + "Q" or "Alt" + "F4" + instance.add(new KeyCodeCombination(KeyCode.Q, KeyCombination.CONTROL_DOWN), ShutdownHelper::exit); + + // Add the option to logout using "Control"+"Shift"+"L" if not in login scene + instance.addForNotExcluded(new KeyCodeCombination(KeyCode.L, KeyCombination.CONTROL_DOWN, KeyCombination.SHIFT_DOWN), + ShutdownHelper::logout, + SceneInfo.LOGIN_SCENE); + + // Add option to open settings scene with "Control"+"S", if not in login scene + instance.addForNotExcluded(new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN), + () -> Context.getInstance().getSceneContext().load(SceneInfo.SETTINGS_SCENE), + SceneInfo.SETTINGS_SCENE, + SceneInfo.LOGIN_SCENE); + } +} diff --git a/client/src/main/java/envoy/client/data/shortcuts/GlobalKeyShortcuts.java b/client/src/main/java/envoy/client/data/shortcuts/GlobalKeyShortcuts.java new file mode 100644 index 0000000..b1f2e0b --- /dev/null +++ b/client/src/main/java/envoy/client/data/shortcuts/GlobalKeyShortcuts.java @@ -0,0 +1,118 @@ +package envoy.client.data.shortcuts; + +import java.util.*; +import java.util.Map.Entry; + +import javafx.scene.input.KeyCombination; + +import envoy.client.ui.SceneContext.SceneInfo; + +/** + * Contains all KeyBoardshotcuts used throughout the application. + * + * @author Leon Hofmeister + * @since Envoy Client v0.3-beta + */ +public final class GlobalKeyShortcuts { + + /** + * Helper class for the Entry interface. + * + * @author Leon Hofmeister + * @since Envoy Client v0.3-beta + */ + public final class KeyCombinationAction implements Entry { + + private final KeyCombination keys; + private final Runnable action; + + /** + * Creates a new {@code KeyCombinationAction}. + * + * @param keys the keys to press to perform the given action + * @param action the action to perform + * @since Envoy Client v0.3-beta + */ + public KeyCombinationAction(KeyCombination keys, Runnable action) { + if (keys == null || action == null) throw new NullPointerException("Cannot create key combination action"); + this.keys = keys; + this.action = action; + } + + @Override + public KeyCombination getKey() { return keys; } + + @Override + public Runnable getValue() { return action; } + + @Override + public Runnable setValue(Runnable none) { + throw new UnsupportedOperationException("Reassignment of keyboard shortcut actions is not supported"); + } + + } + + private final EnumMap> shortcuts = new EnumMap<>(SceneInfo.class); + + private static GlobalKeyShortcuts instance = new GlobalKeyShortcuts(); + + private GlobalKeyShortcuts() { + for (final var sceneInfo : SceneInfo.values()) + shortcuts.put(sceneInfo, new HashSet()); + } + + /** + * @return the instance of global keyboard shortcuts. + * @since Envoy Client v0.3-beta + */ + public static GlobalKeyShortcuts getInstance() { return instance; } + + /** + * Adds the given keyboard shortcut and its action to all scenes. + * + * @param keys the keys to press to perform the given action + * @param action the action to perform + * @since Envoy Client v0.3-beta + */ + public void add(KeyCombination keys, Runnable action) { + final var keyCombinationAction = new KeyCombinationAction(keys, action); + shortcuts.values().forEach(collection -> collection.add(keyCombinationAction)); + } + + /** + * Adds the given keyboard shortcut and its action to all scenes that are not + * part of exclude. + * + * @param keys the keys to press to perform the given action + * @param action the action to perform + * @param exclude the scenes that should be excluded from receiving this + * keyboard shortcut + * @since Envoy Client v0.3-beta + */ + public void addForNotExcluded(KeyCombination keys, Runnable action, SceneInfo... exclude) { + + // Computing the remaining sceneInfos + final var include = new SceneInfo[SceneInfo.values().length - exclude.length]; + int index = 0; + outer: + for (final var sceneInfo : SceneInfo.values()) { + for (final var excluded : exclude) + if (sceneInfo.equals(excluded)) continue outer; + include[index++] = sceneInfo; + } + + // Adding the action to the remaining sceneInfos + final var keyCombinationAction = new KeyCombinationAction(keys, action); + for (final var sceneInfo : include) + shortcuts.get(sceneInfo).add(keyCombinationAction); + } + + /** + * Returns all stored keyboard shortcuts for the given scene constant + * + * @param sceneInfo the currently loading scene + * @return all stored keyboard shortcuts for this scene + * @since Envoy Client v0.3-beta + */ + public Collection getKeyboardShortcuts(SceneInfo sceneInfo) { return shortcuts.get(sceneInfo); } +} diff --git a/client/src/main/java/envoy/client/data/shortcuts/KeyboardMapping.java b/client/src/main/java/envoy/client/data/shortcuts/KeyboardMapping.java new file mode 100644 index 0000000..f98c055 --- /dev/null +++ b/client/src/main/java/envoy/client/data/shortcuts/KeyboardMapping.java @@ -0,0 +1,25 @@ +package envoy.client.data.shortcuts; + +import java.util.Map; + +import javafx.scene.input.KeyCombination; + +import envoy.client.ui.SceneContext; + +/** + * Provides methods to set the keyboard shortcuts for a specific scene. + * Should only be implemented by Controllers of Scenes so that these methods can + * automatically be called inside {@link SceneContext} as soon + * as the underlying fxml file has been loaded. + * + * @author Leon Hofmeister + * @since Envoy Client v0.3-beta + */ +public interface KeyboardMapping { + + /** + * @return all keyboard shortcuts of a scene + * @since Envoy Client v0.3-beta + */ + Map getKeyboardShortcuts(); +} diff --git a/client/src/main/java/envoy/client/data/shortcuts/package-info.java b/client/src/main/java/envoy/client/data/shortcuts/package-info.java new file mode 100644 index 0000000..0e8591c --- /dev/null +++ b/client/src/main/java/envoy/client/data/shortcuts/package-info.java @@ -0,0 +1,7 @@ +/** + * Contains the necessary classes to enable using keyboard shortcuts in Envoy. + * + * @author Leon Hofmeister + * @since Envoy Client v0.3-beta + */ +package envoy.client.data.shortcuts; diff --git a/client/src/main/java/envoy/client/ui/SceneContext.java b/client/src/main/java/envoy/client/ui/SceneContext.java index e458407..eb790c4 100644 --- a/client/src/main/java/envoy/client/ui/SceneContext.java +++ b/client/src/main/java/envoy/client/ui/SceneContext.java @@ -7,12 +7,11 @@ import java.util.logging.Level; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.*; -import javafx.scene.input.*; import javafx.stage.Stage; import envoy.client.data.Settings; +import envoy.client.data.shortcuts.*; import envoy.client.event.*; -import envoy.client.helper.ShutdownHelper; import envoy.util.EnvoyLog; import dev.kske.eventbus.*; @@ -100,12 +99,18 @@ public final class SceneContext implements EventListener { try { final var rootNode = (Parent) loader.load(getClass().getResourceAsStream(sceneInfo.path)); final var scene = new Scene(rootNode); - controllerStack.push(loader.getController()); + final var controller = loader.getController(); + controllerStack.push(controller); sceneStack.push(scene); stage.setScene(scene); - supplyKeyboardShortcuts(sceneInfo, scene); + // Supply the global custom keyboard shortcuts for that scene + for (final var shortcut : GlobalKeyShortcuts.getInstance().getKeyboardShortcuts(sceneInfo)) + scene.getAccelerators().put(shortcut.getKey(), shortcut.getValue()); + + // Supply the scene specific keyboard shortcuts + if (controller instanceof KeyboardMapping) scene.getAccelerators().putAll(((KeyboardMapping) controller).getKeyboardShortcuts()); // 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 @@ -120,22 +125,6 @@ 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 811ba61..a269397 100644 --- a/client/src/main/java/envoy/client/ui/Startup.java +++ b/client/src/main/java/envoy/client/ui/Startup.java @@ -11,6 +11,7 @@ import javafx.scene.control.Alert.AlertType; import javafx.stage.Stage; import envoy.client.data.*; +import envoy.client.data.shortcuts.EnvoyShortcutConfig; import envoy.client.helper.ShutdownHelper; import envoy.client.net.Client; import envoy.client.ui.SceneContext.SceneInfo; @@ -84,6 +85,9 @@ public final class Startup extends Application { stage.setTitle("Envoy"); stage.getIcons().add(IconUtil.loadIcon("envoy_logo")); + // Configure global shortcuts used + EnvoyShortcutConfig.initializeEnvoyShortcuts(); + // Create scene context final var sceneContext = new SceneContext(stage); context.setSceneContext(sceneContext); diff --git a/client/src/main/java/envoy/client/ui/controller/SettingsScene.java b/client/src/main/java/envoy/client/ui/controller/SettingsScene.java index f69475a..13f97e4 100644 --- a/client/src/main/java/envoy/client/ui/controller/SettingsScene.java +++ b/client/src/main/java/envoy/client/ui/controller/SettingsScene.java @@ -1,9 +1,13 @@ package envoy.client.ui.controller; +import java.util.*; + import javafx.fxml.FXML; import javafx.scene.control.*; +import javafx.scene.input.*; import envoy.client.data.Context; +import envoy.client.data.shortcuts.KeyboardMapping; import envoy.client.ui.listcell.ListCellFactory; import envoy.client.ui.settings.*; @@ -13,7 +17,7 @@ import envoy.client.ui.settings.*; * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta */ -public final class SettingsScene { +public final class SettingsScene implements KeyboardMapping { @FXML private ListView settingsList; @@ -38,4 +42,11 @@ public final class SettingsScene { @FXML private void backButtonClicked() { Context.getInstance().getSceneContext().pop(); } + + @Override + public Map getKeyboardShortcuts() { + final var map = new HashMap(); + map.put(new KeyCodeCombination(KeyCode.B, KeyCombination.CONTROL_DOWN), this::backButtonClicked); + return map; + } }