diff --git a/package.json b/package.json index e4dca94..caee8b8 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "watch": "tsc -w", "start": "npm run build && electron ./ts-out/main.js", "startThemeManager": "npm run build && electron ./ts-out/main.js themes", + "startKeybindManager": "npm run build && electron ./ts-out/main.js keybinds", "startWayland": "npm run build && electron ./ts-out/main.js --ozone-platform-hint=auto --enable-features=WebRTCPipeWireCapturer,WaylandWindowDecorations", "package": "npm run build && electron-builder", "packageQuick": "npm run build && electron-builder --dir", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index decd34e..2e89b16 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,7 +38,7 @@ devDependencies: version: 2.4.1 electron: specifier: ^25.3.0 - version: 25.3.0 + version: 25.3.1 electron-builder: specifier: ^23.6.0 version: 23.6.0 @@ -1072,8 +1072,8 @@ packages: - supports-color dev: true - /electron@25.3.0: - resolution: {integrity: sha512-cyqotxN+AroP5h2IxUsJsmehYwP5LrFAOO7O7k9tILME3Sa1/POAg3shrhx4XEnaAMyMqMLxzGvkzCVxzEErnA==} + /electron@25.3.1: + resolution: {integrity: sha512-t0QXXqgf0/P0OJ9LU3qpcBMK+wL0FRwTQfooBaaG08v5hywPzc1yplfb3l4tS1xC0Ttw8IBaKLBeEoRgxBRHjg==} engines: {node: '>= 12.20.55'} hasBin: true requiresBuild: true diff --git a/src/keybindMaker/main.ts b/src/keybindMaker/main.ts index dc484aa..cc2a11d 100644 --- a/src/keybindMaker/main.ts +++ b/src/keybindMaker/main.ts @@ -1,5 +1,6 @@ -import {BrowserWindow, app, shell} from "electron"; +import {BrowserWindow, app, globalShortcut, ipcMain, shell} from "electron"; import path from "path"; +import {getConfig, registerGlobalKeybinds, setConfig} from "../utils"; let keybindWindow: BrowserWindow; let instance = 0; @@ -13,7 +14,7 @@ export function createKeybindWindow(): void { } } else { keybindWindow = new BrowserWindow({ - width: 660, + width: 720, height: 670, title: `ArmCord Global Keybinds Maker`, darkTheme: true, @@ -26,12 +27,40 @@ export function createKeybindWindow(): void { } }); async function makerLoadPage(): Promise { + globalShortcut.unregisterAll(); keybindWindow.loadURL(`file://${__dirname}/maker.html`); } keybindWindow.webContents.setWindowOpenHandler(({url}) => { shell.openExternal(url); return {action: "deny"}; }); + ipcMain.on("addKeybind", async (_event, keybind) => { + var keybinds = await getConfig("keybinds"); + keybind.replace(" ", "Space"); + if (keybinds.includes(keybind)) return; + keybinds.push(keybind); + await setConfig("keybinds", keybinds); + keybindWindow.webContents.reload(); + }); + ipcMain.on("removeKeybind", async (_event, keybind) => { + var keybinds = await getConfig("keybinds"); + const index = keybinds.indexOf(keybind); + keybinds.splice(index, 1); + await setConfig("keybinds", keybinds); + keybindWindow.webContents.reload(); + }); + keybindWindow.webContents.on("did-finish-load", async () => { + for (const keybind of await getConfig("keybinds")) { + console.log(keybind); + keybindWindow.webContents.send("keybindCombo", keybind); + } + }); + keybindWindow.on("close", () => { + registerGlobalKeybinds(); + }); makerLoadPage(); + keybindWindow.on("close", () => { + instance = 0; + }); } } diff --git a/src/keybindMaker/maker.html b/src/keybindMaker/maker.html index e69de29..da27e5c 100644 --- a/src/keybindMaker/maker.html +++ b/src/keybindMaker/maker.html @@ -0,0 +1,392 @@ + + + + + + + +

Global keybinds are disabled while this window is open.

