Fix web transcript cross-message selection
This commit is contained in:
@@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
|
||||
import '../../../../ui/adaptive/host.dart';
|
||||
import 'chat_link_browser_menu.dart';
|
||||
import 'chat_links.dart';
|
||||
import 'chat_transcript_selection.dart';
|
||||
|
||||
/// A link's tap recognizer. A primary mouse press on a link belongs to the
|
||||
/// link, as a press on a web page's anchor does: otherwise the selection area
|
||||
@@ -67,6 +68,7 @@ class ChatLinkSpans {
|
||||
final recognizer = _recognizers.putIfAbsent(url, _LinkTapRecognizer.new)
|
||||
..onTapDown = (_) {
|
||||
_pressed = url;
|
||||
ChatTranscriptSelectionScope.maybeOf(context)?.menuLink = url;
|
||||
}
|
||||
..onTap = () {
|
||||
_pressed = null;
|
||||
@@ -98,14 +100,20 @@ class ChatLinkSpans {
|
||||
/// has no `MaterialLocalizations`, which `SelectionArea` asserts on, so one
|
||||
/// English default localization scope is added there for its context menu.
|
||||
Widget selectionArea(BuildContext context, Widget child) {
|
||||
final transcriptSelection = ChatTranscriptSelectionScope.maybeOf(context);
|
||||
final area = Listener(
|
||||
onPointerDown: (event) {
|
||||
_menuLink = event.buttons == kSecondaryMouseButton ? _hovered : null;
|
||||
transcriptSelection?.menuLink = _menuLink;
|
||||
_pressed = null;
|
||||
},
|
||||
child: SelectionArea(contextMenuBuilder: _selectionMenu, child: child),
|
||||
child: transcriptSelection == null
|
||||
? SelectionArea(contextMenuBuilder: _selectionMenu, child: child)
|
||||
: child,
|
||||
);
|
||||
if (!usesCupertinoHost(context)) return area;
|
||||
if (transcriptSelection != null || !usesCupertinoHost(context)) {
|
||||
return area;
|
||||
}
|
||||
return Localizations(
|
||||
locale: Localizations.maybeLocaleOf(context) ?? const Locale('en'),
|
||||
delegates: const [
|
||||
|
||||
@@ -5,11 +5,12 @@ import 'package:flutter/material.dart';
|
||||
import 'chat_link_launcher.dart';
|
||||
import 'chat_link_spans.dart';
|
||||
import 'chat_links.dart';
|
||||
import 'chat_transcript_selection.dart';
|
||||
|
||||
/// Plain selectable text with its bare links made tappable: the operator's
|
||||
/// own message keeps every character exactly as typed (no markdown), and only
|
||||
/// `http(s)://`/`www.` runs outside `` `code` `` become links. Text without a
|
||||
/// link stays a plain [SelectableText].
|
||||
/// link stays a plain [SelectableText] outside a shared transcript document.
|
||||
class ChatLinkText extends StatefulWidget {
|
||||
const ChatLinkText(this.text, {super.key});
|
||||
|
||||
@@ -40,7 +41,11 @@ class _ChatLinkTextState extends State<ChatLinkText> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final text = widget.text;
|
||||
if (_found.isEmpty) return SelectableText(text);
|
||||
if (_found.isEmpty) {
|
||||
return ChatTranscriptSelectionScope.maybeOf(context) == null
|
||||
? SelectableText(text)
|
||||
: Text(text);
|
||||
}
|
||||
final spans = <InlineSpan>[];
|
||||
var at = 0;
|
||||
for (final link in _found) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
|
||||
import '../../../../ui/layout/wheel_scroll_forwarding.dart';
|
||||
import 'chat_transcript_selection.dart';
|
||||
import '../../../../ui/text/monospace.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'chat_link_launcher.dart';
|
||||
@@ -170,7 +170,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
),
|
||||
);
|
||||
case MarkdownTable(header: final header, rows: final rows):
|
||||
return HorizontalScrollView(
|
||||
return ChatTranscriptHorizontalScrollView(
|
||||
child: Table(
|
||||
defaultColumnWidth: const IntrinsicColumnWidth(),
|
||||
border: TableBorder.all(color: _dividerColor(context, cupertino)),
|
||||
@@ -210,7 +210,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
decoration: BoxDecoration(color: _codeFill(context, cupertino)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: HorizontalScrollView(
|
||||
child: ChatTranscriptHorizontalScrollView(
|
||||
child: Text(
|
||||
code,
|
||||
style: const TextStyle(fontFamily: appMonospaceFamily),
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
import '../../../../ui/adaptive/host.dart';
|
||||
import '../../../../ui/layout/wheel_scroll_forwarding.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,
|
||||
),
|
||||
),
|
||||
);
|
||||
if (!usesCupertinoHost(context)) return area;
|
||||
return Localizations(
|
||||
locale: Localizations.maybeLocaleOf(context) ?? const Locale('en'),
|
||||
delegates: const [
|
||||
DefaultWidgetsLocalizations.delegate,
|
||||
DefaultMaterialLocalizations.delegate,
|
||||
DefaultCupertinoLocalizations.delegate,
|
||||
],
|
||||
child: 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});
|
||||
final Widget child;
|
||||
|
||||
@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 = HorizontalScrollView(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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user