diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index 372cac39..38a85e77 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -1004,13 +1004,31 @@ class _ChatComposerBarState extends State { return lines.clamp(1, 3); } + /// The most lines the field grows to before it scrolls: up to + /// [_composerMaxLines], but never more than about 40% of the visible + /// height (the keyboard excluded), so the conversation stays in view. + static const int _composerMaxLines = 12; + + int _maxLines(BuildContext context) { + final media = MediaQuery.of(context); + // The scaffold hides the keyboard from its body; the window still has it. + final keyboard = MediaQueryData.fromView( + View.of(context), + ).viewInsets.bottom; + final visible = media.size.height - keyboard; + final lineHeight = MediaQuery.textScalerOf(context).scale(22); + final fit = (visible * 0.4 / lineHeight).floor(); + return fit.clamp(3, _composerMaxLines); + } + Widget _field(bool cupertino) { + final maxLines = _maxLines(context); if (cupertino) { // CupertinoTextField lays its placeholder out with up to maxLines lines // but sizes an empty field by minLines, so a wrapped placeholder was // clipped on a narrow pane. While empty the field is exactly as tall as // its placeholder (at most 3 lines, then ellipsised); typing restores - // the normal 1-6 lines. + // the normal range (see _maxLines). return LayoutBuilder( builder: (context, constraints) { final empty = _text.text.isEmpty; @@ -1031,7 +1049,7 @@ class _ChatComposerBarState extends State { minLines: lines, // Keep the empty field multiline too: a single-line web editor // strips Shift+Enter's newline on the next text edit. - maxLines: empty && lines > 1 ? lines : 6, + maxLines: empty && lines > 1 ? lines : maxLines, keyboardType: TextInputType.multiline, textInputAction: _enterSends ? TextInputAction.send @@ -1048,7 +1066,7 @@ class _ChatComposerBarState extends State { controller: _text, focusNode: _focus, minLines: 1, - maxLines: 6, + maxLines: maxLines, keyboardType: TextInputType.multiline, textInputAction: _enterSends ? TextInputAction.send 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 new file mode 100644 index 00000000..7a2b9e16 --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_max_lines_test.dart @@ -0,0 +1,43 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'chat_composer_bar_fixtures.dart'; + +/// The composer grows to 12 lines before it scrolls, but never past about +/// 40% of the visible height (the keyboard excluded). +void main() { + int? maxLinesOf(WidgetTester tester, bool cupertino) { + final field = find.byKey(const Key('chat-composer-field')); + return cupertino + ? tester.widget(field).maxLines + : tester.widget(field).maxLines; + } + + for (final cupertino in [false, true]) { + final host = cupertino ? 'Cupertino' : 'Material'; + testWidgets('$host: a tall window allows 12 lines; a phone with its ' + 'keyboard open fewer', (tester) async { + final gateway = ComposerBarGateway()..page = fixtureTurn(); + addTearDown(gateway.close); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + + tester.view.physicalSize = const Size(900, 1000); + await tester.pumpWidget(composerThread(gateway, cupertino: cupertino)); + await tester.pump(); + // A non-empty field (the Cupertino placeholder sizing only applies + // while empty). + await tester.enterText(find.byKey(const Key('chat-composer-field')), 'x'); + await tester.pump(); + expect(maxLinesOf(tester, cupertino), 12); + + tester.view.physicalSize = const Size(390, 844); + tester.view.viewInsets = const FakeViewPadding(bottom: 340); + await tester.pump(); + final phone = maxLinesOf(tester, cupertino)!; + expect(phone, lessThan(12)); + expect(phone, greaterThan(6)); + }); + } +}