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

nativeTerminalModule.ts

126 lines3.9 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 /** React Native family name; a system mono name selects the platform monospace font. */
44 readonly fontFamily: string;
45 /** `[cols, rows]` of the shared PTY, or empty before the server reports one. */
46 readonly sharedGrid?: ReadonlyArray<number>;
47 readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void;
48 /** iOS only: clipboard text from a system paste (edit menu, ⌘V, shortcut bar). */
49 readonly onPaste?: (event: NativeSyntheticEvent<TerminalPasteEvent>) => void;
50 readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void;
52​
53export interface NativeTerminalSurfaceHandle {
54 readonly writeOutput: (data: string, reset: boolean) => Promise<void>;
55 /** Absent on native builds that predate it. */
56 readonly resetInput?: () => Promise<void>;
58​
59let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined;
60let nativeTerminalSurfaceViewResolutionFailed = false;
61​
62function getExpoViewConfig(moduleName: string) {
63 return (globalThis as typeof globalThis & ExpoGlobalWithViewConfig).expo?.getViewConfig?.(
64 moduleName,
65 );
67​
68export function resolveNativeTerminalSurfaceView(): ComponentType<NativeTerminalSurfaceProps> | null {
69 if (cachedNativeTerminalSurfaceView) {
70 return cachedNativeTerminalSurfaceView;
71 }
72​
73 if (nativeTerminalSurfaceViewResolutionFailed) {
74 return null;
75 }
76​
77 if (getExpoViewConfig(NATIVE_TERMINAL_MODULE_NAME) == null) {
78 return null;
79 }
80​
81 try {
82 cachedNativeTerminalSurfaceView = requireNativeView<NativeTerminalSurfaceProps>(
83 NATIVE_TERMINAL_MODULE_NAME,
84 );
85 } catch (cause) {
86 nativeTerminalSurfaceViewResolutionFailed = true;
87 console.error(
88 new NativeViewResolutionError({
89 nativeModuleName: NATIVE_TERMINAL_MODULE_NAME,
90 cause,
91 }),
92 );
93 return null;
94 }
95​
96 return cachedNativeTerminalSurfaceView ?? null;
98​
99export function getNativeTerminalHardwareKeyRevision(): number | null {
100 try {
101 if (typeof requireOptionalNativeModule !== "function") {
102 return null;
103 }
104 const module = requireOptionalNativeModule<{ readonly hardwareKeyRevision?: number }>(
105 NATIVE_TERMINAL_MODULE_NAME,
106 );
107 return module?.hardwareKeyRevision ?? null;
108 } catch {
109 return null;
110 }
112​
113export function hasNativeTerminalSurface() {
114 return resolveNativeTerminalSurfaceView() !== null;
116​
117export function supportsNativeTerminalOutput(): boolean {
118 try {
119 const module = requireOptionalNativeModule<{ readonly outputRevision?: number }>(
120 NATIVE_TERMINAL_MODULE_NAME,
121 );
122 return (module?.outputRevision ?? 0) >= 1 && hasNativeTerminalSurface();
123 } catch {
124 return false;
125 }