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

window.ts

146 lines5.4 KB

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