Skip to content

Commit 967196d

Browse files
fix(mobile): terminal accessory survives Android's did-only IME repair
A lone keyboardDidHide no longer hides the accessory (on Android that turned off terminal autoFocus and cleared focus mid-repair); instead the next show after it starts a new show epoch, so a dismissed accessory still comes back. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 98d0423 commit 967196d

3 files changed

Lines changed: 46 additions & 21 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import {
9090
} from "./terminalInput";
9191
import { createTerminalPasteSession } from "./terminalPaste";
9292
import { cacheTerminalGridSize, getCachedTerminalGridSize } from "./terminalUiState";
93-
import { getKeyboardShowing, subscribeKeyboardShowing } from "./terminalKeyboardShowing";
93+
import { getKeyboardShowEpoch, subscribeKeyboardShowing } from "./terminalKeyboardShowing";
9494

9595
function TerminalHeader(props: {
9696
readonly subtitle: string;
@@ -572,12 +572,12 @@ function ThreadTerminal(props: {
572572
}, [hostPlatform]);
573573
const isKeyboardVisible = useKeyboardState((state) => state.isVisible);
574574
const keyboardHeight = useKeyboardState((state) => state.height);
575-
const keyboardShowing = useSyncExternalStore(subscribeKeyboardShowing, getKeyboardShowing);
575+
const keyboardShowEpoch = useSyncExternalStore(subscribeKeyboardShowing, getKeyboardShowEpoch);
576576
// The accessory follows the keyboard, except that the dismiss and show
577577
// buttons flip it ahead of the keyboard animation.
578-
const [accessory, setAccessory] = useState({ keyboardShowing, dismissed: false });
579-
if (accessory.keyboardShowing !== keyboardShowing) {
580-
setAccessory({ keyboardShowing, dismissed: !keyboardShowing });
578+
const [accessory, setAccessory] = useState({ keyboardShowEpoch, dismissed: false });
579+
if (accessory.keyboardShowEpoch !== keyboardShowEpoch) {
580+
setAccessory({ keyboardShowEpoch, dismissed: keyboardShowEpoch === 0 });
581581
}
582582
const isAccessoryDismissed = accessory.dismissed;
583583
const isAccessoryVisible = canOperateTerminal && isKeyboardVisible && !isAccessoryDismissed;

‎apps/mobile/src/features/terminal/terminalKeyboardShowing.test.ts‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,38 @@ vi.mock("react-native-keyboard-controller", () => ({
1414
},
1515
}));
1616

17-
import { getKeyboardShowing, subscribeKeyboardShowing } from "./terminalKeyboardShowing";
17+
import { getKeyboardShowEpoch, subscribeKeyboardShowing } from "./terminalKeyboardShowing";
1818

1919
function emit(name: string) {
2020
for (const listener of listeners.get(name) ?? []) listener();
2121
}
2222

2323
// Android's syncKeyboardPosition repairs a nonanimated IME inset change with
2424
// keyboardDidHide then keyboardDidShow, and no will-events.
25-
it("follows Android's nonanimated hide and show, which send only the did-events", () => {
25+
it("keeps the accessory through Android's did-only repair and starts a new show after it", () => {
2626
const onChange = vi.fn();
2727
const unsubscribe = subscribeKeyboardShowing(onChange);
28-
emit("keyboardWillShow");
29-
30-
emit("keyboardDidHide");
31-
const hidden = { showing: getKeyboardShowing(), notified: onChange.mock.calls.length };
32-
emit("keyboardDidShow");
28+
const trace = ["keyboardWillShow", "keyboardDidShow", "keyboardDidHide", "keyboardDidShow"].map(
29+
(name) => {
30+
emit(name);
31+
return getKeyboardShowEpoch();
32+
},
33+
);
34+
emit("keyboardWillHide");
3335

36+
const [shown, settled, repairHidden, repairShown] = trace;
3437
expect({
35-
hidden,
36-
shown: { showing: getKeyboardShowing(), notified: onChange.mock.calls.length },
37-
}).toEqual({ hidden: { showing: false, notified: 2 }, shown: { showing: true, notified: 3 } });
38+
neverHidden: trace.every((epoch) => epoch !== 0),
39+
sameShow: settled === shown && repairHidden === shown,
40+
newShow: repairShown !== shown,
41+
hiddenAfterWillHide: getKeyboardShowEpoch(),
42+
notified: onChange.mock.calls.length,
43+
}).toEqual({
44+
neverHidden: true,
45+
sameShow: true,
46+
newShow: true,
47+
hiddenAfterWillHide: 0,
48+
notified: 3,
49+
});
3850
unsubscribe();
3951
});

‎apps/mobile/src/features/terminal/terminalKeyboardShowing.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,40 @@ import { KeyboardController, KeyboardEvents } from "react-native-keyboard-contro
22

33
// `useKeyboardState` only flips to hidden on didHide; the accessory must
44
// leave as soon as the keyboard starts hiding, so it follows the will-events.
5-
// Android shows and hides a nonanimated IME inset with the did-events alone.
5+
// Android repairs a nonanimated IME change with didHide then didShow alone. A lone
6+
// didHide must not hide the accessory, which would clear the terminal's focus, but
7+
// the next show after it starts a new show so a dismissed accessory comes back.
8+
let showEpoch = 1;
69
let keyboardShowing = KeyboardController.isVisible();
10+
let hiddenWithoutWillHide = false;
11+
712
export function subscribeKeyboardShowing(onChange: () => void) {
813
keyboardShowing = KeyboardController.isVisible();
14+
hiddenWithoutWillHide = false;
915
const show = () => {
16+
const repaired = hiddenWithoutWillHide;
17+
hiddenWithoutWillHide = false;
18+
if (keyboardShowing && !repaired) return;
1019
keyboardShowing = true;
20+
showEpoch += 1;
1121
onChange();
1222
};
1323
const willShow = KeyboardEvents.addListener("keyboardWillShow", show);
1424
const didShow = KeyboardEvents.addListener("keyboardDidShow", show);
15-
const hide = () => {
25+
const willHide = KeyboardEvents.addListener("keyboardWillHide", () => {
1626
keyboardShowing = false;
1727
onChange();
18-
};
19-
const willHide = KeyboardEvents.addListener("keyboardWillHide", hide);
20-
const didHide = KeyboardEvents.addListener("keyboardDidHide", hide);
28+
});
29+
const didHide = KeyboardEvents.addListener("keyboardDidHide", () => {
30+
hiddenWithoutWillHide = keyboardShowing;
31+
});
2132
return () => {
2233
willShow.remove();
2334
didShow.remove();
2435
willHide.remove();
2536
didHide.remove();
2637
};
2738
}
28-
export const getKeyboardShowing = () => keyboardShowing;
39+
40+
// 0 while hidden; otherwise a number that changes with every new show.
41+
export const getKeyboardShowEpoch = () => (keyboardShowing ? showEpoch : 0);

0 commit comments

Comments
 (0)