From 93e9042a8b54e563f501a0cb056b0a4faad11134 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 19 Nov 2026 10:15:12 +0000 Subject: [PATCH] Wrap chat markdown tables to the transcript width Tables no longer scroll sideways: columns size to content and shrink to fit, wrapping cell text; an unbreakable token breaks inside its cell. Co-Authored-By: Claude Opus 5.5 --- .../ui/transcript/chat_markdown_view.dart | 33 ++++++------ .../transcript/chat_table_column_width.dart | 33 ++++++++++++ .../chat_thread_wheel_scroll_test.dart | 5 +- .../chat_horizontal_viewport_test.dart | 11 ++-- .../chat_markdown_table_wrap_test.dart | 54 +++++++++++++++++++ app/tool/chat_table_wrap_probe.dart | 40 ++++++++++++++ 6 files changed, 154 insertions(+), 22 deletions(-) create mode 100644 app/lib/features/chat/ui/transcript/chat_table_column_width.dart create mode 100644 app/test/features/chat/ui/transcript/chat_markdown_table_wrap_test.dart create mode 100644 app/tool/chat_table_wrap_probe.dart diff --git a/app/lib/features/chat/ui/transcript/chat_markdown_view.dart b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart index af2862d6..e1c0fcdf 100644 --- a/app/lib/features/chat/ui/transcript/chat_markdown_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart @@ -11,13 +11,14 @@ import 'chat_link_spans.dart'; import 'chat_markdown.dart'; import 'chat_markdown_cache.dart'; import 'chat_message_bubble.dart'; +import 'chat_table_column_width.dart'; /// Renders one assistant text part with native text widgets and the host's own /// theme roles. Links (`http`, `https`, `mailto` and `www.` hosts only) open /// through [onOpenLink] when given, else the [ChatLinkScope] launcher, which by /// default opens them outside the app. No font family, size, colour or component /// dimension is overridden; code keeps its exact whitespace and scrolls -/// sideways instead of widening the transcript. +/// sideways instead of widening the transcript, while table cells wrap. class ChatMarkdownView extends StatefulWidget { const ChatMarkdownView({super.key, required this.text, this.onOpenLink}); @@ -176,24 +177,22 @@ class _ChatMarkdownViewState extends State { ), ); case MarkdownTable(header: final header, rows: final rows): - return ChatTranscriptHorizontalScrollView( - label: 'table', - child: Table( - defaultColumnWidth: const IntrinsicColumnWidth(), - border: TableBorder.all(color: _dividerColor(context)), - children: [ + // Cells wrap to the transcript width; the table never scrolls sideways. + return Table( + defaultColumnWidth: ChatTableColumnWidth(header.length), + border: TableBorder.all(color: _dividerColor(context)), + children: [ + TableRow( + decoration: BoxDecoration(color: _headerFill(context)), + children: [ + for (final cell in header) _cell(context, cell, header: true), + ], + ), + for (final row in rows) TableRow( - decoration: BoxDecoration(color: _headerFill(context)), - children: [ - for (final cell in header) _cell(context, cell, header: true), - ], + children: [for (final cell in row) _cell(context, cell)], ), - for (final row in rows) - TableRow( - children: [for (final cell in row) _cell(context, cell)], - ), - ], - ), + ], ); case MarkdownCode(language: final language, code: final code): final labelStyle = _roleStyle( diff --git a/app/lib/features/chat/ui/transcript/chat_table_column_width.dart b/app/lib/features/chat/ui/transcript/chat_table_column_width.dart new file mode 100644 index 00000000..cc2d1309 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_table_column_width.dart @@ -0,0 +1,33 @@ +import 'dart:math' as math; + +import 'package:flutter/widgets.dart'; + +/// Sizes a markdown table column to its content but lets it wrap to fit the +/// transcript instead of scrolling sideways. +/// +/// A column asks for its widest line ([IntrinsicColumnWidth]). When the table +/// is wider than its container, [Table] shrinks columns towards their minimum, +/// which is normally the longest single word. That floor is capped at an equal +/// share of the container, so a long identifier or URL breaks inside its cell +/// rather than pushing the table past the edge. +class ChatTableColumnWidth extends TableColumnWidth { + const ChatTableColumnWidth(this.columnCount); + + final int columnCount; + + static const _content = IntrinsicColumnWidth(); + + @override + double maxIntrinsicWidth(Iterable cells, double containerWidth) => + _content.maxIntrinsicWidth(cells, containerWidth); + + @override + double minIntrinsicWidth(Iterable cells, double containerWidth) { + final words = _content.minIntrinsicWidth(cells, containerWidth); + if (!containerWidth.isFinite || columnCount < 1) return words; + return math.min(words, containerWidth / columnCount); + } + + @override + double? flex(Iterable cells) => null; +} diff --git a/app/test/features/chat/conversation/chat_thread_wheel_scroll_test.dart b/app/test/features/chat/conversation/chat_thread_wheel_scroll_test.dart index 4ba0719d..6acc2a24 100644 --- a/app/test/features/chat/conversation/chat_thread_wheel_scroll_test.dart +++ b/app/test/features/chat/conversation/chat_thread_wheel_scroll_test.dart @@ -11,7 +11,7 @@ import 'chat_wheel_fixtures.dart'; // a long transcript is split at an anchor (offsets above it are negative); a // short one with a tall newest row stays top-anchored. -/// The horizontal scrollable inside (a code block) or around (a table) [target]. +/// The horizontal scrollable inside or around [target]. ScrollPosition _horizontal(WidgetTester tester, Finder target) { final scrollables = [ find.descendant(of: target.first, matching: find.byType(Scrollable)), @@ -148,7 +148,8 @@ void main() { } } - for (final kind in [ChatWheelKind.wide, ChatWheelKind.table]) { + // Tables wrap to the transcript width, so only wide code scrolls sideways. + for (final kind in [ChatWheelKind.wide]) { testWidgets('a sideways gesture or Shift + wheel over $kind scrolls it, ' 'not the transcript', (tester) async { final gateway = FakeChatGateway(); diff --git a/app/test/features/chat/ui/transcript/chat_horizontal_viewport_test.dart b/app/test/features/chat/ui/transcript/chat_horizontal_viewport_test.dart index c3e0450b..bc989cba 100644 --- a/app/test/features/chat/ui/transcript/chat_horizontal_viewport_test.dart +++ b/app/test/features/chat/ui/transcript/chat_horizontal_viewport_test.dart @@ -149,7 +149,7 @@ void main() { }, ); - testWidgets('code and table labels identify the scrollable region', ( + testWidgets('a code block scrolls sideways while a table wraps', ( tester, ) async { await tester.pumpWidget( @@ -167,9 +167,14 @@ void main() { ); expect( find.bySemanticsLabel('Horizontally scrollable table'), - findsOneWidget, + findsNothing, + ); + expect(find.text('Scroll horizontally'), findsOneWidget); + final viewport = tester.getRect(find.byType(ChatMarkdownView)); + expect( + tester.getRect(find.byType(Table)).right, + lessThanOrEqualTo(viewport.right), ); - expect(find.text('Scroll horizontally'), findsNWidgets(2)); expect(tester.takeException(), isNull); }); } diff --git a/app/test/features/chat/ui/transcript/chat_markdown_table_wrap_test.dart b/app/test/features/chat/ui/transcript/chat_markdown_table_wrap_test.dart new file mode 100644 index 00000000..94750bb0 --- /dev/null +++ b/app/test/features/chat/ui/transcript/chat_markdown_table_wrap_test.dart @@ -0,0 +1,54 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart'; + +import 'chat_markdown_test_support.dart'; + +void main() { + Future pumpTable(WidgetTester tester, String text) async { + await phoneViewport(tester); + await tester.pumpWidget(markdownHost(ChatMarkdownView(text: text))); + await tester.pumpAndSettle(); + expect(tester.takeException(), isNull); + } + + Rect tableRect(WidgetTester tester) => tester.getRect(find.byType(Table)); + // The phone viewport is 390 logical pixels wide. + const screenRight = 390.0; + + testWidgets('a wide table wraps its cells inside the transcript', ( + tester, + ) async { + await pumpTable( + tester, + '| Item | Description | Status |\n| --- | --- | --- |\n' + '| one | ${List.filled(30, 'word').join(' ')} | done |', + ); + expect( + find.descendant( + of: find.byType(ChatMarkdownView), + matching: find.byType(Scrollable), + ), + findsNothing, + ); + expect(tableRect(tester).right, lessThanOrEqualTo(screenRight)); + final description = tester.getSize(find.textContaining('word word')); + final status = tester.getSize(find.text('done')); + // The long cell wrapped onto several lines; the short one did not. + expect(description.height, greaterThan(status.height * 2)); + }); + + testWidgets('an unbreakable token breaks inside its cell', (tester) async { + await pumpTable( + tester, + '| Path | Note |\n| --- | --- |\n' + '| ${'a' * 200} | ${'b' * 200} |', + ); + expect(tableRect(tester).right, lessThanOrEqualTo(screenRight)); + }); + + testWidgets('a narrow table keeps its content width', (tester) async { + await pumpTable(tester, '| a | b |\n| --- | --- |\n| 1 | 2 |'); + expect(tableRect(tester).width, lessThan(screenRight / 2)); + }); +} diff --git a/app/tool/chat_table_wrap_probe.dart b/app/tool/chat_table_wrap_probe.dart new file mode 100644 index 00000000..cdce037a --- /dev/null +++ b/app/tool/chat_table_wrap_probe.dart @@ -0,0 +1,40 @@ +import 'package:flutter/material.dart'; + +import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart'; + +const _sample = ''' +A wide table wraps its cells to the transcript width instead of scrolling: + +| Stage | What it does | Result | Owner | +| --- | --- | --- | --- | +| implement | Writes the focused change in its own worktree and runs the area tests before handing off to review | passed | Luna | +| review | Reads the actual diff, checks regressions, and asks for fixes when the change misses the requested behaviour | changes requested | reviewer | +| land | Merges into main after checks, then verifies ancestry | pending | manager | + +A long token breaks inside its cell: + +| Path | Note | +| --- | --- | +| app/lib/features/chat/ui/transcript/chat_markdown_view_with_a_very_long_name.dart | short | + +A narrow table keeps its content width: + +| a | b | +| --- | --- | +| 1 | 2 | +'''; + +/// Markdown tables at the window width, for a wrap capture. +void main() { + runApp( + MaterialApp( + theme: ThemeData(brightness: Brightness.dark), + home: Scaffold( + body: ListView( + padding: const EdgeInsets.all(16), + children: const [ChatMarkdownView(text: _sample)], + ), + ), + ), + ); +}