203 lines
7.0 KiB
Dart
203 lines
7.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/material.dart';
|
|
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
|
|
/// claims the press as a selection drag once the pointer moves
|
|
/// [kPrecisePointerPanSlop] (2 px), so an ordinary click with a little hand or
|
|
/// trackpad movement opened nothing. The click still cancels past the tap
|
|
/// slop, and a selection drag that starts on plain text still crosses links.
|
|
/// Touch, stylus and secondary presses keep competing in the arena, so a long
|
|
/// press still selects and offers Copy link.
|
|
class _LinkTapRecognizer extends TapGestureRecognizer {
|
|
@override
|
|
void addAllowedPointer(PointerDownEvent event) {
|
|
super.addAllowedPointer(event);
|
|
if (event.kind == PointerDeviceKind.mouse &&
|
|
event.buttons == kPrimaryMouseButton) {
|
|
resolve(GestureDisposition.accepted);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The link spans of one selectable text block, and the selection area that
|
|
/// hosts them. A tap opens the link. A long press (or a native right-click)
|
|
/// shows the selection menu with "Copy link" added; on the web, where the
|
|
/// browser owns the right-click menu over text, a right-click on a link opens
|
|
/// a "Copy link" menu instead. None of this consumes the text selection
|
|
/// gestures, so links never break selecting a message.
|
|
///
|
|
/// The owner calls [dispose], and rebuilds its spans when [onChanged] fires
|
|
/// (hover emphasis is a paint-only style change, which keeps a selection).
|
|
class ChatLinkSpans {
|
|
ChatLinkSpans({required this.onChanged});
|
|
|
|
/// Tests set this to exercise the web right-click path on the VM.
|
|
@visibleForTesting
|
|
static bool? debugOwnsSecondaryClick;
|
|
|
|
final VoidCallback onChanged;
|
|
final Map<String, _LinkTapRecognizer> _recognizers = {};
|
|
final ContextMenuController _linkMenu = ContextMenuController();
|
|
String? _hovered;
|
|
String? _pressed;
|
|
String? _menuLink;
|
|
bool _disposed = false;
|
|
|
|
bool get _ownsSecondaryClick =>
|
|
debugOwnsSecondaryClick ?? linkOwnsSecondaryClick;
|
|
|
|
/// [label] styled as a link to [target] and opened through [onOpen]. An
|
|
/// unsafe target keeps its label as plain text.
|
|
InlineSpan link(
|
|
BuildContext context,
|
|
String target,
|
|
List<InlineSpan> label, {
|
|
required ValueChanged<Uri> onOpen,
|
|
}) {
|
|
final uri = chatLinkUri(target);
|
|
if (uri == null) return TextSpan(children: label);
|
|
final url = uri.toString();
|
|
final recognizer = _recognizers.putIfAbsent(url, _LinkTapRecognizer.new)
|
|
..onTapDown = (_) {
|
|
_pressed = url;
|
|
ChatTranscriptSelectionScope.maybeOf(context)?.menuLink = url;
|
|
}
|
|
..onTap = () {
|
|
_pressed = null;
|
|
onOpen(uri);
|
|
}
|
|
..onSecondaryTapUp = _ownsSecondaryClick
|
|
? (details) => _showLinkMenu(context, details.globalPosition, url)
|
|
: null;
|
|
// The host's primary colour: a Material app's colour scheme primary, a
|
|
// Cupertino app's tint.
|
|
final color = CupertinoTheme.of(context).primaryColor;
|
|
return _leaves(
|
|
TextSpan(
|
|
style: TextStyle(
|
|
color: color,
|
|
decoration: TextDecoration.underline,
|
|
decorationColor: color,
|
|
decorationThickness: _hovered == url ? 2.5 : 1,
|
|
),
|
|
children: label,
|
|
),
|
|
recognizer,
|
|
url,
|
|
);
|
|
}
|
|
|
|
/// The native [SelectionArea] around [child], with "Copy link" in its menu
|
|
/// when the press that opened the menu was on a link. A Cupertino-only app
|
|
/// 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: transcriptSelection == null
|
|
? SelectionArea(contextMenuBuilder: _selectionMenu, child: child)
|
|
: child,
|
|
);
|
|
if (transcriptSelection != null || !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(BuildContext context, SelectableRegionState state) {
|
|
// A touch long press opens the menu after the link's tap-down recorded it.
|
|
final link = _menuLink ??= _pressed;
|
|
return AdaptiveTextSelectionToolbar.buttonItems(
|
|
anchors: state.contextMenuAnchors,
|
|
buttonItems: [
|
|
if (link != null) _copyItem(link, state.hideToolbar),
|
|
...state.contextMenuButtonItems,
|
|
],
|
|
);
|
|
}
|
|
|
|
void _showLinkMenu(BuildContext context, Offset position, String url) {
|
|
_linkMenu.show(
|
|
context: context,
|
|
contextMenuBuilder: (_) => TapRegion(
|
|
onTapOutside: (_) => _linkMenu.remove(),
|
|
child: AdaptiveTextSelectionToolbar.buttonItems(
|
|
anchors: TextSelectionToolbarAnchors(primaryAnchor: position),
|
|
buttonItems: [_copyItem(url, _linkMenu.remove)],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
ContextMenuButtonItem _copyItem(String url, VoidCallback done) =>
|
|
ContextMenuButtonItem(
|
|
label: 'Copy link',
|
|
onPressed: () {
|
|
Clipboard.setData(ClipboardData(text: url));
|
|
done();
|
|
},
|
|
);
|
|
|
|
void dispose() {
|
|
_disposed = true;
|
|
if (_hovered != null) suppressBrowserMenuOverLink(false);
|
|
_linkMenu.remove();
|
|
for (final recognizer in _recognizers.values) {
|
|
recognizer.dispose();
|
|
}
|
|
}
|
|
|
|
void _hover(String? url) {
|
|
if (_disposed || _hovered == url) return;
|
|
suppressBrowserMenuOverLink(url != null);
|
|
_hovered = url;
|
|
onChanged();
|
|
}
|
|
|
|
/// A `TextSpan` only dispatches a recognizer or hover when it also owns
|
|
/// text, so the link's handlers are copied onto its text leaves. The style
|
|
/// stays on the shared parent.
|
|
InlineSpan _leaves(
|
|
InlineSpan span,
|
|
TapGestureRecognizer recognizer,
|
|
String url,
|
|
) {
|
|
if (span is! TextSpan) return span;
|
|
final children = span.children;
|
|
final leaf = span.text != null;
|
|
return TextSpan(
|
|
text: span.text,
|
|
style: span.style,
|
|
recognizer: leaf ? recognizer : null,
|
|
mouseCursor: leaf ? SystemMouseCursors.click : null,
|
|
onEnter: leaf ? (_) => _hover(url) : null,
|
|
onExit: leaf ? (_) => _hover(null) : null,
|
|
semanticsLabel: span.semanticsLabel,
|
|
children: children == null
|
|
? null
|
|
: [for (final child in children) _leaves(child, recognizer, url)],
|
|
);
|
|
}
|
|
}
|