From 1c20f354606e010a15b9b0661818efb9732adcd3 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Tue, 4 Apr 2023 01:35:37 +0200 Subject: [PATCH] Implement VencordDesktop, improve tray --- src/main/ipc.ts | 9 +++++-- src/main/mainWindow.ts | 51 ++++++++++++++++++++++------------- src/preload/VencordDesktop.ts | 9 +++++++ src/preload/index.ts | 5 +++- src/shared/IpcEvents.ts | 1 + src/shared/paths.ts | 4 +++ 6 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 src/preload/VencordDesktop.ts create mode 100644 src/shared/paths.ts diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 7661134..4eee396 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -1,8 +1,13 @@ -import { ipcMain } from "electron"; +import { app, ipcMain } from "electron"; import { join } from "path"; -import { GET_PRELOAD_FILE } from "../shared/IpcEvents"; +import { GET_PRELOAD_FILE, RELAUNCH } from "../shared/IpcEvents"; import { VENCORD_FILES_DIR } from "./constants"; ipcMain.on(GET_PRELOAD_FILE, e => { e.returnValue = join(VENCORD_FILES_DIR, "preload.js"); }); + +ipcMain.handle(RELAUNCH, () => { + app.relaunch(); + app.exit(); +}); diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 57103c4..e0450c4 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -1,5 +1,6 @@ import { BrowserWindow, Menu, Tray, app } from "electron"; import { join } from "path"; +import { ICON_PATH } from "../shared/paths"; export function createMainWindow() { let isQuitting = false; @@ -13,9 +14,31 @@ export function createMainWindow() { devTools: true, preload: join(__dirname, "preload.js") }, - icon: join(__dirname, "..", "..", "static", "icon.ico") + icon: ICON_PATH }); + const trayMenu = Menu.buildFromTemplate([ + { + label: "Open", + click() { + win.show(); + }, + enabled: false + }, + { + label: "Quit Vencord Desktop", + click() { + isQuitting = true; + app.quit(); + } + } + ]); + + const tray = new Tray(ICON_PATH); + tray.setToolTip("Vencord Desktop"); + tray.setContextMenu(trayMenu); + tray.on("click", () => win.show()); + app.on("before-quit", () => { isQuitting = true; }); @@ -29,24 +52,14 @@ export function createMainWindow() { return false; }); - const tray = new Tray(join(__dirname, "..", "..", "static", "icon.ico")); - tray.setToolTip("Vencord Desktop"); - tray.setContextMenu(Menu.buildFromTemplate([ - { - label: "Open", - click() { - win.show(); - } - }, - { - label: "Quit", - click() { - isQuitting = true; - app.quit(); - } - } - ])); - tray.on("click", () => win.show()); + win.on("show", () => { + trayMenu.items[0].enabled = false; + }); + + win.on("hide", () => { + trayMenu.items[0].enabled = true; + }); + win.loadURL("https://discord.com/app"); diff --git a/src/preload/VencordDesktop.ts b/src/preload/VencordDesktop.ts new file mode 100644 index 0000000..d2089a9 --- /dev/null +++ b/src/preload/VencordDesktop.ts @@ -0,0 +1,9 @@ +import { ipcRenderer } from "electron"; +import { RELAUNCH } from "../shared/IpcEvents"; + +export const VencordDesktop = { + app: { + relaunch: () => ipcRenderer.invoke(RELAUNCH) + } +} + diff --git a/src/preload/index.ts b/src/preload/index.ts index 28a6286..6dd7605 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1,4 +1,7 @@ -import { ipcRenderer } from "electron"; +import { contextBridge, ipcRenderer } from "electron"; import { GET_PRELOAD_FILE } from "../shared/IpcEvents"; +import { VencordDesktop } from "./VencordDesktop"; + +contextBridge.exposeInMainWorld("VencordDesktop", VencordDesktop); require(ipcRenderer.sendSync(GET_PRELOAD_FILE)); diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index 373e29e..35e4d87 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -1 +1,2 @@ export const GET_PRELOAD_FILE = "VCD_GET_PRELOAD_FILE"; +export const RELAUNCH = "VCD_RELAUNCH"; diff --git a/src/shared/paths.ts b/src/shared/paths.ts new file mode 100644 index 0000000..73d1b11 --- /dev/null +++ b/src/shared/paths.ts @@ -0,0 +1,4 @@ +import { join } from "path"; + +export const STATIC_DIR = join(__dirname, "..", "..", "static"); +export const ICON_PATH = join(STATIC_DIR, "icon.ico");