diff --git a/app/lib/features/chat/conversation/chat_pane_presentation.dart b/app/lib/features/chat/conversation/chat_pane_presentation.dart index f878a888..84b51acb 100644 --- a/app/lib/features/chat/conversation/chat_pane_presentation.dart +++ b/app/lib/features/chat/conversation/chat_pane_presentation.dart @@ -28,14 +28,10 @@ class ChatPanePresentation extends InheritedWidget { /// The conversation's bar under a [ChatPanePresentation]. /// -/// Without one the bar is itself. As its chat becomes active the bar fades -/// and slides down into place over the transcript, which already keeps clear -/// of it. As its chat becomes the peek the bar fades out in place, keeping -/// its height until it has gone (by then the arriving chat covers it). A -/// resting peek keeps the bar offstage: not painted, taking no height, no -/// pointers and no semantics, so its transcript starts at the top, yet still -/// mounted, so the next switch fades the same bar rather than building a new -/// one (whose pieces would replay their entrances). +/// Both header and composer use the same retained tree. Opacity, slide and +/// occupied height follow the pane's reveal together, so the peek has no +/// controls or reserved chrome space. Repaint boundaries retain each bar's +/// picture; changing the real viewport height still requires transcript layout. class ChatPaneBarReveal extends StatelessWidget { const ChatPaneBarReveal({super.key, required this.bar}); @@ -55,16 +51,20 @@ class ChatPaneBarReveal extends StatelessWidget { animation: reveal, child: IgnorePointer( ignoring: peek, - child: FadeTransition( - opacity: reveal, - child: SlideTransition( - position: Tween( - begin: const Offset(0, -slide), - end: Offset.zero, - ).animate(reveal), - // The bar's own picture holds while it slides; only its offset - // and alpha change per tick. - child: RepaintBoundary(child: bar), + child: SizeTransition( + alignment: Alignment.topCenter, + sizeFactor: reveal, + child: FadeTransition( + opacity: reveal, + child: SlideTransition( + position: Tween( + begin: const Offset(0, -slide), + end: Offset.zero, + ).animate(reveal), + // The bar's own picture holds while it slides; only its offset + // and alpha change per tick. + child: RepaintBoundary(child: bar), + ), ), ), ), diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index e49add25..53627959 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -1072,7 +1072,7 @@ class _ChatThreadViewState extends State { ), ); final page = FloatingBarOverlay( - bar: composerBar, + bar: ChatPaneBarReveal(bar: composerBar), child: SegmentedBarOverlay( bar: ChatPaneBarReveal(bar: _header(context, inShellBar)), child: NotificationListener( diff --git a/app/lib/features/chat/workspace/chat_mobile_panes.dart b/app/lib/features/chat/workspace/chat_mobile_panes.dart index 8e297338..ef01b69b 100644 --- a/app/lib/features/chat/workspace/chat_mobile_panes.dart +++ b/app/lib/features/chat/workspace/chat_mobile_panes.dart @@ -57,13 +57,10 @@ class ChatMobilePanes { /// One retained chat in the phone presentation. /// -/// Both chats keep the same full content box ([contentHeight]), aligned to -/// the pane's top and clipped to it, so a switch moves and clips retained -/// pictures without laying a transcript out again. The peek shows its -/// transcript from the top (its bar steps aside through -/// [ChatPanePresentation]); a drag on it scrolls that transcript and a tap -/// activates the chat. The tree keeps one shape in both roles, so a chat -/// changing role keeps its state. +/// The scrollable receives the pane's actual visible constraints. A peek is +/// never a clipped full-height transcript: its bottom must be reachable. +/// Stable siblings, focus scopes and repaint boundaries retain chat state as +/// the rectangles resize during a swap. class ChatMobilePaneViewport extends StatelessWidget { const ChatMobilePaneViewport({ super.key, @@ -74,7 +71,6 @@ class ChatMobilePaneViewport extends StatelessWidget { required this.onActivate, required this.label, required this.paneId, - this.contentHeight, }); final bool peek; @@ -86,7 +82,6 @@ class ChatMobilePaneViewport extends StatelessWidget { final VoidCallback onActivate; final String label; final String paneId; - final double? contentHeight; @override Widget build(BuildContext context) => ClipRect( @@ -100,23 +95,15 @@ class ChatMobilePaneViewport extends StatelessWidget { onTap: peek ? onActivate : null, child: ChatPeekGesture( onTap: peek ? onActivate : null, - child: OverflowBox( - alignment: Alignment.topCenter, - minHeight: contentHeight, - maxHeight: contentHeight, - child: SizedBox( - height: contentHeight, - child: ExcludeFocus( - excluding: peek, - child: ExcludeSemantics( - excluding: peek, - child: RepaintBoundary( - child: ChatPanePresentation( - peek: peek, - barReveal: barReveal, - child: child, - ), - ), + child: ExcludeFocus( + excluding: peek, + child: ExcludeSemantics( + excluding: peek, + child: RepaintBoundary( + child: ChatPanePresentation( + peek: peek, + barReveal: barReveal, + child: child, ), ), ), diff --git a/app/lib/features/chat/workspace/chat_pane_backdrop.dart b/app/lib/features/chat/workspace/chat_pane_backdrop.dart new file mode 100644 index 00000000..a50f3df6 --- /dev/null +++ b/app/lib/features/chat/workspace/chat_pane_backdrop.dart @@ -0,0 +1,75 @@ +import 'dart:ui' as ui; +import 'package:flutter/rendering.dart'; +import 'package:flutter/widgets.dart'; + +/// One bounded, temporary picture behind crossing panes. It fills vacated +/// regions without a second live chat tree or per-frame capture. The current +/// picture can seed an interrupted swap; every old image is released after +/// its last painted frame. Pixel scale is capped at two (~9 MB at 560×1000). +class ChatPaneBackdrop extends ChangeNotifier { + ChatPaneBackdrop(this.motion) { + motion.addStatusListener(_settled); + } + final Animation motion; + final pictureKey = GlobalKey(); + ui.Image? image; + bool _scheduled = false; + bool _disposed = false; + + void capture(BuildContext context) { + final boundary = pictureKey.currentContext?.findRenderObject(); + if (boundary is! _PictureBoundary || !boundary.hasPicture) { + return; + } + var ready = true; + assert(() { + ready = !boundary.debugNeedsPaint; + return true; + }()); + if (!ready) return; + final old = image; + image = boundary.toImageSync( + pixelRatio: MediaQuery.devicePixelRatioOf(context).clamp(1, 2), + ); + if (old != null) _release(old); + } + + void _settled(AnimationStatus status) { + if (status != AnimationStatus.completed || _scheduled || image == null) { + return; + } + _scheduled = true; + WidgetsBinding.instance.addPostFrameCallback((_) { + _scheduled = false; + if (_disposed || motion.status != AnimationStatus.completed) return; + final old = image; + image = null; + notifyListeners(); + if (old != null) _release(old); + }); + } + + void _release(ui.Image old) => + WidgetsBinding.instance.addPostFrameCallback((_) => old.dispose()); + + @override + void dispose() { + _disposed = true; + motion.removeStatusListener(_settled); + image?.dispose(); + image = null; + super.dispose(); + } +} + +/// RepaintBoundary with a release-safe check for an already painted layer. +class ChatPanePicture extends RepaintBoundary { + const ChatPanePicture({super.key, required super.child}); + @override + RenderRepaintBoundary createRenderObject(BuildContext context) => + _PictureBoundary(); +} + +class _PictureBoundary extends RenderRepaintBoundary { + bool get hasPicture => attached && hasSize && layer != null; +} diff --git a/app/lib/features/chat/workspace/chat_pane_switch_motion.dart b/app/lib/features/chat/workspace/chat_pane_switch_motion.dart index cb299567..ad7d48b4 100644 --- a/app/lib/features/chat/workspace/chat_pane_switch_motion.dart +++ b/app/lib/features/chat/workspace/chat_pane_switch_motion.dart @@ -1,74 +1,36 @@ -import 'dart:math' as math; import 'dart:ui' show lerpDouble; - import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; -/// The phone presentation's switch: a cover, then a settle. -/// -/// The chat coming forward rises from the peek slot over the active chat, -/// which stays where it is until covered; the rising pane keeps its bottom -/// edge at the screen's bottom so nothing shows between them. Once it has -/// arrived ([riseShare] of [duration]), the covered chat moves to the slot -/// unseen and slides up into it from the bottom edge. Each bar follows the -/// rise: [rise] for the chat coming forward, [recede] for the one going back. -/// -/// The content boxes keep their fixed size throughout, so a tick moves and -/// clips retained pictures; nothing inside a transcript is laid out again. +/// One continuous phone-pane swap. Both rectangles and both chrome reveals +/// follow the same ease; an interrupted swap starts at their current values. +/// The app's scheduler speed preference scales this ticker once globally. class ChatPaneSwitchMotion { ChatPaneSwitchMotion({required TickerProvider vsync}) : _controller = AnimationController( vsync: vsync, duration: duration, value: 1, - ) { - rise = CurvedAnimation( - parent: _controller, - curve: const Interval(0, riseShare, curve: curve), - ); - settle = CurvedAnimation( - parent: _controller, - curve: const Interval(riseShare, 1, curve: curve), - ); - recede = ReverseAnimation(rise); - } - - /// A touch quicker than a stock iOS transition, with its kind of ease. - static const Duration duration = Duration(milliseconds: 260); - static const Curve curve = Cubic(.2, .8, .2, 1); - - /// The share of [duration] in which the chat coming forward rises; the rest - /// settles the peek beneath it. - static const double riseShare = .7; + ); + static const Duration duration = Duration(milliseconds: 360); + static const Curve curve = Cubic(.22, .7, .24, 1); final AnimationController _controller; - - /// 0 as a switch starts, 1 once the chat coming forward has arrived. - late final CurvedAnimation rise; - - /// 0 until the chat coming forward has arrived, 1 once the peek has settled. - late final CurvedAnimation settle; - - /// 1 as a switch starts, 0 once the chat coming forward has arrived. - late final Animation recede; - - /// Ticks through both phases, for rebuilding positions. Animation get animation => _controller.view; - + final Map> _reveals = {}; Map _from = const {}; Map _to = const {}; - String? _activeId; + Map _barFrom = const {}; + Map _barTo = const {}; Size? _size; - bool get isAnimating => _controller.isAnimating; + double get _progress => curve.transform(_controller.value); + + Animation revealFor(String id) => + _reveals.putIfAbsent(id, () => _PaneReveal(this, id)); + double _reveal(String id) => + lerpDouble(_barFrom[id] ?? 1, _barTo[id] ?? 1, _progress)!; - /// Records this layout's [rects] for a phone presentation of [size] with - /// [activeId] in front. - /// - /// A switch (the active pane changed) in an unchanged size travels from - /// where the panes are now, so one that interrupts another continues - /// smoothly. Any other change of rects, and every change when [animate] is - /// off, takes effect at once. void layout({ required Map rects, required Size size, @@ -79,76 +41,58 @@ class ChatPaneSwitchMotion { final travel = switching && animate && _size == size && _to.isNotEmpty; _size = size; if (travel) { - _from = { - for (final id in {..._to.keys, ...rects.keys}) - id: rectFor(id) ?? rects[id]!, - }; + final ids = {..._to.keys, ...rects.keys}; + _from = {for (final id in ids) id: rectFor(id) ?? rects[id]!}; + _barFrom = {for (final id in ids) id: _reveal(id)}; _to = rects; - _activeId = activeId; + _barTo = {for (final id in rects.keys) id: id == activeId ? 1 : 0}; _controller.forward(from: 0); - return; - } - _activeId = activeId; - if (mapEquals(rects, _to)) return; - _from = rects; - _to = rects; - _settle(); - } - - /// Leaves the phone presentation: panes take their rects directly and every - /// bar shows. - void rest() { - if (_to.isEmpty && _size == null) return; - _from = const {}; - _to = const {}; - _activeId = null; - _size = null; - _settle(); - } - - void _settle() { - if (_controller.isAnimating || _controller.value != 1) { + } else if (!mapEquals(rects, _to) || !animate) { + _from = rects; + _to = rects; + _barFrom = _barTo = { + for (final id in rects.keys) id: id == activeId ? 1 : 0, + }; _controller.value = 1; } } - /// Whether the chat coming forward has somewhere to rise from. - bool get _forwardRises { - final id = _activeId; - if (id == null) return false; - final from = _from[id]; - return from != null && from != _to[id]; + void rest() { + if (_to.isEmpty && _size == null) return; + _from = _to = const {}; + _barFrom = _barTo = const {}; + _size = null; + _controller.value = 1; } - /// Where [id]'s pane is now; null outside the phone presentation or for a - /// pane it does not place. Rect? rectFor(String id) { final to = _to[id]; - if (to == null) return null; - final from = _from[id] ?? to; - if (from == to) return to; - final time = _controller.value; - if (id == _activeId) { - if (time >= riseShare) return to; - final t = rise.value; - return Rect.fromLTRB( - lerpDouble(from.left, to.left, t)!, - lerpDouble(from.top, to.top, t)!, - lerpDouble(from.right, to.right, t)!, - // The bottom edge waits: the gap that would open below the content - // box is the slot, filled only once this pane has covered the other. - math.max(from.bottom, lerpDouble(from.bottom, to.bottom, t)!), - ); - } - if (!_forwardRises) return Rect.lerp(from, to, curve.transform(time))!; - if (time < riseShare) return from; - final top = lerpDouble(_size?.height ?? to.bottom, to.top, settle.value)!; - return Rect.fromLTWH(to.left, top, to.width, to.height); + return to == null ? null : Rect.lerp(_from[id] ?? to, to, _progress); } - void dispose() { - rise.dispose(); - settle.dispose(); - _controller.dispose(); - } + void dispose() => _controller.dispose(); +} + +/// Stable per-pane listenable: a role swap never replaces the reveal animation +/// or jumps its opacity back to an endpoint when a switch is interrupted. +class _PaneReveal extends Animation { + _PaneReveal(this.motion, this.id); + final ChatPaneSwitchMotion motion; + final String id; + @override + double get value => motion._reveal(id); + @override + AnimationStatus get status => motion._controller.status; + @override + void addListener(VoidCallback listener) => + motion._controller.addListener(listener); + @override + void removeListener(VoidCallback listener) => + motion._controller.removeListener(listener); + @override + void addStatusListener(AnimationStatusListener listener) => + motion._controller.addStatusListener(listener); + @override + void removeStatusListener(AnimationStatusListener listener) => + motion._controller.removeStatusListener(listener); } diff --git a/app/lib/features/chat/workspace/chat_workspace.dart b/app/lib/features/chat/workspace/chat_workspace.dart index 461c9b80..9aaf0328 100644 --- a/app/lib/features/chat/workspace/chat_workspace.dart +++ b/app/lib/features/chat/workspace/chat_workspace.dart @@ -8,6 +8,7 @@ import 'chat_workspace_divider.dart'; import 'chat_workspace_geometry.dart'; import 'chat_mobile_panes.dart'; import 'chat_pane_switch_motion.dart'; +import 'chat_pane_backdrop.dart'; export 'chat_workspace_controller.dart'; export 'chat_workspace_geometry.dart' show ChatPaneDirection; @@ -53,11 +54,17 @@ class _ChatWorkspaceState extends State ChatWorkspaceGeometry? _geometry; String? _lastActiveId; String? _peekId; + late final ChatPaneBackdrop _backdrop; + void _backdropChanged() { + if (mounted) setState(() {}); + } @override void initState() { super.initState(); _motion = ChatPaneSwitchMotion(vsync: this); + _backdrop = ChatPaneBackdrop(_motion.animation) + ..addListener(_backdropChanged); HardwareKeyboard.instance.addHandler(_key); } @@ -107,6 +114,7 @@ class _ChatWorkspaceState extends State @override void dispose() { HardwareKeyboard.instance.removeHandler(_key); + _backdrop.dispose(); _motion.dispose(); _focus.dispose(); for (final node in _panes.values) { @@ -142,6 +150,11 @@ class _ChatWorkspaceState extends State size: constraints.biggest, ) : null; + if (mobile != null && + switching && + !MediaQuery.disableAnimationsOf(context)) { + _backdrop.capture(context); + } if (mobile == null) { _motion.rest(); } else { @@ -160,38 +173,45 @@ class _ChatWorkspaceState extends State WidgetsBinding.instance.addPostFrameCallback((_) => node.dispose()); } final ordered = mobile?.paintOrder(panes) ?? panes; - return Stack( - fit: StackFit.expand, - clipBehavior: Clip.hardEdge, - children: [ - for (final pane in ordered) - // A switch moves the retained pane view; the view itself is - // built once per layout and only repositioned per tick. - AnimatedBuilder( - key: ValueKey('chat-workspace-${pane.id}'), - animation: _motion.animation, - child: _paneView(pane, mobile, collapsed, activeId), - builder: (context, child) => Positioned.fromRect( - rect: - _motion.rectFor(pane.id) ?? - (collapsed - ? Offset.zero & constraints.biggest - : geometry.panes[pane.id]!), - child: child!, + return ChatPanePicture( + key: _backdrop.pictureKey, + child: Stack( + fit: StackFit.expand, + clipBehavior: Clip.hardEdge, + children: [ + if (_backdrop.image != null) + IgnorePointer( + child: RawImage(image: _backdrop.image, fit: BoxFit.fill), ), - ), - if (!collapsed && mobile == null) - for (final divider in geometry.dividers) - Positioned.fromRect( - key: ValueKey('chat-workspace-${divider.split.id}'), - rect: divider.rect, - child: ChatWorkspaceDivider( - geometry: divider, - onChanged: (ratio) => - widget.controller.resize(divider.split.id, ratio), + for (final pane in ordered) + // A switch moves the retained pane view; the view itself is + // built once per layout; ticks resize its real scroll viewport. + AnimatedBuilder( + key: ValueKey('chat-workspace-${pane.id}'), + animation: _motion.animation, + child: _paneView(pane, mobile, collapsed, activeId), + builder: (context, child) => Positioned.fromRect( + rect: + _motion.rectFor(pane.id) ?? + (collapsed + ? Offset.zero & constraints.biggest + : geometry.panes[pane.id]!), + child: child!, ), ), - ], + if (!collapsed && mobile == null) + for (final divider in geometry.dividers) + Positioned.fromRect( + key: ValueKey('chat-workspace-${divider.split.id}'), + rect: divider.rect, + child: ChatWorkspaceDivider( + geometry: divider, + onChanged: (ratio) => + widget.controller.resize(divider.split.id, ratio), + ), + ), + ], + ), ); }, ), @@ -215,8 +235,7 @@ class _ChatWorkspaceState extends State child: ChatMobilePaneViewport( peek: peek, active: active, - barReveal: peek ? _motion.recede : _motion.rise, - contentHeight: mobile?.contentHeight, + barReveal: _motion.revealFor(pane.id), label: widget.paneTitle?.call(pane) ?? pane.sessionId, paneId: pane.id, onActivate: () { diff --git a/app/test/features/chat/conversation/chat_pane_presentation_test.dart b/app/test/features/chat/conversation/chat_pane_presentation_test.dart index 1d6b84cd..9597b9b7 100644 --- a/app/test/features/chat/conversation/chat_pane_presentation_test.dart +++ b/app/test/features/chat/conversation/chat_pane_presentation_test.dart @@ -75,24 +75,35 @@ void main() { findsOneWidget, ); expect(listTop(tester), 8); + expect( + find.byWidgetPredicate( + (widget) => widget is EditableText && !widget.readOnly, + ), + findsNothing, + ); + expect(find.byType(EditableText, skipOffstage: false), findsWidgets); expect(find.text('Message 0'), findsOneWidget); }); - testWidgets('a leaving bar fades in place, keeping the list clear', ( - tester, - ) async { - await _pump(tester, peek: true, reveal: .4); - expect(header, findsOneWidget); - expect(listTop(tester), closeTo(tester.getSize(header).height + 8, .5)); - final fade = tester.widget( - find.ancestor(of: header, matching: find.byType(FadeTransition)).first, - ); - expect(fade.opacity.value, .4); - expect( - find.ancestor(of: header, matching: find.byType(IgnorePointer)), - findsWidgets, - ); - }); + testWidgets( + 'leaving controls fade and release their viewport height smoothly', + (tester) async { + await _pump(tester, peek: true, reveal: .4); + expect(header, findsOneWidget); + expect( + listTop(tester), + closeTo(tester.getSize(header).height * .4 + 8, .5), + ); + final fade = tester.widget( + find.ancestor(of: header, matching: find.byType(FadeTransition)).first, + ); + expect(fade.opacity.value, .4); + expect( + find.ancestor(of: header, matching: find.byType(IgnorePointer)), + findsWidgets, + ); + }, + ); testWidgets('without a presentation the bar is itself', (tester) async { final gateway = FakeChatGateway(); @@ -115,7 +126,13 @@ void main() { expect(header, findsOneWidget); Widget? child; tester - .element(find.byType(ChatPaneBarReveal)) + .element( + find.byWidgetPredicate( + (widget) => + widget is ChatPaneBarReveal && + widget.bar.key == const Key('chat-thread-header'), + ), + ) .visitChildren((element) => child = element.widget); expect(child?.key, const Key('chat-thread-header')); }); diff --git a/app/test/features/chat/workspace/chat_mobile_panes_test.dart b/app/test/features/chat/workspace/chat_mobile_panes_test.dart index f6909b91..4a2a546c 100644 --- a/app/test/features/chat/workspace/chat_mobile_panes_test.dart +++ b/app/test/features/chat/workspace/chat_mobile_panes_test.dart @@ -1,5 +1,3 @@ -import 'dart:math' as math; - import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -123,212 +121,99 @@ class _Thread extends StatelessWidget { const _top = (800 - 6) * .8; const _peekTop = _top + 6; -final _duration = ChatPaneSwitchMotion.duration.inMilliseconds; - -/// The rise's progress [ms] into a switch (1 once the chat has arrived). -double _rise(int ms) => ChatPaneSwitchMotion.curve.transform( - math.min(1, ms / _duration / ChatPaneSwitchMotion.riseShare), -); - -/// The settle's progress [ms] into a switch (0 until the chat has arrived). -double _settle(int ms) => ChatPaneSwitchMotion.curve.transform( - math.max( - 0, - (ms / _duration - ChatPaneSwitchMotion.riseShare) / - (1 - ChatPaneSwitchMotion.riseShare), - ), -); -Finder _rect(String pane) => find.byKey(ValueKey('chat-workspace-$pane')); +Finder rect(String pane) => find.byKey(ValueKey('chat-workspace-$pane')); void main() { + void phone(WidgetTester tester) { + tester.view.physicalSize = const Size(560, 800); + tester.view.devicePixelRatio = 1; + tester.platformDispatcher.accessibilityFeaturesTestValue = + const FakeAccessibilityFeatures(disableAnimations: false); + addTearDown(tester.platformDispatcher.clearAccessibilityFeaturesTestValue); + addTearDown(tester.view.reset); + } + testWidgets( - 'active chat travels to the top on the switch curve with a stable thread layout', + 'one eased swap moves both panes, interruptions retain draft and reveal', (tester) async { - tester.view.physicalSize = const Size(560, 800); - tester.view.devicePixelRatio = 1; - tester.platformDispatcher.accessibilityFeaturesTestValue = - const FakeAccessibilityFeatures(disableAnimations: false); - addTearDown( - tester.platformDispatcher.clearAccessibilityFeaturesTestValue, - ); - addTearDown(tester.view.reset); + phone(tester); final controller = ChatWorkspaceController()..open('a'); - final first = controller.activePaneId!; + final a = controller.activePaneId!; controller.splitActive('b', ChatSplitAxis.horizontal); - final second = controller.activePaneId!; - controller.resize(controller.root!.id, .31); - final saved = controller.snapshot()!.encode(); + final b = controller.activePaneId!; await tester.pumpWidget(_host(controller)); await tester.pumpAndSettle(); - final aState = tester.state(find.byKey(const ValueKey('a'))); - final bState = tester.state(find.byKey(const ValueKey('b'))); - await tester.enterText( - find.descendant( - of: find.byKey(const ValueKey('b')), - matching: find.byType(EditableText), - ), - 'Draft b', - ); - expect(tester.getSize(_rect(first)).height, closeTo((800 - 6) * .2, 1)); - expect(tester.getRect(_rect(first)).top, closeTo(_peekTop, 1)); - expect(tester.getSize(_rect(second)).height, closeTo(_top, 1)); - await tester.pumpAndSettle(); - await tester.tap(find.byKey(ValueKey('chat-peek-$first'))); - expect( - controller.activePane?.sessionId, - 'a', - reason: 'tap activates immediately', - ); - // The switch starts where the panes are: the tapped chat rises over - // the other, which stays put until covered. + final aState = tester.state<_DraftState>(find.byKey(const ValueKey('a'))); + final bState = tester.state<_DraftState>(find.byKey(const ValueKey('b'))); + bState.controller.text = 'Retained draft'; + controller.activate(a); await tester.pump(); - expect(tester.getRect(_rect(first)).top, closeTo(_peekTop, 1)); - expect(tester.getRect(_rect(second)).top, 0); - final aLayouts = (aState as _DraftState).layouts; - final bLayouts = (bState as _DraftState).layouts; - await tester.pump(const Duration(milliseconds: 60)); - final firstTop = tester.getRect(_rect(first)).top; - expect(firstTop, closeTo(_peekTop * (1 - _rise(60)), 1)); - expect(tester.getRect(_rect(first)).bottom, 800); - expect( - tester.getRect(_rect(second)), - const Offset(0, 0) & const Size(560, _top), - ); - await tester.pump(const Duration(milliseconds: 60)); - expect(tester.getRect(_rect(first)).top, lessThan(firstTop)); - expect( - tester.getRect(_rect(first)).top, - closeTo(_peekTop * (1 - _rise(120)), 1), - ); - expect(tester.getRect(_rect(second)).top, 0); - // The run only composites retained content: neither transcript nor - // composer receives a new size on any tick. + await tester.pump(const Duration(milliseconds: 90)); + final t = ChatPaneSwitchMotion.curve.transform(.25); + expect(tester.getRect(rect(a)).top, closeTo(_peekTop * (1 - t), .5)); + expect(tester.getRect(rect(b)).top, closeTo(_peekTop * t, .5)); expect( tester.getSize(find.byKey(const ValueKey('a'))).height, - closeTo(_top, 1), + closeTo(tester.getRect(rect(a)).height, .5), ); - expect( - tester.getSize(find.byKey(const ValueKey('b'))).height, - closeTo(_top, 1), - ); - expect(aState.layouts, aLayouts); - expect(bState.layouts, bLayouts); - // Arrived: the covered chat slides up into the slot from the bottom. - await tester.pump(const Duration(milliseconds: 80)); - expect(tester.getRect(_rect(first)).top, 0); - expect(tester.getSize(_rect(first)).height, closeTo(_top, 1)); - final secondTop = tester.getRect(_rect(second)).top; - expect(secondTop, inExclusiveRange(_peekTop, 800)); - expect(secondTop, closeTo(800 - (800 - _peekTop) * _settle(200), 1)); - expect(tester.getSize(_rect(second)).height, closeTo(800 - _peekTop, 1)); - await tester.pump(const Duration(milliseconds: 100)); - expect(tester.getRect(_rect(second)).top, closeTo(_peekTop, 1)); - expect(aState.layouts, aLayouts); - expect(bState.layouts, bLayouts); + final beforeA = tester.getRect(rect(a)); + final beforeB = tester.getRect(rect(b)); + double reveal(String session) => tester + .widget( + find + .ancestor( + of: find.byKey(ValueKey(session)), + matching: find.byType(ChatPanePresentation), + ) + .first, + ) + .barReveal + .value; + final bar = reveal('a'); + controller.activate(b); + await tester.pump(); + expect(tester.getRect(rect(a)), beforeA); + expect(tester.getRect(rect(b)), beforeB); + expect(reveal('a'), closeTo(bar, .001)); + await tester.pump(const Duration(milliseconds: 90)); + expect(tester.getRect(rect(a)).top, greaterThan(beforeA.top)); + expect(reveal('a'), lessThan(bar)); await tester.pumpAndSettle(); - expect(controller.activePane?.sessionId, 'a'); + expect(tester.getRect(rect(a)).top, _peekTop); + expect( + tester.getSize(find.byKey(const ValueKey('a'))).height, + closeTo((800 - 6) * .2, .5), + ); expect(tester.state(find.byKey(const ValueKey('a'))), same(aState)); expect(tester.state(find.byKey(const ValueKey('b'))), same(bState)); - expect(bState.controller.text, 'Draft b'); - expect( - tester - .widget( - find.descendant( - of: find.byKey(const ValueKey('b')), - matching: find.byType(EditableText), - ), - ) - .focusNode - .hasFocus, - isFalse, - ); - // A switch that interrupts another continues from where the panes are: - // the rising chat returns to the slot while the covered one stays. - await tester.tap(find.byKey(ValueKey('chat-peek-$second'))); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 60)); - final interrupted = tester.getRect(_rect(second)).top; - expect(interrupted, inExclusiveRange(0, _peekTop)); - controller.activate(first); - await tester.pump(); - expect(tester.getRect(_rect(second)).top, closeTo(interrupted, 1)); - expect(tester.getRect(_rect(first)).top, 0); - await tester.pump(const Duration(milliseconds: 60)); - expect( - tester.getRect(_rect(second)).top, - inExclusiveRange(interrupted, _peekTop), - ); - expect(tester.getRect(_rect(first)).top, 0); - await tester.pumpAndSettle(); - expect(tester.getRect(_rect(second)).top, closeTo(_peekTop, 1)); - await tester.tap(find.byKey(ValueKey('chat-peek-$second'))); - await tester.pumpAndSettle(); - expect(tester.getRect(_rect(second)).top, 0); - expect(tester.getRect(_rect(first)).top, closeTo(_peekTop, 1)); - expect(controller.snapshot()!.encode(), saved); - await tester.pumpWidget(_host(controller, mobile: false)); - await tester.pumpAndSettle(); - expect(tester.state(find.byKey(const ValueKey('a'))), same(aState)); - expect( - tester.getSize(_rect(first)).width, - lessThan(tester.getSize(_rect(second)).width), - ); + expect(bState.controller.text, 'Retained draft'); await tester.pumpWidget(const SizedBox()); controller.dispose(); }, ); - + testWidgets('reduced motion settles immediately', (tester) async { + phone(tester); + final controller = ChatWorkspaceController()..open('a'); + final a = controller.activePaneId!; + controller.splitActive('b', ChatSplitAxis.horizontal); + final b = controller.activePaneId!; + await tester.pumpWidget(_host(controller, reduced: true)); + controller.activate(a); + await tester.pump(); + expect(tester.getRect(rect(a)).top, 0); + expect(tester.getRect(rect(b)).top, _peekTop); + await tester.pumpWidget(const SizedBox()); + controller.dispose(); + }); testWidgets( - 'three retained panes expose active+previous and honor reduced motion', + 'peek uses visible viewport, reaches final row and retains reading offset', (tester) async { - tester.view.physicalSize = const Size(560, 800); - tester.view.devicePixelRatio = 1; - addTearDown(tester.view.reset); - final controller = ChatWorkspaceController()..open('a'); - final first = controller.activePaneId!; - controller.splitActive('b', ChatSplitAxis.horizontal); - controller.splitActive('c', ChatSplitAxis.vertical); - await tester.pumpWidget(_host(controller, reduced: true)); - await tester.pumpAndSettle(); - expect(find.byType(_Draft), findsNWidgets(2)); - final third = controller.activePaneId!; - controller.activate(first); - await tester.pump(); - // Reduced motion: the panes take their places at once. - expect(tester.getSize(_rect(first)).height, closeTo(_top, 1)); - expect(tester.getRect(_rect(first)).top, 0); - expect(tester.getRect(_rect(third)).top, closeTo(_peekTop, 1)); - await tester.pump(const Duration(milliseconds: 100)); - expect(tester.getRect(_rect(first)).top, 0); - expect(tester.getRect(_rect(third)).top, closeTo(_peekTop, 1)); - expect(find.byType(_Draft), findsNWidgets(2)); - expect(find.byType(_Draft, skipOffstage: false), findsNWidgets(3)); - controller.activate(third); - await tester.pump(); - expect(tester.getSize(_rect(third)).height, closeTo(_top, 1)); - expect(tester.takeException(), isNull); - await tester.pumpWidget(const SizedBox()); - controller.dispose(); - }, - ); - - testWidgets( - 'peek shows its transcript from the top, scrolls under a drag and opens on a tap', - (tester) async { - tester.view.physicalSize = const Size(560, 800); - tester.view.devicePixelRatio = 1; - addTearDown(tester.view.reset); + phone(tester); final scrolls = {'a': ScrollController(), 'b': ScrollController()}; - addTearDown(() { - for (final scroll in scrolls.values) { - scroll.dispose(); - } - }); - final pressed = []; final controller = ChatWorkspaceController()..open('a'); - final first = controller.activePaneId!; + final a = controller.activePaneId!; controller.splitActive('b', ChatSplitAxis.horizontal); - final second = controller.activePaneId!; - Finder bar(String id) => find.byKey(ValueKey('bar-$id')); + final b = controller.activePaneId!; + var pressed = 0; await tester.pumpWidget( _host( controller, @@ -336,88 +221,45 @@ void main() { key: ValueKey(pane.sessionId), id: pane.sessionId, scroll: scrolls[pane.sessionId]!, - onPressed: () => pressed.add(pane.sessionId), + onPressed: () => pressed++, ), ), ); await tester.pumpAndSettle(); - // The active chat keeps its bar and its list clear of it; the peek has - // no bar and its list starts at the pane's top edge. - expect(bar('b'), findsOneWidget); - expect(bar('a'), findsNothing); + final peek = tester.getRect(rect(a)); expect( - tester.getRect(find.byKey(const ValueKey('row-b-0'))).top, - tester.getRect(_rect(second)).top + 48, - ); - expect( - tester.getRect(find.byKey(const ValueKey('row-a-0'))).top, - tester.getRect(_rect(first)).top, - ); - // A drag scrolls the peek's list without opening it. - await tester.drag( - find.byKey(ValueKey('chat-peek-$first')), - const Offset(0, -40), + scrolls['a']!.position.viewportDimension, + closeTo(peek.height, .5), ); + expect(find.byKey(const ValueKey('bar-a')), findsNothing); + await tester.dragFrom(peek.center, const Offset(0, -90)); await tester.pumpAndSettle(); expect(scrolls['a']!.offset, greaterThan(0)); - expect(controller.activePane?.sessionId, 'b'); - // A tap, even on a button inside, opens the chat and presses nothing. - await tester.tap(find.byKey(const ValueKey('button-a'))); - expect(controller.activePane?.sessionId, 'a'); - expect(pressed, isEmpty); - await tester.pump(); - // During the rise the leaving bar fades out in place (its list moves - // only once covered) and the arriving bar fades in over a list that - // already keeps clear of it. - await tester.pump(const Duration(milliseconds: 80)); - expect(bar('b'), findsOneWidget); - expect(bar('a'), findsOneWidget); - double opacity(String id) => tester - .widget( - find - .ancestor(of: bar(id), matching: find.byType(FadeTransition)) - .first, - ) - .opacity - .value; - expect(opacity('b'), inExclusiveRange(0, 1)); - expect(opacity('a'), inExclusiveRange(0, 1)); - expect(opacity('a'), closeTo(1 - opacity('b'), .001)); - expect( - tester.getRect(find.byKey(const ValueKey('row-b-0'))).top, - tester.getRect(_rect(second)).top + 48, - ); - expect( - tester.getRect(find.byKey(const ValueKey('row-a-0'))).top, - tester.getRect(_rect(first)).top + 48 - scrolls['a']!.offset, - ); + expect(controller.activePaneId, b); + scrolls['a']!.jumpTo(scrolls['a']!.position.maxScrollExtent); await tester.pumpAndSettle(); - expect(bar('a'), findsOneWidget); - expect(bar('b'), findsNothing); - expect(opacity('a'), 1); - expect( - tester.getRect(find.byKey(const ValueKey('row-b-0'))).top, - tester.getRect(_rect(second)).top, - ); - // The active chat's button still works. - await tester.tap(find.byKey(const ValueKey('button-a'))); + final last = tester.getRect(find.byKey(const ValueKey('row-a-39'))); + expect(last.bottom, closeTo(peek.bottom, .5)); + expect(last.top, greaterThanOrEqualTo(peek.top)); + scrolls['a']!.jumpTo(300); await tester.pump(); - expect(pressed, ['a']); - final semantics = tester.ensureSemantics(); - await tester.pump(); - final peek = find.byKey(ValueKey('chat-peek-$second')); + await tester.tap(find.byKey(ValueKey('chat-peek-$a'))); + await tester.pumpAndSettle(); + expect(controller.activePaneId, a); + expect(scrolls['a']!.offset, 300); + controller.activate(b); + await tester.pumpAndSettle(); + expect(scrolls['a']!.offset, 300); expect( - tester.getSemantics(peek), - matchesSemantics( - identifier: 'chat-peek-$second', - label: 'Expand b', - isButton: true, - hasTapAction: true, - ), + scrolls['a']!.position.viewportDimension, + closeTo(peek.height, .5), ); - semantics.dispose(); + expect(pressed, 0); await tester.pumpWidget(const SizedBox()); controller.dispose(); + for (final scroll in scrolls.values) { + scroll.dispose(); + } }, ); } diff --git a/app/tool/capture/README.md b/app/tool/capture/README.md index a5e64c8d..60ca297f 100644 --- a/app/tool/capture/README.md +++ b/app/tool/capture/README.md @@ -157,3 +157,10 @@ The tree has an assets subfolder, a hidden folder, PNG/JPEG miniatures, text, JSON, CSV, PDF, archive, log and video entries. Image/text/table/archive files have bundled bytes; PDF and video entries demonstrate type labels and menus only. Use the real server for their external handlers and playback acceptance. + +For mobile peek regression captures, `chatPaneScrollMetrics()` returns every +retained transcript's `{id,pixels,min,max,viewport,top,height}` as a JSON array. +Open a chat, use Add chat pane, then pick the other fixture chat. A wheel/swipe +over the inactive pane must change its own offset without activating it; +at its bottom `pixels` reaches `max` and its `viewport` equals the small +visible height. Use `film=key:chat-peek-` to record the swap. diff --git a/app/tool/perf/chat_scroll_probe.dart b/app/tool/perf/chat_scroll_probe.dart index 6417a231..e28e6c3d 100644 --- a/app/tool/perf/chat_scroll_probe.dart +++ b/app/tool/perf/chat_scroll_probe.dart @@ -21,6 +21,9 @@ import 'package:pipelines_app/features/chat/ui/transcript/chat_injected_rows.dar @JS('chatScrollMetrics') external set _chatScrollMetrics(JSFunction probe); +@JS('chatPaneScrollMetrics') +external set _chatPaneScrollMetrics(JSFunction probe); + @JS('chatScrollTargets') external set _chatScrollTargets(JSFunction probe); @@ -29,6 +32,7 @@ external set _chatSidewaysOffsets(JSFunction probe); void installChatScrollProbe() { _chatScrollMetrics = (() => _metrics()?.toJS).toJS; + _chatPaneScrollMetrics = (() => _paneMetrics().toJS).toJS; _chatScrollTargets = (() => _targets().toJS).toJS; _chatSidewaysOffsets = (() => _sideways().toJS).toJS; } @@ -152,3 +156,44 @@ String? _metrics() { 'viewport': position.viewportDimension, }); } + +/// All retained transcript viewports, so a capture can prove that the inactive +/// peek reaches its actual bottom without activating a chat or mutating data. +String _paneMetrics() { + final panes = >[]; + void visit(Element element) { + if (element.widget is ChatTranscriptScrollView) { + ScrollPosition? position; + void inside(Element child) { + if (child is StatefulElement && child.state is ScrollableState) { + final candidate = (child.state as ScrollableState).position; + if (candidate.axis == Axis.vertical) position ??= candidate; + } + child.visitChildren(inside); + } + + element.visitChildren(inside); + final scroll = position; + final box = element.findRenderObject(); + if (scroll != null && + scroll.hasContentDimensions && + box is RenderBox && + box.hasSize) { + panes.add({ + 'id': (element.widget.key as ValueKey?)?.value.toString(), + 'pixels': scroll.pixels, + 'min': scroll.minScrollExtent, + 'max': scroll.maxScrollExtent, + 'viewport': scroll.viewportDimension, + 'top': box.localToGlobal(Offset.zero).dy, + 'height': box.size.height, + }); + } + return; + } + element.visitChildren(visit); + } + + WidgetsBinding.instance.rootElement?.visitChildren(visit); + return jsonEncode(panes); +} diff --git a/docs/notes/mobile-chat-peek.md b/docs/notes/mobile-chat-peek.md new file mode 100644 index 00000000..6bd36494 --- /dev/null +++ b/docs/notes/mobile-chat-peek.md @@ -0,0 +1,15 @@ + +## Viewport and motion correction + +The inactive peek now receives its visible height as real layout constraints. +A full active-height OverflowBox had understated its scroll extent, clipping +newest content even at the list's measured bottom. Both header and composer +remain mounted but release their occupied height as they fade; the resting +peek is transcript-only. Reading offsets and drafts survive role changes. + +Both panes and their chrome now share one 360 ms easing curve, with no late +second phase. Interrupted switches interpolate from current geometry and +opacity. Reduced motion settles immediately; the global scheduler speed +preference scales ticker time once. Repaint boundaries and stable sibling +keys retain pictures and state, while real viewport resizing intentionally +lays out scroll content during the short transition.