Chat links: a mouse click opens the link even when the pointer drifts

The selection area claimed a primary mouse press on a link as a selection
drag once the pointer moved 2 px, so in the browser ordinary clicks with a
little hand or trackpad movement opened nothing. A link's recognizer now
accepts a primary mouse press on pointer down, as an anchor does; the click
still cancels past the tap slop, drags from plain text still select across
links, and touch long press keeps Copy link.
This commit is contained in:
2026-11-03 18:35:43 +00:00
parent fa6c0864db
commit 7f313cf5ef
4 changed files with 176 additions and 2 deletions
@@ -7,6 +7,25 @@ import '../../../../ui/adaptive/host.dart';
import 'chat_link_browser_menu.dart';
import 'chat_links.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
@@ -24,7 +43,7 @@ class ChatLinkSpans {
static bool? debugOwnsSecondaryClick;
final VoidCallback onChanged;
final Map<String, TapGestureRecognizer> _recognizers = {};
final Map<String, _LinkTapRecognizer> _recognizers = {};
final ContextMenuController _linkMenu = ContextMenuController();
String? _hovered;
String? _pressed;
@@ -45,7 +64,7 @@ class ChatLinkSpans {
final uri = chatLinkUri(target);
if (uri == null) return TextSpan(children: label);
final url = uri.toString();
final recognizer = _recognizers.putIfAbsent(url, TapGestureRecognizer.new)
final recognizer = _recognizers.putIfAbsent(url, _LinkTapRecognizer.new)
..onTapDown = (_) {
_pressed = url;
}