From 15a49a31e1baf1b310a73204cd54bf2f0dfe9cf8 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Sat, 13 Jan 2024 19:04:32 +0100 Subject: [PATCH] add clipboard.copyImage ipc the web clipboard api only works when focused. so when you try to copy an image but immediately unfocus while its still fetching the image, it will fail. this ipc ensures it will always work even without focus --- src/main/ipc.ts | 9 ++++++++- src/preload/VesktopNative.ts | 4 ++++ src/shared/IpcEvents.ts | 4 +++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 504bd10..1e3cca4 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -7,7 +7,7 @@ if (process.platform === "linux") import("./virtmic"); import { execFile } from "child_process"; -import { app, BrowserWindow, dialog, RelaunchOptions, session, shell } from "electron"; +import { app, BrowserWindow, clipboard, dialog, nativeImage, RelaunchOptions, session, shell } from "electron"; import { mkdirSync, readFileSync, watch } from "fs"; import { open, readFile } from "fs/promises"; import { release } from "os"; @@ -120,6 +120,13 @@ handle(IpcEvents.SELECT_VENCORD_DIR, async () => { handle(IpcEvents.SET_BADGE_COUNT, (_, count: number) => setBadgeCount(count)); +handle(IpcEvents.CLIPBOARD_COPY_IMAGE, async (_, buf: ArrayBuffer, src: string) => { + clipboard.write({ + html: ``, + image: nativeImage.createFromBuffer(Buffer.from(buf)) + }); +}); + function readCss() { return readFile(VENCORD_QUICKCSS_FILE, "utf-8").catch(() => ""); } diff --git a/src/preload/VesktopNative.ts b/src/preload/VesktopNative.ts index d72329d..d17bd23 100644 --- a/src/preload/VesktopNative.ts +++ b/src/preload/VesktopNative.ts @@ -71,5 +71,9 @@ export const VesktopNative = { onActivity(cb: (data: string) => void) { ipcRenderer.on(IpcEvents.ARRPC_ACTIVITY, (_, data: string) => cb(data)); } + }, + clipboard: { + copyImage: (imageBuffer: Uint8Array, imageSrc: string) => + invoke(IpcEvents.CLIPBOARD_COPY_IMAGE, imageBuffer, imageSrc) } }; diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index bd857c4..df64403 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -47,5 +47,7 @@ export const enum IpcEvents { VIRT_MIC_START_SYSTEM = "VCD_VIRT_MIC_START_ALL", VIRT_MIC_STOP = "VCD_VIRT_MIC_STOP", - ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY" + ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY", + + CLIPBOARD_COPY_IMAGE = "VCD_CLIPBOARD_COPY_IMAGE" }