From 05def05cc280675c9534d244a9b417a065a74d97 Mon Sep 17 00:00:00 2001 From: Oleh Polisan Date: Thu, 18 Apr 2024 15:12:13 +0300 Subject: [PATCH] Added tray icon voice detection --- src/main/ipc.ts | 4 ++- src/main/mainWindow.ts | 25 ++++++++++++++- src/preload/VesktopNative.ts | 3 +- src/renderer/patches/index.ts | 1 + src/renderer/patches/tray.ts | 59 +++++++++++++++++++++++++++++++++++ src/shared/IpcEvents.ts | 4 ++- src/shared/paths.ts | 4 +++ 7 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/renderer/patches/tray.ts diff --git a/src/main/ipc.ts b/src/main/ipc.ts index e0bf131..e010f2b 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -18,7 +18,7 @@ import { IpcEvents } from "../shared/IpcEvents"; import { setBadgeCount } from "./appBadge"; import { autoStart } from "./autoStart"; import { VENCORD_FILES_DIR, VENCORD_QUICKCSS_FILE, VENCORD_THEMES_DIR } from "./constants"; -import { mainWin } from "./mainWindow"; +import { mainWin, setTrayIcon } from "./mainWindow"; import { Settings } from "./settings"; import { handle, handleSync } from "./utils/ipcWrappers"; import { PopoutWindows } from "./utils/popout"; @@ -153,3 +153,5 @@ watch( mainWin?.webContents.postMessage("VencordThemeUpdate", void 0); }) ); + +handle(IpcEvents.SET_TRAY_ICON, (_, iconName: string) => setTrayIcon(iconName)); diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 7e0afde..1a87fcf 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -21,7 +21,7 @@ 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 { ICON_PATH, SPEAKING_ICON_PATH, MUTED_ICON_PATH, DEAFENED_ICON_PATH, IDLE_ICON_PATH } from "../shared/paths"; import { createAboutWindow } from "./about"; import { initArRPC } from "./arrpc"; import { @@ -40,6 +40,9 @@ import { makeLinksOpenExternally } from "./utils/makeLinksOpenExternally"; import { applyDeckKeyboardFix, askToApplySteamLayout, isDeckGameMode } from "./utils/steamOS"; import { downloadVencordFiles, ensureVencordFiles } from "./utils/vencordLoader"; +import { fluxDispatcher } from "@vencord/types/webpack/common"; +import { onceReady } from "@vencord/types/webpack"; + let isQuitting = false; let tray: Tray; @@ -478,3 +481,23 @@ export async function createWindows() { initArRPC(); } + +export async function setTrayIcon(iconName) { + if (!tray) return; + switch (iconName) { + case "speaking": + tray.setImage(SPEAKING_ICON_PATH); + break; + case "muted": + tray.setImage(MUTED_ICON_PATH); + break; + case "deafened": + tray.setImage(DEAFENED_ICON_PATH); + break; + case "idle": + tray.setImage(IDLE_ICON_PATH); + break; + default: + tray.setImage(ICON_PATH); + } +} diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index 184b095..cd8027c 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -24,7 +24,8 @@ export const VesktopNative = { relaunch: () => invoke(IpcEvents.RELAUNCH), getVersion: () => sendSync(IpcEvents.GET_VERSION), setBadgeCount: (count: number) => invoke(IpcEvents.SET_BADGE_COUNT, count), - supportsWindowsTransparency: () => sendSync(IpcEvents.SUPPORTS_WINDOWS_TRANSPARENCY) + supportsWindowsTransparency: () => sendSync(IpcEvents.SUPPORTS_WINDOWS_TRANSPARENCY), + setTrayIcon: (iconName: string) => invoke(IpcEvents.SET_TRAY_ICON, iconName) }, autostart: { isEnabled: () => sendSync(IpcEvents.AUTOSTART_ENABLED), diff --git a/src/renderer/patches/index.ts b/src/renderer/patches/index.ts index c243b14..bbe8bae 100644 --- a/src/renderer/patches/index.ts +++ b/src/renderer/patches/index.ts @@ -11,3 +11,4 @@ import "./hideSwitchDevice"; import "./screenShareFixes"; import "./spellCheck"; import "./windowsTitleBar"; +import "./tray"; diff --git a/src/renderer/patches/tray.ts b/src/renderer/patches/tray.ts new file mode 100644 index 0000000..390428e --- /dev/null +++ b/src/renderer/patches/tray.ts @@ -0,0 +1,59 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * Vesktop, a desktop app aiming to give you a snappier Discord Experience + * Copyright (c) 2024 Vendicated and Vencord contributors + */ + +import { findByPropsLazy, findStoreLazy, onceReady } from "@vencord/types/webpack"; +import { FluxDispatcher, UserStore } from "@vencord/types/webpack/common"; + +const logger = new Vencord.Util.Logger("VesktopTray"); + +const muteActions = findByPropsLazy("isSelfMute"); +const deafActions = findByPropsLazy("isSelfDeaf"); + +onceReady.then(() => { + const userID = UserStore.getCurrentUser().id; + + FluxDispatcher.subscribe("SPEAKING", params => { + if (params.userId === userID) { + if (params.speakingFlags) { + VesktopNative.app.setTrayIcon("speaking"); + } else { + if (deafActions.isSelfDeaf()) { + VesktopNative.app.setTrayIcon("deafened"); + } else if (muteActions.isSelfMute()) { + VesktopNative.app.setTrayIcon("muted"); + } else { + VesktopNative.app.setTrayIcon("idle"); + } + } + } + }); + + FluxDispatcher.subscribe("AUDIO_TOGGLE_SELF_DEAF", () => { + if (deafActions.isSelfDeaf()) { + VesktopNative.app.setTrayIcon("deafened"); + } else if (muteActions.isSelfMute()) { + VesktopNative.app.setTrayIcon("muted"); + } else { + VesktopNative.app.setTrayIcon("idle"); + } + }); + + FluxDispatcher.subscribe("AUDIO_TOGGLE_SELF_MUTE", () => { + if (muteActions.isSelfMute()) { + VesktopNative.app.setTrayIcon("muted"); + } else { + VesktopNative.app.setTrayIcon("idle"); + } + }); + + FluxDispatcher.subscribe("RTC_CONNECTION_STATE", params => { + if (params.state == "RTC_CONNECTED") { + VesktopNative.app.setTrayIcon("idle"); + } else if (params.state == "RTC_DISCONNECTED") { + VesktopNative.app.setTrayIcon("main"); + } + }); +}); diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index df64403..63df0d9 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -49,5 +49,7 @@ export const enum IpcEvents { ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY", - CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE" + CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE", + + SET_TRAY_ICON = "VCD_SET_TRAY_ICON" } diff --git a/src/shared/paths.ts b/src/shared/paths.ts index 483250a..8ef1390 100644 --- a/src/shared/paths.ts +++ b/src/shared/paths.ts @@ -10,3 +10,7 @@ export const STATIC_DIR = /* @__PURE__ */ join(__dirname, "..", "..", "static"); export const VIEW_DIR = /* @__PURE__ */ join(STATIC_DIR, "views"); export const BADGE_DIR = /* @__PURE__ */ join(STATIC_DIR, "badges"); export const ICON_PATH = /* @__PURE__ */ join(STATIC_DIR, "icon.png"); +export const SPEAKING_ICON_PATH = /* @__PURE__ */ join(STATIC_DIR, "speaking.png"); +export const MUTED_ICON_PATH = /* @__PURE__ */ join(STATIC_DIR, "muted.png"); +export const DEAFENED_ICON_PATH = /* @__PURE__ */ join(STATIC_DIR, "deafened.png"); +export const IDLE_ICON_PATH = /* @__PURE__ */ join(STATIC_DIR, "idle.png");