The best way to control Claude Code, Codex and more from any computer or phone

main
ts 241 lines 8.72 KB
Raw
1 import {
2 DesktopPendingSnapShot,
3 DesktopSnapShot as DesktopSnapShotSchema,
4 DesktopSnapShotAnimationDestination,
5 DesktopSnapShotId,
6 DesktopSnapShotShortcutAvailability,
7 DesktopSnapShotState,
8 DesktopSnapShotSetupAction,
9 SnapShotShortcut,
10 DesktopCaptureConfigRequest,
11 DesktopCaptureConfigPreview,
12 DesktopCaptureConfigApplied,
13 } from "@t3tools/contracts";
14 import * as Effect from "effect/Effect";
15 import * as Option from "effect/Option";
16 import * as Schema from "effect/Schema";
17 import { msg } from "gt-i18n";
18 import type * as Electron from "electron";
19
20 import * as ElectronWindow from "../../electron/ElectronWindow.ts";
21 import * as ElectronDialog from "../../electron/ElectronDialog.ts";
22 import * as DesktopSnapShot from "../../snapShot/DesktopSnapShot.ts";
23 import { m } from "../../i18n/messages.ts";
24 import * as IpcChannels from "../channels.ts";
25 import * as DesktopIpc from "../DesktopIpc.ts";
26
27 class SnapShotIpcUnauthorizedSenderError extends Schema.TaggedError<SnapShotIpcUnauthorizedSenderError>()(
28 "SnapShotIpcUnauthorizedSenderError",
29 {},
30 ) {
31 override get message(): string {
32 return "Snapshot request was rejected.";
33 }
34 }
35
36 const ensureTrustedSnapShotSender = Effect.fn("desktop.ipc.snapShot.ensureTrustedSender")(
37 function* (event: DesktopIpc.DesktopIpcInvokeEvent | undefined) {
38 const main = yield* (yield* ElectronWindow.ElectronWindow).main;
39 if (
40 event === undefined ||
41 Option.isNone(main) ||
42 main.value.webContents.id !== event.sender.id
43 ) {
44 return yield* new SnapShotIpcUnauthorizedSenderError();
45 }
46 return main.value;
47 },
48 );
49
50 function snapShotScreenFrame(
51 viewportFrame: DesktopSnapShotAnimationDestination["viewportFrame"],
52 contentBounds: Electron.Rectangle,
53 zoomFactor: number,
54 ): Electron.Rectangle {
55 return {
56 x: contentBounds.x + viewportFrame.x * zoomFactor,
57 y: contentBounds.y + viewportFrame.y * zoomFactor,
58 width: viewportFrame.width * zoomFactor,
59 height: viewportFrame.height * zoomFactor,
60 };
61 }
62
63 export function snapShotRelativeFrame(
64 frame: DesktopSnapShotAnimationDestination["viewportFrame"],
65 bounds: Electron.Rectangle,
66 zoom: number,
67 ): Electron.Rectangle | undefined {
68 if (bounds.width <= 0 || bounds.height <= 0) return undefined;
69 return {
70 x: (frame.x * zoom) / bounds.width,
71 y: (frame.y * zoom) / bounds.height,
72 width: (frame.width * zoom) / bounds.width,
73 height: (frame.height * zoom) / bounds.height,
74 };
75 }
76
77 export const getSnapShotState = DesktopIpc.makeIpcMethod({
78 channel: IpcChannels.GET_SNAP_SHOT_STATE_CHANNEL,
79 payload: Schema.Void,
80 result: DesktopSnapShotState,
81 handler: Effect.fn("desktop.ipc.snapShot.getState")(function* () {
82 return yield* (yield* DesktopSnapShot.DesktopSnapShot).state;
83 }),
84 });
85
86 export const requestSnapShotPermissions = DesktopIpc.makeIpcMethod({
87 channel: IpcChannels.REQUEST_SNAP_SHOT_PERMISSIONS_CHANNEL,
88 payload: Schema.Boolean,
89 result: Schema.Void,
90 handler: Effect.fn("desktop.ipc.snapShot.requestPermissions")(
91 function* (includeAccessibility, event) {
92 yield* ensureTrustedSnapShotSender(event);
93 yield* (yield* DesktopSnapShot.DesktopSnapShot).requestPermissions(includeAccessibility);
94 },
95 ),
96 });
97
98 export const setupSnapShot = DesktopIpc.makeIpcMethod({
99 channel: IpcChannels.SETUP_SNAP_SHOT_CHANNEL,
100 payload: DesktopSnapShotSetupAction,
101 result: Schema.Void,
102 handler: Effect.fn("desktop.ipc.snapShot.setup")(function* (action, event) {
103 yield* ensureTrustedSnapShotSender(event);
104 yield* (yield* DesktopSnapShot.DesktopSnapShot).setup(action);
105 }),
106 });
107
108 export const previewSnapShotConfig = DesktopIpc.makeIpcMethod({
109 channel: IpcChannels.PREVIEW_SNAP_SHOT_CONFIG_CHANNEL,
110 payload: DesktopCaptureConfigRequest,
111 result: Schema.NullOr(DesktopCaptureConfigPreview),
112 handler: Effect.fn("desktop.ipc.snapShot.previewConfig")(function* (request, event) {
113 const window = yield* ensureTrustedSnapShotSender(event);
114 const capture = yield* DesktopSnapShot.DesktopSnapShot;
115 let selectedPath: string | undefined;
116 if (request.chooseFile) {
117 const state = yield* capture.state;
118 const paths = yield* (yield* ElectronDialog.ElectronDialog).pickFiles({
119 owner: Option.some(window),
120 defaultPath: Option.fromUndefinedOr(state.shortcutConfigPath),
121 filters: [
122 {
123 name: m(msg("Desktop config")),
124 extensions: state.linuxBackend === "niri" ? ["kdl"] : ["conf", "lua"],
125 },
126 ],
127 multiple: false,
128 });
129 selectedPath = paths[0];
130 if (!selectedPath) return null;
131 }
132 return yield* capture.previewConfig(request, selectedPath);
133 }),
134 });
135
136 export const applySnapShotConfig = DesktopIpc.makeIpcMethod({
137 channel: IpcChannels.APPLY_SNAP_SHOT_CONFIG_CHANNEL,
138 payload: DesktopSnapShotId,
139 result: DesktopCaptureConfigApplied,
140 handler: Effect.fn("desktop.ipc.snapShot.applyConfig")(function* (id, event) {
141 yield* ensureTrustedSnapShotSender(event);
142 return yield* (yield* DesktopSnapShot.DesktopSnapShot).applyConfig(id);
143 }),
144 });
145
146 export const checkSnapShotShortcut = DesktopIpc.makeIpcMethod({
147 channel: IpcChannels.CHECK_SNAP_SHOT_SHORTCUT_CHANNEL,
148 payload: SnapShotShortcut,
149 result: DesktopSnapShotShortcutAvailability,
150 handler: Effect.fn("desktop.ipc.snapShot.checkShortcut")(function* (shortcut, event) {
151 yield* ensureTrustedSnapShotSender(event);
152 return yield* (yield* DesktopSnapShot.DesktopSnapShot).checkShortcut(shortcut);
153 }),
154 });
155
156 export const setSnapShotShortcutSuppressed = DesktopIpc.makeIpcMethod({
157 channel: IpcChannels.SET_SNAP_SHOT_SHORTCUT_SUPPRESSED_CHANNEL,
158 payload: Schema.Boolean,
159 result: Schema.Void,
160 handler: Effect.fn("desktop.ipc.snapShot.setShortcutSuppressed")(function* (suppressed, event) {
161 yield* ensureTrustedSnapShotSender(event);
162 yield* (yield* DesktopSnapShot.DesktopSnapShot).setShortcutSuppressed(suppressed);
163 }),
164 });
165
166 export const listPendingSnapShots = DesktopIpc.makeIpcMethod({
167 channel: IpcChannels.LIST_PENDING_SNAP_SHOTS_CHANNEL,
168 payload: Schema.Void,
169 result: Schema.Array(DesktopPendingSnapShot),
170 handler: Effect.fn("desktop.ipc.snapShot.listPending")(function* (_, event) {
171 yield* ensureTrustedSnapShotSender(event);
172 return yield* (yield* DesktopSnapShot.DesktopSnapShot).listPending;
173 }),
174 });
175
176 export const readSnapShot = DesktopIpc.makeIpcMethod({
177 channel: IpcChannels.READ_SNAP_SHOT_CHANNEL,
178 payload: DesktopSnapShotId,
179 result: DesktopSnapShotSchema,
180 handler: Effect.fn("desktop.ipc.snapShot.read")(function* (id, event) {
181 yield* ensureTrustedSnapShotSender(event);
182 return yield* (yield* DesktopSnapShot.DesktopSnapShot).read(id);
183 }),
184 });
185
186 export const setSnapShotAnimationDestination = DesktopIpc.makeIpcMethod({
187 channel: IpcChannels.SET_SNAP_SHOT_ANIMATION_DESTINATION_CHANNEL,
188 payload: DesktopSnapShotAnimationDestination,
189 result: Schema.Void,
190 handler: Effect.fn("desktop.ipc.snapShot.setAnimationDestination")(
191 function* (destination, event) {
192 const window = yield* ensureTrustedSnapShotSender(event);
193 if (
194 destination.viewportFrame.width <= 0 ||
195 destination.viewportFrame.height <= 0 ||
196 destination.borderWidth < 0 ||
197 destination.cornerRadius < 0
198 ) {
199 return;
200 }
201 yield* (yield* DesktopSnapShot.DesktopSnapShot).setAnimationDestination(destination.id, {
202 relativeFrame: snapShotRelativeFrame(
203 destination.viewportFrame,
204 window.getContentBounds(),
205 window.webContents.getZoomFactor(),
206 ),
207 frame: snapShotScreenFrame(
208 destination.viewportFrame,
209 window.getContentBounds(),
210 window.webContents.getZoomFactor(),
211 ),
212 backgroundColor: destination.backgroundColor,
213 borderColor: destination.borderColor,
214 borderWidth: destination.borderWidth * window.webContents.getZoomFactor(),
215 cornerRadius: destination.cornerRadius * window.webContents.getZoomFactor(),
216 scaleFactor: window.webContents.getZoomFactor(),
217 details: destination.details,
218 });
219 },
220 ),
221 });
222
223 export const dismissSnapShotAnimation = DesktopIpc.makeIpcMethod({
224 channel: IpcChannels.DISMISS_SNAP_SHOT_ANIMATION_CHANNEL,
225 payload: DesktopSnapShotId,
226 result: Schema.Void,
227 handler: Effect.fn("desktop.ipc.snapShot.dismissAnimation")(function* (id, event) {
228 yield* ensureTrustedSnapShotSender(event);
229 yield* (yield* DesktopSnapShot.DesktopSnapShot).dismissAnimation(id);
230 }),
231 });
232
233 export const acknowledgeSnapShot = DesktopIpc.makeIpcMethod({
234 channel: IpcChannels.ACKNOWLEDGE_SNAP_SHOT_CHANNEL,
235 payload: DesktopSnapShotId,
236 result: Schema.Void,
237 handler: Effect.fn("desktop.ipc.snapShot.acknowledge")(function* (id, event) {
238 yield* ensureTrustedSnapShotSender(event);
239 yield* (yield* DesktopSnapShot.DesktopSnapShot).acknowledge(id);
240 }),
241 });