Open History to see the last commit
1import { contextBridge, ipcRenderer } from "electron";
2import type { DesktopBridge } from "@t3tools/contracts";
3
4const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
5const CONFIRM_CHANNEL = "desktop:confirm";
6const SET_THEME_CHANNEL = "desktop:set-theme";
7const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
8const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
9const MENU_ACTION_CHANNEL = "desktop:menu-action";
10const UPDATE_STATE_CHANNEL = "desktop:update-state";
11const UPDATE_GET_STATE_CHANNEL = "desktop:update-get-state";
12const UPDATE_CHECK_CHANNEL = "desktop:update-check";
13const UPDATE_DOWNLOAD_CHANNEL = "desktop:update-download";
14const UPDATE_INSTALL_CHANNEL = "desktop:update-install";
15const GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL = "desktop:get-local-environment-bootstrap";
16const GET_SERVER_EXPOSURE_STATE_CHANNEL = "desktop:get-server-exposure-state";
17const SET_SERVER_EXPOSURE_MODE_CHANNEL = "desktop:set-server-exposure-mode";
18
19contextBridge.exposeInMainWorld("desktopBridge", {
20 getLocalEnvironmentBootstrap: () => {
21 const result = ipcRenderer.sendSync(GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL);
22 if (typeof result !== "object" || result === null) {
23 return null;
24 }
25 return result as ReturnType<DesktopBridge["getLocalEnvironmentBootstrap"]>;
26 },
27 getServerExposureState: () => ipcRenderer.invoke(GET_SERVER_EXPOSURE_STATE_CHANNEL),
28 setServerExposureMode: (mode) => ipcRenderer.invoke(SET_SERVER_EXPOSURE_MODE_CHANNEL, mode),
29 pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL),
30 confirm: (message) => ipcRenderer.invoke(CONFIRM_CHANNEL, message),
31 setTheme: (theme) => ipcRenderer.invoke(SET_THEME_CHANNEL, theme),
32 showContextMenu: (items, position) => ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items, position),
33 openExternal: (url: string) => ipcRenderer.invoke(OPEN_EXTERNAL_CHANNEL, url),
34 onMenuAction: (listener) => {
35 const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
36 if (typeof action !== "string") return;
37 listener(action);
38 };
39
40 ipcRenderer.on(MENU_ACTION_CHANNEL, wrappedListener);
41 return () => {
42 ipcRenderer.removeListener(MENU_ACTION_CHANNEL, wrappedListener);
43 };
44 },
45 getUpdateState: () => ipcRenderer.invoke(UPDATE_GET_STATE_CHANNEL),
46 checkForUpdate: () => ipcRenderer.invoke(UPDATE_CHECK_CHANNEL),
47 downloadUpdate: () => ipcRenderer.invoke(UPDATE_DOWNLOAD_CHANNEL),
48 installUpdate: () => ipcRenderer.invoke(UPDATE_INSTALL_CHANNEL),
49 onUpdateState: (listener) => {
50 const wrappedListener = (_event: Electron.IpcRendererEvent, state: unknown) => {
51 if (typeof state !== "object" || state === null) return;
52 listener(state as Parameters<typeof listener>[0]);
53 };
54
55 ipcRenderer.on(UPDATE_STATE_CHANNEL, wrappedListener);
56 return () => {
57 ipcRenderer.removeListener(UPDATE_STATE_CHANNEL, wrappedListener);
58 };
59 },
60} satisfies DesktopBridge);