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

View file

@ -114,7 +114,7 @@ handle(IpcEvents.SELECT_VENCORD_DIR, async () => {
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() {
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";
let isQuitting = false;
export const trayContainer: { tray: Tray | null } = {
tray: null
};
applyDeckKeyboardFix();
@ -51,7 +48,11 @@ app.on("before-quit", () => {
isQuitting = true;
});
export let mainWin: BrowserWindow;
type VencordBrowserWindow = BrowserWindow & {
_vencord_tray?: Tray;
};
export let mainWin: VencordBrowserWindow;
function makeSettingsListenerHelpers<O extends object>(o: SettingsStore<O>) {
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 [addVencordSettingsListener, removeVencordSettingsListeners] = makeSettingsListenerHelpers(VencordSettings);
function initTray(win: BrowserWindow) {
function initTray(win: VencordBrowserWindow) {
const trayMenu = Menu.buildFromTemplate([
{
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.setContextMenu(trayMenu);
tray.on("click", () => win.show());
@ -331,10 +332,10 @@ function initWindowBoundsListeners(win: BrowserWindow) {
win.on("move", saveBounds);
}
function initSettingsListeners(win: BrowserWindow) {
function initSettingsListeners(win: VencordBrowserWindow) {
addSettingsListener("tray", enable => {
if (enable) initTray(win);
else trayContainer.tray?.destroy();
else win._vencord_tray?.destroy();
});
addSettingsListener("disableMinSize", disable => {
if (disable) {
@ -372,7 +373,7 @@ function initSpellCheck(win: BrowserWindow) {
});
}
function createMainWindow() {
function createMainWindow(): VencordBrowserWindow {
// Clear up previous settings listeners
removeSettingsListeners();
removeVencordSettingsListeners();

View file

@ -23,7 +23,7 @@ export const VesktopNative = {
app: {
relaunch: () => invoke<void>(IpcEvents.RELAUNCH),
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)
},
autostart: {

View file

@ -13,10 +13,6 @@ let GuildReadStateStore: any;
let NotificationSettingsStore: any;
export function setBadge() {
const { appBadge, trayBadge } = Settings.store;
if (appBadge === false && trayBadge === false) return;
try {
const mentionCount = GuildReadStateStore.getTotalMentionCount();
const pendingRequests = RelationshipStore.getPendingCount();
@ -26,7 +22,8 @@ export function setBadge() {
let totalCount = mentionCount + pendingRequests;
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) {
console.error(e);
}

View file

@ -20,7 +20,9 @@ export default function SettingsUi() {
const { autostart } = VesktopNative;
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 && [
"discordWindowsTitleBar",
"Discord Titlebar",
@ -34,6 +36,18 @@ export default function SettingsUi() {
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],
[
"disableMinSize",
@ -93,32 +107,18 @@ export default function SettingsUi() {
onChange={v => {
Settings.appBadge = v;
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"
>
Notification Badge
</Switch>
{Settings.tray && (
<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]) => (
{switches.map(([key, text, note, def, predicate, onChange]) => (
<Switch
value={(Settings[key as any] ?? def ?? false) && predicate?.() !== false}
disabled={predicate && !predicate()}
onChange={v => (Settings[key as any] = v)}
onChange={onChange ?? (v => (Settings[key as any] = v))}
note={note}
key={key}
>