diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 67706b75..2ee2675e 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -1261,24 +1261,26 @@ class _ChatThreadViewState extends State { // Slivers mount only visible rows. Defer constructing // their chips and checking cached widgets until then, // rather than visiting the entire history per delta. - child: ChatCopyGutter( - width: rows[index]['kind'] == 'assistant' - ? gutter - : 0, - child: Builder( - builder: (_) => _entering( - rows[index], - _transcriptRow( + child: ChatTranscriptRowSelection( + child: ChatCopyGutter( + width: rows[index]['kind'] == 'assistant' + ? gutter + : 0, + child: Builder( + builder: (_) => _entering( rows[index], - keys[index], - onDismissCompaction: - compactionNotice != null && - chatNoticeActivityCondition( - rows[index], - ) == - compactionNotice.condition - ? () => _dismiss(compactionNotice) - : null, + _transcriptRow( + rows[index], + keys[index], + onDismissCompaction: + compactionNotice != null && + chatNoticeActivityCondition( + rows[index], + ) == + compactionNotice.condition + ? () => _dismiss(compactionNotice) + : null, + ), ), ), ), diff --git a/app/lib/features/chat/ui/transcript/chat_transcript_selection.dart b/app/lib/features/chat/ui/transcript/chat_transcript_selection.dart index f7f65cfe..5744b868 100644 --- a/app/lib/features/chat/ui/transcript/chat_transcript_selection.dart +++ b/app/lib/features/chat/ui/transcript/chat_transcript_selection.dart @@ -96,6 +96,43 @@ class _ChatTranscriptSelectionAreaState ); } +/// One transcript row's text, grouped into a single selectable of the +/// transcript document. +/// +/// The transcript's scroll view keeps its selectables in screen order, and +/// re-merges them by comparing global transforms whenever rows scroll in. +/// Without this group every text run of every mounted row is one of those +/// selectables; with it the scroll view orders rows and each row orders only +/// its own runs, once, when it mounts. Selection still spans rows, and copy +/// concatenates rows in the same order as before. +class ChatTranscriptRowSelection extends StatefulWidget { + const ChatTranscriptRowSelection({super.key, required this.child}); + final Widget child; + + @override + State createState() => + _ChatTranscriptRowSelectionState(); +} + +class _ChatTranscriptRowSelectionState + extends State { + final _delegate = StaticSelectionContainerDelegate(); + + @override + void dispose() { + _delegate.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + if (ChatTranscriptSelectionScope.maybeOf(context) == null) { + return widget.child; + } + return SelectionContainer(delegate: _delegate, child: widget.child); + } +} + /// Horizontal code/table viewports remain part of the transcript document. /// Flutter's nested scrollable treats an outside point as before its content /// if either coordinate is negative, and after it if either exceeds its size. diff --git a/app/test/features/chat/conversation/chat_thread_view_test.dart b/app/test/features/chat/conversation/chat_thread_view_test.dart index 1d204d05..15d06608 100644 --- a/app/test/features/chat/conversation/chat_thread_view_test.dart +++ b/app/test/features/chat/conversation/chat_thread_view_test.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; @@ -76,6 +77,32 @@ void main() { tester.getTopLeft(find.text('Second question')).dy, lessThan(tester.getTopLeft(find.text('Last answer.')).dy), ); + // Each row is one selectable of the scroll view's document: its text + // registers with the row's container, so scrolling re-sorts rows + // rather than every text run of the transcript. + SelectionRegistrar? registrar(String text) => + SelectionContainer.maybeOf(tester.element(find.text(text).first)); + SelectionContainerDelegate? rowDelegate(String text) => tester + .widget( + find + .descendant( + of: find.ancestor( + of: find.text(text).first, + matching: find.byType(ChatTranscriptRowSelection), + ), + matching: find.byType(SelectionContainer), + ) + .first, + ) + .delegate; + for (final text in ['Second question', 'Last answer.']) { + expect(registrar(text), isNotNull); + expect(registrar(text), same(rowDelegate(text))); + } + expect( + registrar('Second question'), + isNot(same(registrar('Last answer.'))), + ); final composer = find.descendant( of: find.byKey(const ValueKey('chat-composer-s')), matching: find.byType(EditableText),