Skip to content

Commit 03110f3

Browse files
fix(mobile): terminal accessory returns on Android's didShow-only IME repair
The terminal's accessory store followed only keyboardWillShow and keyboardWillHide. After a hide, Android's nonanimated IME repair sends keyboardDidShow alone, so with the library state now visible the accessory stayed dismissed and the Android control row was hidden too. Move the store into its own module, listen to keyboardDidShow as well, and test it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 29d68e8 commit 03110f3

3 files changed

Lines changed: 66 additions & 21 deletions

File tree

‎apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import * as Schema from "effect/Schema";
2525
import { AsyncResult } from "effect/reactivity";
2626
import {
2727
KeyboardController,
28-
KeyboardEvents,
2928
KeyboardStickyView,
3029
useKeyboardState,
3130
} from "react-native-keyboard-controller";
@@ -91,6 +90,7 @@ import {
9190
} from "./terminalInput";
9291
import { createTerminalPasteSession } from "./terminalPaste";
9392
import { cacheTerminalGridSize, getCachedTerminalGridSize } from "./terminalUiState";
93+
import { getKeyboardShowing, subscribeKeyboardShowing } from "./terminalKeyboardShowing";
9494

9595
function TerminalHeader(props: {
9696
readonly subtitle: string;
@@ -354,26 +354,6 @@ function readLiveTerminalSessions(
354354
}));
355355
}
356356

357-
// `useKeyboardState` only flips to hidden on didHide; the accessory must
358-
// leave as soon as the keyboard starts hiding, so it follows the will-events.
359-
let keyboardShowing = KeyboardController.isVisible();
360-
function subscribeKeyboardShowing(onChange: () => void) {
361-
keyboardShowing = KeyboardController.isVisible();
362-
const willShow = KeyboardEvents.addListener("keyboardWillShow", () => {
363-
keyboardShowing = true;
364-
onChange();
365-
});
366-
const willHide = KeyboardEvents.addListener("keyboardWillHide", () => {
367-
keyboardShowing = false;
368-
onChange();
369-
});
370-
return () => {
371-
willShow.remove();
372-
willHide.remove();
373-
};
374-
}
375-
const getKeyboardShowing = () => keyboardShowing;
376-
377357
function ThreadTerminal(props: {
378358
readonly params: ThreadTerminalRouteScreenProps["route"]["params"];
379359
readonly routeEnvironmentId: EnvironmentId | null;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect, it, vi } from "vite-plus/test";
2+
3+
const listeners = vi.hoisted(() => new Map<string, Set<() => void>>());
4+
5+
vi.mock("react-native-keyboard-controller", () => ({
6+
KeyboardController: { isVisible: () => false },
7+
KeyboardEvents: {
8+
addListener: (name: string, listener: () => void) => {
9+
const named = listeners.get(name) ?? new Set();
10+
named.add(listener);
11+
listeners.set(name, named);
12+
return { remove: () => named.delete(listener) };
13+
},
14+
},
15+
}));
16+
17+
import { getKeyboardShowing, subscribeKeyboardShowing } from "./terminalKeyboardShowing";
18+
19+
function emit(name: string) {
20+
for (const listener of listeners.get(name) ?? []) listener();
21+
}
22+
23+
// Android's syncKeyboardPosition repairs a nonanimated IME inset change with
24+
// keyboardDidHide then keyboardDidShow, and no keyboardWillShow.
25+
it("brings the accessory back when Android restores the keyboard with keyboardDidShow alone", () => {
26+
const onChange = vi.fn();
27+
const unsubscribe = subscribeKeyboardShowing(onChange);
28+
emit("keyboardWillShow");
29+
emit("keyboardWillHide");
30+
emit("keyboardDidHide");
31+
onChange.mockClear();
32+
33+
emit("keyboardDidShow");
34+
35+
expect({ showing: getKeyboardShowing(), notified: onChange.mock.calls.length }).toEqual({
36+
showing: true,
37+
notified: 1,
38+
});
39+
unsubscribe();
40+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { KeyboardController, KeyboardEvents } from "react-native-keyboard-controller";
2+
3+
// `useKeyboardState` only flips to hidden on didHide; the accessory must
4+
// leave as soon as the keyboard starts hiding, so it follows the will-events.
5+
// Android restores a nonanimated IME inset with keyboardDidShow alone.
6+
let keyboardShowing = KeyboardController.isVisible();
7+
export function subscribeKeyboardShowing(onChange: () => void) {
8+
keyboardShowing = KeyboardController.isVisible();
9+
const show = () => {
10+
keyboardShowing = true;
11+
onChange();
12+
};
13+
const willShow = KeyboardEvents.addListener("keyboardWillShow", show);
14+
const didShow = KeyboardEvents.addListener("keyboardDidShow", show);
15+
const willHide = KeyboardEvents.addListener("keyboardWillHide", () => {
16+
keyboardShowing = false;
17+
onChange();
18+
});
19+
return () => {
20+
willShow.remove();
21+
didShow.remove();
22+
willHide.remove();
23+
};
24+
}
25+
export const getKeyboardShowing = () => keyboardShowing;

0 commit comments

Comments
 (0)