Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 95 additions & 30 deletions apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<TerminalMenuSession> = buildTerminalMenuSessions({
knownSessions: knownSessions ?? [],
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 ? (
Expand Down Expand Up @@ -897,11 +951,13 @@ function ThreadTerminal(props: {
<EmptyState title="Terminal unavailable" detail={terminal.error} />
) : (
<>
<View
style={{
flex: 1,
paddingBottom: terminalBottomInset,
}}
<Animated.View
style={[
{ flex: 1 },
Platform.OS === "android"
? androidTerminalInsetStyle
: { paddingBottom: terminalBottomInset },
]}
>
<View
pointerEvents="none"
Expand Down Expand Up @@ -935,22 +991,28 @@ function ThreadTerminal(props: {
terminalKey={terminalKey}
theme={terminalTheme}
/>
</View>
</Animated.View>

{Platform.OS === "android" && !isKeyboardVisible ? (
<View className="min-h-14 flex-row items-center gap-2 bg-card-alt px-2">
{hasNativeTerminalSurface() ? (
<Button label="Attach output" tone="text" onPress={requestCapture} />
) : null}
<View className="flex-1" />
<IconButton
accessibilityLabel="Show keyboard"
disabled={!canOperateTerminal}
icon="keyboard"
onPress={handleShowKeyboard}
/>
{Platform.OS === "android" ? (
<View
className="bg-card-alt"
onLayout={(event) => setAndroidBarHeight(event.nativeEvent.layout.height)}
style={{ paddingBottom: insets.bottom }}
>
<View className="min-h-14 flex-row items-center gap-2 bg-card-alt px-2">
{hasNativeTerminalSurface() ? (
<Button label="Attach output" tone="text" onPress={requestCapture} />
) : null}
<View className="flex-1" />
<IconButton
accessibilityLabel="Show keyboard"
disabled={!canOperateTerminal}
icon="keyboard"
onPress={handleShowKeyboard}
/>
</View>
</View>
) : Platform.OS !== "android" && hasNativeTerminalSurface() ? (
) : hasNativeTerminalSurface() ? (
<Pressable
accessibilityRole="button"
onPress={() => {
Expand All @@ -969,18 +1031,21 @@ function ThreadTerminal(props: {
<Text style={{ color: terminalTheme.foreground }}>Attach visible output</Text>
</Pressable>
) : null}
{isAccessoryVisible ? (
{isAccessoryVisible || (Platform.OS === "android" && canOperateTerminal) ? (
<KeyboardStickyView
style={{ position: "absolute", bottom: 0, left: 0, right: 0 }}
offset={{ closed: 0, opened: 0 }}
>
<View
<Animated.View
className="border-t"
style={{
backgroundColor: terminalTheme.background,
borderTopColor: terminalTheme.border,
minHeight: TERMINAL_ACCESSORY_HEIGHT,
}}
style={[
{
backgroundColor: terminalTheme.background,
borderTopColor: terminalTheme.border,
minHeight: TERMINAL_ACCESSORY_HEIGHT,
},
Platform.OS === "android" ? androidAccessoryStyle : null,
]}
>
<ComposerToolbarRow paddingBottom={4} paddingHorizontal={8} paddingTop={4}>
<ComposerToolbarScroller
Expand Down Expand Up @@ -1023,7 +1088,7 @@ function ThreadTerminal(props: {
showChevron={false}
/>
</ComposerToolbarRow>
</View>
</Animated.View>
</KeyboardStickyView>
) : canOperateTerminal && !isKeyboardVisible && Platform.OS !== "android" ? (
<Pressable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ vi.mock("react-native-keyboard-controller", () => ({
},
}));

import { getKeyboardShowEpoch, subscribeKeyboardShowing } from "./terminalKeyboardShowing";
import {
getKeyboardShowEpoch,
runWhenKeyboardSettles,
subscribeKeyboardShowing,
} from "./terminalKeyboardShowing";

function emit(name: string) {
for (const listener of listeners.get(name) ?? []) listener();
Expand Down Expand Up @@ -64,3 +68,20 @@ it("keeps the accessory through Android's did-only repair and starts a new show
});
for (const unsubscribe of unsubscribes) unsubscribe();
});

it("runs only the latest deferred call once the keyboard stops moving", () => {
emit("keyboardDidHide");
const owner = {};
const reported: number[] = [];
runWhenKeyboardSettles(owner, () => reported.push(1));
emit("keyboardWillShow");
for (const rows of [40, 35, 30]) runWhenKeyboardSettles(owner, () => reported.push(rows));
const whileOpening = [...reported];
emit("keyboardDidShow");
emit("keyboardWillHide");
runWhenKeyboardSettles(owner, () => reported.push(45));
emit("keyboardDidHide");
emit("keyboardDidHide");

expect({ whileOpening, reported }).toEqual({ whileOpening: [1], reported: [1, 30, 45] });
});
30 changes: 28 additions & 2 deletions apps/mobile/src/features/terminal/terminalKeyboardShowing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,53 @@ const subscribers = new Set<() => void>();
let showEpoch = 1;
let keyboardShowing = KeyboardController.isVisible();
let hiddenSinceShow = !keyboardShowing;
let keyboardMoving = false;
const settleRuns = new Map<object, () => void>();

const notify = () => {
for (const subscriber of subscribers) subscriber();
};
const settle = () => {
keyboardMoving = false;
const runs = [...settleRuns.values()];
settleRuns.clear();
for (const run of runs) run();
};
const show = () => {
if (hiddenSinceShow) showEpoch += 1;
hiddenSinceShow = false;
keyboardShowing = true;
notify();
};
KeyboardEvents.addListener("keyboardWillShow", show);
KeyboardEvents.addListener("keyboardDidShow", show);
KeyboardEvents.addListener("keyboardWillShow", () => {
keyboardMoving = true;
show();
});
KeyboardEvents.addListener("keyboardDidShow", () => {
show();
settle();
});
KeyboardEvents.addListener("keyboardWillHide", () => {
keyboardMoving = true;
keyboardShowing = false;
hiddenSinceShow = true;
notify();
});
KeyboardEvents.addListener("keyboardDidHide", () => {
hiddenSinceShow = true;
settle();
});

// Runs now, or once the keyboard stops moving; a later call for the same owner replaces it.
export function runWhenKeyboardSettles(owner: object, run: () => void) {
if (!keyboardMoving) {
settleRuns.delete(owner);
run();
return;
}
settleRuns.set(owner, run);
}

export function subscribeKeyboardShowing(onChange: () => void) {
subscribers.add(onChange);
return () => {
Expand Down
Loading