From c683d10008efef0ee8285a1bf10f1216cedbd4cc Mon Sep 17 00:00:00 2001 From: Cole Lashley Date: Tue, 23 Apr 2024 11:18:39 -0700 Subject: [PATCH] Keybinding fixes (#591) * keybinding fixes * changed screen and session commands to functions rather than slash commands to avoid printing errors * added a static wait for console commands * added sleep hardcode * add an isArray check for commandStr --- assets/default-keybindings.json | 62 ++++++++++------------------- src/app/workspace/workspaceview.tsx | 5 ++- src/models/model.ts | 9 ++++- src/util/keyutil.ts | 43 +++++++++++++++----- src/util/util.ts | 8 ++++ 5 files changed, 76 insertions(+), 51 deletions(-) diff --git a/assets/default-keybindings.json b/assets/default-keybindings.json index fa7bb60b7..fd93e6637 100644 --- a/assets/default-keybindings.json +++ b/assets/default-keybindings.json @@ -107,12 +107,12 @@ { "command": "app:openConnectionsView", "keys": [], - "commandStr": "/mainview connections" + "commandStr": ["/mainview connections"] }, { "command": "app:openSettingsView", "keys": [], - "commandStr": "/mainview clientsettings" + "commandStr": ["/mainview clientsettings"] }, { "command": "app:newTab", @@ -149,48 +149,39 @@ }, { "command": "app:selectTab-1", - "keys": ["Cmd:1"], - "commandStr":"/screen 1" + "keys": ["Cmd:1"] }, { "command": "app:selectTab-2", - "keys": ["Cmd:2"], - "commandStr":"/screen 2" + "keys": ["Cmd:2"] }, { "command": "app:selectTab-3", - "keys": ["Cmd:3"], - "commandStr":"/screen 3" + "keys": ["Cmd:3"] }, { "command": "app:selectTab-4", - "keys": ["Cmd:4"], - "commandStr":"/screen 4" + "keys": ["Cmd:4"] }, { "command": "app:selectTab-5", - "keys": ["Cmd:5"], - "commandStr":"/screen 5" + "keys": ["Cmd:5"] }, { "command": "app:selectTab-6", - "keys": ["Cmd:6"], - "commandStr":"/screen 6" + "keys": ["Cmd:6"] }, { "command": "app:selectTab-7", - "keys": ["Cmd:7"], - "commandStr":"/screen 7" + "keys": ["Cmd:7"] }, { "command": "app:selectTab-8", - "keys": ["Cmd:8"], - "commandStr":"/screen 8" + "keys": ["Cmd:8"] }, { "command": "app:selectTab-9", - "keys": ["Cmd:9"], - "commandStr":"/screen 9" + "keys": ["Cmd:9"] }, { "command": "app:selectTabLeft", @@ -202,48 +193,39 @@ }, { "command": "app:selectWorkspace-1", - "keys": ["Cmd:Ctrl:1"], - "commandStr": "/session 1" + "keys": ["Cmd:Ctrl:1"] }, { "command": "app:selectWorkspace-2", - "keys": ["Cmd:Ctrl:2"], - "commandStr": "/session 2" + "keys": ["Cmd:Ctrl:2"] }, { "command": "app:selectWorkspace-3", - "keys": ["Cmd:Ctrl:3"], - "commandStr": "/session 3" + "keys": ["Cmd:Ctrl:3"] }, { "command": "app:selectWorkspace-4", - "keys": ["Cmd:Ctrl:4"], - "commandStr": "/session 4" + "keys": ["Cmd:Ctrl:4"] }, { "command": "app:selectWorkspace-5", - "keys": ["Cmd:Ctrl:5"], - "commandStr": "/session 5" + "keys": ["Cmd:Ctrl:5"] }, { "command": "app:selectWorkspace-6", - "keys": ["Cmd:Ctrl:6"], - "commandStr": "/session 6" + "keys": ["Cmd:Ctrl:6"] }, { "command": "app:selectWorkspace-7", - "keys": ["Cmd:Ctrl:7"], - "commandStr": "/session 7" + "keys": ["Cmd:Ctrl:7"] }, { "command": "app:selectWorkspace-8", - "keys": ["Cmd:Ctrl:8"], - "commandStr": "/session 8" + "keys": ["Cmd:Ctrl:8"] }, { "command": "app:selectWorkspace-9", - "keys": ["Cmd:Ctrl:9"], - "commandStr": "/session 9" + "keys": ["Cmd:Ctrl:9"] }, { "command": "app:toggleSidebar", @@ -256,7 +238,7 @@ { "command": "app:openBookmarksView", "keys": ["Cmd:b"], - "commandStr": "/bookmarks:show" + "commandStr": ["/bookmarks:show"] }, { "command": "bookmarks:edit", @@ -301,7 +283,7 @@ { "command": "cmdinput:openHistory", "keys": ["Ctrl:r"], - "commandStr": "/history" + "commandStr": ["/history"] }, { "command": "cmdinput:openAIChat", diff --git a/src/app/workspace/workspaceview.tsx b/src/app/workspace/workspaceview.tsx index 58943fb16..cd8e41100 100644 --- a/src/app/workspace/workspaceview.tsx +++ b/src/app/workspace/workspaceview.tsx @@ -47,7 +47,10 @@ class SessionKeybindings extends React.Component<{}, {}> { return true; }); for (let index = 1; index <= 9; index++) { - keybindManager.registerKeybinding("mainview", "session", "app:selectTab-" + index, null); + keybindManager.registerKeybinding("mainview", "session", "app:selectTab-" + index, (waveEvent) => { + GlobalModel.onSwitchScreenCmd(index); + return true; + }); } keybindManager.registerKeybinding("mainview", "session", "app:selectTabLeft", (waveEvent) => { GlobalModel.onBracketCmd(-1); diff --git a/src/models/model.ts b/src/models/model.ts index 2955a41be..260c6e39c 100644 --- a/src/models/model.ts +++ b/src/models/model.ts @@ -294,7 +294,10 @@ class Model { initAppKeybindings() { for (let index = 1; index <= 9; index++) { - this.keybindManager.registerKeybinding("app", "model", "app:selectWorkspace-" + index, null); + this.keybindManager.registerKeybinding("app", "model", "app:selectWorkspace-" + index, (waveEvent) => { + this.onSwitchSessionCmd(index); + return true; + }); } this.keybindManager.registerKeybinding("app", "model", "app:focusCmdInput", (waveEvent) => { this.onFocusCmdInputPressed(); @@ -845,6 +848,10 @@ class Model { } } + onSwitchScreenCmd(digit: number) { + GlobalCommandRunner.switchScreen(String(digit)); + } + onSwitchSessionCmd(digit: number) { GlobalCommandRunner.switchSession(String(digit)); } diff --git a/src/util/keyutil.ts b/src/util/keyutil.ts index e9f285034..15cc0811f 100644 --- a/src/util/keyutil.ts +++ b/src/util/keyutil.ts @@ -1,6 +1,7 @@ import * as React from "react"; import * as mobx from "mobx"; import * as electron from "electron"; +import * as util from "@/util/util"; import { parse } from "node:path"; import { v4 as uuidv4 } from "uuid"; import defaultKeybindingsFile from "../../assets/default-keybindings.json"; @@ -25,18 +26,19 @@ const KeyTypeCode = "code"; type KeybindCallback = (event: WaveKeyboardEvent) => boolean; type KeybindConfigArray = Array; -type KeybindConfig = { command: string; keys: Array; commandStr?: string; info?: string }; +type KeybindConfig = { command: string; keys: Array; commandStr?: Array; info?: string }; const Callback = "callback"; const Command = "command"; const DumpLogs = false; +const DefaultConsoleCommandWait = 200; type Keybind = { domain: string; keybinding: string; action: string; callback: KeybindCallback; - commandStr: string; + commandStr: Array; }; const KeybindLevels = ["system", "modal", "app", "mainview", "pane", "plugin", "control"]; @@ -51,6 +53,7 @@ class KeybindManager { globalModel: any; activeKeybindsVersion: OV; lastKeyData: { domain: string; keyPress: string }; + consoleCommandWait: number; constructor(GlobalModel: any) { this.levelMap = new Map(); @@ -69,6 +72,7 @@ class KeybindManager { this.globalModel = GlobalModel; this.initKeyDescriptionsMap(); this.lastKeyData = { domain: "none", keyPress: "none" }; + this.consoleCommandWait = DefaultConsoleCommandWait; } initKeyDescriptionsMap() { @@ -105,7 +109,7 @@ class KeybindManager { if ( defaultCmd != null && defaultCmd.commandStr != null && - (curKeybind.commandStr == null || curKeybind.commandStr == "") + (curKeybind.commandStr == null || curKeybind.commandStr.length == 0) ) { curKeybind.commandStr = this.keyDescriptionsMap.get(curKeybind.command).commandStr; } @@ -173,14 +177,14 @@ class KeybindManager { getUIDescription(keyDescription: string, prettyPrint: boolean = true): KeybindConfig { let keybinds = this.getKeybindsFromDescription(keyDescription, prettyPrint); if (!this.keyDescriptionsMap.has(keyDescription)) { - return { keys: keybinds, info: "", command: keyDescription, commandStr: "" }; + return { keys: keybinds, info: "", command: keyDescription, commandStr: [] }; } let curKeybindConfig = this.keyDescriptionsMap.get(keyDescription); let curInfo = ""; if (curKeybindConfig.info) { curInfo = curKeybindConfig.info; } - let curCommandStr = ""; + let curCommandStr = []; if (curKeybindConfig.commandStr) { curCommandStr = curKeybindConfig.commandStr; } @@ -224,10 +228,15 @@ class KeybindManager { runSlashCommand(curKeybind: Keybind): boolean { let curConfigKeybind = this.keyDescriptionsMap.get(curKeybind.keybinding); - if (curConfigKeybind == null || curConfigKeybind.commandStr == null || curKeybind.commandStr == "") { + if ( + curConfigKeybind == null || + curConfigKeybind.commandStr == null || + !util.isArray(curConfigKeybind.commandStr) || + curConfigKeybind.commandStr.length == 0 + ) { return false; } - let commandsList = curConfigKeybind.commandStr.trim().split(";"); + let commandsList = [...curConfigKeybind.commandStr]; this.runIndividualSlashCommand(commandsList); return true; } @@ -237,13 +246,29 @@ class KeybindManager { return true; } let curCommand = commandsList.shift(); - let prtn = this.globalModel.submitRawCommand(curCommand, false, false); + let prtn = this.globalModel.submitRawCommand(curCommand, false, true); prtn.then((rtn) => { if (!rtn.success) { console.log("error running command ", curCommand); return false; } - return this.runIndividualSlashCommand(commandsList); + if (curCommand.trim()[0] != "/") { + setTimeout(() => { + return this.runIndividualSlashCommand(commandsList); + }, this.consoleCommandWait); + } else if (curCommand.includes("/sleep")) { + const sleepMsStr = curCommand.trim().replace("/sleep", "").trim(); + const sleepMs = Number(sleepMsStr); + if (Number.isNaN(sleepMs)) { + console.log("sleep error: couldn't parse arg"); + return false; + } + setTimeout(() => { + return this.runIndividualSlashCommand(commandsList); + }, sleepMs); + } else { + return this.runIndividualSlashCommand(commandsList); + } }).catch((error) => { console.log("caught error running command ", curCommand, ": ", error); return false; diff --git a/src/util/util.ts b/src/util/util.ts index d3822a6f4..1fe2a3d25 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -13,6 +13,13 @@ function isBlank(s: string): boolean { return s == null || s == ""; } +function isArray(obj: any): boolean { + if (obj == null) { + return false; + } + return Array.isArray(obj) || mobx.isObservableArray(obj); +} + function handleNotOkResp(resp: any, url: URL): Promise { const errMsg = sprintf( "Bad status code response from fetch '%s': code=%d %s", @@ -412,6 +419,7 @@ export { isModKeyPress, incObs, isBlank, + isArray, getTodayStr, getYesterdayStr, getDateStr,