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

preview.ts

456 lines16.6 KB

1import {
2 DesktopPreviewAnnotationSendEnabledInputSchema,
3 DesktopPreviewAnnotationThemeInputSchema,
4 DesktopPreviewArtifactInputSchema,
5 DesktopPreviewConfigInputSchema,
6 DesktopPreviewNavigateInputSchema,
7 DesktopPreviewRecordingArtifactSchema,
8 DesktopPreviewRecordingSaveInputSchema,
9 DesktopPreviewRegisterWebviewInputSchema,
10 DesktopPreviewScreenshotArtifactSchema,
11 DesktopPreviewSetAudioMutedInputSchema,
12 DesktopPreviewSetColorSchemeInputSchema,
13 DesktopPreviewSetZoomFactorInputSchema,
14 BrowserImportResult,
15 BrowserImportSource,
16 DesktopPreviewClearDataInputSchema,
17 DesktopPreviewImportCookiesInputSchema,
18 DesktopPreviewCreateTabInputSchema,
19 DesktopPreviewTabInputSchema,
20 DesktopPreviewWebviewConfigSchema,
21 PreviewAnnotationSubmissionResultSchema,
22 PreviewForwardedShortcut,
23 MAX_KEYBINDINGS_COUNT,
24} from "@t3tools/contracts";
25import * as Effect from "effect/Effect";
26import * as Schema from "effect/Schema";
27import * as Option from "effect/Option";
28import * as NodeURL from "node:url";
29​
30import * as ElectronWindow from "../../electron/ElectronWindow.ts";
31import * as BrowserImport from "../../preview/BrowserImport/BrowserImport.ts";
32import * as BrowserSession from "../../preview/BrowserSession.ts";
33import * as PreviewManager from "../../preview/Manager.ts";
34import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
35import { PREVIEW_WEBVIEW_PREFERENCES } from "../../preview/WebviewPreferences.ts";
36import * as IpcChannels from "../channels.ts";
37import * as DesktopIpc from "../DesktopIpc.ts";
38​
39export const installPreviewEventForwarding = Effect.fn(
40 "desktop.ipc.preview.installEventForwarding",
41)(function* () {
42 const electronWindow = yield* ElectronWindow.ElectronWindow;
43 const manager = yield* PreviewManager.PreviewManager;
44 yield* manager.subscribeStateChanges((tabId, state) =>
45 electronWindow.sendAll(IpcChannels.PREVIEW_STATE_CHANGE_CHANNEL, tabId, state),
46 );
47 yield* manager.subscribeRecordingFrames((frame) =>
48 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_FRAME_CHANNEL, frame),
49 );
50 yield* manager.subscribeRecordingInputs((event) =>
51 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_INPUT_CHANNEL, event),
52 );
53 yield* manager.subscribePointerEvents((event) =>
54 electronWindow.sendAll(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, event),
55 );
56 yield* manager.subscribeOpenLinks((event) =>
57 electronWindow.sendAll(IpcChannels.PREVIEW_OPEN_LINK_CHANNEL, event),
58 );
59});
60​
61export const setForwardedShortcuts = DesktopIpc.makeIpcMethod({
62 channel: IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL,
63 payload: Schema.Array(PreviewForwardedShortcut).check(Schema.isMaxLength(MAX_KEYBINDINGS_COUNT)),
64 result: Schema.Void,
65 handler: Effect.fn("desktop.ipc.preview.setForwardedShortcuts")(function* (shortcuts) {
66 const manager = yield* PreviewManager.PreviewManager;
67 yield* manager.setForwardedShortcuts(shortcuts);
68 }),
69});
70​
71export const createTab = DesktopIpc.makeIpcMethod({
72 channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL,
73 payload: DesktopPreviewCreateTabInputSchema,
74 result: Schema.Void,
75 handler: Effect.fn("desktop.ipc.preview.createTab")(function* ({
76 tabId,
77 zoomFactor,
78 colorScheme,
79 serverTab,
80 environmentId,
81 profileId,
82 url,
83 }) {
84 const manager = yield* PreviewManager.PreviewManager;
85 yield* manager.createTab(tabId, {
86 zoomFactor,
87 colorScheme,
88 serverTab,
89 environmentId,
90 profileId,
91 url,
92 });
93 }),
94});
95​
96export const closeTab = DesktopIpc.makeIpcMethod({
97 channel: IpcChannels.PREVIEW_CLOSE_TAB_CHANNEL,
98 payload: DesktopPreviewTabInputSchema,
99 result: Schema.Void,
100 handler: Effect.fn("desktop.ipc.preview.closeTab")(function* ({ tabId }) {
101 const manager = yield* PreviewManager.PreviewManager;
102 yield* manager.closeTab(tabId);
103 }),
104});
105​
106export const registerWebview = DesktopIpc.makeIpcMethod({
107 channel: IpcChannels.PREVIEW_REGISTER_WEBVIEW_CHANNEL,
108 payload: DesktopPreviewRegisterWebviewInputSchema,
109 result: Schema.Void,
110 handler: Effect.fn("desktop.ipc.preview.registerWebview")(function* ({ tabId, webContentsId }) {
111 const manager = yield* PreviewManager.PreviewManager;
112 yield* manager.registerWebview(tabId, webContentsId);
113 }),
114});
115​
116export const navigate = DesktopIpc.makeIpcMethod({
117 channel: IpcChannels.PREVIEW_NAVIGATE_CHANNEL,
118 payload: DesktopPreviewNavigateInputSchema,
119 result: Schema.Void,
120 handler: Effect.fn("desktop.ipc.preview.navigate")(function* ({ tabId, url }) {
121 const manager = yield* PreviewManager.PreviewManager;
122 yield* manager.navigate(tabId, url);
123 }),
124});
125​
126const tabMethod = (
127 channel: string,
128 name: string,
129 invoke: (
130 manager: PreviewManager.PreviewManager["Service"],
131 tabId: string,
132 ) => Effect.Effect<void, PreviewManager.PreviewManagerError>,
133) =>
134 DesktopIpc.makeIpcMethod({
135 channel,
136 payload: DesktopPreviewTabInputSchema,
137 result: Schema.Void,
138 handler: Effect.fn(name)(function* ({ tabId }) {
139 const manager = yield* PreviewManager.PreviewManager;
140 yield* invoke(manager, tabId);
141 }),
142 });
143​
144export const goBack = tabMethod(
145 IpcChannels.PREVIEW_GO_BACK_CHANNEL,
146 "desktop.ipc.preview.goBack",
147 (manager, tabId) => manager.goBack(tabId),
148);
149export const goForward = tabMethod(
150 IpcChannels.PREVIEW_GO_FORWARD_CHANNEL,
151 "desktop.ipc.preview.goForward",
152 (manager, tabId) => manager.goForward(tabId),
153);
154export const refresh = tabMethod(
155 IpcChannels.PREVIEW_REFRESH_CHANNEL,
156 "desktop.ipc.preview.refresh",
157 (manager, tabId) => manager.refresh(tabId),
158);
159export const zoomIn = tabMethod(
160 IpcChannels.PREVIEW_ZOOM_IN_CHANNEL,
161 "desktop.ipc.preview.zoomIn",
162 (manager, tabId) => manager.zoomIn(tabId),
163);
164export const zoomOut = tabMethod(
165 IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL,
166 "desktop.ipc.preview.zoomOut",
167 (manager, tabId) => manager.zoomOut(tabId),
168);
169export const resetZoom = tabMethod(
170 IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL,
171 "desktop.ipc.preview.resetZoom",
172 (manager, tabId) => manager.resetZoom(tabId),
173);
174export const hardReload = tabMethod(
175 IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL,
176 "desktop.ipc.preview.hardReload",
177 (manager, tabId) => manager.hardReload(tabId),
178);
179export const setZoomFactor = DesktopIpc.makeIpcMethod({
180 channel: IpcChannels.PREVIEW_SET_ZOOM_FACTOR_CHANNEL,
181 payload: DesktopPreviewSetZoomFactorInputSchema,
182 result: Schema.Void,
183 handler: Effect.fn("desktop.ipc.preview.setZoomFactor")(function* ({ tabId, zoomFactor }) {
184 const manager = yield* PreviewManager.PreviewManager;
185 yield* manager.setZoomFactor(tabId, zoomFactor);
186 }),
187});
188export const setColorScheme = DesktopIpc.makeIpcMethod({
189 channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
190 payload: DesktopPreviewSetColorSchemeInputSchema,
191 result: Schema.Void,
192 handler: Effect.fn("desktop.ipc.preview.setColorScheme")(function* ({ tabId, colorScheme }) {
193 const manager = yield* PreviewManager.PreviewManager;
194 yield* manager.setColorScheme(tabId, colorScheme);
195 }),
196});
197export const setAudioMuted = DesktopIpc.makeIpcMethod({
198 channel: IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL,
199 payload: DesktopPreviewSetAudioMutedInputSchema,
200 result: Schema.Void,
201 handler: Effect.fn("desktop.ipc.preview.setAudioMuted")(function* ({ tabId, audioMuted }) {
202 const manager = yield* PreviewManager.PreviewManager;
203 yield* manager.setAudioMuted(tabId, audioMuted);
204 }),
205});
206export const openDevTools = tabMethod(
207 IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
208 "desktop.ipc.preview.openDevTools",
209 (manager, tabId) => manager.openDevTools(tabId),
210);
211export const cancelPickElement = tabMethod(
212 IpcChannels.PREVIEW_CANCEL_PICK_ELEMENT_CHANNEL,
213 "desktop.ipc.preview.cancelPickElement",
214 (manager, tabId) => manager.cancelPickElement(tabId),
215);
216export const startRecording = DesktopIpc.makeIpcMethod({
217 channel: IpcChannels.PREVIEW_RECORDING_START_CHANNEL,
218 payload: DesktopPreviewTabInputSchema,
219 result: Schema.Void,
220 handler: Effect.fn("desktop.ipc.preview.startRecording")(function* ({ tabId }) {
221 const manager = yield* PreviewManager.PreviewManager;
222 const store = yield* DesktopClientSettings.DesktopClientSettings;
223 const settings = yield* store.get;
224 const options = Option.map(settings, (value) => ({
225 showKeyPresses: value.browserRecordingShowKeyPresses,
226 showMousePresses: value.browserRecordingShowMousePresses,
227 }));
228 yield* manager.startRecording(tabId, Option.getOrUndefined(options));
229 }),
230});
231export const stopRecording = tabMethod(
232 IpcChannels.PREVIEW_RECORDING_STOP_CHANNEL,
233 "desktop.ipc.preview.stopRecording",
234 (manager, tabId) => manager.stopRecording(tabId),
235);
236export const openPictureInPicture = tabMethod(
237 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_OPEN_CHANNEL,
238 "desktop.ipc.preview.openPictureInPicture",
239 (manager, tabId) => manager.openPictureInPicture(tabId),
240);
241export const closePictureInPicture = tabMethod(
242 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL,
243 "desktop.ipc.preview.closePictureInPicture",
244 (manager, tabId) => manager.closePictureInPicture(tabId),
245);
246​
247export const clearCookies = DesktopIpc.makeIpcMethod({
248 channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
249 payload: DesktopPreviewClearDataInputSchema,
250 result: Schema.Void,
251 handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* ({ environmentId, profileId }) {
252 const manager = yield* PreviewManager.PreviewManager;
253 yield* manager.clearCookies(yield* resolveClearPartitions(manager, environmentId, profileId));
254 }),
255});
256​
257export const clearCache = DesktopIpc.makeIpcMethod({
258 channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
259 payload: DesktopPreviewClearDataInputSchema,
260 result: Schema.Void,
261 handler: Effect.fn("desktop.ipc.preview.clearCache")(function* ({ environmentId, profileId }) {
262 const manager = yield* PreviewManager.PreviewManager;
263 yield* manager.clearCache(yield* resolveClearPartitions(manager, environmentId, profileId));
264 }),
265});
266​
267/**
268 * Clearing without a profile keeps the historical "everything" behaviour for
269 * an explicit all-profiles action; naming a profile confines it to that
270 * profile's partition so one profile's sign-out cannot reach the others.
271 */
272const resolveClearPartitions = Effect.fn("desktop.ipc.preview.resolveClearPartitions")(function* (
273 manager: PreviewManager.PreviewManager["Service"],
274 environmentId: string,
275 profileId: string | undefined,
276) {
277 if (profileId === undefined) return undefined;
278 const { scope, persistent, namespace } = BrowserSession.resolvePartitionScope(
279 environmentId,
280 profileId,
281 );
282 // Loading the session is what puts the partition in the map the clear walks.
283 // Deriving the partition string alone leaves nothing to match, so clearing a
284 // profile with no tab open this run — after a restart, or when deleting a
285 // profile — would report success and delete nothing.
286 yield* manager.getBrowserSession(scope, persistent, namespace);
287 return [yield* manager.getBrowserPartition(scope, persistent, namespace)];
288});
289​
290export const getPreviewConfig = DesktopIpc.makeIpcMethod({
291 channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
292 payload: DesktopPreviewConfigInputSchema,
293 result: DesktopPreviewWebviewConfigSchema,
294 handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId, profileId }) {
295 const manager = yield* PreviewManager.PreviewManager;
296 const { scope, persistent, namespace } = BrowserSession.resolvePartitionScope(
297 environmentId,
298 profileId,
299 );
300 // Creating the session first is what installs the UA rewrite and permission
301 // handlers; a guest that attached to an untouched partition would run with
302 // Electron's default UA and Chromium's default permission behaviour.
303 yield* manager.getBrowserSession(scope, persistent, namespace);
304 return {
305 partition: yield* manager.getBrowserPartition(scope, persistent, namespace),
306 webPreferences: PREVIEW_WEBVIEW_PREFERENCES,
307 preloadUrl: NodeURL.pathToFileURL(`${__dirname}/preview-pick-preload.cjs`).href,
308 };
309 }),
310});
311​
312/**
313 * Registered separately from `methods`: these carry `BrowserImport` in their
314 * context and their own failure type, so they do not unify with the
315 * manager-backed handlers the shared loop iterates.
316 */
317export const listBrowserImportSources = DesktopIpc.makeIpcMethod({
318 channel: IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL,
319 payload: Schema.Void,
320 result: Schema.Array(BrowserImportSource),
321 handler: Effect.fn("desktop.ipc.preview.listBrowserImportSources")(function* () {
322 const browserImport = yield* BrowserImport.BrowserImport;
323 return yield* browserImport.listSources;
324 }),
325});
326​
327export const importBrowserCookies = DesktopIpc.makeIpcMethod({
328 channel: IpcChannels.PREVIEW_IMPORT_COOKIES_CHANNEL,
329 payload: DesktopPreviewImportCookiesInputSchema,
330 result: BrowserImportResult,
331 handler: Effect.fn("desktop.ipc.preview.importBrowserCookies")(function* ({
332 environmentId,
333 ...importInput
334 }) {
335 const browserImport = yield* BrowserImport.BrowserImport;
336 // Derived in main from the same helper the webview config uses, so cookies
337 // land in exactly the partition the profile's tabs attach to.
338 const { scope, persistent, namespace } = BrowserSession.resolvePartitionScope(
339 environmentId,
340 importInput.targetProfileId,
341 );
342 return yield* browserImport.importCookies({
343 input: importInput,
344 scope,
345 persistent,
346 ...(namespace === undefined ? {} : { namespace }),
347 });
348 }),
349});
350​
351export const setAnnotationTheme = DesktopIpc.makeIpcMethod({
352 channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL,
353 payload: DesktopPreviewAnnotationThemeInputSchema,
354 result: Schema.Void,
355 handler: Effect.fn("desktop.ipc.preview.setAnnotationTheme")(function* ({ theme }) {
356 const manager = yield* PreviewManager.PreviewManager;
357 yield* manager.setAnnotationTheme(theme);
358 }),
359});
360​
361export const pickElement = DesktopIpc.makeIpcMethod({
362 channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
363 payload: DesktopPreviewTabInputSchema,
364 result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema),
365 handler: Effect.fn("desktop.ipc.preview.pickElement")(function* ({ tabId }) {
366 const manager = yield* PreviewManager.PreviewManager;
367 return yield* manager.pickElement(tabId);
368 }),
369});
370​
371export const setAnnotationSendEnabled = DesktopIpc.makeIpcMethod({
372 channel: IpcChannels.PREVIEW_SET_ANNOTATION_SEND_ENABLED_CHANNEL,
373 payload: DesktopPreviewAnnotationSendEnabledInputSchema,
374 result: Schema.Void,
375 handler: Effect.fn("desktop.ipc.preview.setAnnotationSendEnabled")(function* ({
376 tabId,
377 enabled,
378 }) {
379 const manager = yield* PreviewManager.PreviewManager;
380 yield* manager.setAnnotationSendEnabled(tabId, enabled);
381 }),
382});
383​
384export const captureScreenshot = DesktopIpc.makeIpcMethod({
385 channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL,
386 payload: DesktopPreviewTabInputSchema,
387 result: DesktopPreviewScreenshotArtifactSchema,
388 handler: Effect.fn("desktop.ipc.preview.captureScreenshot")(function* ({ tabId }) {
389 const manager = yield* PreviewManager.PreviewManager;
390 return yield* manager.captureScreenshot(tabId);
391 }),
392});
393​
394export const revealArtifact = DesktopIpc.makeIpcMethod({
395 channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL,
396 payload: DesktopPreviewArtifactInputSchema,
397 result: Schema.Void,
398 handler: Effect.fn("desktop.ipc.preview.revealArtifact")(function* ({ path }) {
399 const manager = yield* PreviewManager.PreviewManager;
400 yield* manager.revealArtifact(path);
401 }),
402});
403​
404export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
405 channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL,
406 payload: DesktopPreviewArtifactInputSchema,
407 result: Schema.Void,
408 handler: Effect.fn("desktop.ipc.preview.copyArtifactToClipboard")(function* ({ path }) {
409 const manager = yield* PreviewManager.PreviewManager;
410 yield* manager.copyArtifactToClipboard(path);
411 }),
412});
413​
414export const saveRecording = DesktopIpc.makeIpcMethod({
415 channel: IpcChannels.PREVIEW_RECORDING_SAVE_CHANNEL,
416 payload: DesktopPreviewRecordingSaveInputSchema,
417 result: DesktopPreviewRecordingArtifactSchema,
418 handler: Effect.fn("desktop.ipc.preview.saveRecording")(function* ({ tabId, mimeType, data }) {
419 const manager = yield* PreviewManager.PreviewManager;
420 return yield* manager.saveRecording(tabId, mimeType, data);
421 }),
422});
423​
424export const methods = [
425 setForwardedShortcuts,
426 createTab,
427 closeTab,
428 registerWebview,
429 navigate,
430 goBack,
431 goForward,
432 refresh,
433 zoomIn,
434 zoomOut,
435 resetZoom,
436 hardReload,
437 setColorScheme,
438 setZoomFactor,
439 setAudioMuted,
440 openDevTools,
441 clearCookies,
442 clearCache,
443 getPreviewConfig,
444 setAnnotationTheme,
445 setAnnotationSendEnabled,
446 pickElement,
447 cancelPickElement,
448 captureScreenshot,
449 revealArtifact,
450 copyArtifactToClipboard,
451 openPictureInPicture,
452 closePictureInPicture,
453 startRecording,
454 stopRecording,
455 saveRecording,
456] as const;