diff --git a/mobile/lib/features/channels/compose_bar/compose_bar_widget.dart b/mobile/lib/features/channels/compose_bar/compose_bar_widget.dart index ee6f578497..677acc3bd1 100644 --- a/mobile/lib/features/channels/compose_bar/compose_bar_widget.dart +++ b/mobile/lib/features/channels/compose_bar/compose_bar_widget.dart @@ -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 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(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: () { diff --git a/mobile/lib/features/channels/compose_bar/helpers.dart b/mobile/lib/features/channels/compose_bar/helpers.dart index f1160f835d..d387e33656 100644 --- a/mobile/lib/features/channels/compose_bar/helpers.dart +++ b/mobile/lib/features/channels/compose_bar/helpers.dart @@ -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 isExpanded, + required ValueNotifier 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 clipboardHasImage, +}) { + useEffect(() { + if (defaultTargetPlatform != TargetPlatform.iOS) return null; + + var disposed = false; + Future 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 diff --git a/mobile/test/features/channels/compose_bar_test.dart b/mobile/test/features/channels/compose_bar_test.dart index 8c644e5f26..c4db980fc2 100644 --- a/mobile/test/features/channels/compose_bar_test.dart +++ b/mobile/test/features/channels/compose_bar_test.dart @@ -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); diff --git a/mobile/test/features/channels/compose_bar_test/mention_search_tests.dart b/mobile/test/features/channels/compose_bar_test/mention_search_tests.dart index b89404f9ec..f956f1665a 100644 --- a/mobile/test/features/channels/compose_bar_test/mention_search_tests.dart +++ b/mobile/test/features/channels/compose_bar_test/mention_search_tests.dart @@ -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 = []; + final focusNode = FocusNode(); + addTearDown(focusNode.dispose); + String? sentText; + List? 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 >[]}) async { + sentText = text; + sent = keys; + }, + ), + ); + await _expandComposer(tester); + final controller = tester.widget(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'); + }); }