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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<ChatMarkdownView> {
|
||||
),
|
||||
);
|
||||
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(
|
||||
|
||||
@@ -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<RenderBox> cells, double containerWidth) =>
|
||||
_content.maxIntrinsicWidth(cells, containerWidth);
|
||||
|
||||
@override
|
||||
double minIntrinsicWidth(Iterable<RenderBox> 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<RenderBox> cells) => null;
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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<void> 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));
|
||||
});
|
||||
}
|
||||
@@ -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)],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user