@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 112 lines 3.81 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off -- This native boundary keeps one JXA process open while the permission helper tracks System Settings.
2 import * as NodeChildProcess from "node:child_process";
3 import * as Schema from "effect/Schema";
4 import type * as Electron from "electron";
5
6 const SettingsWindow = Schema.NullOr(
7 Schema.Struct({
8 x: Schema.Finite,
9 y: Schema.Finite,
10 width: Schema.Finite,
11 height: Schema.Finite,
12 frontmost: Schema.Boolean,
13 }),
14 );
15 export type SettingsWindow = typeof SettingsWindow.Type;
16 const decodeSettingsWindow = Schema.decodeUnknownSync(Schema.fromJsonString(SettingsWindow));
17
18 // Window bounds/PIDs need no Screen Recording grant. Match the bundle id to avoid localized titles.
19 // Keep one osascript process while Settings moves.
20 const SETTINGS_WINDOW_SCRIPT = `
21 ObjC.import("CoreGraphics");
22 ObjC.import("AppKit");
23 ObjC.import("unistd");
24 function run() {
25 let previous = "";
26 while ($.getppid() !== 1) {
27 const apps = $.NSRunningApplication.runningApplicationsWithBundleIdentifier("com.apple.systempreferences");
28 let result = null;
29 if (apps.count > 0) {
30 const pid = apps.objectAtIndex(0).processIdentifier;
31 const front = $.NSWorkspace.sharedWorkspace.frontmostApplication;
32 const list = $.CGWindowListCopyWindowInfo(
33 $.kCGWindowListOptionOnScreenOnly | $.kCGWindowListExcludeDesktopElements,
34 $.kCGNullWindowID
35 );
36 if (list) {
37 $.CFMakeCollectable(list);
38 const count = $.CFArrayGetCount(list);
39 for (let i = 0; i < count; i++) {
40 const w = ObjC.castRefToObject($.CFArrayGetValueAtIndex(list, i));
41 if (w.objectForKey("kCGWindowOwnerPID").js !== pid || w.objectForKey("kCGWindowLayer").js !== 0) continue;
42 const b = ObjC.deepUnwrap(w.objectForKey("kCGWindowBounds"));
43 if (b.Width < 500 || b.Height < 350) continue;
44 result = { x: b.X, y: b.Y, width: b.Width, height: b.Height, frontmost: !front.isNil() && front.processIdentifier === pid };
45 break;
46 }
47 }
48 }
49 const line = JSON.stringify(result);
50 if (line !== previous) {
51 const data = $(line + "\\n").dataUsingEncoding($.NSUTF8StringEncoding);
52 $.NSFileHandle.fileHandleWithStandardOutput.writeData(data);
53 previous = line;
54 }
55 $.NSThread.sleepForTimeInterval(result && result.frontmost ? 0.5 : 1);
56 }
57 }`;
58
59 export function settingsHelperBounds(settings: NonNullable<SettingsWindow>): Electron.Rectangle {
60 const sidebarWidth = 216;
61 const inset = 16;
62 const width = Math.min(560, settings.width - sidebarWidth - inset * 2);
63 return {
64 x: Math.round(settings.x + sidebarWidth + (settings.width - sidebarWidth - width) / 2),
65 y: Math.round(settings.y + settings.height - 140 - inset),
66 width: Math.round(width),
67 height: 140,
68 };
69 }
70
71 export function watchMacSettingsWindow(
72 onChange: (window: SettingsWindow) => void,
73 onUnavailable: () => void,
74 ): () => void {
75 const child = NodeChildProcess.spawn(
76 "/usr/bin/osascript",
77 ["-l", "JavaScript", "-e", SETTINGS_WINDOW_SCRIPT],
78 {
79 stdio: ["ignore", "pipe", "ignore"],
80 },
81 );
82 let pending = "";
83 let closed = false;
84 child.stdout.setEncoding("utf8");
85 child.stdout.on("data", (chunk: string) => {
86 pending += chunk;
87 let end: number;
88 while ((end = pending.indexOf("\n")) !== -1) {
89 const line = pending.slice(0, end);
90 pending = pending.slice(end + 1);
91 if (closed) return;
92 let settings: SettingsWindow;
93 try {
94 settings = decodeSettingsWindow(line);
95 } catch {
96 onUnavailable();
97 continue;
98 }
99 onChange(settings);
100 }
101 });
102 const onExit = () => {
103 if (!closed) onUnavailable();
104 };
105 child.on("error", onExit);
106 child.on("exit", onExit);
107 return () => {
108 closed = true;
109 child.stdout.removeAllListeners("data");
110 child.kill();
111 };
112 }