diff --git a/lib/chat/chat_history_view.dart b/lib/chat/chat_history_view.dart index c584c06e1..6bc9473cd 100644 --- a/lib/chat/chat_history_view.dart +++ b/lib/chat/chat_history_view.dart @@ -734,6 +734,11 @@ class _ChatHistoryViewState extends State /// The press went to the bar over the selection (see [_placeBubbleBy]). bool _pressOnBubble = false; + /// The last press in the history, which [_handlePointerDown] saw (and, + /// selecting, gave a bar of its own): not one outside the bar's to take + /// that away again (see [_buildBubble]). + int? _historyPress; + // --- Scrubbing --------------------------------------------------------------- // // The minimap's glass or the bar's thumb dragged: each frame a jump to rows @@ -1758,6 +1763,7 @@ class _ChatHistoryViewState extends State void _handlePointerDown(PointerDownEvent event) { _pressedDevices.add(event.device); + _historyPress = event.pointer; final onBubble = _pressOnBubble; _pressOnBubble = false; if (!onBubble) _hideBubble(); @@ -2303,7 +2309,13 @@ class _ChatHistoryViewState extends State key: ValueKey(_bubbleShows), child: shown != 0 ? TapRegion( - onTapOutside: (_) => _hideBubble(), + // A press elsewhere in the app. One in the history was + // seen there first (it hears it first, being deeper): it + // took the bar away and, starting a selection, may have + // had it come back, which this would undo. + onTapOutside: (event) { + if (event.pointer != _historyPress) _hideBubble(); + }, child: PopIn( // As the user's messages are: their fill and line. child: SelectionBubble( diff --git a/test/chat/history_selection_test.dart b/test/chat/history_selection_test.dart index 8b76deec6..1fbfbc0a1 100644 --- a/test/chat/history_selection_test.dart +++ b/test/chat/history_selection_test.dart @@ -1632,6 +1632,79 @@ void main() { expect(bar, findsOneWidget, reason: 'left where it is'); }, variant: TargetPlatformVariant.only(TargetPlatform.macOS)); + testWidgets('a drag begun while the bar shows has a bar of its own; a press ' + 'outside the history takes it away', (tester) async { + const first = 'The parser drops the last token of every line.'; + const second = 'Then the printer has nothing to close the block with.'; + final feed = _Feed([ + const UserMessageItem(text: 'Why does it fail?'), + const AssistantTextItem('$first\n\n$second'), + ])..streaming = false; + addTearDown(feed.dispose); + const outside = Key('outside'); + await tester.pumpWidget( + MaterialApp( + theme: buildAppTheme(), + home: Scaffold( + body: Column( + children: [ + Expanded( + child: ChatHistoryView(feed: feed, onQuote: (_) {}), + ), + const SizedBox(key: outside, height: 60), + ], + ), + ), + ), + ); + await tester.pumpAndSettle(); + RenderParagraph paragraph(String text) => + tester.renderObject( + find.byWidgetPredicate( + (widget) => widget is RichText && widget.text.toPlainText() == text, + ), + ); + Offset at(RenderParagraph p, double x) => + p.localToGlobal(Offset(x, p.size.height / 2)); + final bar = find.byType(SelectionBubble); + Future select(RenderParagraph p) async { + final drag = await tester.startGesture( + at(p, 2), + kind: PointerDeviceKind.mouse, + ); + await tester.pump(); + await drag.moveTo(at(p, 160)); + await tester.pumpAndSettle(); + await drag.up(); + await tester.pumpAndSettle(); + } + + await select(paragraph(first)); + expect(bar, findsOneWidget); + // The next, the bar still there: its press in the history is not one + // outside the bar's, which would take the new one away (only every + // other selection had a bar). + final below = paragraph(second); + await select(below); + expect(bar, findsOneWidget); + expect( + tester.getRect(bar).bottom, + lessThanOrEqualTo(below.localToGlobal(Offset.zero).dy), + ); + expect( + tester.getRect(bar).bottom, + greaterThan(paragraph(first).localToGlobal(Offset.zero).dy), + reason: 'over the second paragraph, not the first', + ); + + await tester.tapAt( + tester.getCenter(find.byKey(outside)), + kind: PointerDeviceKind.mouse, + ); + await tester.pumpAndSettle(); + expect(bar, findsNothing); + }, variant: TargetPlatformVariant.only(TargetPlatform.macOS)); + testWidgets('dragged from the end of a message down into the reply, the ' 'bar is over the reply\'s text, not over the message the drag began ' 'beside', (tester) async {