Open History to see the last commit
1import {
2 ContextMenuItemSchema,
3 DesktopAppBrandingSchema,
4 DesktopEnvironmentBootstrapSchema,
5 DesktopThemeSchema,
6 PickFolderOptionsSchema,
7} from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9import * as Option from "effect/Option";
10import * as Schema from "effect/Schema";
11
12import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
13import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
14import * as ElectronDialog from "../../electron/ElectronDialog.ts";
15import * as ElectronMenu from "../../electron/ElectronMenu.ts";
16import * as ElectronShell from "../../electron/ElectronShell.ts";
17import * as ElectronTheme from "../../electron/ElectronTheme.ts";
18import * as ElectronWindow from "../../electron/ElectronWindow.ts";
19import * as IpcChannels from "../channels.ts";
20import { makeIpcMethod, makeSyncIpcMethod } from "../DesktopIpc.ts";
21
22const ContextMenuPosition = Schema.Struct({
23 x: Schema.Number,
24 y: Schema.Number,
25});
26
27const ContextMenuInput = Schema.Struct({
28 items: Schema.Array(ContextMenuItemSchema),
29 position: Schema.optionalKey(ContextMenuPosition),
30});
31
32function toWebSocketBaseUrl(httpBaseUrl: URL): string {
33 const url = new URL(httpBaseUrl.href);
34 url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
35 return url.href;
36}
37
38export const getAppBranding = makeSyncIpcMethod({
39 channel: IpcChannels.GET_APP_BRANDING_CHANNEL,
40 result: Schema.NullOr(DesktopAppBrandingSchema),
41 handler: Effect.fn("desktop.ipc.window.getAppBranding")(function* () {
42 const environment = yield* DesktopEnvironment.DesktopEnvironment;
43 return environment.branding;
44 }),
45});
46
47export const getLocalEnvironmentBootstrap = makeSyncIpcMethod({
48 channel: IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL,
49 result: Schema.NullOr(DesktopEnvironmentBootstrapSchema),
50 handler: Effect.fn("desktop.ipc.window.getLocalEnvironmentBootstrap")(function* () {
51 const backendManager = yield* DesktopBackendManager.DesktopBackendManager;
52 const config = yield* backendManager.currentConfig;
53 return Option.match(config, {
54 onNone: () => null,
55 onSome: ({ bootstrap, httpBaseUrl }) => ({
56 label: "Local environment",
57 httpBaseUrl: httpBaseUrl.href,
58 wsBaseUrl: toWebSocketBaseUrl(httpBaseUrl),
59 ...(bootstrap.desktopBootstrapToken
60 ? { bootstrapToken: bootstrap.desktopBootstrapToken }
61 : {}),
62 }),
63 });
64 }),
65});
66
67export const pickFolder = makeIpcMethod({
68 channel: IpcChannels.PICK_FOLDER_CHANNEL,
69 payload: Schema.UndefinedOr(PickFolderOptionsSchema),
70 result: Schema.NullOr(Schema.String),
71 handler: Effect.fn("desktop.ipc.window.pickFolder")(function* (options) {
72 const dialog = yield* ElectronDialog.ElectronDialog;
73 const electronWindow = yield* ElectronWindow.ElectronWindow;
74 const environment = yield* DesktopEnvironment.DesktopEnvironment;
75 const selectedPath = yield* dialog.pickFolder({
76 owner: yield* electronWindow.focusedMainOrFirst,
77 defaultPath: environment.resolvePickFolderDefaultPath(options),
78 });
79 return Option.getOrNull(selectedPath);
80 }),
81});
82
83export const confirm = makeIpcMethod({
84 channel: IpcChannels.CONFIRM_CHANNEL,
85 payload: Schema.String,
86 result: Schema.Boolean,
87 handler: Effect.fn("desktop.ipc.window.confirm")(function* (message) {
88 const dialog = yield* ElectronDialog.ElectronDialog;
89 const electronWindow = yield* ElectronWindow.ElectronWindow;
90 return yield* electronWindow.focusedMainOrFirst.pipe(
91 Effect.flatMap((owner) => dialog.confirm({ owner, message })),
92 );
93 }),
94});
95
96export const setTheme = makeIpcMethod({
97 channel: IpcChannels.SET_THEME_CHANNEL,
98 payload: DesktopThemeSchema,
99 result: Schema.Void,
100 handler: Effect.fn("desktop.ipc.window.setTheme")(function* (theme) {
101 const electronTheme = yield* ElectronTheme.ElectronTheme;
102 yield* electronTheme.setSource(theme);
103 }),
104});
105
106export const showContextMenu = makeIpcMethod({
107 channel: IpcChannels.CONTEXT_MENU_CHANNEL,
108 payload: ContextMenuInput,
109 result: Schema.NullOr(Schema.String),
110 handler: Effect.fn("desktop.ipc.window.showContextMenu")(function* (input) {
111 const electronMenu = yield* ElectronMenu.ElectronMenu;
112 const electronWindow = yield* ElectronWindow.ElectronWindow;
113 const window = yield* electronWindow.focusedMainOrFirst;
114 if (Option.isNone(window)) {
115 return null;
116 }
117
118 const selectedItemId = yield* electronMenu.showContextMenu({
119 window: window.value,
120 items: input.items,
121 position: Option.fromNullishOr(input.position),
122 });
123 return Option.getOrNull(selectedItemId);
124 }),
125});
126
127export const openExternal = makeIpcMethod({
128 channel: IpcChannels.OPEN_EXTERNAL_CHANNEL,
129 payload: Schema.String,
130 result: Schema.Boolean,
131 handler: Effect.fn("desktop.ipc.window.openExternal")(function* (url) {
132 const shell = yield* ElectronShell.ElectronShell;
133 return yield* shell.openExternal(url);
134 }),
135});