Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

preload.ts

78 lines4.2 KB

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