From cf23b6d028479898ff8a29f9b3a137a66e525cf4 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Wed, 5 Apr 2023 05:19:48 +0200 Subject: [PATCH] Make clicking notifications focus VC Desktop --- src/globals.d.ts | 5 +++++ src/main/ipc.ts | 9 +++++++-- src/main/mainWindow.ts | 4 +++- src/preload/VencordDesktop.ts | 9 ++++++--- src/renderer/fixes.ts | 11 +++++++++++ src/renderer/index.ts | 2 ++ src/shared/IpcEvents.ts | 3 ++- 7 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/globals.d.ts create mode 100644 src/renderer/fixes.ts diff --git a/src/globals.d.ts b/src/globals.d.ts new file mode 100644 index 0000000..b55bab3 --- /dev/null +++ b/src/globals.d.ts @@ -0,0 +1,5 @@ +declare global { + export var VencordDesktop: typeof import("./preload/VencordDesktop").VencordDesktop; +} + +export { }; diff --git a/src/main/ipc.ts b/src/main/ipc.ts index b295ea6..5964dfa 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -1,8 +1,9 @@ import { app, ipcMain, shell } from "electron"; import { readFileSync } from "fs"; import { join } from "path"; -import { GET_RENDERER_SCRIPT, GET_SETTINGS, GET_VENCORD_PRELOAD_FILE, RELAUNCH, SET_SETTINGS, SHOW_IN_FOLDER } from "../shared/IpcEvents"; +import { FOCUS, GET_RENDERER_SCRIPT, GET_SETTINGS, GET_VENCORD_PRELOAD_FILE, RELAUNCH, SET_SETTINGS, SHOW_ITEM_IN_FOLDER } from "../shared/IpcEvents"; import { VENCORD_FILES_DIR } from "./constants"; +import { mainWin } from "./mainWindow"; import { PlainSettings, setSettings } from "./settings"; ipcMain.on(GET_VENCORD_PRELOAD_FILE, e => { @@ -26,6 +27,10 @@ ipcMain.handle(RELAUNCH, () => { app.exit(); }); -ipcMain.handle(SHOW_IN_FOLDER, (_, path) => { +ipcMain.handle(SHOW_ITEM_IN_FOLDER, (_, path) => { shell.showItemInFolder(path); }); + +ipcMain.handle(FOCUS, () => { + mainWin?.focus(); +}); diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 702b617..7798ba7 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -9,6 +9,8 @@ app.on("before-quit", () => { isQuitting = true; }); +export let mainWin: BrowserWindow; + function initWindowOpenHandler(win: BrowserWindow) { win.webContents.setWindowOpenHandler(({ url }) => { switch (url) { @@ -115,7 +117,7 @@ function initWindowBoundsListeners(win: BrowserWindow) { } export function createMainWindow() { - const win = new BrowserWindow({ + const win = mainWin = new BrowserWindow({ show: false, autoHideMenuBar: true, webPreferences: { diff --git a/src/preload/VencordDesktop.ts b/src/preload/VencordDesktop.ts index 7d896d0..6363884 100644 --- a/src/preload/VencordDesktop.ts +++ b/src/preload/VencordDesktop.ts @@ -1,17 +1,20 @@ import { ipcRenderer } from "electron"; import type { Settings } from "../main/settings"; -import { GET_SETTINGS, RELAUNCH, SET_SETTINGS, SHOW_IN_FOLDER } from "../shared/IpcEvents"; +import { FOCUS, GET_SETTINGS, RELAUNCH, SET_SETTINGS, SHOW_ITEM_IN_FOLDER } from "../shared/IpcEvents"; export const VencordDesktop = { app: { relaunch: () => ipcRenderer.invoke(RELAUNCH) }, - files: { - showInFolder: (path: string) => ipcRenderer.invoke(SHOW_IN_FOLDER, path) + fileManager: { + showItemInFolder: (path: string) => ipcRenderer.invoke(SHOW_ITEM_IN_FOLDER, path) }, settings: { get: () => ipcRenderer.sendSync(GET_SETTINGS), set: (settings: typeof Settings) => ipcRenderer.invoke(SET_SETTINGS, settings) + }, + win: { + focus: () => ipcRenderer.invoke(FOCUS) } } diff --git a/src/renderer/fixes.ts b/src/renderer/fixes.ts new file mode 100644 index 0000000..0e6f67b --- /dev/null +++ b/src/renderer/fixes.ts @@ -0,0 +1,11 @@ +// Make clicking Notifications focus the window +const originalSetOnClick = Object.getOwnPropertyDescriptor(Notification.prototype, "onclick")!.set!; +Object.defineProperty(Notification.prototype, "onclick", { + set(onClick) { + originalSetOnClick.call(this, function () { + onClick.apply(this, arguments); + VencordDesktop.win.focus(); + }); + }, + configurable: true +}); diff --git a/src/renderer/index.ts b/src/renderer/index.ts index 109aa48..7c17265 100644 --- a/src/renderer/index.ts +++ b/src/renderer/index.ts @@ -1 +1,3 @@ +import "./fixes"; + console.log("read if cute :3"); diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index b973ee7..c3a1530 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -1,6 +1,7 @@ export const GET_VENCORD_PRELOAD_FILE = "VCD_GET_VC_PRELOAD_FILE"; export const GET_RENDERER_SCRIPT = "VCD_GET_RENDERER_SCRIPT"; export const RELAUNCH = "VCD_RELAUNCH"; -export const SHOW_IN_FOLDER = "VCD_SHOW_IN_FOLDER"; +export const SHOW_ITEM_IN_FOLDER = "VCD_SHOW_ITEM_IN_FOLDER"; export const GET_SETTINGS = "VCD_GET_SETTINGS"; export const SET_SETTINGS = "VCD_SET_SETTINGS"; +export const FOCUS = "VC_FOCUS";