Keybindings second wave - System Keybindings (#386)

* added system level keybindings

* added process key event

* added fix for code check

* add event.returnValue, remove console.logs, change sendSync to send
This commit is contained in:
Cole Lashley 2024-03-06 21:31:03 -08:00 committed by GitHub
parent 29acdc6eff
commit 036b0ff989
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 8 deletions

View File

@ -354,8 +354,6 @@ function createMainWindow(clientData: ClientDataType | null): Electron.BrowserWi
e.preventDefault(); e.preventDefault();
if (!input.alt) { if (!input.alt) {
win.webContents.send("i-cmd", mods); win.webContents.send("i-cmd", mods);
} else {
win.webContents.toggleDevTools();
} }
return; return;
} }
@ -520,6 +518,13 @@ app.on("window-all-closed", () => {
if (unamePlatform !== "darwin") app.quit(); if (unamePlatform !== "darwin") app.quit();
}); });
electron.ipcMain.on("toggle-developer-tools", (event) => {
if (MainWindow != null) {
MainWindow.webContents.toggleDevTools();
}
event.returnValue = true;
});
electron.ipcMain.on("get-id", (event) => { electron.ipcMain.on("get-id", (event) => {
event.returnValue = instanceId + ":" + event.processId; event.returnValue = instanceId + ":" + event.processId;
}); });

View File

