diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 38a85e77..4ea73202 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -138,6 +138,10 @@ class _ChatComposerBarState extends State { text: widget.composer.draft, ); final FocusNode _focus = FocusNode(debugLabel: 'chat-composer'); + + /// The field's own scroll position, so a new last line is brought into + /// view as soon as it exists (see [_revealCaretLine]). + final ScrollController _fieldScroll = ScrollController(); late final ComposerFocusRestorer _focusRestorer; final ChatAttachmentDraft _attachments = ChatAttachmentDraft(); @@ -284,6 +288,22 @@ class _ChatComposerBarState extends State { if (_selectedCommand >= suggestions.length) _selectedCommand = 0; }); unawaited(widget.composer.setDraft(value)); + _revealCaretLine(value); + } + + /// At the field's line limit, a newline at the end leaves the caret on a + /// new empty line the field only scrolls to once it has a character. + /// Scroll there right away whenever the caret is at the end. + void _revealCaretLine(String value) { + final caret = _text.selection; + if (!caret.isCollapsed || caret.baseOffset != value.length) return; + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted || !_fieldScroll.hasClients) return; + final position = _fieldScroll.position; + if (position.pixels < position.maxScrollExtent) { + _fieldScroll.jumpTo(position.maxScrollExtent); + } + }); } KeyEventResult _onKey(FocusNode node, KeyEvent event) { @@ -620,6 +640,7 @@ class _ChatComposerBarState extends State { _focusRestorer.dispose(); _text.dispose(); _focus.dispose(); + _fieldScroll.dispose(); super.dispose(); } @@ -1039,6 +1060,7 @@ class _ChatComposerBarState extends State { key: const Key('chat-composer-field'), controller: _text, focusNode: _focus, + scrollController: _fieldScroll, placeholder: widget.placeholder, // The pill draws the field's surface; the field adds no border. decoration: null, @@ -1065,6 +1087,7 @@ class _ChatComposerBarState extends State { key: const Key('chat-composer-field'), controller: _text, focusNode: _focus, + scrollController: _fieldScroll, minLines: 1, maxLines: maxLines, keyboardType: TextInputType.multiline, diff --git a/app/test/features/chat/conversation/chat_composer_max_lines_test.dart b/app/test/features/chat/conversation/chat_composer_max_lines_test.dart index 7a2b9e16..6100fbbc 100644 --- a/app/test/features/chat/conversation/chat_composer_max_lines_test.dart +++ b/app/test/features/chat/conversation/chat_composer_max_lines_test.dart @@ -40,4 +40,31 @@ void main() { expect(phone, greaterThan(6)); }); } + + for (final cupertino in [false, true]) { + final host = cupertino ? 'Cupertino' : 'Material'; + testWidgets('$host: a newline at the line limit scrolls to the new ' + 'empty line at once', (tester) async { + final gateway = ComposerBarGateway()..page = fixtureTurn(); + addTearDown(gateway.close); + tester.view.devicePixelRatio = 1; + tester.view.physicalSize = const Size(900, 1000); + addTearDown(tester.view.reset); + await tester.pumpWidget(composerThread(gateway, cupertino: cupertino)); + await tester.pump(); + final field = find.byKey(const Key('chat-composer-field')); + // Past the 12-line limit, then a newline at the end: no character + // typed on the new line yet. + final text = List.generate(14, (i) => 'line $i').join('\n'); + await tester.enterText(field, '$text\n'); + await tester.pump(); + await tester.pump(); + final scrollable = tester.state( + find.descendant(of: field, matching: find.byType(Scrollable)).first, + ); + final position = scrollable.position; + expect(position.maxScrollExtent, greaterThan(0)); + expect(position.pixels, moreOrLessEquals(position.maxScrollExtent)); + }); + } }