From 72f83c3ac43c681f84fa793ac3aebcf93ec68c5e Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Tue, 11 Jul 2023 19:35:15 +0000 Subject: [PATCH] feat: add hide menu bar items on macOS (#50) Co-authored-by: V --- src/main/mainWindow.ts | 110 ++++++++++++++++++++----------------- src/shared/utils/guards.ts | 13 +++++ 2 files changed, 74 insertions(+), 49 deletions(-) create mode 100644 src/shared/utils/guards.ts diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 872ba6c..65f2c46 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -4,9 +4,10 @@ * Copyright (c) 2023 Vendicated and Vencord contributors */ -import { app, BrowserWindow, BrowserWindowConstructorOptions, Menu, Tray } from "electron"; +import { app, BrowserWindow, BrowserWindowConstructorOptions, Menu, MenuItemConstructorOptions, Tray } from "electron"; import { join } from "path"; import { IpcEvents } from "shared/IpcEvents"; +import { isTruthy } from "shared/utils/guards"; import { once } from "shared/utils/once"; import type { SettingsStore } from "shared/utils/SettingsStore"; @@ -105,60 +106,71 @@ function initTray(win: BrowserWindow) { function initMenuBar(win: BrowserWindow) { const isWindows = process.platform === "win32"; + const isDarwin = process.platform === "darwin"; const wantCtrlQ = !isWindows || VencordSettings.store.winCtrlQ; + const subMenu = [ + { + label: "About Vencord Desktop", + click: createAboutWindow + }, + { + label: "Force Update Vencord", + async click() { + await downloadVencordFiles(); + app.relaunch(); + app.quit(); + }, + toolTip: "Vencord Desktop will automatically restart after this operation" + }, + { + label: "Relaunch", + accelerator: "CmdOrCtrl+Shift+R", + click() { + app.relaunch(); + app.quit(); + } + }, + isDarwin && { + label: "Hide", + role: "hide" + }, + isDarwin && { + label: "Hide others", + role: "hideOthers" + }, + { + label: "Quit", + accelerator: wantCtrlQ ? "CmdOrCtrl+Q" : void 0, + visible: !isWindows, + role: "quit", + click() { + app.quit(); + } + }, + { + label: "Quit", + accelerator: isWindows ? "Alt+F4" : void 0, + visible: isWindows, + role: "quit", + click() { + app.quit(); + } + }, + // See https://github.com/electron/electron/issues/14742 and https://github.com/electron/electron/issues/5256 + { + label: "Zoom in (hidden, hack for Qwertz and others)", + accelerator: "CmdOrCtrl+=", + role: "zoomIn", + visible: false + } + ] satisfies Array; + const menu = Menu.buildFromTemplate([ { label: "Vencord Desktop", role: "appMenu", - submenu: [ - { - label: "About Vencord Desktop", - click: createAboutWindow - }, - { - label: "Force Update Vencord", - async click() { - await downloadVencordFiles(); - app.relaunch(); - app.quit(); - }, - toolTip: "Vencord Desktop will automatically restart after this operation" - }, - { - label: "Relaunch", - accelerator: "CmdOrCtrl+Shift+R", - click() { - app.relaunch(); - app.quit(); - } - }, - { - label: "Quit", - accelerator: wantCtrlQ ? "CmdOrCtrl+Q" : void 0, - visible: !isWindows, - role: "quit", - click() { - app.quit(); - } - }, - { - label: "Quit", - accelerator: isWindows ? "Alt+F4" : void 0, - visible: isWindows, - role: "quit", - click() { - app.quit(); - } - }, - // See https://github.com/electron/electron/issues/14742 and https://github.com/electron/electron/issues/5256 - { - label: "Zoom in (hidden, hack for Qwertz and others)", - accelerator: "CmdOrCtrl+=", - role: "zoomIn", - visible: false - } - ] + submenu: subMenu.filter(isTruthy) }, { role: "fileMenu" }, { role: "editMenu" }, diff --git a/src/shared/utils/guards.ts b/src/shared/utils/guards.ts new file mode 100644 index 0000000..ac6cb20 --- /dev/null +++ b/src/shared/utils/guards.ts @@ -0,0 +1,13 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + +export function isTruthy(item: T): item is Exclude { + return Boolean(item); +} + +export function isNonNullish(item: T): item is Exclude { + return item != null; +}