diff --git a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx index 90fac17f8004a..d0c9314bc3541 100644 --- a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx +++ b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx @@ -26,8 +26,15 @@ import { AsyncResult } from "effect/reactivity"; import { KeyboardController, KeyboardStickyView, + useGenericKeyboardHandler, useKeyboardState, + useReanimatedKeyboardAnimation, } from "react-native-keyboard-controller"; +import Animated, { + useAnimatedStyle, + useDerivedValue, + useSharedValue, +} from "react-native-reanimated"; import { ComposerToolbarButton, @@ -89,7 +96,11 @@ import { } from "./terminalInput"; import { createTerminalPasteSession } from "./terminalPaste"; import { cacheTerminalGridSize, getCachedTerminalGridSize } from "./terminalUiState"; -import { getKeyboardShowEpoch, subscribeKeyboardShowing } from "./terminalKeyboardShowing"; +import { + getKeyboardShowEpoch, + runWhenKeyboardSettles, + subscribeKeyboardShowing, +} from "./terminalKeyboardShowing"; function TerminalHeader(props: { readonly subtitle: string; @@ -585,6 +596,42 @@ function ThreadTerminal(props: { : !SHOWCASE_ENABLED; const terminalBottomInset = (isKeyboardVisible ? keyboardHeight : 0) + (isAccessoryVisible ? TERMINAL_ACCESSORY_HEIGHT : 0); + // Android's keyboard state lands after the IME finishes moving, so the terminal inset, the + // accessory and the bar below the terminal all follow the keyboard on the UI thread instead. + const keyboardAnimation = useReanimatedKeyboardAnimation(); + const keyboardOpening = useSharedValue(0); + useGenericKeyboardHandler( + { + onStart: (event) => { + "worklet"; + keyboardOpening.set(event.height > 0 ? 1 : 0); + }, + }, + [], + ); + const [androidBarHeight, setAndroidBarHeight] = useState(0); + const accessoryAllowed = canOperateTerminal ? 1 : 0; + // Did-only IME repairs never start an animation, so the JS state still shows the accessory. + const accessoryShownByState = isAccessoryVisible ? 1 : 0; + const androidAccessoryReserve = useDerivedValue( + () => + TERMINAL_ACCESSORY_HEIGHT * + accessoryAllowed * + Math.max(keyboardOpening.value, accessoryShownByState) * + keyboardAnimation.progress.value, + ); + // The bar stays laid out under the keyboard, so the terminal only needs what exceeds it. + const androidTerminalInsetStyle = useAnimatedStyle(() => ({ + paddingBottom: Math.max( + 0, + -keyboardAnimation.height.value + androidAccessoryReserve.value - androidBarHeight, + ), + })); + // The accessory rises from under the keyboard's top edge by the height reserved for it. + const androidAccessoryStyle = useAnimatedStyle(() => ({ + opacity: androidAccessoryReserve.value > 0 ? 1 : 0, + transform: [{ translateY: TERMINAL_ACCESSORY_HEIGHT - androidAccessoryReserve.value }], + })); const terminalMenuSessions: ReadonlyArray = buildTerminalMenuSessions({ knownSessions: knownSessions ?? [], @@ -666,7 +713,15 @@ function ThreadTerminal(props: { size, ); } - terminalSessionDriver.reportGridSize(size); + // The view shrinks locally on every frame of the Android keyboard animation; the PTY + // only needs the size it settles at. + if (Platform.OS === "android") { + runWhenKeyboardSettles(terminalSessionDriver, () => + terminalSessionDriver.reportGridSize(size), + ); + } else { + terminalSessionDriver.reportGridSize(size); + } }; const handleSelectTerminal = (nextTerminalId: string) => { @@ -841,7 +896,6 @@ function ThreadTerminal(props: { : terminalTheme.background, // Measured top reservation is zero when UIKit already inset the body or moved the bar. paddingTop: contentTopInset, - paddingBottom: Platform.OS === "android" && !isKeyboardVisible ? insets.bottom : 0, }} > {!isEnvironmentReady ? ( @@ -897,11 +951,13 @@ function ThreadTerminal(props: { ) : ( <> - - + - {Platform.OS === "android" && !isKeyboardVisible ? ( - - {hasNativeTerminalSurface() ? ( -