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
| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | |
| 7 | import type { DesktopUpdateChannel } from "@t3tools/contracts"; |
| 8 | import * as Electron from "electron"; |
| 9 | import { msg } from "gt-i18n"; |
| 10 | |
| 11 | import { makeComponentLogger } from "../app/DesktopObservability.ts"; |
| 12 | import * as ElectronApp from "../electron/ElectronApp.ts"; |
| 13 | import * as ElectronDialog from "../electron/ElectronDialog.ts"; |
| 14 | import * as ElectronMenu from "../electron/ElectronMenu.ts"; |
| 15 | import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; |
| 16 | import { applyLanguage, m, onLocaleChange } from "../i18n/messages.ts"; |
| 17 | import * as DesktopClientSettings from "../settings/DesktopClientSettings.ts"; |
| 18 | import * as DesktopUpdates from "../updates/DesktopUpdates.ts"; |
| 19 | import * as DesktopWindow from "./DesktopWindow.ts"; |
| 20 | |
| 21 | export class DesktopApplicationMenuActionError extends Schema.TaggedError<DesktopApplicationMenuActionError>()( |
| 22 | "DesktopApplicationMenuActionError", |
| 23 | { |
| 24 | action: Schema.String, |
| 25 | cause: Schema.Defect(), |
| 26 | }, |
| 27 | ) { |
| 28 | override get message(): string { |
| 29 | return `Desktop menu action "${this.action}" failed.`; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | export class DesktopApplicationMenu extends Context.Service< |
| 34 | DesktopApplicationMenu, |
| 35 | { |
| 36 | readonly configure: Effect.Effect<void>; |
| 37 | } |
| 38 | >()("@t3tools/desktop/window/DesktopApplicationMenu") {} |
| 39 | |
| 40 | type DesktopApplicationMenuRuntimeServices = |
| 41 | | DesktopUpdates.DesktopUpdates |
| 42 | | DesktopWindow.DesktopWindow |
| 43 | | ElectronDialog.ElectronDialog; |
| 44 | |
| 45 | const { logInfo: logUpdaterInfo } = makeComponentLogger("desktop-updater"); |
| 46 | |
| 47 | const { logError: logMenuError } = makeComponentLogger("desktop-menu"); |
| 48 | |
| 49 | const dispatchMenuAction = Effect.fn("desktop.menu.dispatchMenuAction")(function* ( |
| 50 | action: string, |
| 51 | ): Effect.fn.Return<void, DesktopWindow.DesktopWindowError, DesktopWindow.DesktopWindow> { |
| 52 | const desktopWindow = yield* DesktopWindow.DesktopWindow; |
| 53 | yield* desktopWindow.dispatchMenuAction(action, { |
| 54 | reveal: action !== "paste-as-text", |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | const zoomMainWindow = Effect.fn("desktop.menu.zoomMainWindow")(function* ( |
| 59 | direction: DesktopWindow.MainWindowZoomDirection, |
| 60 | ): Effect.fn.Return<void, never, DesktopWindow.DesktopWindow> { |
| 61 | const desktopWindow = yield* DesktopWindow.DesktopWindow; |
| 62 | yield* desktopWindow.zoomMain(direction); |
| 63 | }); |
| 64 | |
| 65 | const runMainContentsCommand = Effect.fn("desktop.menu.runMainContentsCommand")(function* ( |
| 66 | command: DesktopWindow.MainWindowContentsCommand, |
| 67 | ): Effect.fn.Return<void, never, DesktopWindow.DesktopWindow> { |
| 68 | const desktopWindow = yield* DesktopWindow.DesktopWindow; |
| 69 | yield* desktopWindow.runMainContentsCommand(command); |
| 70 | }); |
| 71 | |
| 72 | const trackLabel = (channel: DesktopUpdateChannel) => |
| 73 | channel === "nightly" |
| 74 | ? m(msg("Nightly", { $context: "release track" })) |
| 75 | : m(msg("Stable", { $context: "release track" })); |
| 76 | |
| 77 | const checkForUpdatesFromMenu = Effect.gen(function* () { |
| 78 | const updates = yield* DesktopUpdates.DesktopUpdates; |
| 79 | const electronDialog = yield* ElectronDialog.ElectronDialog; |
| 80 | const result = yield* updates.check("menu"); |
| 81 | const updateState = result.state; |
| 82 | |
| 83 | if (updateState.status === "up-to-date") { |
| 84 | yield* electronDialog.showMessageBox({ |
| 85 | type: "info", |
| 86 | title: m(msg("You're up to date!")), |
| 87 | message: m(msg("T3 Code {version} is currently the newest version available."), { |
| 88 | version: updateState.currentVersion, |
| 89 | }), |
| 90 | buttons: [m(msg("OK"))], |
| 91 | }); |
| 92 | } else if (updateState.status === "available") { |
| 93 | const { response } = yield* electronDialog.showMessageBox({ |
| 94 | type: "info", |
| 95 | title: m(msg("Update available")), |
| 96 | message: |
| 97 | updateState.availableVersion === null |
| 98 | ? m(msg("A new version of T3 Code is available.")) |
| 99 | : m(msg("T3 Code {version} is available."), { version: updateState.availableVersion }), |
| 100 | detail: m(msg("You're running T3 Code {version}."), { |
| 101 | version: updateState.currentVersion, |
| 102 | }), |
| 103 | buttons: [ |
| 104 | m(msg("Download", { $context: "button, downloads an app update" })), |
| 105 | m(msg("Later")), |
| 106 | ], |
| 107 | defaultId: 0, |
| 108 | cancelId: 1, |
| 109 | }); |
| 110 | if (response === 0) yield* updates.download; |
| 111 | } else if (updateState.status === "downloading") { |
| 112 | yield* electronDialog.showMessageBox({ |
| 113 | type: "info", |
| 114 | title: m(msg("Update downloading")), |
| 115 | message: |
| 116 | updateState.availableVersion === null |
| 117 | ? m(msg("A new version of T3 Code is already downloading.")) |
| 118 | : m(msg("T3 Code {version} is already downloading."), { |
| 119 | version: updateState.availableVersion, |
| 120 | }), |
| 121 | buttons: [m(msg("OK"))], |
| 122 | }); |
| 123 | } else if (updateState.status === "downloaded") { |
| 124 | const version = updateState.downloadedVersion ?? updateState.availableVersion; |
| 125 | const { response } = yield* electronDialog.showMessageBox({ |
| 126 | type: "info", |
| 127 | title: m(msg("Update ready")), |
| 128 | message: |
| 129 | version === null |
| 130 | ? m(msg("A new version of T3 Code is ready to install.")) |
| 131 | : m(msg("T3 Code {version} is ready to install."), { version }), |
| 132 | detail: m(msg("T3 Code will restart to install it. Any running tasks will be interrupted.")), |
| 133 | buttons: [m(msg("Restart", { $context: "button, restarts the app" })), m(msg("Later"))], |
| 134 | defaultId: 0, |
| 135 | cancelId: 1, |
| 136 | }); |
| 137 | if (response === 0) yield* updates.install; |
| 138 | } else if (updateState.status === "error") { |
| 139 | yield* electronDialog.showMessageBox({ |
| 140 | type: "warning", |
| 141 | title: m(msg("Update check failed")), |
| 142 | message: m(msg("Could not check for updates.")), |
| 143 | detail: |
| 144 | updateState.trackMismatch !== undefined |
| 145 | ? m( |
| 146 | msg( |
| 147 | "This {build} build can't update from the {saved} track on its own. Switch the update track back to {build}, or use Review beside Update track to retry the switch to {saved}.", |
| 148 | ), |
| 149 | { |
| 150 | build: trackLabel(updateState.trackMismatch.buildChannel), |
| 151 | saved: trackLabel(updateState.channel), |
| 152 | }, |
| 153 | ) |
| 154 | : (updateState.message ?? m(msg("An unknown error occurred. Please try again later."))), |
| 155 | buttons: [m(msg("OK"))], |
| 156 | }); |
| 157 | } |
| 158 | }).pipe(Effect.withSpan("desktop.menu.checkForUpdates")); |
| 159 | |
| 160 | const handleCheckForUpdatesMenuClick = Effect.gen(function* () { |
| 161 | const updates = yield* DesktopUpdates.DesktopUpdates; |
| 162 | const electronDialog = yield* ElectronDialog.ElectronDialog; |
| 163 | const disabledReason = yield* updates.disabledReason; |
| 164 | if (Option.isSome(disabledReason)) { |
| 165 | yield* logUpdaterInfo("manual update check requested, but updates are disabled", { |
| 166 | disabledReason: disabledReason.value, |
| 167 | }); |
| 168 | yield* electronDialog.showMessageBox({ |
| 169 | type: "info", |
| 170 | title: m(msg("Updates unavailable")), |
| 171 | message: m(msg("Automatic updates are not available right now.")), |
| 172 | detail: disabledReason.value, |
| 173 | buttons: [m(msg("OK"))], |
| 174 | }); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // Opening a window before backend confirmation can strand it on failed auth bootstrap. |
| 179 | const desktopWindow = yield* DesktopWindow.DesktopWindow; |
| 180 | yield* desktopWindow.activate; |
| 181 | yield* checkForUpdatesFromMenu; |
| 182 | }).pipe(Effect.withSpan("desktop.menu.handleCheckForUpdatesClick")); |
| 183 | |
| 184 | /** @public */ |
| 185 | export const make = Effect.gen(function* () { |
| 186 | const electronMenu = yield* ElectronMenu.ElectronMenu; |
| 187 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 188 | const electronApp = yield* ElectronApp.ElectronApp; |
| 189 | const clientSettings = yield* Effect.serviceOption(DesktopClientSettings.DesktopClientSettings); |
| 190 | const context = yield* Effect.context<DesktopApplicationMenuRuntimeServices>(); |
| 191 | const runPromise = Effect.runPromiseWith(context); |
| 192 | const runFork = Effect.runForkWith(context); |
| 193 | |
| 194 | const runMenuEffect = <E>( |
| 195 | action: string, |
| 196 | effect: Effect.Effect<void, E, DesktopApplicationMenuRuntimeServices>, |
| 197 | ) => { |
| 198 | void runPromise( |
| 199 | effect.pipe( |
| 200 | Effect.annotateLogs({ action }), |
| 201 | Effect.withSpan("desktop.menu.action"), |
| 202 | Effect.catchCause((cause) => { |
| 203 | const error = new DesktopApplicationMenuActionError({ action, cause }); |
| 204 | return logMenuError(error.message, { error }); |
| 205 | }), |
| 206 | ), |
| 207 | ); |
| 208 | }; |
| 209 | |
| 210 | const configure = Effect.gen(function* () { |
| 211 | const checkForUpdatesClick = () => { |
| 212 | runMenuEffect("check-for-updates", handleCheckForUpdatesMenuClick); |
| 213 | }; |
| 214 | const settingsClick = () => { |
| 215 | runMenuEffect("open-settings", dispatchMenuAction("open-settings")); |
| 216 | }; |
| 217 | // Chromium and the renderer already handle this chord; injecting another paste duplicates text. |
| 218 | const pasteAsTextClick = ( |
| 219 | _item: Electron.MenuItem, |
| 220 | _window: Electron.BaseWindow | undefined, |
| 221 | event: Electron.KeyboardEvent, |
| 222 | ) => { |
| 223 | if (event.triggeredByAccelerator === true) return; |
| 224 | runMenuEffect("paste-as-text", dispatchMenuAction("paste-as-text")); |
| 225 | }; |
| 226 | const zoomClick = (direction: DesktopWindow.MainWindowZoomDirection) => () => { |
| 227 | runMenuEffect(`zoom-${direction}`, zoomMainWindow(direction)); |
| 228 | }; |
| 229 | const mainContentsClick = (command: DesktopWindow.MainWindowContentsCommand) => () => { |
| 230 | runMenuEffect(command, runMainContentsCommand(command)); |
| 231 | }; |
| 232 | const template: Electron.MenuItemConstructorOptions[] = []; |
| 233 | |
| 234 | if (environment.platform === "darwin") { |
| 235 | template.push({ |
| 236 | label: environment.displayName, |
| 237 | submenu: [ |
| 238 | { role: "about", label: m(msg("About {appName}"), { appName: environment.displayName }) }, |
| 239 | { |
| 240 | label: m(msg("Check for Updates...")), |
| 241 | click: checkForUpdatesClick, |
| 242 | }, |
| 243 | { type: "separator" }, |
| 244 | { |
| 245 | label: m(msg("Settings...")), |
| 246 | accelerator: "CmdOrCtrl+,", |
| 247 | click: settingsClick, |
| 248 | }, |
| 249 | { type: "separator" }, |
| 250 | { role: "services", label: m(msg("Services", { $context: "macOS app menu item" })) }, |
| 251 | { type: "separator" }, |
| 252 | { role: "hide", label: m(msg("Hide {appName}"), { appName: environment.displayName }) }, |
| 253 | { role: "hideOthers", label: m(msg("Hide Others")) }, |
| 254 | { |
| 255 | role: "unhide", |
| 256 | label: m(msg("Show All", { $context: "macOS app menu item, shows hidden apps" })), |
| 257 | }, |
| 258 | { type: "separator" }, |
| 259 | { role: "quit", label: m(msg("Quit {appName}"), { appName: environment.displayName }) }, |
| 260 | ], |
| 261 | }); |
| 262 | } |
| 263 | |
| 264 | template.push( |
| 265 | { |
| 266 | label: m(msg("File", { $context: "menu bar title" })), |
| 267 | submenu: [ |
| 268 | ...(environment.platform === "darwin" |
| 269 | ? [] |
| 270 | : [ |
| 271 | { |
| 272 | label: m(msg("Settings...")), |
| 273 | accelerator: "CmdOrCtrl+,", |
| 274 | click: settingsClick, |
| 275 | }, |
| 276 | { type: "separator" as const }, |
| 277 | ]), |
| 278 | { |
| 279 | label: m(msg("Close Developer Tools")), |
| 280 | accelerator: "CmdOrCtrl+W", |
| 281 | visible: false, |
| 282 | click: () => { |
| 283 | Electron.webContents |
| 284 | .getAllWebContents() |
| 285 | .find((contents) => !contents.isDestroyed() && contents.isDevToolsFocused()) |
| 286 | ?.closeDevTools(); |
| 287 | }, |
| 288 | }, |
| 289 | // Cmd/Ctrl+W belongs to the renderer's tab close; it must never close the window. |
| 290 | environment.platform === "darwin" |
| 291 | ? { label: m(msg("Close Window")), click: (_item, window) => window?.close() } |
| 292 | : { |
| 293 | role: "quit", |
| 294 | label: |
| 295 | environment.platform === "win32" |
| 296 | ? m(msg("Exit", { $context: "File menu item, quits the app" })) |
| 297 | : m(msg("Quit")), |
| 298 | }, |
| 299 | ], |
| 300 | }, |
| 301 | { |
| 302 | label: m(msg("Edit", { $context: "menu bar title" })), |
| 303 | submenu: [ |
| 304 | { role: "undo", label: m(msg("Undo", { $context: "Edit menu item" })) }, |
| 305 | { role: "redo", label: m(msg("Redo", { $context: "Edit menu item" })) }, |
| 306 | { type: "separator" }, |
| 307 | { role: "cut", label: m(msg("Cut", { $context: "Edit menu item" })) }, |
| 308 | { role: "copy", label: m(msg("Copy", { $context: "Edit menu item" })) }, |
| 309 | { role: "paste", label: m(msg("Paste", { $context: "Edit menu item" })) }, |
| 310 | { |
| 311 | label: m(msg("Paste as Text")), |
| 312 | accelerator: "CmdOrCtrl+Shift+V", |
| 313 | click: pasteAsTextClick, |
| 314 | }, |
| 315 | { role: "delete", label: m(msg("Delete", { $context: "Edit menu item" })) }, |
| 316 | { type: "separator" }, |
| 317 | { role: "selectAll", label: m(msg("Select All", { $context: "Edit menu item" })) }, |
| 318 | ...(environment.platform === "darwin" |
| 319 | ? [ |
| 320 | { type: "separator" as const }, |
| 321 | { |
| 322 | label: m(msg("Speech", { $context: "menu, text-to-speech" })), |
| 323 | submenu: [ |
| 324 | { role: "startSpeaking" as const, label: m(msg("Start Speaking")) }, |
| 325 | { role: "stopSpeaking" as const, label: m(msg("Stop Speaking")) }, |
| 326 | ], |
| 327 | }, |
| 328 | ] |
| 329 | : []), |
| 330 | ], |
| 331 | }, |
| 332 | { |
| 333 | label: m(msg("View", { $context: "menu bar title" })), |
| 334 | submenu: [ |
| 335 | /* Native reload/DevTools/zoom roles target focused guests; these commands must target the app. */ |
| 336 | { |
| 337 | label: m(msg("Reload")), |
| 338 | accelerator: "CmdOrCtrl+R", |
| 339 | click: mainContentsClick("reload"), |
| 340 | }, |
| 341 | { |
| 342 | label: m(msg("Force Reload")), |
| 343 | accelerator: "Shift+CmdOrCtrl+R", |
| 344 | click: mainContentsClick("forceReload"), |
| 345 | }, |
| 346 | { |
| 347 | label: m(msg("Toggle Developer Tools")), |
| 348 | accelerator: environment.platform === "darwin" ? "Alt+Command+I" : "Ctrl+Shift+I", |
| 349 | click: mainContentsClick("toggleDevTools"), |
| 350 | }, |
| 351 | { type: "separator" }, |
| 352 | { label: m(msg("Actual Size")), accelerator: "CmdOrCtrl+0", click: zoomClick("reset") }, |
| 353 | { label: m(msg("Zoom In")), accelerator: "CmdOrCtrl+=", click: zoomClick("in") }, |
| 354 | { |
| 355 | label: m(msg("Zoom In")), |
| 356 | accelerator: "CmdOrCtrl+Plus", |
| 357 | visible: false, |
| 358 | click: zoomClick("in"), |
| 359 | }, |
| 360 | ...(environment.platform === "darwin" |
| 361 | ? [ |
| 362 | { |
| 363 | label: m(msg("Zoom In")), |
| 364 | accelerator: "CmdOrCtrl+Shift+=", |
| 365 | visible: false, |
| 366 | click: zoomClick("in"), |
| 367 | }, |
| 368 | ] |
| 369 | : []), |
| 370 | { label: m(msg("Zoom Out")), accelerator: "CmdOrCtrl+-", click: zoomClick("out") }, |
| 371 | // Keypad codes do not match number-row accelerators. |
| 372 | { |
| 373 | label: m(msg("Actual Size")), |
| 374 | accelerator: "CmdOrCtrl+num0", |
| 375 | visible: false, |
| 376 | click: zoomClick("reset"), |
| 377 | }, |
| 378 | { |
| 379 | label: m(msg("Zoom In")), |
| 380 | accelerator: "CmdOrCtrl+numadd", |
| 381 | visible: false, |
| 382 | click: zoomClick("in"), |
| 383 | }, |
| 384 | { |
| 385 | label: m(msg("Zoom Out")), |
| 386 | accelerator: "CmdOrCtrl+numsub", |
| 387 | visible: false, |
| 388 | click: zoomClick("out"), |
| 389 | }, |
| 390 | { type: "separator" }, |
| 391 | { role: "togglefullscreen", label: m(msg("Toggle Full Screen")) }, |
| 392 | ], |
| 393 | }, |
| 394 | { |
| 395 | // Electron's default Window submenu adds a Ctrl+W close role off macOS. |
| 396 | role: "windowMenu", |
| 397 | label: m(msg("Window", { $context: "menu bar title" })), |
| 398 | submenu: [ |
| 399 | { role: "minimize", label: m(msg("Minimize", { $context: "Window menu item" })) }, |
| 400 | { |
| 401 | role: "zoom", |
| 402 | label: m(msg("Zoom", { $context: "Window menu item, resizes the window" })), |
| 403 | }, |
| 404 | ...(environment.platform === "darwin" |
| 405 | ? [ |
| 406 | { type: "separator" as const }, |
| 407 | { role: "front" as const, label: m(msg("Bring All to Front")) }, |
| 408 | ] |
| 409 | : []), |
| 410 | ], |
| 411 | }, |
| 412 | { |
| 413 | role: "help", |
| 414 | label: m(msg("Help", { $context: "menu bar title" })), |
| 415 | submenu: [ |
| 416 | { |
| 417 | label: m(msg("Check for Updates...")), |
| 418 | click: checkForUpdatesClick, |
| 419 | }, |
| 420 | ], |
| 421 | }, |
| 422 | ); |
| 423 | |
| 424 | yield* electronMenu.setApplicationMenu(template); |
| 425 | }).pipe(Effect.withSpan("desktop.menu.configure")); |
| 426 | |
| 427 | // Load the saved language before the first build so the menu never starts in English. |
| 428 | const applySavedLanguage = Effect.gen(function* () { |
| 429 | if (Option.isNone(clientSettings)) return; |
| 430 | const saved = yield* clientSettings.value.get.pipe(Effect.orElseSucceed(() => Option.none())); |
| 431 | if (Option.isSome(saved)) { |
| 432 | yield* applyLanguage(saved.value.language).pipe( |
| 433 | Effect.provideService(ElectronApp.ElectronApp, electronApp), |
| 434 | ); |
| 435 | } |
| 436 | }); |
| 437 | |
| 438 | let rebuildsOnLocaleChange = false; |
| 439 | return DesktopApplicationMenu.of({ |
| 440 | configure: applySavedLanguage.pipe( |
| 441 | Effect.andThen(configure), |
| 442 | Effect.tap(() => |
| 443 | Effect.sync(() => { |
| 444 | if (rebuildsOnLocaleChange) return; |
| 445 | rebuildsOnLocaleChange = true; |
| 446 | onLocaleChange(() => runFork(configure)); |
| 447 | }), |
| 448 | ), |
| 449 | ), |
| 450 | }); |
| 451 | }); |
| 452 | |
| 453 | export const layer = Layer.effect(DesktopApplicationMenu, make); |