fix(mobile): close the mention chooser while the composer has no focus

With the chooser open for `@bo`, dragging the keyboard away collapsed
the composer to one line but left the chooser row floating above it
(scenario 09). The chooser now follows the field's focus, as buzz-app's
does (recommendation 8): it hides whenever the text field loses focus,
through the keyboard-dismiss drag, the Android back key, a tap elsewhere
or Shift+Tab traversal, and comes back when focus returns.

The draft, the live query and its rows are untouched while hidden. The
composer keeps watching the same candidate and search providers, so a
return to the field shows the same rows in the same order without a new
directory search, and a search that was running finishes into the hidden
rows. The `#channel` chooser lives in the same panel and follows focus
the same way. Hardware Tab and Enter accept a row only while the panel
is shown. A tap on a chooser row is a touch outside the field, which
takes no focus on mobile, so a row still inserts its pick, the search
retry still runs, and the outside-channel prompt is unchanged. The edit
sheet is unchanged.

The composer widget had passed the mobile file-size ceiling, so the
expansion spring and the iOS clipboard probe move to `helpers.dart` as
`_useComposerExpansionMotion` and `_useComposerClipboardImage`.

Tests: the search journey opens `@bo`, unfocuses the field and sees the
chooser gone with the draft and caret kept, refocuses and sees Bob then
Bobby again with no new search, taps Bob with focus kept and sends him
tagged, then checks `#cur` the same way; the Shift+Tab test now expects
the chooser to leave with focus and return with the query. The journey
fails when the focus gate is removed. `dart format`, `flutter analyze`,
`just file-size-check` and the full mobile `flutter test` suite pass;
one run hit the known community snapshot timing flake, which passes
solo.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
This commit is contained in:
Matt Toohey
2026-11-18 15:42:02 +11:00
parent 38e8f2e1e0
commit e008151c6c
4 changed files with 190 additions and 62 deletions

View File

