Add Enhanced Keyboard Shortcut Mechanism (#91)

Reviewed-on: https://git.kske.dev/zdm/envoy/pulls/91
Reviewed-by: DieGurke <maxi@kske.dev>
Reviewed-by: kske <kai@kske.dev>
This commit is contained in:
2020-10-12 16:12:23 +02:00
parent 08bd915f04
commit 75f0a65517
7 changed files with 175 additions and 37 deletions

View File

@ -0,0 +1,44 @@
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;
import envoy.client.util.UserUtil;
/**
* 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 with "Control" + "Q" or "Alt" + "F4" as offered by
// some desktop environments
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),
UserUtil::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);
}
}

View File

@ -0,0 +1,76 @@
package envoy.client.data.shortcuts;
import java.util.*;
import javafx.scene.input.KeyCombination;
import envoy.client.ui.SceneContext.SceneInfo;
/**
* Contains all keyboard shortcuts used throughout the application.
*
* @author Leon Hofmeister
* @since Envoy Client v0.3-beta
*/
public final class GlobalKeyShortcuts {
private final EnumMap<SceneInfo, Map<KeyCombination, Runnable>> shortcuts = new EnumMap<>(SceneInfo.class);
private static GlobalKeyShortcuts instance = new GlobalKeyShortcuts();
private GlobalKeyShortcuts() {
for (final var sceneInfo : SceneInfo.values())
shortcuts.put(sceneInfo, new HashMap<KeyCombination, Runnable>());
}
/**
* @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) { shortcuts.values().forEach(collection -> collection.put(keys, action)); }
/**
* 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
for (final var sceneInfo : include)
shortcuts.get(sceneInfo).put(keys, action);
}
/**
* 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 Map<KeyCombination, Runnable> getKeyboardShortcuts(SceneInfo sceneInfo) { return shortcuts.get(sceneInfo); }
}

View File

@ -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<KeyCombination, Runnable> getKeyboardShortcuts();
}

View File

@ -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;