From 066afb837bf5d7f9e14460b289ef1b567b4b0d40 Mon Sep 17 00:00:00 2001 From: Oleh Polisan Date: Sun, 18 Aug 2024 11:52:21 +0300 Subject: [PATCH] ref: use dbus-native for getting accent color --- package.json | 3 ++- src/main/mainWindow.ts | 59 +++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index ca5cf49..3e98fe0 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "electron-updater": "^6.2.1" }, "optionalDependencies": { - "@vencord/venmic": "^6.1.0" + "@vencord/venmic": "^6.1.0", + "@homebridge/dbus-native": "0.6.0" }, "devDependencies": { "@fal-works/esbuild-plugin-global-externals": "^2.1.2", diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 639faec..92e70e4 100755 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -4,7 +4,7 @@ * Copyright (c) 2023 Vendicated and Vencord contributors */ -import { execFileSync } from "child_process"; +import dbus from "@homebridge/dbus-native"; import { app, BrowserWindow, @@ -508,35 +508,36 @@ export async function createWindows() { 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+)\)/); + return new Promise((resolve, reject) => { + const sessionBus = dbus.sessionBus(); + sessionBus + .getService("org.freedesktop.portal.Desktop") + .getInterface( + "/org/freedesktop/portal/desktop", + "org.freedesktop.portal.Settings", + function (err, settings) { + if (err) { + resolve(""); + return; + } + settings.Read("org.freedesktop.appearance", "accent-color", function (err, result) { + if (err) { + resolve(""); + return; + } + const [r, g, b] = result[1][0][1][0]; + const r255 = Math.round(r * 255); + const g255 = Math.round(g * 255); + const b255 = Math.round(b * 255); - 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 ""; + const toHex = (value: number) => value.toString(16).padStart(2, "0"); + const hexColor = `#${toHex(r255)}${toHex(g255)}${toHex(b255)}`; + resolve(hexColor); + }); + } + ); + }); } else { - return `#${systemPreferences.getAccentColor?.() || ""}`; + return Promise.resolve(`#${systemPreferences.getAccentColor?.() || ""}`); } }