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_SET_CHANNEL_CHANNEL = "desktop:update-set-channel";
13const UPDATE_CHECK_CHANNEL = "desktop:update-check";
14const UPDATE_DOWNLOAD_CHANNEL = "desktop:update-download";
15const UPDATE_INSTALL_CHANNEL = "desktop:update-install";
16const GET_APP_BRANDING_CHANNEL = "desktop:get-app-branding";
17const GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL = "desktop:get-local-environment-bootstrap";
18const GET_CLIENT_SETTINGS_CHANNEL = "desktop:get-client-settings";
19const SET_CLIENT_SETTINGS_CHANNEL = "desktop:set-client-settings";
20const GET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL = "desktop:get-saved-environment-registry";
21const SET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL = "desktop:set-saved-environment-registry";
22const GET_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:get-saved-environment-secret";
23const SET_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:set-saved-environment-secret";
24const REMOVE_SAVED_ENVIRONMENT_SECRET_CHANNEL = "desktop:remove-saved-environment-secret";
25const GET_SERVER_EXPOSURE_STATE_CHANNEL = "desktop:get-server-exposure-state";
26const SET_SERVER_EXPOSURE_MODE_CHANNEL = "desktop:set-server-exposure-mode";
27
28contextBridge.exposeInMainWorld("desktopBridge", {
29 getAppBranding: () => {
30 const result = ipcRenderer.sendSync(GET_APP_BRANDING_CHANNEL);
31 if (typeof result !== "object" || result === null) {
32 return null;
33 }
34 return result as ReturnType<DesktopBridge["getAppBranding"]>;
35 },
36 getLocalEnvironmentBootstrap: () => {
37 const result = ipcRenderer.sendSync(GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL);
38 if (typeof result !== "object" || result === null) {
39 return null;
40 }
41 return result as ReturnType<DesktopBridge["getLocalEnvironmentBootstrap"]>;
42 },
43 getClientSettings: () => ipcRenderer.invoke(GET_CLIENT_SETTINGS_CHANNEL),
44 setClientSettings: (settings) => ipcRenderer.invoke(SET_CLIENT_SETTINGS_CHANNEL, settings),
45 getSavedEnvironmentRegistry: () => ipcRenderer.invoke(GET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL),
46 setSavedEnvironmentRegistry: (records) =>
47 ipcRenderer.invoke(SET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL, records),
48 getSavedEnvironmentSecret: (environmentId) =>
49 ipcRenderer.invoke(GET_SAVED_ENVIRONMENT_SECRET_CHANNEL, environmentId),
50 setSavedEnvironmentSecret: (environmentId, secret) =>
51 ipcRenderer.invoke(SET_SAVED_ENVIRONMENT_SECRET_CHANNEL, environmentId, secret),
52 removeSavedEnvironmentSecret: (environmentId) =>
53 ipcRenderer.invoke(REMOVE_SAVED_ENVIRONMENT_SECRET_CHANNEL, environmentId),
54 getServerExposureState: () => ipcRenderer.invoke(GET_SERVER_EXPOSURE_STATE_CHANNEL),
55 setServerExposureMode: (mode) => ipcRenderer.invoke(SET_SERVER_EXPOSURE_MODE_CHANNEL, mode),
56 pickFolder: (options) => ipcRenderer.invoke(PICK_FOLDER_CHANNEL, options),
57 confirm: (message) => ipcRenderer.invoke(CONFIRM_CHANNEL, message),
58 setTheme: (theme) => ipcRenderer.invoke(SET_THEME_CHANNEL, theme),
59 showContextMenu: (items, position) => ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items, position),
60 openExternal: (url: string) => ipcRenderer.invoke(OPEN_EXTERNAL_CHANNEL, url),
61 onMenuAction: (listener) => {
62 const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
63 if (typeof action !== "string") return;
64 listener(action);
65 };
66
67 ipcRenderer.on(MENU_ACTION_CHANNEL, wrappedListener);
68 return () => {
69 ipcRenderer.removeListener(MENU_ACTION_CHANNEL, wrappedListener);
70 };
71 },
72 getUpdateState: () => ipcRenderer.invoke(UPDATE_GET_STATE_CHANNEL),
73 setUpdateChannel: (channel) => ipcRenderer.invoke(UPDATE_SET_CHANNEL_CHANNEL, channel),
74 checkForUpdate: () => ipcRenderer.invoke(UPDATE_CHECK_CHANNEL),
75 downloadUpdate: () => ipcRenderer.invoke(UPDATE_DOWNLOAD_CHANNEL),
76 installUpdate: () => ipcRenderer.invoke(UPDATE_INSTALL_CHANNEL),
77 onUpdateState: (listener) => {
78 const wrappedListener = (_event: Electron.IpcRendererEvent, state: unknown) => {
79 if (typeof state !== "object" || state === null) return;
80 listener(state as Parameters<typeof listener>[0]);
81 };
82
83 ipcRenderer.on(UPDATE_STATE_CHANNEL, wrappedListener);
84 return () => {
85 ipcRenderer.removeListener(UPDATE_STATE_CHANNEL, wrappedListener);
86 };
87 },
88} satisfies DesktopBridge);