Fix web transcript cross-message selection
This commit is contained in:
@@ -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