diff --git a/lib/chat/chat_history_view.dart b/lib/chat/chat_history_view.dart index b1a8f59f9..c584c06e1 100644 --- a/lib/chat/chat_history_view.dart +++ b/lib/chat/chat_history_view.dart @@ -2413,8 +2413,7 @@ class _ChatHistoryViewState extends State final viewport = RenderAbstractViewport.maybeOf(item); if (viewport is! RenderBox) return false; final top = - item.localToGlobal(Offset.zero).dy - - (viewport as RenderBox).localToGlobal(Offset.zero).dy + + item.localToGlobal(Offset.zero, ancestor: viewport).dy + _gapBefore(index); return top < _editorInset; } @@ -2595,8 +2594,12 @@ class _ChatHistoryViewState extends State // Not following the end as the editor grows: that would push its top out // of a short view, the end of the list in view but not the editor // (#1257). It is brought into view as it grows (_ensureEditorVisible); - // closed, it follows again. - _followedBeforeEdit = _scrollController.anchored; + // closed, it follows again. A click opened it: the press stopped the list + // following while held, to follow again as it is let go, after this + // (the message hears it first, see _handlePointerUp); it is this that + // has it follow again now. + _followedBeforeEdit = _scrollController.anchored || _followedBeforePress; + _followedBeforePress = false; _scrollController.anchored = false; setState(() { _closingIndex = null; @@ -2719,9 +2722,7 @@ class _ChatHistoryViewState extends State .where((box) => _indexOf(box) == index) .firstOrNull; if (item == null) return null; - final top = - item.localToGlobal(Offset(0, _gapBefore(index))).dy - - layer.localToGlobal(Offset.zero).dy; + final top = _topIn(layer, item, _gapBefore(index)); return _sticks ? math.max(top, inset) : top; } // Not findRenderObject: painted in the frame whose layout dropped the @@ -2731,9 +2732,7 @@ class _ChatHistoryViewState extends State (_editorPlaceholderKey.currentContext as Element?)?.renderObject as RenderBox?; if (placeholder != null && placeholder.attached && placeholder.hasSize) { - final top = - placeholder.localToGlobal(Offset.zero).dy - - layer.localToGlobal(Offset.zero).dy; + final top = _topIn(layer, placeholder, 0); return _sticks ? math.max(top, inset) : top; } final first = _laidOutItems().firstOrNull; @@ -2744,6 +2743,12 @@ class _ChatHistoryViewState extends State return null; } + /// How far [y] in [box] is below the top of [layer], in [layer]'s units: + /// not the difference of their places on screen, which the window's zoom + /// scales (see [UiZoom]) while [layer] paints in units of its own. + static double _topIn(RenderBox layer, RenderBox box, double y) => + MatrixUtils.transformPoint(box.getTransformTo(layer), Offset(0, y)).dy; + // --- The sticky user message --------------------------------------------- /// Room above the stuck message: as above the list's first item, and at diff --git a/lib/chat/widgets/user_message_bubble.dart b/lib/chat/widgets/user_message_bubble.dart index 5a89764fc..748b362e5 100644 --- a/lib/chat/widgets/user_message_bubble.dart +++ b/lib/chat/widgets/user_message_bubble.dart @@ -66,7 +66,11 @@ class UserMessageBubble extends StatefulWidget { if (box == null || !box.attached || !box.hasSize) return null; final fitted = UserMessageStyle.current.value == UserMessageStyle.bubble; return ( - rect: box.localToGlobal(Offset.zero) & box.size, + // On screen, zoomed (see UiZoom) as its place is. + rect: MatrixUtils.transformRect( + box.getTransformTo(null), + Offset.zero & box.size, + ), radius: fitted ? _MessageShape.radius : radius, tail: fitted ? 1.0 : 0.0, ); @@ -919,7 +923,10 @@ class _RenderEditMorph extends RenderProxyBox { super.paint(context, offset); return; } - final from = _fromHere ??= _from.rect.shift(-localToGlobal(Offset.zero)); + final from = _fromHere ??= MatrixUtils.inverseTransformRect( + getTransformTo(null), + _from.rect, + ); final rect = Rect.lerp(from, Offset.zero & size, t)!; final radius = _from.radius + (UserMessageEditMorph.radius - _from.radius) * t; diff --git a/test/chat/message_edit_visibility_test.dart b/test/chat/message_edit_visibility_test.dart index 4f1b541db..81fac5993 100644 --- a/test/chat/message_edit_visibility_test.dart +++ b/test/chat/message_edit_visibility_test.dart @@ -14,6 +14,7 @@ import 'package:baocode/chat/widgets/user_message_bubble.dart'; import 'package:baocode/kernel/agent_kernel.dart'; import 'package:baocode/kernel/claude_code/claude_code_kernel.dart'; import 'package:baocode/theme/app_theme.dart'; +import 'package:baocode/theme/ui_zoom.dart'; import '../kernel_test.dart' show FakeCli; @@ -51,6 +52,7 @@ Future _stopped( Size size = const Size(1000, 600), bool following = false, bool toolRunning = false, + double zoom = 1, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; @@ -78,8 +80,11 @@ Future _stopped( MaterialApp( theme: buildAppTheme(), builder: (context, child) => MediaQuery( - data: MediaQuery.of(context).copyWith(disableAnimations: reducedMotion), - child: child!, + data: UiZoom.zoomed( + MediaQuery.of(context), + zoom, + ).copyWith(disableAnimations: reducedMotion), + child: UiZoom(scale: zoom, child: child), ), home: ChatScreen(session: session, embedded: true), ), @@ -137,12 +142,16 @@ Future _stopped( expect(position.pixels, lessThan(position.maxScrollExtent)); } -Future _edit(WidgetTester tester) async { +Future _edit( + WidgetTester tester, { + PointerDeviceKind kind = PointerDeviceKind.touch, +}) async { await tester.tap( find.descendant( of: _bubble(), matching: find.text(_message, findRichText: true), ), + kind: kind, ); await tester.pumpAndSettle(); expect(_editor(), findsOneWidget); @@ -229,6 +238,39 @@ void main() { } }, variant: platforms); + testWidgets('clicked with a mouse, the editor no more follows the end than ' + 'tapped ($style)', (tester) async { + await _stopped(tester, style, following: true); + await _edit(tester, kind: PointerDeviceKind.mouse); + _visible(tester); + final position = _position(tester); + // The editor's bottom at the view's: the list's own room under it is + // left out of view. + expect(position.pixels, lessThan(position.maxScrollExtent)); + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + expect(position.pixels, position.maxScrollExtent); + await tester.pumpWidget(const SizedBox()); + }, variant: platforms); + + testWidgets('under the window\'s zoom, the editor opens where its message ' + 'is ($style)', (tester) async { + for (final zoom in [0.8, 1.25]) { + await _stopped(tester, style, zoom: zoom); + final before = tester.getRect(_bubble()); + final offset = _position(tester).pixels; + await _edit(tester); + _visible(tester); + // On screen, the scroll is zoomed as the rest is. + final scrolled = (_position(tester).pixels - offset) * zoom; + expect( + tester.getRect(_editor()).top, + closeTo(before.top - scrolled, 1), + ); + await tester.pumpWidget(const SizedBox()); + } + }, variant: platforms); + testWidgets('a growing message editor stays above the composer ($style)', ( tester, ) async {