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();
if (!input.alt) {
win.webContents.send("i-cmd", mods);
} else {
win.webContents.toggleDevTools();
}
return;
}
@ -520,6 +518,13 @@ app.on("window-all-closed", () => {
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) => {
event.returnValue = instanceId + ":" + event.processId;
});

View File

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

View File

@ -19,7 +19,7 @@ import { WSControl } from "./ws";
import { cmdStatusIsRunning } from "@/app/line/lineutil";
import * as appconst from "@/app/appconst";
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 { ScreenLines } from "./screenlines";
import { InputModel } from "./input";
@ -107,6 +107,7 @@ class Model {
remotesModel: RemotesModel;
lineHeightEnv: LineHeightEnv;
keybindManager: KeybindManager;
inputModel: InputModel;
pluginsModel: PluginsModel;
bookmarksModel: BookmarksModel;
@ -141,6 +142,7 @@ class Model {
this.runUpdate(message, interactive);
});
this.ws.reconnect();
this.keybindManager = new KeybindManager();
this.inputModel = new InputModel(this);
this.pluginsModel = new PluginsModel(this);
this.bookmarksModel = new BookmarksModel(this);
@ -169,6 +171,13 @@ class Model {
}
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().onICmd(this.onICmd.bind(this));
getApi().onLCmd(this.onLCmd.bind(this));
@ -487,6 +496,7 @@ class Model {
e.preventDefault();
}
}
this.keybindManager.processKeyEvent(e, waveEvent);
}
deleteActiveLine(): boolean {

View File

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

View File

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