@@ -187,64 +187,18 @@ class ComposeBar extends HookConsumerWidget {
final resolvedHint =
hintText ??
(channelName.isNotEmpty ? 'Message #$channelName' : 'Message\u2026');
useEffect(
() {
final target =
isComposerExpanded.value && androidImeTransitionStarted.value
? 1.0
: 0.0;
if (reducedMotion) {
composerExpansionController.value = target;
} else if ((composerExpansionController.value - target).abs() > 0.001) {
composerExpansionController.animateWith(
SpringSimulation(
SpringDescription.withDurationAndBounce(
duration: const Duration(milliseconds: 220),
bounce: 0.08,
),
composerExpansionController.value,
target,
0,
snapToEnd: true,
),
);
}
return null;
},
[
isComposerExpanded.value,
androidImeTransitionStarted.value,
reducedMotion,
],
_useComposerExpansionMotion(
controller: composerExpansionController,
isExpanded: isComposerExpanded,
androidImeTransitionStarted: androidImeTransitionStarted,
reducedMotion: reducedMotion,
);
_useComposerClipboardImage(
context: context,
ref: ref,
focusNode: focusNode,
clipboardHasImage: clipboardHasImage,
);
useEffect(() {
if (defaultTargetPlatform != TargetPlatform.iOS) return null;
var disposed = false;
Future<void> refreshClipboardAvailability() async {
final hasImage = await ref
.read(mediaUploadServiceProvider)
.clipboardHasImage();
if (!disposed && context.mounted) {
clipboardHasImage.value = hasImage;
}
}
void refreshWhenFocused() {
if (focusNode.hasFocus) refreshClipboardAvailability();
}
final lifecycleListener = AppLifecycleListener(
onResume: refreshClipboardAvailability,
);
focusNode.addListener(refreshWhenFocused);
refreshClipboardAvailability();
return () {
disposed = true;
focusNode.removeListener(refreshWhenFocused);
lifecycleListener.dispose();
};
}, [focusNode]);
// Mention state --------------------------------------------------------
final mentionQuery = useState<String?>(null);
@@ -1028,9 +982,16 @@ class ComposeBar extends HookConsumerWidget {
);
final readOnly = interactionLocked || (draftMembers != null && sending);
// The chooser belongs to the focused field (recommendation 8). It hides
// while the field has no focus, after the keyboard is dismissed or a tap
// lands elsewhere, and comes back unchanged when focus returns: the query,
// its rows and its search are kept, only the panel is withheld. A tap on
// a row is a touch outside the field, which never takes its focus.
final chooserOpen = useListenable(focusNode).hasFocus;
// Matches what _composerSuggestionPanel renders, and only while the field
// can be edited.
final pickerVisible =
chooserOpen &&
(channelSuggestions.isNotEmpty ||
suggestions.isNotEmpty ||
mentionRows.searchFailed) &&
@@ -1061,10 +1022,10 @@ class ComposeBar extends HookConsumerWidget {
);
final suggestionPanel = _composerSuggestionPanel(
channelSuggestions: channelSuggestions,
mentionSuggestions: suggestions,
channelSuggestions: chooserOpen ? channelSuggestions : const [],
mentionSuggestions: chooserOpen ? suggestions : const [],
unavailableMentions: mentionRows.unavailable,
mentionSearchFailed: mentionRows.searchFailed,
mentionSearchFailed: chooserOpen && mentionRows.searchFailed,
reserveMentionHeight: mentionRows.reserveHeight,
mentionSearching: mentionRows.searching,
onMentionSearchRetry: () {

View File

@@ -51,6 +51,76 @@ void _useComposerChannelNames(
}, [controller, channelNamesKey]);
}
// Springs the composer between its capsule and expanded shapes. On Android
// the expansion waits for the IME transition to start.
void _useComposerExpansionMotion({
required AnimationController controller,
required ValueNotifier<bool> isExpanded,
required ValueNotifier<bool> androidImeTransitionStarted,
required bool reducedMotion,
}) {
useEffect(() {
final target = isExpanded.value && androidImeTransitionStarted.value
? 1.0
: 0.0;
if (reducedMotion) {
controller.value = target;
} else if ((controller.value - target).abs() > 0.001) {
controller.animateWith(
SpringSimulation(
SpringDescription.withDurationAndBounce(
duration: const Duration(milliseconds: 220),
bounce: 0.08,
),
controller.value,
target,
0,
snapToEnd: true,
),
);
}
return null;
}, [isExpanded.value, androidImeTransitionStarted.value, reducedMotion]);
}
// iOS only: whether the clipboard holds an image, for the paste action.
// Re-checked when the field takes focus and when the app resumes.
void _useComposerClipboardImage({
required BuildContext context,
required WidgetRef ref,
required FocusNode focusNode,
required ValueNotifier<bool> clipboardHasImage,
}) {
useEffect(() {
if (defaultTargetPlatform != TargetPlatform.iOS) return null;
var disposed = false;
Future<void> refreshClipboardAvailability() async {
final hasImage = await ref
.read(mediaUploadServiceProvider)
.clipboardHasImage();
if (!disposed && context.mounted) {
clipboardHasImage.value = hasImage;
}
}
void refreshWhenFocused() {
if (focusNode.hasFocus) refreshClipboardAvailability();
}
final lifecycleListener = AppLifecycleListener(
onResume: refreshClipboardAvailability,
);
focusNode.addListener(refreshWhenFocused);
refreshClipboardAvailability();
return () {
disposed = true;
focusNode.removeListener(refreshWhenFocused);
lifecycleListener.dispose();
};
}, [focusNode]);
}
const _typingThrottleMs = 3000;
/// How long a keyboard that was up before an inactive spell may take to come

View File

@@ -2826,15 +2826,26 @@ void main() {
});
testWidgets('Shift+Tab does not accept a mention', (tester) async {
final controller = await pumpComposer(tester);
final focus = FocusNode();
addTearDown(focus.dispose);
final controller = await pumpComposer(tester, focusNode: focus);
await tester.enterText(find.byType(TextField), 'hi @a');
await tester.pump();
expect(popover(), findsOneWidget);
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.pump();
await tester.pumpAndSettle();
// Shift+Tab traverses focus away from the field, so the chooser
// follows it; the query stays live and the chooser returns with it.
expect(controller.text, 'hi @a');
expect(focus.hasFocus, isFalse);
expect(popover(), findsNothing);
await tester.tap(find.text('hi @a'));
await tester.pumpAndSettle();
expect(focus.hasFocus, isTrue);
expect(controller.text, 'hi @a');
expect(popover(), findsOneWidget);
await tester.pump(mentionSearchDebounce * 2);

View File

@@ -4,6 +4,7 @@ part of '../compose_bar_test.dart';
/// name someone (buzz-app `src/bundled/mentions/README.md`, sections 2 and
/// 6; project recommendation 5). Prose typed after a picked chip, and a
/// query a finished search has already refuted, never leave the device.
/// The chooser itself follows the field's focus (recommendation 8).
void mentionSearchTests() {
final bob = 'b' * 64;
final alice = 'a' * 64;
@@ -185,4 +186,89 @@ void mentionSearchTests() {
expect(searched, ['z']);
},
);
testWidgets('the chooser follows the field\'s focus and keeps its rows', (
tester,
) async {
final searched = <String>[];
final focusNode = FocusNode();
addTearDown(focusNode.dispose);
String? sentText;
List<String>? sent;
await tester.pumpWidget(
_buildComposeBar(
uploadService: _testUploadService(nostr.Keys.generate().nsec),
currentPubkey: nostr.Keys.generate().public,
members: members,
channels: [_makeCurrentChannel()],
focusNode: focusNode,
searchPeople: (query) async {
searched.add(query);
return fold(bobby.displayName!).startsWith(fold(query))
? [bobby]
: const [];
},
onSend: (text, keys, {mediaTags = const <List<String>>[]}) async {
sentText = text;
sent = keys;
},
),
);
await _expandComposer(tester);
final controller = tester.widget<TextField>(field).controller!;
final channelChooser = find.byKey(const ValueKey('channel-suggestions'));
void expectBobThenBobby() {
expect(row('Bob'), findsOneWidget);
expect(row('Bobby'), findsOneWidget);
expect(
tester.getTopLeft(row('Bob')).dy,
lessThan(tester.getTopLeft(row('Bobby')).dy),
);
}
// `@bo` lists the member Bob above the directory's Bobby.
await tester.enterText(field, 'hi @bo');
await pause(tester);
expect(searched, ['bo']);
expectBobThenBobby();
// The keyboard dismissed, or a tap elsewhere, takes the field's focus:
// the chooser closes while the draft and its live query stay.
focusNode.unfocus();
await tester.pumpAndSettle();
expect(popover, findsNothing);
expect(controller.text, 'hi @bo');
expect(controller.selection.baseOffset, 6);
// Focus returning shows the same rows again without a new search.
await tester.tap(find.text('hi @bo'));
await tester.pumpAndSettle();
expect(focusNode.hasFocus, isTrue);
expectBobThenBobby();
expect(searched, ['bo']);
// A tap on a row is not a tap away: it inserts the pick and keeps focus.
await tester.tap(row('Bob'));
await tester.pumpAndSettle();
expect(controller.text, 'hi @Bob ');
expect(focusNode.hasFocus, isTrue);
expect(popover, findsNothing);
await tester.tap(find.byIcon(BuzzIcons.arrowUp));
await tester.pumpAndSettle();
expect(sentText, 'hi @Bob');
expect(sent, [bob]);
// The channel chooser follows focus the same way.
await _expandComposer(tester);
await tester.enterText(field, '#cur');
await tester.pumpAndSettle();
expect(channelChooser, findsOneWidget);
focusNode.unfocus();
await tester.pumpAndSettle();
expect(channelChooser, findsNothing);
await tester.tap(find.text('#cur'));
await tester.pumpAndSettle();
expect(channelChooser, findsOneWidget);
expect(controller.text, '#cur');
});
}