@ -1,6 +1,7 @@
let { contextBridge, ipcRenderer } = require("electron"); let { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("api", { contextBridge.exposeInMainWorld("api", {
toggleDeveloperTools: () => ipcRenderer.send("toggle-developer-tools"),
getId: () => ipcRenderer.sendSync("get-id"), getId: () => ipcRenderer.sendSync("get-id"),
getPlatform: () => ipcRenderer.sendSync("get-platform"), getPlatform: () => ipcRenderer.sendSync("get-platform"),
getIsDev: () => ipcRenderer.sendSync("get-isdev"), getIsDev: () => ipcRenderer.sendSync("get-isdev"),

View File

@ -19,7 +19,7 @@ import { WSControl } from "./ws";
import { cmdStatusIsRunning } from "@/app/line/lineutil"; import { cmdStatusIsRunning } from "@/app/line/lineutil";
import * as appconst from "@/app/appconst"; import * as appconst from "@/app/appconst";
import { remotePtrToString, cmdPacketString } from "@/util/modelutil"; import { remotePtrToString, cmdPacketString } from "@/util/modelutil";
import { checkKeyPressed, adaptFromReactOrNativeKeyEvent, setKeyUtilPlatform } from "@/util/keyutil"; import { KeybindManager, checkKeyPressed, adaptFromReactOrNativeKeyEvent, setKeyUtilPlatform } from "@/util/keyutil";
import { Session } from "./session"; import { Session } from "./session";
import { ScreenLines } from "./screenlines"; import { ScreenLines } from "./screenlines";
import { InputModel } from "./input"; import { InputModel } from "./input";
@ -107,6 +107,7 @@ class Model {
remotesModel: RemotesModel; remotesModel: RemotesModel;
lineHeightEnv: LineHeightEnv; lineHeightEnv: LineHeightEnv;
keybindManager: KeybindManager;
inputModel: InputModel; inputModel: InputModel;
pluginsModel: PluginsModel; pluginsModel: PluginsModel;
bookmarksModel: BookmarksModel; bookmarksModel: BookmarksModel;
@ -141,6 +142,7 @@ class Model {
this.runUpdate(message, interactive); this.runUpdate(message, interactive);
}); });
this.ws.reconnect(); this.ws.reconnect();
this.keybindManager = new KeybindManager();
this.inputModel = new InputModel(this); this.inputModel = new InputModel(this);
this.pluginsModel = new PluginsModel(this); this.pluginsModel = new PluginsModel(this);
this.bookmarksModel = new BookmarksModel(this); this.bookmarksModel = new BookmarksModel(this);
@ -169,6 +171,13 @@ class Model {
} }
return fontSize; return fontSize;
}); });
this.keybindManager.registerKeybinding("system", "electron", "any", (waveEvent) => {
if (this.keybindManager.checkKeyPressed(waveEvent, "system:toggleDeveloperTools")) {
getApi().toggleDeveloperTools();
return true;
}
return false;
});
getApi().onTCmd(this.onTCmd.bind(this)); getApi().onTCmd(this.onTCmd.bind(this));
getApi().onICmd(this.onICmd.bind(this)); getApi().onICmd(this.onICmd.bind(this));
getApi().onLCmd(this.onLCmd.bind(this)); getApi().onLCmd(this.onLCmd.bind(this));
@ -487,6 +496,7 @@ class Model {
e.preventDefault(); e.preventDefault();
} }
} }
this.keybindManager.processKeyEvent(e, waveEvent);
} }
deleteActiveLine(): boolean { deleteActiveLine(): boolean {

View File

@ -878,6 +878,7 @@ declare global {
}; };
type ElectronApi = { type ElectronApi = {
toggleDeveloperTools: () => void;
getId: () => string; getId: () => string;
getIsDev: () => boolean; getIsDev: () => boolean;
getPlatform: () => string; getPlatform: () => string;

View File

@ -17,7 +17,7 @@ type KeyPressDecl = {
keyType: string; keyType: string;
}; };
const KeyTypeCodeRegex = /:c{(.*)}/; const KeyTypeCodeRegex = /c{(.*)}/;
const KeyTypeKey = "key"; const KeyTypeKey = "key";
const KeyTypeCode = "code"; const KeyTypeCode = "code";
@ -44,9 +44,7 @@ class KeybindManager {
if (this.checkKeyPressed(event, curKeybind.keybinding)) { if (this.checkKeyPressed(event, curKeybind.keybinding)) {
let shouldReturn = false; let shouldReturn = false;
if (curKeybind.callback != null) { if (curKeybind.callback != null) {
console.log("Calling callback", curKeybind.domain);
shouldReturn = curKeybind.callback(event); shouldReturn = curKeybind.callback(event);
console.log("callback return value", shouldReturn);
} }
if (!shouldReturn && this.domainCallbacks.has(curKeybind.domain)) { if (!shouldReturn && this.domainCallbacks.has(curKeybind.domain)) {
let curDomainCallback = this.domainCallbacks.get(curKeybind.domain); let curDomainCallback = this.domainCallbacks.get(curKeybind.domain);
@ -69,7 +67,7 @@ class KeybindManager {
processKeyEvent(nativeEvent: any, event: WaveKeyboardEvent) { processKeyEvent(nativeEvent: any, event: WaveKeyboardEvent) {
let modalLevel = this.levelMap.get("modal"); let modalLevel = this.levelMap.get("modal");
if (modalLevel.length != 0) { if (modalLevel.length != 0) {
console.log("processing modal"); // console.log("processing modal");
// special case when modal keybindings are present // special case when modal keybindings are present
let shouldReturn = this.processLevel(nativeEvent, event, modalLevel); let shouldReturn = this.processLevel(nativeEvent, event, modalLevel);
if (shouldReturn) { if (shouldReturn) {
@ -163,7 +161,7 @@ class KeybindManager {
for (let index = 0; index < keybindsArray.length; index++) { for (let index = 0; index < keybindsArray.length; index++) {
let curKeybind = keybindsArray[index]; let curKeybind = keybindsArray[index];
if (curKeybind.domain == domain && keybindingIsEqual(curKeybind.keybinding, keybinding)) { if (curKeybind.domain == domain && keybindingIsEqual(curKeybind.keybinding, keybinding)) {
console.log("unregistering keybinding"); // console.log("unregistering keybinding");
keybindsArray.splice(index, 1); keybindsArray.splice(index, 1);
index--; index--;
this.levelMap.set(level, keybindsArray); this.levelMap.set(level, keybindsArray);