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;
}
+21
View File
@@ -13,6 +13,16 @@
links outside backtick code become links. Text without a link stays
`SelectableText`.
- Inline code and fenced code are never linkified.
- Click: a primary mouse press on a link belongs to the link (its recognizer
accepts on pointer down), like a press on a web page's anchor. Before, the
`SelectionArea` claimed the press as a selection drag once the pointer moved
`kPrecisePointerPanSlop` (2 px), so in the browser a click with a little
hand or trackpad drift opened nothing (headless Chrome: 0-2 px opened, 3, 5
and 8 px did not). The click still cancels past the tap slop (18 px); a
selection drag that starts on plain text still crosses links; a drag that
starts on a link selects nothing (as a browser drags an anchor). Touch,
stylus and right-click presses keep competing in the arena, so a long press
still selects and offers Copy link.
- Style: host primary colour (`CupertinoTheme.primaryColor`), underline; hover
thickens the underline (paint-only, selection survives) with a click cursor.
- Copy link: long press adds it to the selection toolbar. On native desktop the
@@ -23,6 +33,17 @@
rebuilds without its platform menu wrapper, remounts its subtree, throws in
`StaticSelectionContainerDelegate.hasSize` and drops the selection.
Browser check: `tool/capture/examples/chat-link-click.json` drifts a click
5 px on a link and drags a selection across links, and expects exactly one
`window.open`. Checked in headless Chrome (Nov 2026), desktop and touch
emulation (iPhone/Android/iPad user agents): tap and click open through
`window.open` with user activation active; right-click Copy link (desktop)
and long-press Copy link (touch) write the URL to `navigator.clipboard` and it
reads back. Not verified: real iOS/macOS Safari (popup and clipboard user
activation rules differ) and real devices. In headless CPU rendering the
text-selection toolbars (stock Copy/Select all as well as Copy link) draw no
surface; popup menus do, so this looks like the renderer, not the links.
Browser capture: `?links=1` on the chat fixture (`tool/perf/chat_scroll_fixture.dart`).
With the capture kit's semantics tree on, a CDP click lands on link semantics
nodes whose rects are the bounding box of a wrapped link, so verify clicks with
@@ -0,0 +1,113 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_link_launcher.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import 'chat_markdown_test_support.dart';
// A mouse click on a link opens it even when the pointer moves a few pixels
// between press and release: the selection area used to claim that press as a
// selection drag after 2 px, so ordinary clicks in the browser opened nothing.
void main() {
late List<Uri> opened;
Future<RenderParagraph> pump(WidgetTester tester) async {
await phoneViewport(tester);
opened = [];
await tester.pumpWidget(
ChatLinkScope(
launcher: (uri) async {
opened.add(uri);
return true;
},
child: markdownHost(
const ChatMarkdownView(text: 'go [here](https://x.com/c) now'),
),
),
);
await tester.pumpAndSettle();
return tester.renderObject<RenderParagraph>(find.byType(RichText).first);
}
Offset centreOf(RenderParagraph paragraph, String text) {
final start = paragraph.text.toPlainText().indexOf(text);
final box = paragraph.getBoxesForSelection(
TextSelection(baseOffset: start, extentOffset: start + text.length),
);
return paragraph.localToGlobal(box.first.toRect().center);
}
Future<void> mouseDrag(WidgetTester tester, Offset from, double dx) async {
final mouse = await tester.startGesture(
from,
kind: PointerDeviceKind.mouse,
);
await tester.pump();
// Three moves, as a hand drifts during a click or drags.
for (var i = 1; i <= 3; i++) {
await mouse.moveTo(from + Offset(dx * i / 3, 0));
await tester.pump();
}
await mouse.up();
await tester.pumpAndSettle();
await mouse.removePointer();
}
for (final dx in [0.0, 3.0, 8.0]) {
testWidgets(
'a mouse click moving ${dx}px opens the link',
(tester) async {
final paragraph = await pump(tester);
await mouseDrag(tester, centreOf(paragraph, 'here'), dx);
expect(opened.map((uri) => '$uri'), ['https://x.com/c']);
},
variant: TargetPlatformVariant.desktop(),
);
}
testWidgets(
'dragging off a link opens nothing',
(tester) async {
final paragraph = await pump(tester);
await mouseDrag(tester, centreOf(paragraph, 'here'), 40);
expect(opened, isEmpty);
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
testWidgets(
'a mouse drag from plain text still selects across the link',
(tester) async {
final paragraph = await pump(tester);
final from = centreOf(paragraph, 'go');
await mouseDrag(tester, from, centreOf(paragraph, 'now').dx - from.dx);
expect(opened, isEmpty);
String? copied;
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async {
if (call.method == 'Clipboard.setData') {
copied = (call.arguments as Map)['text'] as String?;
}
return null;
},
);
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
null,
),
);
await tester.sendKeyDownEvent(LogicalKeyboardKey.meta);
await tester.sendKeyEvent(LogicalKeyboardKey.keyC);
await tester.sendKeyUpEvent(LogicalKeyboardKey.meta);
await tester.pump();
// The link claims only presses on itself; this drag selects across it.
expect(copied, contains('o here n'));
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
}
@@ -0,0 +1,21 @@
{
"size": "880x1000",
"params": "chats=1&msgs=4&links=1",
"steps": [
{ "open": "/chat" },
{ "click": "OpenCode harness\nFixture chat 1\n/workspace/demo", "wait": 2000 },
{ "eval": "window.__opened = []; window.open = (u) => { window.__opened.push(String(u)); return null; }" },
{ "shot": "chat" },
{ "drag": "480,719>5,0", "steps": 3, "wait": 600 },
{
"expect": "window.__opened.length === 1 && window.__opened[0] === 'https://example.com/releases/v2'",
"name": "a click that drifts 5 px still opens the link"
},
{ "drag": "380,719>240,0", "steps": 12, "wait": 600 },
{ "shot": "drag-select-across-links" },
{
"expect": "window.__opened.length === 1",
"name": "a selection drag from plain text across links opens nothing"
}
]
}