The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics globalTimers:off -- Poll TCC only while the native permission helper is open. |
| 2 | import * as Electron from "electron"; |
| 3 | import { msg } from "gt-i18n"; |
| 4 | |
| 5 | import { m } from "../i18n/messages.ts"; |
| 6 | import { MAC_PERMISSION_HELPER_CHANNEL } from "../ipc/channels.ts"; |
| 7 | |
| 8 | import { |
| 9 | settingsHelperBounds, |
| 10 | watchMacSettingsWindow, |
| 11 | type SettingsWindow, |
| 12 | } from "./MacSettingsWindow.ts"; |
| 13 | |
| 14 | import { MAC_PERMISSION_SETUP_TITLES, type MacPermission } from "./MacPermission.ts"; |
| 15 | |
| 16 | const permissionGranted = (permission: MacPermission) => { |
| 17 | if (permission === "screen-recording") |
| 18 | return Electron.systemPreferences.getMediaAccessStatus("screen") === "granted"; |
| 19 | if (permission === "accessibility") |
| 20 | return Electron.systemPreferences.isTrustedAccessibilityClient(false); |
| 21 | return false; |
| 22 | }; |
| 23 | |
| 24 | function macAppBundlePath(executable: string): string | undefined { |
| 25 | return /^(.+\.app)\/Contents\/MacOS\/[^/]+$/.exec(executable)?.[1]; |
| 26 | } |
| 27 | |
| 28 | const escapeHtml = (value: string) => |
| 29 | value.replace(/[&<>"']/g, (character) => { |
| 30 | switch (character) { |
| 31 | case "&": |
| 32 | return "&"; |
| 33 | case "<": |
| 34 | return "<"; |
| 35 | case ">": |
| 36 | return ">"; |
| 37 | case '"': |
| 38 | return """; |
| 39 | default: |
| 40 | return "'"; |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | function helperHtml(permission: MacPermission, icon: string) { |
| 45 | const title = escapeHtml(m(MAC_PERMISSION_SETUP_TITLES[permission])); |
| 46 | return `<!doctype html><html lang="en"><head><meta charset="utf-8"> |
| 47 | <meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src data:; style-src 'unsafe-inline'"> |
| 48 | <title>${title}</title><style> |
| 49 | :root { color-scheme: light dark; --base: #fff; --row: #e7e7e7; --text: #292929; --line: #e3e3e3; } |
| 50 | @media (prefers-color-scheme: dark) { :root { --base: #242424; --row: #383838; --text: #f5f5f5; --line: #484848; } } |
| 51 | * { box-sizing: border-box; } |
| 52 | html { background: transparent; } |
| 53 | body { margin: 0; background: transparent; color: var(--text); font: 15px/22px -apple-system, BlinkMacSystemFont, sans-serif; user-select: none; } |
| 54 | #panel { position: relative; margin: 2px; padding: 20px; height: 136px; border: 1px solid var(--line); border-radius: 24px; background: var(--base); } |
| 55 | header { font-weight: 600; white-space: nowrap; } |
| 56 | button { font: inherit; color: inherit; } |
| 57 | button:focus-visible { outline: 2px solid #007aff; outline-offset: -2px; } |
| 58 | #close { position: absolute; right: 8px; top: 6px; width: 22px; height: 22px; padding: 0; border: 0; border-radius: 50%; background: var(--base); font-size: 18px; cursor: pointer; opacity: 0; } |
| 59 | #panel:hover #close, #close:focus-visible { opacity: 1; } |
| 60 | #app { display: flex; align-items: center; gap: 12px; width: 100%; height: 52px; margin-top: 20px; padding: 8px 12px; border: 0; border-radius: 10px; background: var(--row); cursor: grab; text-align: left; font-size: 16px; font-weight: 600; } |
| 61 | #app:active { cursor: grabbing; } |
| 62 | img { width: 32px; height: 32px; pointer-events: none; } |
| 63 | </style></head><body><main id="panel"> |
| 64 | <button id="close" aria-label="${escapeHtml(m(msg("Close permission helper")))}">×</button> |
| 65 | <header>${escapeHtml(m(msg("↑ Drag T3 Code into the list above")))}</header> |
| 66 | <button id="app" draggable="true" aria-label="${escapeHtml(m(msg("Drag T3 Code to System Settings, or click to reveal in Finder")))}"><img src="${escapeHtml(icon)}" alt="" draggable="false">T3 Code</button> |
| 67 | </main></body></html>`; |
| 68 | } |
| 69 | |
| 70 | export class MacPermissionHelper { |
| 71 | private generation = 0; |
| 72 | private window: Electron.BrowserWindow | undefined; |
| 73 | |
| 74 | close() { |
| 75 | this.generation++; |
| 76 | this.window?.destroy(); |
| 77 | this.window = undefined; |
| 78 | } |
| 79 | |
| 80 | async show( |
| 81 | permission: MacPermission, |
| 82 | preload: string, |
| 83 | owner: Electron.BrowserWindow | null, |
| 84 | iconPaths: readonly string[], |
| 85 | isGranted: () => boolean | Promise<boolean> = () => permissionGranted(permission), |
| 86 | ) { |
| 87 | this.close(); |
| 88 | const generation = this.generation; |
| 89 | if (await isGranted()) return; |
| 90 | if (generation !== this.generation) return; |
| 91 | const bundle = macAppBundlePath(Electron.app.getPath("exe")); |
| 92 | if (!bundle) return; |
| 93 | if (owner?.isDestroyed()) return; |
| 94 | // Finder can return generic icons for mounted artifacts; use the packaging PNG. |
| 95 | const appIcon = iconPaths |
| 96 | .map((iconPath) => Electron.nativeImage.createFromPath(iconPath)) |
| 97 | .find((image) => !image.isEmpty()); |
| 98 | if (!appIcon) throw new Error("The packaged T3 Code icon is missing."); |
| 99 | const icon = appIcon.resize({ width: 64, height: 64 }); |
| 100 | const window = new Electron.BrowserWindow({ |
| 101 | width: 560, |
| 102 | height: 140, |
| 103 | show: false, |
| 104 | frame: false, |
| 105 | transparent: true, |
| 106 | roundedCorners: false, |
| 107 | backgroundColor: "#00000000", |
| 108 | hasShadow: false, |
| 109 | resizable: false, |
| 110 | minimizable: false, |
| 111 | maximizable: false, |
| 112 | fullscreenable: false, |
| 113 | alwaysOnTop: true, |
| 114 | skipTaskbar: true, |
| 115 | title: m(MAC_PERMISSION_SETUP_TITLES[permission]), |
| 116 | webPreferences: { preload, sandbox: true, contextIsolation: true, nodeIntegration: false }, |
| 117 | }); |
| 118 | this.window = window; |
| 119 | const finish = () => { |
| 120 | window.close(); |
| 121 | if (owner && !owner.isDestroyed()) { |
| 122 | owner.show(); |
| 123 | owner.focus(); |
| 124 | } |
| 125 | }; |
| 126 | let checking = false; |
| 127 | const check = async () => { |
| 128 | if (checking || window.isDestroyed()) return; |
| 129 | checking = true; |
| 130 | try { |
| 131 | if ((await isGranted()) && !window.isDestroyed()) finish(); |
| 132 | } catch { |
| 133 | } finally { |
| 134 | checking = false; |
| 135 | } |
| 136 | }; |
| 137 | const onMessage = (event: Electron.IpcMainEvent, action: unknown) => { |
| 138 | if (event.sender !== window.webContents || event.senderFrame !== window.webContents.mainFrame) |
| 139 | return; |
| 140 | if (action === "drag") { |
| 141 | try { |
| 142 | window.webContents.startDrag({ file: bundle, icon }); |
| 143 | } catch { |
| 144 | Electron.shell.showItemInFolder(bundle); |
| 145 | } |
| 146 | } else if (action === "finder") { |
| 147 | Electron.shell.showItemInFolder(bundle); |
| 148 | } else if (action === "close") { |
| 149 | finish(); |
| 150 | } |
| 151 | }; |
| 152 | let settingsWindow: SettingsWindow = null; |
| 153 | let foundSettings = false; |
| 154 | let trackingAvailable = true; |
| 155 | const syncPosition = () => { |
| 156 | if (window.isDestroyed()) return; |
| 157 | if (!trackingAvailable) { |
| 158 | window.hide(); |
| 159 | return; |
| 160 | } |
| 161 | if (!settingsWindow && foundSettings) { |
| 162 | finish(); |
| 163 | return; |
| 164 | } |
| 165 | if (!settingsWindow || (!settingsWindow.frontmost && !window.isFocused())) { |
| 166 | window.hide(); |
| 167 | return; |
| 168 | } |
| 169 | const bounds = settingsHelperBounds(settingsWindow); |
| 170 | const current = window.getBounds(); |
| 171 | if ( |
| 172 | current.x !== bounds.x || |
| 173 | current.y !== bounds.y || |
| 174 | current.width !== bounds.width || |
| 175 | current.height !== bounds.height |
| 176 | ) { |
| 177 | window.setBounds(bounds, false); |
| 178 | } |
| 179 | if (!window.isVisible()) window.showInactive(); |
| 180 | }; |
| 181 | let stopTracking = () => {}; |
| 182 | window.on("blur", syncPosition); |
| 183 | const onOwnerClosed = () => window.destroy(); |
| 184 | Electron.ipcMain.on(MAC_PERMISSION_HELPER_CHANNEL, onMessage); |
| 185 | const timer = setInterval(check, 1_000); |
| 186 | owner?.once("closed", onOwnerClosed); |
| 187 | window.once("closed", () => { |
| 188 | clearInterval(timer); |
| 189 | stopTracking(); |
| 190 | Electron.ipcMain.removeListener(MAC_PERMISSION_HELPER_CHANNEL, onMessage); |
| 191 | owner?.removeListener("closed", onOwnerClosed); |
| 192 | if (this.window === window) this.window = undefined; |
| 193 | }); |
| 194 | window.webContents.setWindowOpenHandler(() => ({ action: "deny" })); |
| 195 | window.webContents.on("will-navigate", (event) => event.preventDefault()); |
| 196 | try { |
| 197 | await window.loadURL( |
| 198 | `data:text/html;charset=utf-8,${encodeURIComponent(helperHtml(permission, icon.toDataURL()))}`, |
| 199 | ); |
| 200 | if (!window.isDestroyed()) { |
| 201 | stopTracking = watchMacSettingsWindow( |
| 202 | (current) => { |
| 203 | trackingAvailable = true; |
| 204 | settingsWindow = current; |
| 205 | if (current) foundSettings = true; |
| 206 | syncPosition(); |
| 207 | }, |
| 208 | () => { |
| 209 | trackingAvailable = false; |
| 210 | syncPosition(); |
| 211 | }, |
| 212 | ); |
| 213 | } |
| 214 | } catch (error) { |
| 215 | if (!window.isDestroyed()) window.destroy(); |
| 216 | throw error; |
| 217 | } |
| 218 | } |
| 219 | } |