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