Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off -- This macOS platform boundary spawns the native modifier-key poller with Node.
2
3import * as NodeChildProcess from "node:child_process";
4
5import type { SnapShotModifier } from "@t3tools/contracts";
6
7const MAC_MODIFIER_PAIR_DEVICE_MASKS: Record<SnapShotModifier, readonly [number, number]> = {
8 shift: [0x2, 0x4],
9 control: [0x1, 0x2000],
10 alt: [0x20, 0x40],
11 meta: [0x8, 0x10],
12};
13
14// CoreGraphics registers osascript as a foreground app; go background-only first to avoid a Dock tile.
15// A key typed since the pair was last fully up means the overlap came from typing (rolled Shifts
16// for consecutive capitals, or a lost key-up), not a deliberate press, so it does not trigger.
17const POLLER_SCRIPT = `
18ObjC.import("AppKit");
19$.NSApplication.sharedApplication.setActivationPolicy($.NSApplicationActivationPolicyProhibited);
20ObjC.import("CoreGraphics");
21ObjC.import("unistd");
22function run(argv) {
23 const left = Number(argv[0]);
24 const right = Number(argv[1]);
25 const both = left | right;
26 const keyDowns = () => $.CGEventSourceCounterForEventType(0, $.kCGEventKeyDown);
27 let active = false;
28 let idleKeyDowns = keyDowns();
29 console.log("ready");
30 while ($.getppid() !== 1) {
31 const flags = $.CGEventSourceFlagsState(0);
32 const pressed = (flags & both) === both;
33 if (pressed && !active && keyDowns() === idleKeyDowns) console.log("trigger");
34 active = pressed;
35 if ((flags & both) === 0) idleKeyDowns = keyDowns();
36 delay(0.05);
37 }
38 return "orphaned";
39}`;
40
41export function startMacModifierPairShortcutProcess(
42 modifier: SnapShotModifier,
43 onTrigger: () => void,
44 onFailure: (error: Error) => void,
45): Promise<() => void> {
46 const [left, right] = MAC_MODIFIER_PAIR_DEVICE_MASKS[modifier];
47 const poller = NodeChildProcess.spawn(
48 "/usr/bin/osascript",
49 ["-l", "JavaScript", "-e", POLLER_SCRIPT, String(left), String(right)],
50 { stdio: ["ignore", "ignore", "pipe"] },
51 );
52
53 return new Promise((resolve, reject) => {
54 let settled = false;
55 let stopped = false;
56 let buffered = "";
57 const stop = () => {
58 if (stopped) return;
59 stopped = true;
60 poller.kill();
61 };
62 const fail = (error: Error) => {
63 if (stopped) return;
64 if (settled) {
65 stop();
66 onFailure(error);
67 return;
68 }
69 settled = true;
70 stop();
71 reject(error);
72 };
73
74 poller.stderr.on("data", (chunk: Buffer) => {
75 buffered += chunk.toString();
76 const lines = buffered.split("\n");
77 buffered = lines.pop() ?? "";
78 for (const line of lines) {
79 const message = line.trim();
80 if (message === "ready" && !settled) {
81 settled = true;
82 resolve(stop);
83 continue;
84 }
85 if (message !== "trigger" || !settled || stopped) continue;
86 try {
87 onTrigger();
88 } catch {}
89 }
90 });
91 poller.once("error", (error) => {
92 fail(error);
93 });
94 poller.once("exit", (code) => {
95 fail(new Error(`Snapshot shortcut helper exited with code ${code}`));
96 });
97 });
98}