From ed9d4320ae30d4aa1bdb9d6f1e08d3dd7313e0a9 Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Fri, 16 Jun 2023 13:11:48 +0200 Subject: [PATCH] separate key bindings definition from implementation --- src/keyBindings/definition.ts | 214 ++++++++++++++++++++++++++++++++++ src/keyBindings/loader.ts | 66 ++++------- 2 files changed, 237 insertions(+), 43 deletions(-) create mode 100644 src/keyBindings/definition.ts diff --git a/src/keyBindings/definition.ts b/src/keyBindings/definition.ts new file mode 100644 index 0000000..8dae4cb --- /dev/null +++ b/src/keyBindings/definition.ts @@ -0,0 +1,214 @@ +interface KeyBinding { + name: string; + description: string; + defaultKeySequence: string; + action: keyof ReturnType; + repeat?: number; +} + +const keyBindings: KeyBinding[] = [ + { + "name": "window-toggle-floating", + "description": "Toggle floating", + "defaultKeySequence": "Meta+Space", + "action": "windowToggleFloating", + }, + { + "name": "focus-left", + "description": "Move focus left", + "defaultKeySequence": "Meta+A", + "action": "focusLeft", + }, + { + "name": "focus-right", + "description": "Move focus right", + "defaultKeySequence": "Meta+D", + "action": "focusRight", + }, + { + "name": "focus-up", + "description": "Move focus up", + "defaultKeySequence": "Meta+W", + "action": "focusUp", + }, + { + "name": "focus-down", + "description": "Move focus down", + "defaultKeySequence": "Meta+S", + "action": "focusDown", + }, + { + "name": "focus-start", + "description": "Move focus to start", + "defaultKeySequence": "Meta+Home", + "action": "focusStart", + }, + { + "name": "focus-end", + "description": "Move focus to end", + "defaultKeySequence": "Meta+End", + "action": "focusEnd", + }, + { + "name": "window-move-left", + "description": "Move window left", + "defaultKeySequence": "Meta+Shift+A", + "action": "windowMoveLeft", + }, + { + "name": "window-move-right", + "description": "Move window right", + "defaultKeySequence": "Meta+Shift+D", + "action": "windowMoveRight", + }, + { + "name": "window-move-up", + "description": "Move window up", + "defaultKeySequence": "Meta+Shift+W", + "action": "windowMoveUp", + }, + { + "name": "window-move-down", + "description": "Move window down", + "defaultKeySequence": "Meta+Shift+S", + "action": "windowMoveDown", + }, + { + "name": "window-move-start", + "description": "Move window to start", + "defaultKeySequence": "Meta+Shift+Home", + "action": "windowMoveStart", + }, + { + "name": "window-move-end", + "description": "Move window to end", + "defaultKeySequence": "Meta+Shift+End", + "action": "windowMoveEnd", + }, + { + "name": "window-expand", + "description": "Expand window", + "defaultKeySequence": "Meta+X", + "action": "windowExpand", + }, + { + "name": "column-move-left", + "description": "Move column left", + "defaultKeySequence": "Meta+Ctrl+Shift+A", + "action": "columnMoveLeft", + }, + { + "name": "column-move-right", + "description": "Move column right", + "defaultKeySequence": "Meta+Ctrl+Shift+D", + "action": "columnMoveRight", + }, + { + "name": "column-move-start", + "description": "Move column to start", + "defaultKeySequence": "Meta+Ctrl+Shift+Home", + "action": "columnMoveStart", + }, + { + "name": "column-move-end", + "description": "Move column to end", + "defaultKeySequence": "Meta+Ctrl+Shift+End", + "action": "columnMoveEnd", + }, + { + "name": "column-expand", + "description": "Expand column", + "defaultKeySequence": "Meta+Ctrl+X", + "action": "columnExpand", + }, + { + "name": "column-expand-visible", + "description": "Expand visible columns", + "defaultKeySequence": "Meta+Ctrl+Alt+X", + "action": "columnExpandVisible", + }, + { + "name": "grid-scroll-focused", + "description": "Scroll to focused window", + "defaultKeySequence": "Meta+Alt+Return", + "action": "gridScrollFocused", + }, + { + "name": "grid-scroll-left-column", + "description": "Scroll one column to the left", + "defaultKeySequence": "Meta+Alt+A", + "action": "gridScrollLeftColumn", + }, + { + "name": "grid-scroll-left-column", + "description": "Scroll one column to the left", + "defaultKeySequence": "Meta+Alt+A", + "action": "gridScrollLeftColumn", + }, + { + "name": "grid-scroll-right-column", + "description": "Scroll one column to the right", + "defaultKeySequence": "Meta+Alt+D", + "action": "gridScrollRightColumn", + }, + { + "name": "grid-scroll-left", + "description": "Scroll left", + "defaultKeySequence": "Meta+Alt+PgUp", + "action": "gridScrollLeft", + }, + { + "name": "grid-scroll-right", + "description": "Scroll right", + "defaultKeySequence": "Meta+Alt+PgDown", + "action": "gridScrollRight", + }, + { + "name": "grid-scroll-start", + "description": "Scroll to start", + "defaultKeySequence": "Meta+Alt+Home", + "action": "gridScrollStart", + }, + { + "name": "grid-scroll-end", + "description": "Scroll to end", + "defaultKeySequence": "Meta+Alt+End", + "action": "gridScrollEnd", + }, + + { + "name": "focus-", + "description": "Move focus to column ", + "defaultKeySequence": "Meta+", + "action": "focusColumn", + "repeat": 9 + }, + { + "name": "window-move-to-column-", + "description": "Move window to column ", + "defaultKeySequence": "Meta+Shift+", + "action": "windowMoveToColumn", + "repeat": 9 + }, + { + "name": "column-move-to-column-", + "description": "Move column to position ", + "defaultKeySequence": "Meta+Ctrl+Shift+", + "action": "columnMoveToColumn", + "repeat": 9 + }, + { + "name": "column-move-to-desktop-", + "description": "Move column to desktop ", + "defaultKeySequence": "Meta+Ctrl+Shift+F", + "action": "columnMoveToDesktop", + "repeat": 12 + }, + { + "name": "tail-move-to-desktop-", + "description": "Move this and all following columns to desktop ", + "defaultKeySequence": "Meta+Ctrl+Shift+Alt+F", + "action": "tailMoveToDesktop", + "repeat": 12 + }, +]; diff --git a/src/keyBindings/loader.ts b/src/keyBindings/loader.ts index 4a359c3..c7f45b6 100644 --- a/src/keyBindings/loader.ts +++ b/src/keyBindings/loader.ts @@ -9,56 +9,36 @@ function catchWrap(f: () => void) { }; } -function registerKeyBinding(title: string, text: string, keySequence: string, callback: () => void) { - KWin.registerShortcut("karousel-"+title, "Karousel: "+text, keySequence, catchWrap(callback)); +function registerKeyBinding(name: string, description: string, keySequence: string, callback: () => void) { + KWin.registerShortcut( + "karousel-" + name, + "Karousel: " + description, + keySequence, + catchWrap(callback), + ); } -function registerNumKeyBindings(title: string, text: string, keySequence: string, callback: (i: number) => void, n: number) { +function registerNumKeyBindings(name: string, description: string, keySequence: string, callback: (i: number) => void, n: number) { for (let i = 0; i < n; i++) { const numKey = String(i + 1); - registerKeyBinding(title+numKey, text+numKey, keySequence+numKey, () => callback(i)); + registerKeyBinding( + name + numKey, + description + numKey, + keySequence + numKey, + () => callback(i), + ); } } function registerKeyBindings(world: World) { const actions = initActions(world); - - registerKeyBinding("window-toggle-floating", "Toggle floating", "Meta+Space", actions.windowToggleFloating); - - registerKeyBinding("focus-left", "Move focus left", "Meta+A", actions.focusLeft); - registerKeyBinding("focus-right", "Move focus right", "Meta+D", actions.focusRight); - registerKeyBinding("focus-up", "Move focus up", "Meta+W", actions.focusUp); - registerKeyBinding("focus-down", "Move focus down", "Meta+S", actions.focusDown); - registerKeyBinding("focus-start", "Move focus to start", "Meta+Home", actions.focusStart); - registerKeyBinding("focus-end", "Move focus to end", "Meta+End", actions.focusEnd); - - registerKeyBinding("window-move-left", "Move window left", "Meta+Shift+A", actions.windowMoveLeft); - registerKeyBinding("window-move-right", "Move window right", "Meta+Shift+D", actions.windowMoveRight); - registerKeyBinding("window-move-up", "Move window up", "Meta+Shift+W", actions.windowMoveUp); - registerKeyBinding("window-move-down", "Move window down", "Meta+Shift+S", actions.windowMoveDown); - registerKeyBinding("window-move-start", "Move window to start", "Meta+Shift+Home", actions.windowMoveStart); - registerKeyBinding("window-move-end", "Move window to end", "Meta+Shift+End", actions.windowMoveEnd); - registerKeyBinding("window-expand", "Expand window", "Meta+X", actions.windowExpand); - - registerKeyBinding("column-move-left", "Move column left", "Meta+Ctrl+Shift+A", actions.columnMoveLeft); - registerKeyBinding("column-move-right", "Move column right", "Meta+Ctrl+Shift+D", actions.columnMoveRight); - registerKeyBinding("column-move-start", "Move column to start", "Meta+Ctrl+Shift+Home", actions.columnMoveStart); - registerKeyBinding("column-move-end", "Move column to end", "Meta+Ctrl+Shift+End", actions.columnMoveEnd); - registerKeyBinding("column-expand", "Expand column", "Meta+Ctrl+X", actions.columnExpand); - registerKeyBinding("column-expand-visible", "Expand visible columns", "Meta+Ctrl+Alt+X", actions.columnExpandVisible) - - registerKeyBinding("grid-scroll-focused", "Scroll to focused window", "Meta+Alt+Return", actions.gridScrollFocused); - registerKeyBinding("grid-scroll-left-column", "Scroll one column to the left", "Meta+Alt+A", actions.gridScrollLeftColumn); - registerKeyBinding("grid-scroll-left-column", "Scroll one column to the left", "Meta+Alt+A", actions.gridScrollLeftColumn); - registerKeyBinding("grid-scroll-right-column", "Scroll one column to the right", "Meta+Alt+D", actions.gridScrollRightColumn); - registerKeyBinding("grid-scroll-left", "Scroll left", "Meta+Alt+PgUp", actions.gridScrollLeft); - registerKeyBinding("grid-scroll-right", "Scroll right", "Meta+Alt+PgDown", actions.gridScrollRight); - registerKeyBinding("grid-scroll-start", "Scroll to start", "Meta+Alt+Home", actions.gridScrollStart); - registerKeyBinding("grid-scroll-end", "Scroll to end", "Meta+Alt+End", actions.gridScrollEnd); - - registerNumKeyBindings("focus-", "Move focus to column ", "Meta+", actions.focusColumn, 9); - registerNumKeyBindings("window-move-to-column-", "Move window to column ", "Meta+Shift+", actions.windowMoveToColumn, 9); - registerNumKeyBindings("column-move-to-column-", "Move column to position ", "Meta+Ctrl+Shift+", actions.columnMoveToColumn, 9); - registerNumKeyBindings("column-move-to-desktop-", "Move column to desktop ", "Meta+Ctrl+Shift+F", actions.columnMoveToDesktop, 12); - registerNumKeyBindings("tail-move-to-desktop-", "Move this and all following columns to desktop ", "Meta+Ctrl+Shift+Alt+F", actions.tailMoveToDesktop, 12); + for (const binding of keyBindings) { + if (binding.repeat === undefined) { + const action = <() => void> actions[binding.action]; + registerKeyBinding(binding.name, binding.description, binding.defaultKeySequence, action); + } else { + const action = <(n: number) => void> actions[binding.action]; + registerNumKeyBindings(binding.name, binding.description, binding.defaultKeySequence, action, binding.repeat); + } + } }