+ +
+
+ + + diff --git a/src/keybindMaker/preload.ts b/src/keybindMaker/preload.ts index e69de29..523b529 100644 --- a/src/keybindMaker/preload.ts +++ b/src/keybindMaker/preload.ts @@ -0,0 +1,36 @@ +import {contextBridge, ipcRenderer} from "electron"; +import {sleep} from "../utils"; +contextBridge.exposeInMainWorld("manager", { + add: (keybindName: string) => ipcRenderer.send("addKeybind", keybindName), + remove: (keybindName: string) => ipcRenderer.send("removeKeybind", keybindName) +}); +ipcRenderer.on("keybindCombo", (_event, keybindName) => { + sleep(1000); + console.log(keybindName); + let e = document.getElementById("cardBox"); + var keys = keybindName.split("+"); + var id = keybindName.replace("+", ""); + var html = ""; + for (var key in keys) { + html += `${keys[key]}`; + } + e?.insertAdjacentHTML( + "beforeend", + ` +
+
+ ${html} + + +
+
+ ` + ); + (document.getElementById(id) as HTMLInputElement)!.checked = true; + (document.getElementById(id) as HTMLInputElement)!.addEventListener("input", function (evt) { + ipcRenderer.send("removeKeybind", keybindName); + }); +}); +sleep(3000).then(() => { + document.getElementById("warning")!.style.display = "none"; +}); diff --git a/src/main.ts b/src/main.ts index 558a459..986bcea 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,6 +22,7 @@ import path from "path"; import {createTManagerWindow} from "./themeManager/main"; import {createSplashWindow} from "./splash/main"; import {createSetupWindow} from "./setup/main"; +import {createKeybindWindow} from "./keybindMaker/main"; export let iconPath: string; export let settings: any; export let customTitlebar: boolean; @@ -46,6 +47,10 @@ async function args(): Promise { app.whenReady().then(async () => { createTManagerWindow(); }); + } else if (args == "keybinds") { + app.whenReady().then(async () => { + createKeybindWindow(); + }); } } args(); // i want my top level awaits diff --git a/src/utils.ts b/src/utils.ts index e18f8c8..841dbad 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import * as fs from "fs"; -import {app, dialog} from "electron"; +import {app, dialog, globalShortcut} from "electron"; import path from "path"; import fetch from "cross-fetch"; import extract from "extract-zip"; @@ -44,6 +44,7 @@ export function setup(): void { armcordCSP: true, minimizeToTray: true, automaticPatches: false, + keybinds: [], alternativePaste: false, mods: "none", spellcheck: true, @@ -244,7 +245,6 @@ export interface Settings { // Only used for external url warning dialog. ignoreProtocolWarning?: boolean; customIcon: string; - windowStyle: string; channel: string; armcordCSP: boolean; @@ -260,6 +260,7 @@ export interface Settings { startMinimized: boolean; useLegacyCapturer: boolean; tray: boolean; + keybinds: Array; inviteWebsocket: boolean; disableAutogain: boolean; trayIcon: string; @@ -285,7 +286,17 @@ export async function setConfig(object: K, toSet: Sett fs.writeFileSync(getConfigLocation(), toSave, "utf-8"); } export async function setConfigBulk(object: Settings): Promise { - let toSave = JSON.stringify(object, null, 4); + let existingData = {}; + try { + const existingDataBuffer = fs.readFileSync(getConfigLocation(), "utf-8"); + existingData = JSON.parse(existingDataBuffer.toString()); + } catch (error) { + // Ignore errors when the file doesn't exist or parsing fails + } + // Merge the existing data with the new data + const mergedData = {...existingData, ...object}; + // Write the merged data back to the file + const toSave = JSON.stringify(mergedData, null, 4); fs.writeFileSync(getConfigLocation(), toSave, "utf-8"); } export async function checkIfConfigExists(): Promise { @@ -389,3 +400,12 @@ export async function installModLoader(): Promise { } } } + +export async function registerGlobalKeybinds() { + const keybinds = await getConfig("keybinds"); + keybinds.forEach((keybind) => { + globalShortcut.register(keybind, () => { + console.log(keybind); + }); + }); +} diff --git a/src/window.ts b/src/window.ts index f7f8ca9..b8c5a05 100644 --- a/src/window.ts +++ b/src/window.ts @@ -11,6 +11,7 @@ import { getConfig, getWindowState, modInstallState, + registerGlobalKeybinds, setConfig, setLang, setWindowState, @@ -198,6 +199,7 @@ async function doAfterDefiningTheWindow(): Promise { if (!fs.existsSync(`${userDataPath}/disabled.txt`)) { fs.writeFileSync(path.join(userDataPath, "/disabled.txt"), ""); } + registerGlobalKeybinds(); mainWindow.webContents.on("did-finish-load", () => { fs.readdirSync(themesFolder).forEach((file) => { try {