29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
import { contextBridge, ipcRenderer } from "electron";
|
|
import type { DesktopBridge } from "@t3tools/contracts";
|
|
|
|
const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
|
const CONFIRM_CHANNEL = "desktop:confirm";
|
|
const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
|
|
const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
|
|
const MENU_ACTION_CHANNEL = "desktop:menu-action";
|
|
const wsUrl = process.env.T3CODE_DESKTOP_WS_URL ?? null;
|
|
|
|
contextBridge.exposeInMainWorld("desktopBridge", {
|
|
getWsUrl: () => wsUrl,
|
|
pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL),
|
|
confirm: (message) => ipcRenderer.invoke(CONFIRM_CHANNEL, message),
|
|
showContextMenu: (items) => ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items),
|
|
openExternal: (url: string) => ipcRenderer.invoke(OPEN_EXTERNAL_CHANNEL, url),
|
|
onMenuAction: (listener) => {
|
|
const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
|
|
if (typeof action !== "string") return;
|
|
listener(action);
|
|
};
|
|
|
|
ipcRenderer.on(MENU_ACTION_CHANNEL, wrappedListener);
|
|
return () => {
|
|
ipcRenderer.removeListener(MENU_ACTION_CHANNEL, wrappedListener);
|
|
};
|
|
},
|
|
} satisfies DesktopBridge);
|