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
This commit is contained in:
Cole Lashley 2024-04-23 11:18:39 -07:00 committed by GitHub
parent 265cc30347
commit c683d10008
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 76 additions and 51 deletions

View File

@ -107,12 +107,12 @@
{ {
"command": "app:openConnectionsView", "command": "app:openConnectionsView",
"keys": [], "keys": [],
"commandStr": "/mainview connections" "commandStr": ["/mainview connections"]
}, },
{ {
"command": "app:openSettingsView", "command": "app:openSettingsView",
"keys": [], "keys": [],
"commandStr": "/mainview clientsettings" "commandStr": ["/mainview clientsettings"]
}, },
{ {
"command": "app:newTab", "command": "app:newTab",
@ -149,48 +149,39 @@
}, },
{ {
"command": "app:selectTab-1", "command": "app:selectTab-1",
"keys": ["Cmd:1"], "keys": ["Cmd:1"]
"commandStr":"/screen 1"
}, },
{ {
"command": "app:selectTab-2", "command": "app:selectTab-2",
"keys": ["Cmd:2"], "keys": ["Cmd:2"]
"commandStr":"/screen 2"
}, },
{ {
"command": "app:selectTab-3", "command": "app:selectTab-3",
"keys": ["Cmd:3"], "keys": ["Cmd:3"]
"commandStr":"/screen 3"
}, },
{ {
"command": "app:selectTab-4", "command": "app:selectTab-4",
"keys": ["Cmd:4"], "keys": ["Cmd:4"]
"commandStr":"/screen 4"
}, },
{ {
"command": "app:selectTab-5", "command": "app:selectTab-5",
"keys": ["Cmd:5"], "keys": ["Cmd:5"]
"commandStr":"/screen 5"
}, },
{ {
"command": "app:selectTab-6", "command": "app:selectTab-6",
"keys": ["Cmd:6"], "keys": ["Cmd:6"]
"commandStr":"/screen 6"
}, },
{ {
"command": "app:selectTab-7", "command": "app:selectTab-7",
"keys": ["Cmd:7"], "keys": ["Cmd:7"]
"commandStr":"/screen 7"
}, },
{ {
"command": "app:selectTab-8", "command": "app:selectTab-8",
"keys": ["Cmd:8"], "keys": ["Cmd:8"]
"commandStr":"/screen 8"
}, },
{ {
"command": "app:selectTab-9", "command": "app:selectTab-9",
"keys": ["Cmd:9"], "keys": ["Cmd:9"]
"commandStr":"/screen 9"
}, },
{ {
"command": "app:selectTabLeft", "command": "app:selectTabLeft",
@ -202,48 +193,39 @@
}, },
{ {
"command": "app:selectWorkspace-1", "command": "app:selectWorkspace-1",
"keys": ["Cmd:Ctrl:1"], "keys": ["Cmd:Ctrl:1"]
"commandStr": "/session 1"
}, },
{ {
"command": "app:selectWorkspace-2", "command": "app:selectWorkspace-2",
"keys": ["Cmd:Ctrl:2"], "keys": ["Cmd:Ctrl:2"]
"commandStr": "/session 2"
}, },
{ {
"command": "app:selectWorkspace-3", "command": "app:selectWorkspace-3",
"keys": ["Cmd:Ctrl:3"], "keys": ["Cmd:Ctrl:3"]
"commandStr": "/session 3"
}, },
{ {
"command": "app:selectWorkspace-4", "command": "app:selectWorkspace-4",
"keys": ["Cmd:Ctrl:4"], "keys": ["Cmd:Ctrl:4"]
"commandStr": "/session 4"
}, },
{ {
"command": "app:selectWorkspace-5", "command": "app:selectWorkspace-5",
"keys": ["Cmd:Ctrl:5"], "keys": ["Cmd:Ctrl:5"]
"commandStr": "/session 5"
}, },
{ {
"command": "app:selectWorkspace-6", "command": "app:selectWorkspace-6",
"keys": ["Cmd:Ctrl:6"], "keys": ["Cmd:Ctrl:6"]
"commandStr": "/session 6"
}, },
{ {
"command": "app:selectWorkspace-7", "command": "app:selectWorkspace-7",
"keys": ["Cmd:Ctrl:7"], "keys": ["Cmd:Ctrl:7"]
"commandStr": "/session 7"
}, },
{ {
"command": "app:selectWorkspace-8", "command": "app:selectWorkspace-8",
"keys": ["Cmd:Ctrl:8"], "keys": ["Cmd:Ctrl:8"]
"commandStr": "/session 8"
}, },
{ {
"command": "app:selectWorkspace-9", "command": "app:selectWorkspace-9",
"keys": ["Cmd:Ctrl:9"], "keys": ["Cmd:Ctrl:9"]
"commandStr": "/session 9"
}, },
{ {
"command": "app:toggleSidebar", "command": "app:toggleSidebar",
@ -256,7 +238,7 @@
{ {
"command": "app:openBookmarksView", "command": "app:openBookmarksView",
"keys": ["Cmd:b"], "keys": ["Cmd:b"],
"commandStr": "/bookmarks:show" "commandStr": ["/bookmarks:show"]
}, },
{ {
"command": "bookmarks:edit", "command": "bookmarks:edit",
@ -301,7 +283,7 @@
{ {
"command": "cmdinput:openHistory", "command": "cmdinput:openHistory",
"keys": ["Ctrl:r"], "keys": ["Ctrl:r"],
"commandStr": "/history" "commandStr": ["/history"]
}, },
{ {
"command": "cmdinput:openAIChat", "command": "cmdinput:openAIChat",

View File

@ -47,7 +47,10 @@ class SessionKeybindings extends React.Component<{}, {}> {
return true; return true;
}); });
for (let index = 1; index <= 9; index++) { 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) => { keybindManager.registerKeybinding("mainview", "session", "app:selectTabLeft", (waveEvent) => {
GlobalModel.onBracketCmd(-1); GlobalModel.onBracketCmd(-1);

View File

@ -294,7 +294,10 @@ class Model {
initAppKeybindings() { initAppKeybindings() {
for (let index = 1; index <= 9; index++) { 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.keybindManager.registerKeybinding("app", "model", "app:focusCmdInput", (waveEvent) => {
this.onFocusCmdInputPressed(); this.onFocusCmdInputPressed();
@ -845,6 +848,10 @@ class Model {
} }
} }
onSwitchScreenCmd(digit: number) {
GlobalCommandRunner.switchScreen(String(digit));
}
onSwitchSessionCmd(digit: number) { onSwitchSessionCmd(digit: number) {
GlobalCommandRunner.switchSession(String(digit)); GlobalCommandRunner.switchSession(String(digit));
} }

View File

@ -1,6 +1,7 @@
import * as React from "react"; import * as React from "react";
import * as mobx from "mobx"; import * as mobx from "mobx";
import * as electron from "electron"; import * as electron from "electron";
import * as util from "@/util/util";
import { parse } from "node:path"; import { parse } from "node:path";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import defaultKeybindingsFile from "../../assets/default-keybindings.json"; import defaultKeybindingsFile from "../../assets/default-keybindings.json";
@ -25,18 +26,19 @@ const KeyTypeCode = "code";
type KeybindCallback = (event: WaveKeyboardEvent) => boolean; type KeybindCallback = (event: WaveKeyboardEvent) => boolean;
type KeybindConfigArray = Array<KeybindConfig>; type KeybindConfigArray = Array<KeybindConfig>;
type KeybindConfig = { command: string; keys: Array<string>; commandStr?: string; info?: string }; type KeybindConfig = { command: string; keys: Array<string>; commandStr?: Array<string>; info?: string };
const Callback = "callback"; const Callback = "callback";
const Command = "command"; const Command = "command";
const DumpLogs = false; const DumpLogs = false;
const DefaultConsoleCommandWait = 200;
type Keybind = { type Keybind = {
domain: string; domain: string;
keybinding: string; keybinding: string;
action: string; action: string;
callback: KeybindCallback; callback: KeybindCallback;
commandStr: string; commandStr: Array<string>;
}; };
const KeybindLevels = ["system", "modal", "app", "mainview", "pane", "plugin", "control"]; const KeybindLevels = ["system", "modal", "app", "mainview", "pane", "plugin", "control"];
@ -51,6 +53,7 @@ class KeybindManager {
globalModel: any; globalModel: any;
activeKeybindsVersion: OV<number>; activeKeybindsVersion: OV<number>;
lastKeyData: { domain: string; keyPress: string }; lastKeyData: { domain: string; keyPress: string };
consoleCommandWait: number;
constructor(GlobalModel: any) { constructor(GlobalModel: any) {
this.levelMap = new Map(); this.levelMap = new Map();
@ -69,6 +72,7 @@ class KeybindManager {
this.globalModel = GlobalModel; this.globalModel = GlobalModel;
this.initKeyDescriptionsMap(); this.initKeyDescriptionsMap();
this.lastKeyData = { domain: "none", keyPress: "none" }; this.lastKeyData = { domain: "none", keyPress: "none" };
this.consoleCommandWait = DefaultConsoleCommandWait;
} }
initKeyDescriptionsMap() { initKeyDescriptionsMap() {
@ -105,7 +109,7 @@ class KeybindManager {
if ( if (
defaultCmd != null && defaultCmd != null &&
defaultCmd.commandStr != null && defaultCmd.commandStr != null &&
(curKeybind.commandStr == null || curKeybind.commandStr == "") (curKeybind.commandStr == null || curKeybind.commandStr.length == 0)
) { ) {
curKeybind.commandStr = this.keyDescriptionsMap.get(curKeybind.command).commandStr; curKeybind.commandStr = this.keyDescriptionsMap.get(curKeybind.command).commandStr;
} }
@ -173,14 +177,14 @@ class KeybindManager {
getUIDescription(keyDescription: string, prettyPrint: boolean = true): KeybindConfig { getUIDescription(keyDescription: string, prettyPrint: boolean = true): KeybindConfig {
let keybinds = this.getKeybindsFromDescription(keyDescription, prettyPrint); let keybinds = this.getKeybindsFromDescription(keyDescription, prettyPrint);
if (!this.keyDescriptionsMap.has(keyDescription)) { 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 curKeybindConfig = this.keyDescriptionsMap.get(keyDescription);
let curInfo = ""; let curInfo = "";
if (curKeybindConfig.info) { if (curKeybindConfig.info) {
curInfo = curKeybindConfig.info; curInfo = curKeybindConfig.info;
} }
let curCommandStr = ""; let curCommandStr = [];
if (curKeybindConfig.commandStr) { if (curKeybindConfig.commandStr) {
curCommandStr = curKeybindConfig.commandStr; curCommandStr = curKeybindConfig.commandStr;
} }
@ -224,10 +228,15 @@ class KeybindManager {
runSlashCommand(curKeybind: Keybind): boolean { runSlashCommand(curKeybind: Keybind): boolean {
let curConfigKeybind = this.keyDescriptionsMap.get(curKeybind.keybinding); 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; return false;
} }
let commandsList = curConfigKeybind.commandStr.trim().split(";"); let commandsList = [...curConfigKeybind.commandStr];
this.runIndividualSlashCommand(commandsList); this.runIndividualSlashCommand(commandsList);
return true; return true;
} }
@ -237,13 +246,29 @@ class KeybindManager {
return true; return true;
} }
let curCommand = commandsList.shift(); let curCommand = commandsList.shift();
let prtn = this.globalModel.submitRawCommand(curCommand, false, false); let prtn = this.globalModel.submitRawCommand(curCommand, false, true);
prtn.then((rtn) => { prtn.then((rtn) => {
if (!rtn.success) { if (!rtn.success) {
console.log("error running command ", curCommand); console.log("error running command ", curCommand);
return false; 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) => { }).catch((error) => {
console.log("caught error running command ", curCommand, ": ", error); console.log("caught error running command ", curCommand, ": ", error);
return false; return false;

View File

@ -13,6 +13,13 @@ function isBlank(s: string): boolean {
return s == null || s == ""; 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<any> { function handleNotOkResp(resp: any, url: URL): Promise<any> {
const errMsg = sprintf( const errMsg = sprintf(
"Bad status code response from fetch '%s': code=%d %s", "Bad status code response from fetch '%s': code=%d %s",
@ -412,6 +419,7 @@ export {
isModKeyPress, isModKeyPress,
incObs, incObs,
isBlank, isBlank,
isArray,
getTodayStr, getTodayStr,
getYesterdayStr, getYesterdayStr,
getDateStr, getDateStr,