Open History to see the last commit
1import {
2 DesktopPreviewAnnotationThemeInputSchema,
3 DesktopPreviewArtifactInputSchema,
4 DesktopPreviewAutomationClickInputSchema,
5 DesktopPreviewAutomationEvaluateInputSchema,
6 DesktopPreviewAutomationPressInputSchema,
7 DesktopPreviewAutomationScrollInputSchema,
8 DesktopPreviewAutomationTypeInputSchema,
9 DesktopPreviewAutomationWaitForInputSchema,
10 DesktopPreviewConfigInputSchema,
11 DesktopPreviewNavigateInputSchema,
12 DesktopPreviewRecordingArtifactSchema,
13 DesktopPreviewRecordingSaveInputSchema,
14 DesktopPreviewRegisterWebviewInputSchema,
15 DesktopPreviewScreenshotArtifactSchema,
16 DesktopPreviewTabInputSchema,
17 DesktopPreviewWebviewConfigSchema,
18 PreviewAnnotationPayloadSchema,
19 PreviewAutomationSnapshot,
20 PreviewAutomationStatus,
21} from "@t3tools/contracts";
22import * as Effect from "effect/Effect";
23import * as Schema from "effect/Schema";
24import * as NodeURL from "node:url";
25
26import * as ElectronWindow from "../../electron/ElectronWindow.ts";
27import * as PreviewManager from "../../preview/Manager.ts";
28import { PREVIEW_WEBVIEW_PREFERENCES } from "../../preview/WebviewPreferences.ts";
29import * as IpcChannels from "../channels.ts";
30import * as DesktopIpc from "../DesktopIpc.ts";
31
32export const installPreviewEventForwarding = Effect.fn(
33 "desktop.ipc.preview.installEventForwarding",
34)(function* () {
35 const electronWindow = yield* ElectronWindow.ElectronWindow;
36 const manager = yield* PreviewManager.PreviewManager;
37 yield* manager.subscribeStateChanges((tabId, state) =>
38 electronWindow.sendAll(IpcChannels.PREVIEW_STATE_CHANGE_CHANNEL, tabId, state),
39 );
40 yield* manager.subscribeRecordingFrames((frame) =>
41 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_FRAME_CHANNEL, frame),
42 );
43 yield* manager.subscribePointerEvents((event) =>
44 electronWindow.sendAll(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, event),
45 );
46});
47
48export const createTab = DesktopIpc.makeIpcMethod({
49 channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL,
50 payload: DesktopPreviewTabInputSchema,
51 result: Schema.Void,
52 handler: Effect.fn("desktop.ipc.preview.createTab")(function* ({ tabId }) {
53 const manager = yield* PreviewManager.PreviewManager;
54 yield* manager.createTab(tabId);
55 }),
56});
57
58export const closeTab = DesktopIpc.makeIpcMethod({
59 channel: IpcChannels.PREVIEW_CLOSE_TAB_CHANNEL,
60 payload: DesktopPreviewTabInputSchema,
61 result: Schema.Void,
62 handler: Effect.fn("desktop.ipc.preview.closeTab")(function* ({ tabId }) {
63 const manager = yield* PreviewManager.PreviewManager;
64 yield* manager.closeTab(tabId);
65 }),
66});
67
68export const registerWebview = DesktopIpc.makeIpcMethod({
69 channel: IpcChannels.PREVIEW_REGISTER_WEBVIEW_CHANNEL,
70 payload: DesktopPreviewRegisterWebviewInputSchema,
71 result: Schema.Void,
72 handler: Effect.fn("desktop.ipc.preview.registerWebview")(function* ({ tabId, webContentsId }) {
73 const manager = yield* PreviewManager.PreviewManager;
74 yield* manager.registerWebview(tabId, webContentsId);
75 }),
76});
77
78export const navigate = DesktopIpc.makeIpcMethod({
79 channel: IpcChannels.PREVIEW_NAVIGATE_CHANNEL,
80 payload: DesktopPreviewNavigateInputSchema,
81 result: Schema.Void,
82 handler: Effect.fn("desktop.ipc.preview.navigate")(function* ({ tabId, url }) {
83 const manager = yield* PreviewManager.PreviewManager;
84 yield* manager.navigate(tabId, url);
85 }),
86});
87
88const tabMethod = (
89 channel: string,
90 name: string,
91 invoke: (
92 manager: PreviewManager.PreviewManager["Service"],
93 tabId: string,
94 ) => Effect.Effect<void, PreviewManager.PreviewManagerError>,
95) =>
96 DesktopIpc.makeIpcMethod({
97 channel,
98 payload: DesktopPreviewTabInputSchema,
99 result: Schema.Void,
100 handler: Effect.fn(name)(function* ({ tabId }) {
101 const manager = yield* PreviewManager.PreviewManager;
102 yield* invoke(manager, tabId);
103 }),
104 });
105
106export const goBack = tabMethod(
107 IpcChannels.PREVIEW_GO_BACK_CHANNEL,
108 "desktop.ipc.preview.goBack",
109 (manager, tabId) => manager.goBack(tabId),
110);
111export const goForward = tabMethod(
112 IpcChannels.PREVIEW_GO_FORWARD_CHANNEL,
113 "desktop.ipc.preview.goForward",
114 (manager, tabId) => manager.goForward(tabId),
115);
116export const refresh = tabMethod(
117 IpcChannels.PREVIEW_REFRESH_CHANNEL,
118 "desktop.ipc.preview.refresh",
119 (manager, tabId) => manager.refresh(tabId),
120);
121export const zoomIn = tabMethod(
122 IpcChannels.PREVIEW_ZOOM_IN_CHANNEL,
123 "desktop.ipc.preview.zoomIn",
124 (manager, tabId) => manager.zoomIn(tabId),
125);
126export const zoomOut = tabMethod(
127 IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL,
128 "desktop.ipc.preview.zoomOut",
129 (manager, tabId) => manager.zoomOut(tabId),
130);
131export const resetZoom = tabMethod(
132 IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL,
133 "desktop.ipc.preview.resetZoom",
134 (manager, tabId) => manager.resetZoom(tabId),
135);
136export const hardReload = tabMethod(
137 IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL,
138 "desktop.ipc.preview.hardReload",
139 (manager, tabId) => manager.hardReload(tabId),
140);
141export const openDevTools = tabMethod(
142 IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
143 "desktop.ipc.preview.openDevTools",
144 (manager, tabId) => manager.openDevTools(tabId),
145);
146export const cancelPickElement = tabMethod(
147 IpcChannels.PREVIEW_CANCEL_PICK_ELEMENT_CHANNEL,
148 "desktop.ipc.preview.cancelPickElement",
149 (manager, tabId) => manager.cancelPickElement(tabId),
150);
151export const startRecording = tabMethod(
152 IpcChannels.PREVIEW_RECORDING_START_CHANNEL,
153 "desktop.ipc.preview.startRecording",
154 (manager, tabId) => manager.startRecording(tabId),
155);
156export const stopRecording = tabMethod(
157 IpcChannels.PREVIEW_RECORDING_STOP_CHANNEL,
158 "desktop.ipc.preview.stopRecording",
159 (manager, tabId) => manager.stopRecording(tabId),
160);
161
162export const clearCookies = DesktopIpc.makeIpcMethod({
163 channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
164 payload: Schema.Void,
165 result: Schema.Void,
166 handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* () {
167 const manager = yield* PreviewManager.PreviewManager;
168 yield* manager.clearCookies();
169 }),
170});
171
172export const clearCache = DesktopIpc.makeIpcMethod({
173 channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
174 payload: Schema.Void,
175 result: Schema.Void,
176 handler: Effect.fn("desktop.ipc.preview.clearCache")(function* () {
177 const manager = yield* PreviewManager.PreviewManager;
178 yield* manager.clearCache();
179 }),
180});
181
182export const getPreviewConfig = DesktopIpc.makeIpcMethod({
183 channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
184 payload: DesktopPreviewConfigInputSchema,
185 result: DesktopPreviewWebviewConfigSchema,
186 handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId }) {
187 const manager = yield* PreviewManager.PreviewManager;
188 yield* manager.getBrowserSession(environmentId);
189 return {
190 partition: yield* manager.getBrowserPartition(environmentId),
191 webPreferences: PREVIEW_WEBVIEW_PREFERENCES,
192 preloadUrl: NodeURL.pathToFileURL(`${__dirname}/preview-pick-preload.cjs`).href,
193 };
194 }),
195});
196
197export const setAnnotationTheme = DesktopIpc.makeIpcMethod({
198 channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL,
199 payload: DesktopPreviewAnnotationThemeInputSchema,
200 result: Schema.Void,
201 handler: Effect.fn("desktop.ipc.preview.setAnnotationTheme")(function* ({ theme }) {
202 const manager = yield* PreviewManager.PreviewManager;
203 yield* manager.setAnnotationTheme(theme);
204 }),
205});
206
207export const pickElement = DesktopIpc.makeIpcMethod({
208 channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
209 payload: DesktopPreviewTabInputSchema,
210 result: Schema.NullOr(PreviewAnnotationPayloadSchema),
211 handler: Effect.fn("desktop.ipc.preview.pickElement")(function* ({ tabId }) {
212 const manager = yield* PreviewManager.PreviewManager;
213 return yield* manager.pickElement(tabId);
214 }),
215});
216
217export const captureScreenshot = DesktopIpc.makeIpcMethod({
218 channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL,
219 payload: DesktopPreviewTabInputSchema,
220 result: DesktopPreviewScreenshotArtifactSchema,
221 handler: Effect.fn("desktop.ipc.preview.captureScreenshot")(function* ({ tabId }) {
222 const manager = yield* PreviewManager.PreviewManager;
223 return yield* manager.captureScreenshot(tabId);
224 }),
225});
226
227export const revealArtifact = DesktopIpc.makeIpcMethod({
228 channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL,
229 payload: DesktopPreviewArtifactInputSchema,
230 result: Schema.Void,
231 handler: Effect.fn("desktop.ipc.preview.revealArtifact")(function* ({ path }) {
232 const manager = yield* PreviewManager.PreviewManager;
233 yield* manager.revealArtifact(path);
234 }),
235});
236
237export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
238 channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL,
239 payload: DesktopPreviewArtifactInputSchema,
240 result: Schema.Void,
241 handler: Effect.fn("desktop.ipc.preview.copyArtifactToClipboard")(function* ({ path }) {
242 const manager = yield* PreviewManager.PreviewManager;
243 yield* manager.copyArtifactToClipboard(path);
244 }),
245});
246
247export const automationStatus = DesktopIpc.makeIpcMethod({
248 channel: IpcChannels.PREVIEW_AUTOMATION_STATUS_CHANNEL,
249 payload: DesktopPreviewTabInputSchema,
250 result: PreviewAutomationStatus,
251 handler: Effect.fn("desktop.ipc.preview.automationStatus")(function* ({ tabId }) {
252 const manager = yield* PreviewManager.PreviewManager;
253 return yield* manager.automationStatus(tabId);
254 }),
255});
256
257export const automationSnapshot = DesktopIpc.makeIpcMethod({
258 channel: IpcChannels.PREVIEW_AUTOMATION_SNAPSHOT_CHANNEL,
259 payload: DesktopPreviewTabInputSchema,
260 result: PreviewAutomationSnapshot,
261 handler: Effect.fn("desktop.ipc.preview.automationSnapshot")(function* ({ tabId }) {
262 const manager = yield* PreviewManager.PreviewManager;
263 return yield* manager.automationSnapshot(tabId);
264 }),
265});
266
267export const automationClick = DesktopIpc.makeIpcMethod({
268 channel: IpcChannels.PREVIEW_AUTOMATION_CLICK_CHANNEL,
269 payload: DesktopPreviewAutomationClickInputSchema,
270 result: Schema.Void,
271 handler: Effect.fn("desktop.ipc.preview.automationClick")(function* ({ tabId, input }) {
272 const manager = yield* PreviewManager.PreviewManager;
273 yield* manager.automationClick(tabId, input);
274 }),
275});
276
277export const automationType = DesktopIpc.makeIpcMethod({
278 channel: IpcChannels.PREVIEW_AUTOMATION_TYPE_CHANNEL,
279 payload: DesktopPreviewAutomationTypeInputSchema,
280 result: Schema.Void,
281 handler: Effect.fn("desktop.ipc.preview.automationType")(function* ({ tabId, input }) {
282 const manager = yield* PreviewManager.PreviewManager;
283 yield* manager.automationType(tabId, input);
284 }),
285});
286
287export const automationPress = DesktopIpc.makeIpcMethod({
288 channel: IpcChannels.PREVIEW_AUTOMATION_PRESS_CHANNEL,
289 payload: DesktopPreviewAutomationPressInputSchema,
290 result: Schema.Void,
291 handler: Effect.fn("desktop.ipc.preview.automationPress")(function* ({ tabId, input }) {
292 const manager = yield* PreviewManager.PreviewManager;
293 yield* manager.automationPress(tabId, input);
294 }),
295});
296
297export const automationScroll = DesktopIpc.makeIpcMethod({
298 channel: IpcChannels.PREVIEW_AUTOMATION_SCROLL_CHANNEL,
299 payload: DesktopPreviewAutomationScrollInputSchema,
300 result: Schema.Void,
301 handler: Effect.fn("desktop.ipc.preview.automationScroll")(function* ({ tabId, input }) {
302 const manager = yield* PreviewManager.PreviewManager;
303 yield* manager.automationScroll(tabId, input);
304 }),
305});
306
307export const automationEvaluate = DesktopIpc.makeIpcMethod({
308 channel: IpcChannels.PREVIEW_AUTOMATION_EVALUATE_CHANNEL,
309 payload: DesktopPreviewAutomationEvaluateInputSchema,
310 result: Schema.Unknown,
311 handler: Effect.fn("desktop.ipc.preview.automationEvaluate")(function* ({ tabId, input }) {
312 const manager = yield* PreviewManager.PreviewManager;
313 return yield* manager.automationEvaluate(tabId, input);
314 }),
315});
316
317export const automationWaitFor = DesktopIpc.makeIpcMethod({
318 channel: IpcChannels.PREVIEW_AUTOMATION_WAIT_FOR_CHANNEL,
319 payload: DesktopPreviewAutomationWaitForInputSchema,
320 result: Schema.Void,
321 handler: Effect.fn("desktop.ipc.preview.automationWaitFor")(function* ({ tabId, input }) {
322 const manager = yield* PreviewManager.PreviewManager;
323 yield* manager.automationWaitFor(tabId, input);
324 }),
325});
326
327export const saveRecording = DesktopIpc.makeIpcMethod({
328 channel: IpcChannels.PREVIEW_RECORDING_SAVE_CHANNEL,
329 payload: DesktopPreviewRecordingSaveInputSchema,
330 result: DesktopPreviewRecordingArtifactSchema,
331 handler: Effect.fn("desktop.ipc.preview.saveRecording")(function* ({ tabId, mimeType, data }) {
332 const manager = yield* PreviewManager.PreviewManager;
333 return yield* manager.saveRecording(tabId, mimeType, data);
334 }),
335});
336
337export const methods = [
338 createTab,
339 closeTab,
340 registerWebview,
341 navigate,
342 goBack,
343 goForward,
344 refresh,
345 zoomIn,
346 zoomOut,
347 resetZoom,
348 hardReload,
349 openDevTools,
350 clearCookies,
351 clearCache,
352 getPreviewConfig,
353 setAnnotationTheme,
354 pickElement,
355 cancelPickElement,
356 captureScreenshot,
357 revealArtifact,
358 copyArtifactToClipboard,
359 automationStatus,
360 automationSnapshot,
361 automationClick,
362 automationType,
363 automationPress,
364 automationScroll,
365 automationEvaluate,
366 automationWaitFor,
367 startRecording,
368 stopRecording,
369 saveRecording,
370] as const;