From 36de526e8e8119d13d71f7da4043c0510c4222d5 Mon Sep 17 00:00:00 2001 From: Cole Lashley Date: Tue, 12 Mar 2024 12:36:52 -0700 Subject: [PATCH] App keybindings (#432) * added app keybindings * removed emain cmd-p * removed boilerplate for emain commands --- ...bindings.json => default-keybindings.json} | 8 + src/electron/emain.ts | 19 +-- src/electron/preload.js | 3 - src/models/model.ts | 157 ++++++++++++------ src/types/custom.d.ts | 3 - src/util/keyutil.ts | 4 +- 6 files changed, 114 insertions(+), 80 deletions(-) rename assets/{keybindings.json => default-keybindings.json} (97%) diff --git a/assets/keybindings.json b/assets/default-keybindings.json similarity index 97% rename from assets/keybindings.json rename to assets/default-keybindings.json index 9507bc7c..c942ec4c 100644 --- a/assets/keybindings.json +++ b/assets/default-keybindings.json @@ -39,6 +39,14 @@ "command": "app:openTabSearchModal", "keys": ["Cmd:p"] }, + { + "command": "app:openConnectionsView", + "keys": [] + }, + { + "command": "app:openSettingsView", + "keys": [] + }, { "command": "app:newTab", "keys": ["Cmd:t"] diff --git a/src/electron/emain.ts b/src/electron/emain.ts index 7d278e7f..5aff80c4 100644 --- a/src/electron/emain.ts +++ b/src/electron/emain.ts @@ -355,13 +355,6 @@ function createMainWindow(clientData: ClientDataType | null): Electron.BrowserWi e.preventDefault(); return; } - if (checkKeyPressed(waveEvent, "Cmd:i")) { - e.preventDefault(); - if (!input.alt) { - win.webContents.send("i-cmd", mods); - } - return; - } if (checkKeyPressed(waveEvent, "Cmd:r")) { e.preventDefault(); win.webContents.send("r-cmd", mods); @@ -377,16 +370,6 @@ function createMainWindow(clientData: ClientDataType | null): Electron.BrowserWi win.webContents.send("w-cmd", mods); return; } - if (checkKeyPressed(waveEvent, "Cmd:h")) { - win.webContents.send("h-cmd", mods); - e.preventDefault(); - return; - } - if (checkKeyPressed(waveEvent, "Cmd:p")) { - win.webContents.send("p-cmd", mods); - e.preventDefault(); - return; - } if (checkKeyPressed(waveEvent, "Cmd:ArrowUp") || checkKeyPressed(waveEvent, "Cmd:ArrowDown")) { if (checkKeyPressed(waveEvent, "Cmd:ArrowUp")) { win.webContents.send("meta-arrowup"); @@ -405,7 +388,7 @@ function createMainWindow(clientData: ClientDataType | null): Electron.BrowserWi e.preventDefault(); return; } - if (input.code.startsWith("Digit") && input.meta) { + if (input.code.startsWith("Digit") && input.meta && !input.control) { const digitNum = parseInt(input.code.substring(5)); if (isNaN(digitNum) || digitNum < 1 || digitNum > 9) { return; diff --git a/src/electron/preload.js b/src/electron/preload.js index 35c0c6b3..14be4eb1 100644 --- a/src/electron/preload.js +++ b/src/electron/preload.js @@ -21,11 +21,8 @@ contextBridge.exposeInMainWorld("api", { getAppUpdateStatus: () => ipcRenderer.sendSync("get-app-update-status"), onAppUpdateStatus: (callback) => ipcRenderer.on("app-update-status", (_, val) => callback(val)), onTCmd: (callback) => ipcRenderer.on("t-cmd", callback), - onICmd: (callback) => ipcRenderer.on("i-cmd", callback), onLCmd: (callback) => ipcRenderer.on("l-cmd", callback), - onHCmd: (callback) => ipcRenderer.on("h-cmd", callback), onWCmd: (callback) => ipcRenderer.on("w-cmd", callback), - onPCmd: (callback) => ipcRenderer.on("p-cmd", callback), onRCmd: (callback) => ipcRenderer.on("r-cmd", callback), onZoomChanged: (callback) => ipcRenderer.on("zoom-changed", callback), onMetaArrowUp: (callback) => ipcRenderer.on("meta-arrowup", callback), diff --git a/src/models/model.ts b/src/models/model.ts index a548d6c2..15af1ae5 100644 --- a/src/models/model.ts +++ b/src/models/model.ts @@ -146,6 +146,7 @@ class Model { this.keybindManager = new KeybindManager(); this.readConfigKeybindings(); this.initSystemKeybindings(); + this.initAppKeybindings(); this.inputModel = new InputModel(this); this.pluginsModel = new PluginsModel(this); this.bookmarksModel = new BookmarksModel(this); @@ -175,10 +176,7 @@ class Model { return fontSize; }); getApi().onTCmd(this.onTCmd.bind(this)); - getApi().onICmd(this.onICmd.bind(this)); getApi().onLCmd(this.onLCmd.bind(this)); - getApi().onHCmd(this.onHCmd.bind(this)); - getApi().onPCmd(this.onPCmd.bind(this)); getApi().onWCmd(this.onWCmd.bind(this)); getApi().onRCmd(this.onRCmd.bind(this)); getApi().onZoomChanged(this.onZoomChanged.bind(this)); @@ -220,12 +218,49 @@ class Model { } initSystemKeybindings() { - this.keybindManager.registerKeybinding("system", "electron", "any", (waveEvent) => { - if (this.keybindManager.checkKeyPressed(waveEvent, "system:toggleDeveloperTools")) { - getApi().toggleDeveloperTools(); + this.keybindManager.registerKeybinding("system", "electron", "system:toggleDeveloperTools", (waveEvent) => { + getApi().toggleDeveloperTools(); + return true; + }); + } + + initAppKeybindings() { + for (let index = 1; index <= 9; index++) { + this.keybindManager.registerKeybinding("app", "model", "app:selectWorkspace-" + index, (waveEvent) => { + this.onSwitchSessionCmd(index); return true; - } - return false; + }); + } + + this.keybindManager.registerKeybinding("app", "model", "app:focusCmdInput", (waveEvent) => { + console.log("focus cmd input callback"); + this.onFocusCmdInputPressed(); + return true; + }); + + this.keybindManager.registerKeybinding("app", "model", "app:bookmarkActiveLine", (waveEvent) => { + this.onBookmarkViewPressed(); + return true; + }); + + this.keybindManager.registerKeybinding("app", "model", "app:openHistory", (waveEvent) => { + this.onOpenHistoryPressed(); + return true; + }); + + this.keybindManager.registerKeybinding("app", "model", "app:openTabSearchModal", (waveEvent) => { + this.onOpenTabSearchModalPressed(); + return true; + }); + + this.keybindManager.registerKeybinding("app", "model", "app:openConnectionsView", (waveEvent) => { + this.onOpenConnectionsViewPressed(); + return true; + }); + + this.keybindManager.registerKeybinding("app", "model", "app:openSettingsView", (waveEvent) => { + this.onOpenSettingsViewPressed(); + return true; }); } @@ -470,56 +505,49 @@ class Model { } if (this.activeMainView.get() == "bookmarks") { this.bookmarksModel.handleDocKeyDown(e); - return; } if (this.activeMainView.get() == "history") { this.historyViewModel.handleDocKeyDown(e); - return; } if (this.activeMainView.get() == "connections") { this.historyViewModel.handleDocKeyDown(e); - return; } if (this.activeMainView.get() == "clientsettings") { this.historyViewModel.handleDocKeyDown(e); - return; - } - if (checkKeyPressed(waveEvent, "Escape")) { - e.preventDefault(); - if (this.activeMainView.get() == "webshare") { - this.showSessionView(); + } else { + if (checkKeyPressed(waveEvent, "Escape")) { + e.preventDefault(); + if (this.activeMainView.get() == "webshare") { + this.showSessionView(); + return; + } + if (this.clearModals()) { + return; + } + const inputModel = this.inputModel; + inputModel.toggleInfoMsg(); + if (inputModel.inputMode.get() != null) { + inputModel.resetInputMode(); + } return; } - if (this.clearModals()) { - return; - } - const inputModel = this.inputModel; - inputModel.toggleInfoMsg(); - if (inputModel.inputMode.get() != null) { - inputModel.resetInputMode(); - } - return; - } - if (checkKeyPressed(waveEvent, "Cmd:b")) { - e.preventDefault(); - GlobalCommandRunner.bookmarksView(); - } - if (this.activeMainView.get() == "session" && checkKeyPressed(waveEvent, "Cmd:Ctrl:s")) { - e.preventDefault(); - const activeScreen = this.getActiveScreen(); - if (activeScreen != null) { - const isSidebarOpen = activeScreen.isSidebarOpen(); - if (isSidebarOpen) { - GlobalCommandRunner.screenSidebarClose(); - } else { - GlobalCommandRunner.screenSidebarOpen(); + if (this.activeMainView.get() == "session" && checkKeyPressed(waveEvent, "Cmd:Ctrl:s")) { + e.preventDefault(); + const activeScreen = this.getActiveScreen(); + if (activeScreen != null) { + const isSidebarOpen = activeScreen.isSidebarOpen(); + if (isSidebarOpen) { + GlobalCommandRunner.screenSidebarClose(); + } else { + GlobalCommandRunner.screenSidebarOpen(); + } } } - } - if (checkKeyPressed(waveEvent, "Cmd:d")) { - const ranDelete = this.deleteActiveLine(); - if (ranDelete) { - e.preventDefault(); + if (checkKeyPressed(waveEvent, "Cmd:d")) { + const ranDelete = this.deleteActiveLine(); + if (ranDelete) { + e.preventDefault(); + } } } this.keybindManager.processKeyEvent(e, waveEvent); @@ -719,8 +747,22 @@ class Model { GlobalCommandRunner.createNewScreen(); } - onICmd(e: any, mods: KeyModsType) { - this.inputModel.giveFocus(); + onBookmarkViewPressed() { + GlobalCommandRunner.bookmarksView(); + } + + onFocusCmdInputPressed() { + if (this.activeMainView.get() != "session") { + mobx.action(() => { + this.activeMainView.set("session"); + setTimeout(() => { + // allows for the session view to load + this.inputModel.giveFocus(); + }, 100); + })(); + } else { + this.inputModel.giveFocus(); + } } onLCmd(e: any, mods: KeyModsType) { @@ -730,14 +772,22 @@ class Model { } } - onHCmd(e: any, mods: KeyModsType) { + onOpenHistoryPressed() { this.historyViewModel.reSearch(); } - onPCmd(e: any, mods: KeyModsType) { + onOpenTabSearchModalPressed() { this.modalsModel.pushModal(appconst.TAB_SWITCHER); } + onOpenConnectionsViewPressed() { + this.activeMainView.set("connections"); + } + + onOpenSettingsViewPressed() { + this.activeMainView.set("clientsettings"); + } + getFocusedLine(): LineFocusType { if (this.inputModel.hasFocus()) { return { cmdInputFocus: true }; @@ -804,11 +854,12 @@ class Model { } } + onSwitchSessionCmd(digit: number) { + console.log("switching to ", digit); + GlobalCommandRunner.switchSession(String(digit)); + } + onDigitCmd(e: any, arg: { digit: number }, mods: KeyModsType) { - if (mods.meta && mods.ctrl) { - GlobalCommandRunner.switchSession(String(arg.digit)); - return; - } GlobalCommandRunner.switchScreen(String(arg.digit)); } diff --git a/src/types/custom.d.ts b/src/types/custom.d.ts index 4395ca3b..20630fdf 100644 --- a/src/types/custom.d.ts +++ b/src/types/custom.d.ts @@ -896,10 +896,7 @@ declare global { getAppUpdateStatus: () => AppUpdateStatusType; onAppUpdateStatus: (callback: (status: AppUpdateStatusType) => void) => void; onTCmd: (callback: (mods: KeyModsType) => void) => void; - onICmd: (callback: (mods: KeyModsType) => void) => void; onLCmd: (callback: (mods: KeyModsType) => void) => void; - onHCmd: (callback: (mods: KeyModsType) => void) => void; - onPCmd: (callback: (mods: KeyModsType) => void) => void; onRCmd: (callback: (mods: KeyModsType) => void) => void; onWCmd: (callback: (mods: KeyModsType) => void) => void; onZoomChanged: (callback: () => void) => void; diff --git a/src/util/keyutil.ts b/src/util/keyutil.ts index 3e030936..705335b9 100644 --- a/src/util/keyutil.ts +++ b/src/util/keyutil.ts @@ -3,7 +3,7 @@ import * as mobx from "mobx"; import * as electron from "electron"; import { parse } from "node:path"; import { v4 as uuidv4 } from "uuid"; -import defaultKeybindingsFile from "../../assets/keybindings.json"; +import defaultKeybindingsFile from "../../assets/default-keybindings.json"; const defaultKeybindings: KeybindConfig = defaultKeybindingsFile; type KeyPressDecl = { @@ -68,7 +68,6 @@ class KeybindManager { let curUserCommand = ""; if (this.userKeybindings != null && this.userKeybindings instanceof Array) { try { - console.log("setting user keybindings"); for (let index = 0; index < this.userKeybindings.length; index++) { let curKeybind = this.userKeybindings[index]; if (curKeybind == null) { @@ -97,7 +96,6 @@ class KeybindManager { } } this.keyDescriptionsMap = newKeyDescriptions; - console.log("key desc map:", this.keyDescriptionsMap); } processLevel(nativeEvent: any, event: WaveKeyboardEvent, keybindsArray: Array): boolean {