Files
pipelines/app/lib/features/chat/ui/transcript/chat_transcript_selection.dart
T
alan fa92b9a1a0 Group each transcript row's text into one selectable
The transcript's scroll view kept every text run of every mounted row as
its own selectable and merged newly mounted runs into that list by
comparing global transforms, which dominated slow scroll frames at 4K.
Each row now wraps its content in a SelectionContainer, so the scroll
view orders rows and each row orders only its own runs once on mount.
Cross-row selection and copy order are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 18:21:28 +00:00

204 lines
6.5 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/rendering.dart';
import 'chat_horizontal_viewport.dart';
/// The selection controller shared by all link spans in one transcript.
///
/// A transcript has one [SelectionArea], but links still need to contribute
/// their link-aware context menu item to that area's menu.
class ChatTranscriptSelectionController {
String? menuLink;
}
class ChatTranscriptSelectionScope extends InheritedWidget {
const ChatTranscriptSelectionScope({
super.key,
required this.controller,
required super.child,
});
final ChatTranscriptSelectionController controller;
static ChatTranscriptSelectionController? maybeOf(BuildContext context) =>
context
.dependOnInheritedWidgetOfExactType<ChatTranscriptSelectionScope>()
?.controller;
@override
bool updateShouldNotify(ChatTranscriptSelectionScope oldWidget) =>
controller != oldWidget.controller;
}
/// One native selection document for a transcript. The composer is deliberately
/// outside this widget, so dragging into it cannot extend the transcript copy.
class ChatTranscriptSelectionArea extends StatefulWidget {
const ChatTranscriptSelectionArea({super.key, required this.child});
/// Allows the web-only document to be exercised by VM widget tests.
@visibleForTesting
static bool? debugEnabled;
final Widget child;
@override
State<ChatTranscriptSelectionArea> createState() =>
_ChatTranscriptSelectionAreaState();
}
class _ChatTranscriptSelectionAreaState
extends State<ChatTranscriptSelectionArea> {
final _controller = ChatTranscriptSelectionController();
@override
Widget build(BuildContext context) {
// The shared document is a web-only behavior. On native platforms each
// message keeps its existing SelectionArea and native link menu behavior.
if (!(ChatTranscriptSelectionArea.debugEnabled ?? kIsWeb)) {
return widget.child;
}
final area = ChatTranscriptSelectionScope(
controller: _controller,
child: Listener(
// A new selection starts without a remembered link. Link recognizers
// can set it again on tap-down/long-press; plain text never reuses a
// previous link's Copy link item.
onPointerDown: (_) => _controller.menuLink = null,
child: SelectionArea(
contextMenuBuilder: (context, state) =>
_selectionMenu(_controller, state),
child: widget.child,
),
),
);
return area;
}
Widget _selectionMenu(
ChatTranscriptSelectionController controller,
SelectableRegionState state,
) => AdaptiveTextSelectionToolbar.buttonItems(
anchors: state.contextMenuAnchors,
buttonItems: [
if (controller.menuLink case final link?)
ContextMenuButtonItem(
label: 'Copy link',
onPressed: () {
Clipboard.setData(ClipboardData(text: link));
state.hideToolbar();
},
),
...state.contextMenuButtonItems,
],
);
}
/// 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<ChatTranscriptRowSelection> createState() =>
_ChatTranscriptRowSelectionState();
}
class _ChatTranscriptRowSelectionState
extends State<ChatTranscriptRowSelection> {
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.
/// For a horizontal viewport in a vertical document, a point below-left must
/// mean after, not before. Give the vertical position priority outside its
/// height, while keeping normal horizontal selection and scrolling inside.
class ChatTranscriptHorizontalScrollView extends StatefulWidget {
const ChatTranscriptHorizontalScrollView({
super.key,
required this.child,
this.label = 'content',
});
final Widget child;
final String label;
@override
State<ChatTranscriptHorizontalScrollView> createState() =>
_ChatTranscriptHorizontalScrollViewState();
}
class _ChatTranscriptHorizontalScrollViewState
extends State<ChatTranscriptHorizontalScrollView> {
final _delegate = _HorizontalViewportSelectionDelegate();
@override
void dispose() {
_delegate.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final view = ChatHorizontalViewport(
label: widget.label,
child: widget.child,
);
if (ChatTranscriptSelectionScope.maybeOf(context) == null) return view;
return SelectionContainer(delegate: _delegate, child: view);
}
}
class _HorizontalViewportSelectionDelegate
extends StaticSelectionContainerDelegate {
@override
SelectionResult handleSelectionEdgeUpdate(SelectionEdgeUpdateEvent event) {
final transform = getTransformTo(null);
final local = MatrixUtils.transformPoint(
Matrix4.inverted(transform),
event.globalPosition,
);
if (local.dy < 0 || local.dy > containerSize.height) {
final position = MatrixUtils.transformPoint(
transform,
Offset(containerSize.width / 2, local.dy),
);
event = event.type == SelectionEventType.startEdgeUpdate
? SelectionEdgeUpdateEvent.forStart(
globalPosition: position,
granularity: event.granularity,
)
: SelectionEdgeUpdateEvent.forEnd(
globalPosition: position,
granularity: event.granularity,
);
}
return super.handleSelectionEdgeUpdate(event);
}
}