From 1b9337ef67c875a483b4f73ec9e7198ffebeb4f5 Mon Sep 17 00:00:00 2001 From: Albert Zhang Date: Wed, 27 Dec 2023 20:33:10 -0500 Subject: [PATCH] Changes in response to review comments and lint --- package.json | 1 - pnpm-lock.yaml | 3 --- scripts/build/build.mts | 18 ++++++++++------ scripts/build/composeTrayIcons.mts | 32 ++++++++++++++++++---------- src/main/appBadge.ts | 8 ++++--- src/main/index.ts | 6 +++--- src/main/ipc.ts | 10 ++++----- src/main/mainWindow.ts | 2 +- src/preload/VesktopNative.ts | 3 +-- src/renderer/appBadge.ts | 5 +++-- src/renderer/components/Settings.tsx | 4 ++-- 11 files changed, 53 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 95e5364..5c81ab8 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "eslint-plugin-prettier": "^5.0.1", "eslint-plugin-simple-import-sort": "^10.0.0", "eslint-plugin-unused-imports": "^3.0.0", - "fast-glob": "3.3", "prettier": "^3.1.0", "sharp": "^0.33.0", "sharp-ico": "^0.1.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d1b72d0..f0ae87b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -69,9 +69,6 @@ devDependencies: eslint-plugin-unused-imports: specifier: ^3.0.0 version: 3.0.0(@typescript-eslint/eslint-plugin@6.13.1)(eslint@8.54.0) - fast-glob: - specifier: '3.3' - version: 3.3.1 prettier: specifier: ^3.1.0 version: 3.1.0 diff --git a/scripts/build/build.mts b/scripts/build/build.mts index a8504ab..d8be629 100644 --- a/scripts/build/build.mts +++ b/scripts/build/build.mts @@ -7,8 +7,8 @@ import { BuildContext, BuildOptions, context } from "esbuild"; import { copyFile } from "fs/promises"; -import vencordDep from "./vencordDep.mjs"; import { composeTrayIcons } from "./composeTrayIcons.mts"; +import vencordDep from "./vencordDep.mjs"; const isDev = process.argv.includes("--dev"); @@ -50,14 +50,20 @@ async function copyVenmic() { ]).catch(() => console.warn("Failed to copy venmic. Building without venmic support")); } -await Promise.all([ - copyVenmic(), - composeTrayIcons({ +async function composeTrayIconsIfSupported() { + if (process.platform === "darwin") return; + + await composeTrayIcons({ icon: "./static/icon.png", - badges: "./static/badges/*", + badgeDir: "./static/badges/", outDir: "./static/dist/tray_icons", createEmpty: true - }), + }); +} + +await Promise.all([ + copyVenmic(), + composeTrayIconsIfSupported(), createContext({ ...NodeCommonOpts, entryPoints: ["src/main/index.ts"], diff --git a/scripts/build/composeTrayIcons.mts b/scripts/build/composeTrayIcons.mts index 8e71e8a..021ae0e 100644 --- a/scripts/build/composeTrayIcons.mts +++ b/scripts/build/composeTrayIcons.mts @@ -1,7 +1,14 @@ -import sharp, { OutputInfo } from "sharp"; -import fastGlob from "fast-glob"; -import type { ImageData } from "sharp-ico"; -import { parse as pathParse, format as pathFormat } from "node:path"; +/* + * SPDX-License-Identifier: GPL-3.0 + * Vesktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2023 Vendicated and Vencord contributors + */ + +import { readdir, stat } from "node:fs/promises"; +import { format as pathFormat, join, parse as pathParse } from "node:path"; + +import sharp from "sharp"; +import { type ImageData, sharpsFromIco } from "sharp-ico"; interface BadgePosition { left?: number; @@ -30,7 +37,7 @@ const DEFAULT_BADGE_OPTIONS: Required = { export async function composeTrayIcons({ icon: iconPath, - badges: badgeGlob, + badgeDir, outDir, outExt = ".png", createEmpty = false, @@ -38,16 +45,19 @@ export async function composeTrayIcons({ badgeOptions = undefined }: { icon: string | Buffer | sharp.Sharp; - badges: string; + badgeDir: string; outDir: string; outExt?: string; createEmpty?: boolean; iconOptions?: ImageDim; badgeOptions?: BadgeOptions; }) { - const badges = await fastGlob.glob(badgeGlob); - if (!badges.length) { - throw new Error(`No badges matching glob '${badgeGlob}' found!`); + const badges: string[] = []; + for (const filename of await readdir(badgeDir)) { + const path = join(badgeDir, filename); + if (!(await stat(path)).isDirectory()) { + badges.push(path); + } } const badgeOptionsFilled = { ...DEFAULT_BADGE_OPTIONS, ...badgeOptions }; @@ -73,7 +83,7 @@ export async function composeTrayIcons({ ext: outExt, base: undefined }); - let out = composeTrayIcon(iconData, iconInfo, badgeData, badgeInfo, badgeOptionsFilled); + const out = composeTrayIcon(iconData, iconInfo, badgeData, badgeInfo, badgeOptionsFilled); const outputInfo = await out.toFile(savePath); return { iconInfo, @@ -115,7 +125,7 @@ async function loadFromImageOrIco( sizeOptions?: ImageDim & { resizeICO?: boolean } ): Promise { if (typeof path === "string" && path.endsWith(".ico")) { - const icos = (await import("sharp-ico")).sharpsFromIco(path, undefined, true) as unknown as ImageData[]; + const icos = sharpsFromIco(path, undefined, true) as unknown as ImageData[]; let icoInfo; if (sizeOptions == null) { icoInfo = icos[icos.length - 1]; diff --git a/src/main/appBadge.ts b/src/main/appBadge.ts index 9ba6b67..e744eed 100644 --- a/src/main/appBadge.ts +++ b/src/main/appBadge.ts @@ -7,7 +7,9 @@ import { app, NativeImage, nativeImage } from "electron"; import { join } from "path"; import { BADGE_DIR, TRAY_ICON_DIR, TRAY_ICON_PATH } from "shared/paths"; + import { globals } from "./mainWindow"; +import { Settings } from "./settings"; const imgCache = new Map(); @@ -31,14 +33,14 @@ function loadTrayIcon(index: number) { let lastIndex: null | number = -1; -export function setBadgeCount(count: number, native: boolean = true, tray: boolean = false) { +export function setBadgeCount(count: number) { const [index, description] = getBadgeIndexAndDescription(count); - if (tray) { + if (Settings.store.trayBadge) { globals.tray?.setImage(loadTrayIcon(index ?? 0)); } - if (!native) return; + if (!Settings.store.appBadge) return; switch (process.platform) { case "linux": diff --git a/src/main/index.ts b/src/main/index.ts index 1268361..4998361 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -9,12 +9,12 @@ import "./ipc"; import { app, BrowserWindow } from "electron"; import { checkUpdates } from "updater/main"; -import { Settings } from "./settings"; import { DATA_DIR } from "./constants"; import { createFirstLaunchTour } from "./firstLaunch"; import { createWindows, globals } from "./mainWindow"; import { registerMediaPermissionsHandler } from "./mediaPermissions"; import { registerScreenShareHandler } from "./screenShare"; +import { Settings } from "./settings"; if (IS_DEV) { require("source-map-support").install(); @@ -43,9 +43,9 @@ function init() { ); app.on("second-instance", (_event, _cmdLine, _cwd, data: any) => { - let mainWin; + const { mainWin } = globals; if (data.IS_DEV) app.quit(); - else if ((mainWin = globals.mainWin)) { + else if (mainWin) { if (mainWin.isMinimized()) mainWin.restore(); if (!mainWin.isVisible()) mainWin.show(); mainWin.focus(); diff --git a/src/main/ipc.ts b/src/main/ipc.ts index a9e0489..8b57b0e 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -15,11 +15,11 @@ import { join } from "path"; import { debounce } from "shared/utils/debounce"; import { IpcEvents } from "../shared/IpcEvents"; +// !!IMPORTANT!! ./appBadge import must occur after ./mainWindow +import { setBadgeCount } from "./appBadge"; import { autoStart } from "./autoStart"; import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR } from "./constants"; import { globals } from "./mainWindow"; -// !!IMPORTANT!! ./appBadge import must occur after ./mainWindow -import { setBadgeCount } from "./appBadge"; import { Settings } from "./settings"; import { handle, handleSync } from "./utils/ipcWrappers"; import { isDeckGameMode, showGamePage } from "./utils/steamOS"; @@ -122,9 +122,9 @@ handle(IpcEvents.SELECT_VENCORD_DIR, async () => { return dir; }); -handle(IpcEvents.SET_BADGE_COUNT, (_, count: number, native: boolean, tray: boolean) => - setBadgeCount(count, native, tray) -); +handle(IpcEvents.SET_BADGE_COUNT, (_, count: number) => { + setBadgeCount(count); +}); function readCss() { return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => ""); diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 5fb3ae7..eeef003 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -24,7 +24,6 @@ import type { SettingsStore } from "shared/utils/SettingsStore"; import { ICON_PATH, TRAY_ICON_PATH } from "../shared/paths"; import { createAboutWindow } from "./about"; import { initArRPC } from "./arrpc"; -import { Settings, VencordSettings } from "./settings"; import { DATA_DIR, DEFAULT_HEIGHT, @@ -35,6 +34,7 @@ import { UserAgent, VENCORD_FILES_DIR } from "./constants"; +import { Settings, VencordSettings } from "./settings"; import { createSplashWindow } from "./splash"; import { makeLinksOpenExternally } from "./utils/makeLinksOpenExternally"; import { applyDeckKeyboardFix, askToApplySteamLayout, isDeckGameMode } from "./utils/steamOS"; diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index 431d52e..d72329d 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -23,8 +23,7 @@ export const VesktopNative = { app: { relaunch: () => invoke(IpcEvents.RELAUNCH), getVersion: () => sendSync(IpcEvents.GET_VERSION), - setBadgeCount: (count: number, native: boolean = true, tray: boolean = false) => - invoke(IpcEvents.SET_BADGE_COUNT, count, native, tray), + setBadgeCount: (count: number) => invoke(IpcEvents.SET_BADGE_COUNT, count), supportsWindowsTransparency: () => sendSync(IpcEvents.SUPPORTS_WINDOWS_TRANSPARENCY) }, autostart: { diff --git a/src/renderer/appBadge.ts b/src/renderer/appBadge.ts index 8eec4cc..0cc95b8 100644 --- a/src/renderer/appBadge.ts +++ b/src/renderer/appBadge.ts @@ -22,8 +22,9 @@ export function setBadge() { let totalCount = mentionCount + pendingRequests; if (!totalCount && hasUnread && !disableUnreadBadge) totalCount = -1; - if (Settings.store.appBadge || Settings.store.trayBadge) - VesktopNative.app.setBadgeCount(totalCount, Settings.store.appBadge, Settings.store.trayBadge); + if (Settings.store.appBadge || Settings.store.trayBadge) { + VesktopNative.app.setBadgeCount(totalCount); + } } catch (e) { console.error(e); } diff --git a/src/renderer/components/Settings.tsx b/src/renderer/components/Settings.tsx index b3cc18f..94cef94 100644 --- a/src/renderer/components/Settings.tsx +++ b/src/renderer/components/Settings.tsx @@ -87,7 +87,7 @@ export default function SettingsUi() { onChange={v => { Settings.appBadge = v; if (v) setBadge(); - else VesktopNative.app.setBadgeCount(0, true, false); + else VesktopNative.app.setBadgeCount(0); }} note="Show mention badge on the app (taskbar/panel) icon" > @@ -121,7 +121,7 @@ export default function SettingsUi() { onChange={v => { Settings.trayBadge = v; if (v) setBadge(); - else VesktopNative.app.setBadgeCount(0, false, true); + else VesktopNative.app.setBadgeCount(0); }} disabled={!(Settings.tray ?? true)} note="Show mention badge on the tray icon"