Skip to content

Commit e132c95

Browse files
committed
fix(mobile): keep keyboard out of model picker
1 parent 6a1169d commit e132c95

6 files changed

Lines changed: 243 additions & 63 deletions

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NativeStackScreenOptions } from "../../native/StackHeader";
22
import { StackActions, useNavigation, usePreventRemove } from "@react-navigation/native";
33
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
4-
import { Alert, InteractionManager, Keyboard, Platform, View, useColorScheme } from "react-native";
4+
import { Alert, InteractionManager, Platform, View, useColorScheme } from "react-native";
55
import { KeyboardAvoidingView, useKeyboardState } from "react-native-keyboard-controller";
66
import { useSafeAreaInsets } from "react-native-safe-area-context";
77
import { useThemeColor } from "../../lib/useThemeColor";
@@ -26,6 +26,7 @@ import { ControlPill, ControlPillMenu } from "../../components/ControlPill";
2626
import { ProviderIcon } from "../../components/ProviderIcon";
2727
import { ComposerSurface } from "./ThreadComposer";
2828
import { ThreadSettingsSheet, threadSettingsSummaryLabel } from "./ThreadSettingsSheet";
29+
import { useThreadSettingsSheetPresentation } from "./use-thread-settings-sheet-presentation";
2930

3031
import { makeTurnCommandMetadata } from "../../lib/commandMetadata";
3132
import { convertPastedImagesToAttachments, pickComposerImages } from "../../lib/composerImages";
@@ -99,9 +100,10 @@ export function NewTaskDraftScreen(props: {
99100
const promptInputRef = useRef<ComposerEditorHandle>(null);
100101
const loadedBranchesProjectKeyRef = useRef<string | null>(null);
101102
const [isComposerFocused, setIsComposerFocused] = useState(false);
102-
const [isSettingsSheetVisible, setIsSettingsSheetVisible] = useState(false);
103-
const isSettingsSheetVisibleRef = useRef(false);
104-
isSettingsSheetVisibleRef.current = isSettingsSheetVisible;
103+
const settingsSheetPresentation = useThreadSettingsSheetPresentation({
104+
editorRef: promptInputRef,
105+
isEditorFocused: isComposerFocused,
106+
});
105107
const [importingShareKey, setImportingShareKey] = useState<string | null>(null);
106108
const [isCancellingShareImport, setIsCancellingShareImport] = useState(false);
107109
const [cancelledIncomingShareId, setCancelledIncomingShareId] = useState<string | null>(null);
@@ -523,8 +525,10 @@ export function NewTaskDraftScreen(props: {
523525
focusFrame = requestAnimationFrame(() => {
524526
// The delayed focus can land after the settings sheet opened, which
525527
// would pop the keyboard underneath its modal.
526-
if (!isSettingsSheetVisibleRef.current) {
528+
if (!settingsSheetPresentation.isActiveRef.current) {
527529
promptInputRef.current?.focus();
530+
} else {
531+
settingsSheetPresentation.restoreFocusAfterSave();
528532
}
529533
});
530534
});
@@ -535,7 +539,11 @@ export function NewTaskDraftScreen(props: {
535539
cancelAnimationFrame(focusFrame);
536540
}
537541
};
538-
}, [selectedProject]);
542+
}, [
543+
selectedProject,
544+
settingsSheetPresentation.isActiveRef,
545+
settingsSheetPresentation.restoreFocusAfterSave,
546+
]);
539547

