Skip to content

Commit

Permalink
Vesktop Vencord#517 & 686 PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
thororen1234 committed Aug 8, 2024
1 parent 61087d7 commit 70f05a6
Show file tree
Hide file tree
Showing 20 changed files with 880 additions and 24 deletions.
31 changes: 26 additions & 5 deletions src/main/appBadge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
* Copyright (c) 2023 Vendicated and Vencord contributors
*/

import { execFile } from "child_process";
import { app, NativeImage, nativeImage } from "electron";
import { join } from "path";
import { IpcEvents } from "shared/IpcEvents";
import { BADGE_DIR } from "shared/paths";

import { mainWin } from "./mainWindow";

const imgCache = new Map<number, NativeImage>();
function loadBadge(index: number) {
const cached = imgCache.get(index);
Expand All @@ -19,13 +23,28 @@ function loadBadge(index: number) {
return img;
}

let lastIndex: null | number = -1;
let lastBadgeIndex: null | number = -1;
export var lastBadgeCount: number = -1;

export function setBadgeCount(count: number) {
lastBadgeCount = count;
switch (process.platform) {
case "linux":
if (count === -1) count = 0;
app.setBadgeCount(count);
if (typeof count !== "number") {
// sanitize
throw new Error("count must be a number");
}

execFile("gdbus", [
"emit",
"--session",
"--object-path",
"/",
"--signal",
"com.canonical.Unity.LauncherEntry.Update",
"application://vesktop.desktop",
`{'count': <int64 ${count === -1 ? 0 : count}>, 'count-visible': <${count !== 0}>}`
]);
break;
case "darwin":
if (count === 0) {
Expand All @@ -36,15 +55,17 @@ export function setBadgeCount(count: number) {
break;
case "win32":
const [index, description] = getBadgeIndexAndDescription(count);
if (lastIndex === index) break;
if (lastBadgeIndex === index) break;

lastIndex = index;
lastBadgeIndex = index;

// circular import shenanigans
const { mainWin } = require("./mainWindow") as typeof import("./mainWindow");
mainWin.setOverlayIcon(index === null ? null : loadBadge(index), description);
break;
}

mainWin.webContents.send(IpcEvents.SET_CURRENT_VOICE_TRAY_ICON);
}

function getBadgeIndexAndDescription(count: number): [number | null, string] {
Expand Down
4 changes: 3 additions & 1 deletion src/main/firstLaunch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ICON_PATH, VIEW_DIR } from "shared/paths";

import { autoStart } from "./autoStart";
import { DATA_DIR } from "./constants";
import { createWindows } from "./mainWindow";
import { createWindows, getAccentColor } from "./mainWindow";
import { Settings, State } from "./settings";
import { makeLinksOpenExternally } from "./utils/makeLinksOpenExternally";

Expand Down Expand Up @@ -47,6 +47,8 @@ export function createFirstLaunchTour() {
console.log(data);
State.store.firstLaunch = false;
Settings.store.discordBranch = data.discordBranch;
Settings.store.tray = true;
Settings.store.trayColor = getAccentColor()?.slice(1);
Settings.store.minimizeToTray = !!data.minimizeToTray;
Settings.store.arRPC = !!data.richPresence;

Expand Down
22 changes: 21 additions & 1 deletion src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ import { IpcEvents } from "../shared/IpcEvents";
import { setBadgeCount } from "./appBadge";
import { autoStart } from "./autoStart";
import { VENCORD_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR } from "./constants";
import { mainWin } from "./mainWindow";
import { getAccentColor, mainWin } from "./mainWindow";
import { Settings, State } from "./settings";
import {
createTrayIcon,
generateTrayIcons,
getIconWithBadge,
getTrayIconFile,
getTrayIconFileSync,
pickTrayIcon,
setTrayIcon
} from "./tray";
import { handle, handleSync } from "./utils/ipcWrappers";
import { PopoutWindows } from "./utils/popout";
import { isDeckGameMode, showGamePage } from "./utils/steamOS";
Expand Down Expand Up @@ -165,3 +174,14 @@ watch(
mainWin?.webContents.postMessage("VencordThemeUpdate", void 0);
})
);

handle(IpcEvents.SET_TRAY_ICON, (_, iconURI) => setTrayIcon(iconURI));
handle(IpcEvents.GET_TRAY_ICON, (_, iconPath) => getTrayIconFile(iconPath));
handleSync(IpcEvents.GET_TRAY_ICON_SYNC, (_, iconPath) => getTrayIconFileSync(iconPath));
handle(IpcEvents.GET_SYSTEM_ACCENT_COLOR, () => getAccentColor());
handle(IpcEvents.CREATE_TRAY_ICON_RESPONSE, (_, iconName, dataURL, isCustomIcon, isSvg) =>
createTrayIcon(iconName, dataURL, isCustomIcon, isSvg)
);
handle(IpcEvents.GENERATE_TRAY_ICONS, () => generateTrayIcons());
handle(IpcEvents.SELECT_TRAY_ICON, async (_, iconName) => pickTrayIcon(iconName));
handle(IpcEvents.GET_ICON_WITH_BADGE, async (_, dataURL) => getIconWithBadge(dataURL));
47 changes: 45 additions & 2 deletions src/main/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright (c) 2023 Vendicated and Vencord contributors
*/

import { execFileSync } from "child_process";
import {
app,
BrowserWindow,
Expand All @@ -14,16 +15,17 @@ import {
nativeTheme,
screen,
session,
systemPreferences,
Tray
} from "electron";
import { rm } from "fs/promises";
import { join } from "path";
import { IpcEvents } from "shared/IpcEvents";
import { ICON_PATH } from "shared/paths";
import { isTruthy } from "shared/utils/guards";
import { once } from "shared/utils/once";
import type { SettingsStore } from "shared/utils/SettingsStore";

import { ICON_PATH } from "../shared/paths";
import { createAboutWindow } from "./about";
import { initArRPC } from "./arrpc";
import {
Expand All @@ -37,13 +39,14 @@ import {
VENCORD_DIR
} from "./constants";
import { Settings, State, VencordSettings } from "./settings";
import { setTrayIcon } from "./tray";
import { addOneTaskSplash, addSplashLog, getSplash } from "./utils/detailedLog";
import { makeLinksOpenExternally } from "./utils/makeLinksOpenExternally";
import { applyDeckKeyboardFix, askToApplySteamLayout, isDeckGameMode } from "./utils/steamOS";
import { downloadVencordAsar, ensureVencordFiles } from "./utils/vencordLoader";

let isQuitting = false;
let tray: Tray;
export let tray: Tray;

applyDeckKeyboardFix();

Expand Down Expand Up @@ -125,6 +128,7 @@ function initTray(win: BrowserWindow) {
]);

tray = new Tray(ICON_PATH);
setTrayIcon("icon");
tray.setToolTip("Equibop");
tray.setContextMenu(trayMenu);
tray.on("click", onTrayClick);
Expand Down Expand Up @@ -449,6 +453,10 @@ function createMainWindow(splash: BrowserWindow) {
});

if (Settings.store.staticTitle) win.on("page-title-updated", e => e.preventDefault());
else if (Settings.store.appBadge)
mainWin.webContents.on("page-title-updated", (_, title) => {
mainWin.setTitle(title.replace(/^\(\d+\)\s*|•\s/, ""));
});
addSplashLog("Initialized Main Window params");

initWindowBoundsListeners(win);
Expand Down Expand Up @@ -531,3 +539,38 @@ export async function createWindows() {

initArRPC();
}

export function getAccentColor() {
if (process.platform === "linux") {
var accentColor = execFileSync("gdbus", [
"call",
"--session",
"--dest",
"org.freedesktop.portal.Desktop",
"--object-path",
"/org/freedesktop/portal/desktop",
"--method",
"org.freedesktop.portal.Settings.Read",
"org.freedesktop.appearance",
"accent-color"
]);
const rgbMatch = accentColor.toString().match(/\((\d+\.\d+),\s*(\d+\.\d+),\s*(\d+\.\d+)\)/);

if (rgbMatch) {
const r = parseFloat(rgbMatch[1]);
const g = parseFloat(rgbMatch[2]);
const b = parseFloat(rgbMatch[3]);

const r255 = Math.round(r * 255);
const g255 = Math.round(g * 255);
const b255 = Math.round(b * 255);

const toHex = (value: number) => value.toString(16).padStart(2, "0");
const hexColor = `#${toHex(r255)}${toHex(g255)}${toHex(b255)}`;
return hexColor;
}
return "";
} else {
return `#${systemPreferences.getAccentColor?.() || ""}`;
}
}
Loading

0 comments on commit 70f05a6

Please sign in to comment.