Build doesn't crash

This commit is contained in:
Albert Zhang 2023-11-29 21:51:02 -05:00
parent 7f1de8e508
commit 02aa716bc8
No known key found for this signature in database
GPG key ID: D74C859E94CA6DDC
6 changed files with 44 additions and 40 deletions

View file

@ -7,8 +7,7 @@
import { app, NativeImage, nativeImage } from "electron"; import { app, NativeImage, nativeImage } from "electron";
import { join } from "path"; import { join } from "path";
import { BADGE_DIR, TRAY_ICON_DIR, TRAY_ICON_PATH } from "shared/paths"; import { BADGE_DIR, TRAY_ICON_DIR, TRAY_ICON_PATH } from "shared/paths";
import { trayContainer } from "./mainWindow"; import type { VencordBrowserWindow } from "./mainWindow";
import { Settings } from "./settings";
const imgCache = new Map<string, NativeImage>(); const imgCache = new Map<string, NativeImage>();
@ -32,17 +31,24 @@ function loadTrayIcon(index: number) {
let lastIndex: null | number = -1; let lastIndex: null | number = -1;
export function setBadgeCount(count: number) { let mainWin: null | VencordBrowserWindow;
function getMainWin() {
if (mainWin != null) return mainWin;
return (mainWin = (require("./mainWindow") as typeof import("./mainWindow")).mainWin);
}
export function setBadgeCount(count: number, tray: boolean = false) {
const [index, description] = getBadgeIndexAndDescription(count); const [index, description] = getBadgeIndexAndDescription(count);
if (Settings?.store.trayBadge) { if (tray) {
trayContainer.tray?.setImage(loadTrayIcon(index ?? 0)); getMainWin()._vencord_tray?.setImage(loadTrayIcon(index ?? 0));
} }
switch (process.platform) { switch (process.platform) {
case "linux": case "linux":
if (count === -1) count = 0; if (count === -1) count = 0;
app.setBadgeCount(count); // Only works if libunity is installed app.setBadgeCount(count); // Only works if libunity is installed
break;
case "darwin": case "darwin":
if (count === 0) { if (count === 0) {
app.dock.setBadge(""); app.dock.setBadge("");
@ -56,7 +62,7 @@ export function setBadgeCount(count: number) {
lastIndex = index; lastIndex = index;
// circular import shenanigans // circular import shenanigans
const { mainWin } = require("./mainWindow") as typeof import("./mainWindow"); const mainWin = getMainWin();
mainWin.setOverlayIcon(index === null ? null : loadBadge(index), description); mainWin.setOverlayIcon(index === null ? null : loadBadge(index), description);
break; break;
} }

View file

@ -114,7 +114,7 @@ handle(IpcEvents.SELECT_VENCORD_DIR, async () => {
return dir; return dir;
}); });
handle(IpcEvents.SET_BADGE_COUNT, (_, count: number) => setBadgeCount(count)); handle(IpcEvents.SET_BADGE_COUNT, (_, count: number, tray: boolean) => setBadgeCount(count, tray));
function readCss() { function readCss() {
return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => ""); return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => "");

View file

@ -41,9 +41,6 @@ import { applyDeckKeyboardFix, askToApplySteamLayout, isDeckGameMode } from "./u
import { downloadVencordFiles, ensureVencordFiles } from "./utils/vencordLoader"; import { downloadVencordFiles, ensureVencordFiles } from "./utils/vencordLoader";
let isQuitting = false; let isQuitting = false;
export const trayContainer: { tray: Tray | null } = {
tray: null
};
applyDeckKeyboardFix(); applyDeckKeyboardFix();
@ -51,7 +48,11 @@ app.on("before-quit", () => {
isQuitting = true; isQuitting = true;
}); });
export let mainWin: BrowserWindow; type VencordBrowserWindow = BrowserWindow & {
_vencord_tray?: Tray;
};
export let mainWin: VencordBrowserWindow;
function makeSettingsListenerHelpers<O extends object>(o: SettingsStore<O>) { function makeSettingsListenerHelpers<O extends object>(o: SettingsStore<O>) {
const listeners = new Map<(data: any) => void, PropertyKey>(); const listeners = new Map<(data: any) => void, PropertyKey>();
@ -74,7 +75,7 @@ function makeSettingsListenerHelpers<O extends object>(o: SettingsStore<O>) {
const [addSettingsListener, removeSettingsListeners] = makeSettingsListenerHelpers(Settings); const [addSettingsListener, removeSettingsListeners] = makeSettingsListenerHelpers(Settings);
const [addVencordSettingsListener, removeVencordSettingsListeners] = makeSettingsListenerHelpers(VencordSettings); const [addVencordSettingsListener, removeVencordSettingsListeners] = makeSettingsListenerHelpers(VencordSettings);
function initTray(win: BrowserWindow) { function initTray(win: VencordBrowserWindow) {
const trayMenu = Menu.buildFromTemplate([ const trayMenu = Menu.buildFromTemplate([
{ {
label: "Open", label: "Open",
@ -120,7 +121,7 @@ function initTray(win: BrowserWindow) {
} }
]); ]);
const tray = (trayContainer.tray = new Tray(TRAY_ICON_PATH)); const tray = new Tray(TRAY_ICON_PATH);
tray.setToolTip("Vesktop"); tray.setToolTip("Vesktop");
tray.setContextMenu(trayMenu); tray.setContextMenu(trayMenu);
tray.on("click", () => win.show()); tray.on("click", () => win.show());
@ -331,10 +332,10 @@ function initWindowBoundsListeners(win: BrowserWindow) {
win.on("move", saveBounds); win.on("move", saveBounds);
} }
function initSettingsListeners(win: BrowserWindow) { function initSettingsListeners(win: VencordBrowserWindow) {
addSettingsListener("tray", enable => { addSettingsListener("tray", enable => {
if (enable) initTray(win); if (enable) initTray(win);
else trayContainer.tray?.destroy(); else win._vencord_tray?.destroy();
}); });
addSettingsListener("disableMinSize", disable => { addSettingsListener("disableMinSize", disable => {
if (disable) { if (disable) {
@ -372,7 +373,7 @@ function initSpellCheck(win: BrowserWindow) {
}); });
} }
function createMainWindow() { function createMainWindow(): VencordBrowserWindow {
// Clear up previous settings listeners // Clear up previous settings listeners
removeSettingsListeners(); removeSettingsListeners();
removeVencordSettingsListeners(); removeVencordSettingsListeners();

View file

@ -23,7 +23,7 @@ export const VesktopNative = {
app: { app: {
relaunch: () => invoke<void>(IpcEvents.RELAUNCH), relaunch: () => invoke<void>(IpcEvents.RELAUNCH),
getVersion: () => sendSync<void>(IpcEvents.GET_VERSION), getVersion: () => sendSync<void>(IpcEvents.GET_VERSION),
setAppBadgeCount: (count: number) => invoke<void>(IpcEvents.SET_BADGE_COUNT, count), setBadgeCount: (count: number, tray: boolean = false) => invoke<void>(IpcEvents.SET_BADGE_COUNT, count, tray),
supportsWindowsTransparency: () => sendSync<boolean>(IpcEvents.SUPPORTS_WINDOWS_TRANSPARENCY) supportsWindowsTransparency: () => sendSync<boolean>(IpcEvents.SUPPORTS_WINDOWS_TRANSPARENCY)
}, },
autostart: { autostart: {

View file

@ -13,10 +13,6 @@ let GuildReadStateStore: any;
let NotificationSettingsStore: any; let NotificationSettingsStore: any;
export function setBadge() { export function setBadge() {
const { appBadge, trayBadge } = Settings.store;
if (appBadge === false && trayBadge === false) return;
try { try {
const mentionCount = GuildReadStateStore.getTotalMentionCount(); const mentionCount = GuildReadStateStore.getTotalMentionCount();
const pendingRequests = RelationshipStore.getPendingCount(); const pendingRequests = RelationshipStore.getPendingCount();
@ -26,7 +22,8 @@ export function setBadge() {
let totalCount = mentionCount + pendingRequests; let totalCount = mentionCount + pendingRequests;
if (!totalCount && hasUnread && !disableUnreadBadge) totalCount = -1; if (!totalCount && hasUnread && !disableUnreadBadge) totalCount = -1;
if (appBadge || trayBadge) VesktopNative.app.setAppBadgeCount(totalCount); if (Settings.store.appBadge || Settings.store.trayBadge)
VesktopNative.app.setBadgeCount(totalCount, Settings.store.trayBadge);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }

View file

@ -20,7 +20,9 @@ export default function SettingsUi() {
const { autostart } = VesktopNative; const { autostart } = VesktopNative;
const [autoStartEnabled, setAutoStartEnabled] = useState(autostart.isEnabled()); const [autoStartEnabled, setAutoStartEnabled] = useState(autostart.isEnabled());
const allSwitches: Array<false | [keyof typeof Settings, string, string, boolean?, (() => boolean)?]> = [ const allSwitches: Array<
false | [keyof typeof Settings, string, string, boolean?, (() => boolean)?, ((value: boolean) => void)?]
> = [
isWindows && [ isWindows && [
"discordWindowsTitleBar", "discordWindowsTitleBar",
"Discord Titlebar", "Discord Titlebar",
@ -34,6 +36,18 @@ export default function SettingsUi() {
true, true,
() => Settings.tray ?? true () => Settings.tray ?? true
], ],
!isMac && [
"trayBadge",
"Tray Notification Badge",
"Show mention badge on the tray icon",
false,
() => Settings.tray ?? true,
v => {
Settings.trayBadge = v;
if (v) setBadge();
else VesktopNative.app.setBadgeCount(0, true);
}
],
["arRPC", "Rich Presence", "Enables Rich Presence via arRPC", false], ["arRPC", "Rich Presence", "Enables Rich Presence via arRPC", false],
[ [
"disableMinSize", "disableMinSize",
@ -93,32 +107,18 @@ export default function SettingsUi() {
onChange={v => { onChange={v => {
Settings.appBadge = v; Settings.appBadge = v;
if (v) setBadge(); if (v) setBadge();
else VesktopNative.app.setAppBadgeCount(0); else VesktopNative.app.setBadgeCount(0, Settings.trayBadge);
}} }}
note="Show mention badge on the app (taskbar/panel) icon" note="Show mention badge on the app (taskbar/panel) icon"
> >
Notification Badge Notification Badge
</Switch> </Switch>
{Settings.tray && ( {switches.map(([key, text, note, def, predicate, onChange]) => (
<Switch
value={Settings.trayBadge ?? true}
onChange={v => {
Settings.trayBadge = v;
if (v) setBadge();
else VesktopNative.app.setAppBadgeCount(0);
}}
note="Show mention badge on the tray icon"
>
Tray Notification Badge
</Switch>
)}
{switches.map(([key, text, note, def, predicate]) => (
<Switch <Switch
value={(Settings[key as any] ?? def ?? false) && predicate?.() !== false} value={(Settings[key as any] ?? def ?? false) && predicate?.() !== false}
disabled={predicate && !predicate()} disabled={predicate && !predicate()}
onChange={v => (Settings[key as any] = v)} onChange={onChange ?? (v => (Settings[key as any] = v))}
note={note} note={note}
key={key} key={key}
> >