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 type { ComponentType, Ref } from "react"; |
| 2 | import type { NativeSyntheticEvent, ViewProps } from "react-native"; |
| 3 | import { requireNativeView, requireOptionalNativeModule } from "expo"; |
| 4 | |
| 5 | import { NativeViewResolutionError } from "../../native/nativeViewResolutionError"; |
| 6 | |
| 7 | const NATIVE_TERMINAL_MODULE_NAME = "T3TerminalSurface"; |
| 8 | |
| 9 | interface ExpoGlobalWithViewConfig { |
| 10 | readonly expo?: { |
| 11 | getViewConfig?: (moduleName: string, viewName?: string) => unknown; |
| 12 | }; |
| 13 | } |
| 14 | |
| 15 | interface TerminalInputEvent { |
| 16 | readonly data: string; |
| 17 | } |
| 18 | |
| 19 | interface TerminalPasteEvent { |
| 20 | readonly text: string; |
| 21 | } |
| 22 | |
| 23 | interface TerminalResizeEvent { |
| 24 | readonly cols: number; |
| 25 | readonly rows: number; |
| 26 | } |
| 27 | |
| 28 | export interface NativeTerminalSurfaceProps extends ViewProps { |
| 29 | readonly ref?: Ref<NativeTerminalSurfaceHandle>; |
| 30 | readonly appearanceScheme?: "light" | "dark"; |
| 31 | readonly autoFocus?: boolean; |
| 32 | readonly readOnly?: boolean; |
| 33 | readonly focusRequest?: number; |
| 34 | readonly captureRequest?: number; |
| 35 | readonly onCapture?: (event: NativeSyntheticEvent<{ readonly text: string }>) => void; |
| 36 | readonly themeConfig?: string; |
| 37 | readonly backgroundColor?: string; |
| 38 | readonly foregroundColor?: string; |
| 39 | readonly mutedForegroundColor?: string; |
| 40 | readonly terminalKey: string; |
| 41 | readonly initialBuffer: string; |
| 42 | readonly fontSize: number; |
| 43 | /** `[cols, rows]` of the shared PTY, or empty before the server reports one. */ |
| 44 | readonly sharedGrid?: ReadonlyArray<number>; |
| 45 | readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void; |
| 46 | /** iOS only: clipboard text from a system paste (edit menu, ⌘V, shortcut bar). */ |
| 47 | readonly onPaste?: (event: NativeSyntheticEvent<TerminalPasteEvent>) => void; |
| 48 | readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void; |
| 49 | } |
| 50 | |
| 51 | export interface NativeTerminalSurfaceHandle { |
| 52 | readonly writeOutput: (data: string, reset: boolean) => Promise<void>; |
| 53 | /** Absent on native builds that predate it. */ |
| 54 | readonly resetInput?: () => Promise<void>; |
| 55 | } |
| 56 | |
| 57 | let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined; |
| 58 | let nativeTerminalSurfaceViewResolutionFailed = false; |
| 59 | |
| 60 | function getExpoViewConfig(moduleName: string) { |
| 61 | return (globalThis as typeof globalThis & ExpoGlobalWithViewConfig).expo?.getViewConfig?.( |
| 62 | moduleName, |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | export function resolveNativeTerminalSurfaceView(): ComponentType<NativeTerminalSurfaceProps> | null { |
| 67 | if (cachedNativeTerminalSurfaceView) { |
| 68 | return cachedNativeTerminalSurfaceView; |
| 69 | } |
| 70 | |
| 71 | if (nativeTerminalSurfaceViewResolutionFailed) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | if (getExpoViewConfig(NATIVE_TERMINAL_MODULE_NAME) == null) { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | cachedNativeTerminalSurfaceView = requireNativeView<NativeTerminalSurfaceProps>( |
| 81 | NATIVE_TERMINAL_MODULE_NAME, |
| 82 | ); |
| 83 | } catch (cause) { |
| 84 | nativeTerminalSurfaceViewResolutionFailed = true; |
| 85 | console.error( |
| 86 | new NativeViewResolutionError({ |
| 87 | nativeModuleName: NATIVE_TERMINAL_MODULE_NAME, |
| 88 | cause, |
| 89 | }), |
| 90 | ); |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | return cachedNativeTerminalSurfaceView ?? null; |
| 95 | } |
| 96 | |
| 97 | export function getNativeTerminalHardwareKeyRevision(): number | null { |
| 98 | try { |
| 99 | if (typeof requireOptionalNativeModule !== "function") { |
| 100 | return null; |
| 101 | } |
| 102 | const module = requireOptionalNativeModule<{ readonly hardwareKeyRevision?: number }>( |
| 103 | NATIVE_TERMINAL_MODULE_NAME, |
| 104 | ); |
| 105 | return module?.hardwareKeyRevision ?? null; |
| 106 | } catch { |
| 107 | return null; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | export function hasNativeTerminalSurface() { |
| 112 | return resolveNativeTerminalSurfaceView() !== null; |
| 113 | } |
| 114 | |
| 115 | export function supportsNativeTerminalOutput(): boolean { |
| 116 | try { |
| 117 | const module = requireOptionalNativeModule<{ readonly outputRevision?: number }>( |
| 118 | NATIVE_TERMINAL_MODULE_NAME, |
| 119 | ); |
| 120 | return (module?.outputRevision ?? 0) >= 1 && hasNativeTerminalSurface(); |
| 121 | } catch { |
| 122 | return false; |
| 123 | } |
| 124 | } |