540548
const environmentMenuActions = useMemo(
541549
() =>
@@ -643,24 +651,6 @@ export function NewTaskDraftScreen(props: {
643651
}),
644652
[currentBranchName, flow.selectedBranchName, flow.workspaceMode],
645653
);
646-
// Order matters: mark the sheet open before dismissing the keyboard so the
647-
// Android draft layout (expanded only while focused) doesn't collapse.
648-
// The keyboard only comes back on close if it was up when the sheet opened.
649-
const wasFocusedBeforeSheetRef = useRef(false);
650-
const openSettingsSheet = useCallback(() => {
651-
wasFocusedBeforeSheetRef.current = isComposerFocused;
652-
setIsSettingsSheetVisible(true);
653-
Keyboard.dismiss();
654-
}, [isComposerFocused]);
655-
const closeSettingsSheet = useCallback((reason: "save" | "dismiss") => {
656-
setIsSettingsSheetVisible(false);
657-
// Only Save/Done restores the keyboard: a dismissal (backdrop or
658-
// grabber, including stray taps near the sheet's edge) closes quietly.
659-
if (reason === "save" && wasFocusedBeforeSheetRef.current) {
660-
promptInputRef.current?.focus();
661-
}
662-
}, []);
663-
664654
function handleEnvironmentMenuAction(event: string) {
665655
if (isIncomingShareTransferPending || !event.startsWith("environment:")) {
666656
return;
@@ -876,7 +866,7 @@ export function NewTaskDraftScreen(props: {
876866
// the touch gesture that opens the keyboard.
877867
// The settings sheet dismisses the keyboard, so its flag keeps the Android
878868
// draft composer expanded through the blur (mirrors ThreadComposer).
879-
const isExpanded = !isAndroid || isComposerFocused || isSettingsSheetVisible;
869+
const isExpanded = !isAndroid || isComposerFocused || settingsSheetPresentation.isActive;
880870
const canStart =
881871
Boolean(flow.selectedProject) &&
882872
Boolean(flow.selectedModel) &&
@@ -936,7 +926,7 @@ export function NewTaskDraftScreen(props: {
936926
iconNode={<ProviderIcon provider={flow.selectedModelOption?.providerDriver} size={16} />}
937927
label={settingsSummaryLabel}
938928
maxWidth={320}
939-
onPress={openSettingsSheet}
929+
onPress={settingsSheetPresentation.open}
940930
/>
941931
<ControlPillMenu
942932
actions={environmentMenuActions}
@@ -965,8 +955,9 @@ export function NewTaskDraftScreen(props: {
965955

966956
const settingsSheet = (
967957
<ThreadSettingsSheet
968-
visible={isSettingsSheetVisible}
969-
onClose={closeSettingsSheet}
958+
visible={settingsSheetPresentation.isVisible}
959+
onClose={settingsSheetPresentation.close}
960+
onDismissed={settingsSheetPresentation.onDismissed}
970961
providerGroups={flow.providerGroups}
971962
selectedModel={flow.selectedModel}
972963
onSelectModel={(option) => flow.setSelectedModelKey(option.key, option.selection.options)}

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { memo, useCallback, useEffect, useMemo, useRef, useState, type RefObject
1919
import {
2020
ActivityIndicator,
2121
Image,
22-
Keyboard,
2322
Platform,
2423
Pressable,
2524
StyleSheet,
@@ -67,6 +66,7 @@ import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";
6766
import { useComposerPathSearch } from "../../state/use-composer-path-search";
6867
import { ComposerCommandPopover, type ComposerCommandItem } from "./ComposerCommandPopover";
6968
import { ThreadSettingsSheet, threadSettingsSummaryLabel } from "./ThreadSettingsSheet";
69+
import { useThreadSettingsSheetPresentation } from "./use-thread-settings-sheet-presentation";
7070

7171
/**
7272
* Height of the collapsed composer (pill + vertical padding, excluding safe-area inset).
@@ -270,16 +270,19 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
270270
const fallbackInputRef = useRef<ComposerEditorHandle>(null);
271271
const inputRef = props.editorRef ?? fallbackInputRef;
272272
const [isFocused, setIsFocused] = useState(false);
273+
const settingsSheetPresentation = useThreadSettingsSheetPresentation({
274+
editorRef: inputRef,
275+
isEditorFocused: isFocused,
276+
});
273277
const wasExpandedBeforePreviewRef = useRef(false);
274278
const inFlightThreadIdsRef = useRef(new Set<string>());
275279
const { onExpandedChange } = props;
276280

277281
const [previewImageUri, setPreviewImageUri] = useState<string | null>(null);
278-
const [isSettingsSheetVisible, setIsSettingsSheetVisible] = useState(false);
279282
const hasContent = props.draftMessage.trim().length > 0 || props.draftAttachments.length > 0;
280-
// The settings sheet dismisses the keyboard (it would cover the sheet), so
281-
// the sheet flag keeps the composer expanded through that blur.
282-
const isExpanded = isFocused || isSettingsSheetVisible;
283+
// Opening and closing count as active so the composer stays expanded while
284+
// focus moves between its native editor and the settings modal.
285+
const isExpanded = isFocused || settingsSheetPresentation.isActive;
283286
const canSend = hasContent;
284287

285288
// Notify the parent from the derived value, not focus events: the parent
@@ -620,27 +623,6 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
620623
interactionMode: currentInteractionMode,
621624
});
622625

623-
// Order matters: mark the sheet open before dismissing the keyboard so
624-
// isExpanded stays true through the blur and the composer doesn't collapse.
625-
// The keyboard only comes back on close if it was up when the sheet opened.
626-
const wasFocusedBeforeSheetRef = useRef(false);
627-
const openSettingsSheet = useCallback(() => {
628-
wasFocusedBeforeSheetRef.current = isFocused;
629-
setIsSettingsSheetVisible(true);
630-
Keyboard.dismiss();
631-
}, [isFocused]);
632-
const closeSettingsSheet = useCallback(
633-
(reason: "save" | "dismiss") => {
634-
setIsSettingsSheetVisible(false);
635-
// Only Save/Done restores the keyboard: a dismissal (backdrop or
636-
// grabber, including stray taps near the sheet's edge) closes quietly.
637-
if (reason === "save" && wasFocusedBeforeSheetRef.current) {
638-
inputRef.current?.focus();
639-
}
640-
},
641-
[inputRef],
642-
);
643-
644626
return (
645627
<Animated.View
646628
className="px-4"
@@ -817,7 +799,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
817799
}
818800
label={settingsSummaryLabel}
819801
maxWidth={320}
820-
onPress={openSettingsSheet}
802+
onPress={settingsSheetPresentation.open}
821803
/>
822804
{showStopAction ? (
823805
<ComposerToolbarButton
@@ -853,8 +835,9 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
853835
</Animated.View>
854836

855837
<ThreadSettingsSheet
856-
visible={isSettingsSheetVisible}
857-
onClose={closeSettingsSheet}
838+
visible={settingsSheetPresentation.isVisible}
839+
onClose={settingsSheetPresentation.close}
840+
onDismissed={settingsSheetPresentation.onDismissed}
858841
providerGroups={threadProviderGroups}
859842
selectedModel={currentModelSelection}
860843
onSelectModel={(option) => props.onUpdateModelSelection(option.selection)}

‎apps/mobile/src/features/threads/ThreadSettingsSheet.tsx‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ import {
1111
getProviderOptionDescriptors,
1212
} from "@t3tools/shared/model";
1313
import * as Haptics from "expo-haptics";
14-
import { useEffect, useState } from "react";
15-
import { Modal, Pressable, ScrollView, Switch, useWindowDimensions, View } from "react-native";
14+
import { useCallback, useEffect, useRef, useState } from "react";
15+
import {
16+
Modal,
17+
Platform,
18+
Pressable,
19+
ScrollView,
20+
Switch,
21+
useWindowDimensions,
22+
View,
23+
} from "react-native";
1624
import { useSafeAreaInsets } from "react-native-safe-area-context";
1725

1826
import { SymbolView } from "../../components/AppSymbol";
@@ -22,6 +30,8 @@ import { cn } from "../../lib/cn";
2230
import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
2331
import { applyProviderOptionSelection, providerOptionValueLabels } from "../../lib/providerOptions";
2432
import { useThemeColor } from "../../lib/useThemeColor";
33+
import { pendingModelAfterPress } from "./thread-settings-sheet-state";
34+
import type { ThreadSettingsSheetCloseReason } from "./use-thread-settings-sheet-presentation";
2535

2636
/**
2737
* The everyday harnesses stay expanded; every other provider (OpenRouter
@@ -293,7 +303,8 @@ export function ThreadSettingsSheet(props: {
293303
* "dismiss" = backdrop, grabber, or system back. Hosts only restore the
294304
* keyboard for "save" so a stray tap outside a control never pops it.
295305
*/
296-
readonly onClose: (reason: "save" | "dismiss") => void;
306+
readonly onClose: (reason: ThreadSettingsSheetCloseReason) => void;
307+
readonly onDismissed: () => void;
297308
readonly providerGroups: ReadonlyArray<ProviderGroup>;
298309
readonly selectedModel: ModelSelection | null;
299310
readonly onSelectModel: (option: ModelOption) => void;
@@ -308,18 +319,31 @@ export function ThreadSettingsSheet(props: {
308319
const [expandedProviders, setExpandedProviders] = useState<ReadonlySet<string>>(() => new Set());
309320
const [pendingModel, setPendingModel] = useState<ModelOption | null>(null);
310321
const [submenu, setSubmenu] = useState<SubmenuPage | null>(null);
322+
const wasPresentedRef = useRef(false);
323+
const notifyDismissed = useCallback(() => {
324+
if (!wasPresentedRef.current) {
325+
return;
326+
}
327+
wasPresentedRef.current = false;
328+
props.onDismissed();
329+
}, [props.onDismissed]);
311330

312331
// Every open starts fresh: no staged model, no submenu, legacy hidden,
313332
// secondary providers folded. The sheet stays mounted between opens, so
314333
// state would otherwise stick around.
315334
useEffect(() => {
316335
if (props.visible) {
336+
wasPresentedRef.current = true;
317337
setShowLegacyToggle(false);
318338
setExpandedProviders(new Set());
319339
setPendingModel(null);
320340
setSubmenu(null);
341+
} else if (Platform.OS === "android" && wasPresentedRef.current) {
342+
// React Native only emits Modal.onDismiss on iOS. Android uses no exit
343+
// animation below, so the post-commit effect is its dismissal boundary.
344+
notifyDismissed();
321345
}
322-
}, [props.visible]);
346+
}, [notifyDismissed, props.visible]);
323347

324348
const isApplied = (option: ModelOption) =>
325349
option.selection.instanceId === props.selectedModel?.instanceId &&
@@ -455,8 +479,9 @@ export function ThreadSettingsSheet(props: {
455479
transparent
456480
statusBarTranslucent
457481
navigationBarTranslucent
458-
animationType="fade"
482+
animationType={Platform.OS === "ios" ? "fade" : "none"}
459483
visible={props.visible}
484+
onDismiss={notifyDismissed}
460485
onRequestClose={submenuContent ? () => setSubmenu(null) : () => props.onClose("dismiss")}
461486
>
462487
<View className="flex-1 justify-end">
@@ -538,7 +563,13 @@ export function ThreadSettingsSheet(props: {
538563
onPress={() => {
539564
void Haptics.selectionAsync();
540565
// Re-tapping the applied model cancels staging.
541-
setPendingModel(isApplied(option) ? null : option);
566+
setPendingModel((current) =>
567+
pendingModelAfterPress({
568+
current,
569+
pressed: option,
570+
pressedIsApplied: isApplied(option),
571+
}),
572+
);
542573
}}
543574
/>
544575
))}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { ProviderInstanceId, type ProviderOptionSelection } from "@t3tools/contracts";
4+
5+
import type { ModelOption } from "../../lib/modelOptions";
6+
import { pendingModelAfterPress } from "./thread-settings-sheet-state";
7+
8+
function modelOption(
9+
model: string,
10+
options: ReadonlyArray<ProviderOptionSelection> = [],
11+
): ModelOption {
12+
return {
13+
key: `codex:${model}`,
14+
label: model,
15+
subtitle: "Codex",
16+
providerKey: "codex",
17+
providerLabel: "Codex",
18+
providerDriver: "codex",
19+
isDefault: false,
20+
isLegacy: false,
21+
capabilities: null,
22+
selection: {
23+
instanceId: ProviderInstanceId.make("codex"),
24+
model,
25+
options,
26+
},
27+
};
28+
}
29+
30+
describe("thread settings sheet state", () => {
31+
it("clears staging when the applied model is pressed", () => {
32+
expect(
33+
pendingModelAfterPress({
34+
current: modelOption("gpt-next"),
35+
pressed: modelOption("gpt-current"),
36+
pressedIsApplied: true,
37+
}),
38+
).toBeNull();
39+
});
40+
41+
it("preserves staged options when the highlighted model is pressed again", () => {
42+
const pending = modelOption("gpt-next", [{ id: "effort", value: "high" }]);
43+
44+
expect(
45+
pendingModelAfterPress({
46+
current: pending,
47+
pressed: modelOption("gpt-next"),
48+
pressedIsApplied: false,
49+
}),
50+
).toBe(pending);
51+
});
52+
53+
it("stages a different model", () => {
54+
const pressed = modelOption("gpt-other");
55+
56+
expect(
57+
pendingModelAfterPress({
58+
current: modelOption("gpt-next"),
59+
pressed,
60+
pressedIsApplied: false,
61+
}),
62+
).toBe(pressed);
63+
});
64+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { ModelOption } from "../../lib/modelOptions";
2+
3+
/** Preserve staged provider options when the highlighted model is tapped again. */
4+
export function pendingModelAfterPress(input: {
5+
readonly current: ModelOption | null;
6+
readonly pressed: ModelOption;
7+
readonly pressedIsApplied: boolean;
8+
}): ModelOption | null {
9+
if (input.pressedIsApplied) {
10+
return null;
11+
}
12+
return input.current?.key === input.pressed.key ? input.current : input.pressed;
13+
}

0 commit comments

Comments
 (0)