Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

nativeTerminalModule.ts

124 lines3.8 KB

1import type { ComponentType, Ref } from "react";
2import type { NativeSyntheticEvent, ViewProps } from "react-native";
3import { requireNativeView, requireOptionalNativeModule } from "expo";
4​
5import { NativeViewResolutionError } from "../../native/nativeViewResolutionError";
6​
7const NATIVE_TERMINAL_MODULE_NAME = "T3TerminalSurface";
8​
9interface ExpoGlobalWithViewConfig {
10 readonly expo?: {
11 getViewConfig?: (moduleName: string, viewName?: string) => unknown;
12 };
14​
15interface TerminalInputEvent {
16 readonly data: string;
18​
19interface TerminalPasteEvent {
20 readonly text: string;
22​
23interface TerminalResizeEvent {
24 readonly cols: number;
25 readonly rows: number;
27​
28export 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;
50​
51export interface NativeTerminalSurfaceHandle {
52 readonly writeOutput: (data: string, reset: boolean) => Promise<void>;
53 /** Absent on native builds that predate it. */
54 readonly resetInput?: () => Promise<void>;
56​
57let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined;
58let nativeTerminalSurfaceViewResolutionFailed = false;
59​
60function getExpoViewConfig(moduleName: string) {
61 return (globalThis as typeof globalThis & ExpoGlobalWithViewConfig).expo?.getViewConfig?.(
62 moduleName,
63 );
65​
66export 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;
96​
97export 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 }
110​
111export function hasNativeTerminalSurface() {
112 return resolveNativeTerminalSurfaceView() !== null;
114​
115export 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 }