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:
@@ -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),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user