Files
pipelines/app/lib/features/chat/ui/transcript/chat_transcript_selection.dart
T
alan fbe1928f59 Remove Cupertino chat transcript branches
Keep the existing Material transcript path while preserving public Cupertino parameters for callers. Narrow transcript tests to the Material-only behavior and remove Cupertino-only fixtures.
2026-11-11 11:46:13 +00:00

167 lines
5.3 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,
],
);
}
/// 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);
}
}