From 357affe2073e21ba57ee8fd8d6a67f5fab07f8e6 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 7 Nov 2026 01:17:38 +0000 Subject: [PATCH] Phone chat peek: transcript-only peek, tap-or-scroll, animated switch The inactive (peek) pane no longer shows the chat's bar: ChatPanePresentation tells the conversation its role and ChatPaneBarReveal keeps a resting peek's bar offstage (zero height, not painted), so the peek reads its transcript from the top. A drag on the peek scrolls that transcript; a tap anywhere on it, even over a button or link, activates the chat (ChatPeekTapRecognizer accepts on release ahead of the arena sweep and yields to drags past the slop). The 'chat-peek-' semantics stay a labelled button. Switching animates (260 ms, Cubic(.2,.8,.2,1)): the tapped chat rises over the active one with its bottom edge pinned, then the covered chat slides up into the slot from the bottom edge; each bar fades and slides in or out over the rise. Content boxes keep their fixed size, so ticks move and clip retained pictures without laying a transcript out again. Reduced motion jumps. The old focus stripe is gone. --- .../conversation/chat_pane_presentation.dart | 75 +++++ .../chat/conversation/chat_thread_view.dart | 3 +- .../chat/workspace/chat_mobile_panes.dart | 168 ++++------- .../workspace/chat_pane_switch_motion.dart | 154 ++++++++++ .../chat/workspace/chat_peek_gesture.dart | 102 +++++++ .../chat/workspace/chat_workspace.dart | 148 +++++---- .../chat_pane_presentation_test.dart | 122 ++++++++ .../workspace/chat_mobile_panes_test.dart | 284 +++++++++++++++--- 8 files changed, 832 insertions(+), 224 deletions(-) create mode 100644 app/lib/features/chat/conversation/chat_pane_presentation.dart create mode 100644 app/lib/features/chat/workspace/chat_pane_switch_motion.dart create mode 100644 app/lib/features/chat/workspace/chat_peek_gesture.dart create mode 100644 app/test/features/chat/conversation/chat_pane_presentation_test.dart diff --git a/app/lib/features/chat/conversation/chat_pane_presentation.dart b/app/lib/features/chat/conversation/chat_pane_presentation.dart new file mode 100644 index 00000000..f878a888 --- /dev/null +++ b/app/lib/features/chat/conversation/chat_pane_presentation.dart @@ -0,0 +1,75 @@ +import 'package:flutter/widgets.dart'; + +/// How a retained chat presents itself where several share the screen. +/// +/// The active chat shows its bar. A peek (the other chat glimpsed below it on +/// a phone) shows transcript only, from its top edge, so the glimpse reads as +/// conversation rather than as a second set of controls. [barReveal] runs +/// once when the roles swap: 0 is a hidden bar and 1 a shown one, so the +/// newly active chat's bar fades and slides in while the peek's fades out. +class ChatPanePresentation extends InheritedWidget { + const ChatPanePresentation({ + super.key, + required this.peek, + required this.barReveal, + required super.child, + }); + + final bool peek; + final Animation barReveal; + + static ChatPanePresentation? maybeOf(BuildContext context) => + context.dependOnInheritedWidgetOfExactType(); + + @override + bool updateShouldNotify(ChatPanePresentation oldWidget) => + peek != oldWidget.peek || barReveal != oldWidget.barReveal; +} + +/// 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). +class ChatPaneBarReveal extends StatelessWidget { + const ChatPaneBarReveal({super.key, required this.bar}); + + final Widget bar; + + /// How far above its place the bar starts, as a share of its height. + static const double slide = .12; + + @override + Widget build(BuildContext context) { + final presentation = ChatPanePresentation.maybeOf(context); + if (presentation == null) return bar; + final reveal = presentation.barReveal; + final peek = presentation.peek; + // One tree in both roles, so the bar's element outlives a switch. + return AnimatedBuilder( + 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), + ), + ), + ), + builder: (context, child) => + Offstage(offstage: peek && reveal.value <= 0, child: child), + ); + } +} diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 5eef36ef..3cf39196 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -48,6 +48,7 @@ import '../../../ui/adaptive/segmented_bar_overlay.dart'; import '../../../ui/layout/app_sheet_header.dart'; import '../../../ui/layout/wheel_scroll_forwarding.dart'; import 'chat_reading_width.dart'; +import 'chat_pane_presentation.dart'; import 'chat_thread_header.dart'; import 'chat_transcript_anchor.dart'; import 'chat_transcript_cache.dart'; @@ -967,7 +968,7 @@ class _ChatThreadViewState extends State { final page = FloatingBarOverlay( bar: composerBar, child: SegmentedBarOverlay( - bar: _header(context, inShellBar), + bar: ChatPaneBarReveal(bar: _header(context, inShellBar)), child: NotificationListener( onNotification: _onScrollMetrics, 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 18eee553..8e297338 100644 --- a/app/lib/features/chat/workspace/chat_mobile_panes.dart +++ b/app/lib/features/chat/workspace/chat_mobile_panes.dart @@ -1,8 +1,8 @@ -import 'package:flutter/semantics.dart'; import 'dart:math' as math; -import 'package:flutter/cupertino.dart'; -import 'package:flutter/material.dart'; -import '../ui/sessions/chat_adaptive.dart'; +import 'package:flutter/semantics.dart'; +import 'package:flutter/widgets.dart'; +import '../conversation/chat_pane_presentation.dart'; +import 'chat_peek_gesture.dart'; import 'chat_workspace_controller.dart'; /// A phone presentation only: saved split axes and ratios are left untouched. @@ -14,6 +14,7 @@ class ChatMobilePanes { required Size size, }) { final active = panes.firstWhere((pane) => pane.id == activeId); + this.activeId = active.id; final peek = panes .where((pane) => pane.id == peekId && pane.id != activeId) @@ -25,6 +26,7 @@ class ChatMobilePanes { return; } this.peekId = peek.id; + this.activeId = active.id; const gap = 6.0; final available = math.max(0.0, size.height - gap); final peekHeight = available * .2; @@ -41,147 +43,85 @@ class ChatMobilePanes { late final double contentHeight; final rects = {}; String? peekId; + String? activeId; + + /// [panes] with the active one last: it is drawn over the other during a + /// switch and takes pointers first. + List paintOrder(List panes) => [ + for (final pane in panes) + if (pane.id != activeId) pane, + for (final pane in panes) + if (pane.id == activeId) pane, + ]; } -/// Both mobile threads retain the same full content layout. Selection changes -/// positions once; only a tiny focus cue animates, so transcript layout, scroll -/// anchoring and backdrop painting do not repeat on every animation tick. -class ChatMobilePaneViewport extends StatefulWidget { +/// 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. +class ChatMobilePaneViewport extends StatelessWidget { const ChatMobilePaneViewport({ super.key, required this.peek, required this.active, - required this.animate, + required this.barReveal, required this.child, required this.onActivate, required this.label, required this.paneId, this.contentHeight, }); + final bool peek; final bool active; - final bool animate; + + /// The bar's reveal for this pane's role (see [ChatPanePresentation]). + final Animation barReveal; final Widget child; final VoidCallback onActivate; final String label; final String paneId; final double? contentHeight; - @override - State createState() => _ChatMobilePaneViewportState(); -} - -class _ChatMobilePaneViewportState extends State - with SingleTickerProviderStateMixin { - late final AnimationController _reveal; - late final CurvedAnimation _accent; - @override - void initState() { - super.initState(); - _reveal = AnimationController( - vsync: this, - duration: const Duration(milliseconds: 120), - value: 1, - ); - _accent = CurvedAnimation(parent: _reveal, curve: Curves.easeOutCubic); - } - - @override - void didUpdateWidget(covariant ChatMobilePaneViewport oldWidget) { - super.didUpdateWidget(oldWidget); - if (!widget.animate || !widget.active) { - _reveal.value = 1; - } else if (!oldWidget.active) { - _reveal.forward(from: 0); - } - } - - @override - void dispose() { - _accent.dispose(); - _reveal.dispose(); - super.dispose(); - } @override Widget build(BuildContext context) => ClipRect( - child: Stack( - fit: StackFit.expand, - children: [ - OverflowBox( + child: Semantics( + key: ValueKey('chat-peek-$paneId'), + identifier: peek ? 'chat-peek-$paneId' : null, + container: peek, + sortKey: peek ? const OrdinalSortKey(1) : null, + label: peek ? 'Expand $label' : null, + button: peek ? true : null, + onTap: peek ? onActivate : null, + child: ChatPeekGesture( + onTap: peek ? onActivate : null, + child: OverflowBox( alignment: Alignment.topCenter, - minHeight: widget.contentHeight, - maxHeight: widget.contentHeight, + minHeight: contentHeight, + maxHeight: contentHeight, child: SizedBox( - height: widget.contentHeight, + height: contentHeight, child: ExcludeFocus( - excluding: widget.peek, + excluding: peek, child: ExcludeSemantics( - excluding: widget.peek, - child: IgnorePointer( - ignoring: widget.peek, - child: RepaintBoundary(child: widget.child), - ), - ), - ), - ), - ), - if (widget.peek) - Semantics( - key: ValueKey('chat-peek-${widget.paneId}'), - identifier: 'chat-peek-${widget.paneId}', - container: true, - sortKey: const OrdinalSortKey(1), - label: 'Expand ${widget.label}', - button: true, - child: GestureDetector( - behavior: HitTestBehavior.opaque, - onTap: widget.onActivate, - ), - ), - if (widget.active && widget.animate) - Positioned( - left: 0, - right: 0, - top: 0, - height: 2, - child: IgnorePointer( - child: RepaintBoundary( - child: CustomPaint( - key: ValueKey('chat-focus-accent-${widget.paneId}'), - painter: ChatPaneFocusAccent( - progress: _accent, - color: - chatControlsStyleOf(context) == - ChatControlsStyle.cupertino - ? CupertinoTheme.of(context).primaryColor - : Theme.of(context).colorScheme.primary, + excluding: peek, + child: RepaintBoundary( + child: ChatPanePresentation( + peek: peek, + barReveal: barReveal, + child: child, ), ), ), ), ), - ], + ), + ), ), ); } - -/// Only this tiny focus stripe repaints during the transition; the rich chat -/// body remains opaque and stationary, avoiding a full-pane alpha/saveLayer. -class ChatPaneFocusAccent extends CustomPainter { - ChatPaneFocusAccent({required this.progress, required this.color}) - : super(repaint: progress); - final Animation progress; - final Color color; - @override - void paint(Canvas canvas, Size size) { - if (progress.value >= 1) return; - canvas.drawRect( - Offset.zero & size, - Paint()..color = color.withValues(alpha: (1 - progress.value) * .7), - ); - } - - @override - bool shouldRepaint(covariant ChatPaneFocusAccent oldDelegate) => - oldDelegate.color != color || oldDelegate.progress != progress; -} diff --git a/app/lib/features/chat/workspace/chat_pane_switch_motion.dart b/app/lib/features/chat/workspace/chat_pane_switch_motion.dart new file mode 100644 index 00000000..cb299567 --- /dev/null +++ b/app/lib/features/chat/workspace/chat_pane_switch_motion.dart @@ -0,0 +1,154 @@ +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. +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; + + 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; + + Map _from = const {}; + Map _to = const {}; + String? _activeId; + Size? _size; + + bool get isAnimating => _controller.isAnimating; + + /// 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, + required String activeId, + required bool switching, + required bool animate, + }) { + 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]!, + }; + _to = rects; + _activeId = activeId; + _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) { + _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]; + } + + /// 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); + } + + void dispose() { + rise.dispose(); + settle.dispose(); + _controller.dispose(); + } +} diff --git a/app/lib/features/chat/workspace/chat_peek_gesture.dart b/app/lib/features/chat/workspace/chat_peek_gesture.dart new file mode 100644 index 00000000..5fa72458 --- /dev/null +++ b/app/lib/features/chat/workspace/chat_peek_gesture.dart @@ -0,0 +1,102 @@ +import 'package:flutter/gestures.dart'; +import 'package:flutter/widgets.dart'; + +/// Taps on the peek open that chat; drags still reach its transcript. +/// +/// Placed above the peek's content, the recognizer joins every pointer's arena +/// along with the transcript's own recognizers. A move past the touch slop +/// rejects it, so the scrollable takes the drag as usual. A release without +/// one accepts it at once, ahead of the sweep that would otherwise hand the +/// tap to a button or link under the finger: a tap anywhere on the peek +/// activates the chat and nothing inside it is pressed. +class ChatPeekTapRecognizer extends OneSequenceGestureRecognizer { + ChatPeekTapRecognizer({super.debugOwner, super.supportedDevices}); + + VoidCallback? onTap; + Offset? _origin; + bool _won = false; + bool _released = false; + + @override + void addAllowedPointer(PointerDownEvent event) { + startTrackingPointer(event.pointer, event.transform); + _origin = event.position; + _won = false; + _released = false; + } + + @override + void handleEvent(PointerEvent event) { + if (event is PointerMoveEvent) { + final slop = computeHitSlop(event.kind, gestureSettings); + if ((event.position - _origin!).distance > slop) { + resolve(GestureDisposition.rejected); + stopTrackingPointer(event.pointer); + } + } else if (event is PointerUpEvent) { + _released = true; + if (_won) { + _tap(); + } else { + // Alone or not, the win must come before the arena's sweep. + resolve(GestureDisposition.accepted); + } + stopTrackingPointer(event.pointer); + } else if (event is PointerCancelEvent) { + resolve(GestureDisposition.rejected); + stopTrackingPointer(event.pointer); + } + } + + @override + void acceptGesture(int pointer) { + // As the arena's only member the win arrives on the press; the tap still + // waits for the release, and a drag past the slop cancels it. + if (_released) { + _tap(); + } else { + _won = true; + } + } + + @override + void rejectGesture(int pointer) => stopTrackingPointer(pointer); + + @override + void didStopTrackingLastPointer(int pointer) { + _origin = null; + _won = false; + _released = false; + } + + void _tap() { + if (onTap case final tap?) invokeCallback('onTap', tap); + } + + @override + String get debugDescription => 'chat peek tap'; +} + +/// Lets [child] (the peek's content) scroll under a drag while a tap calls +/// [onTap]; with no [onTap] it only passes pointers through. +class ChatPeekGesture extends StatelessWidget { + const ChatPeekGesture({super.key, required this.onTap, required this.child}); + + final VoidCallback? onTap; + final Widget child; + + @override + Widget build(BuildContext context) => RawGestureDetector( + behavior: HitTestBehavior.opaque, + gestures: onTap == null + ? const {} + : { + ChatPeekTapRecognizer: + GestureRecognizerFactoryWithHandlers( + () => ChatPeekTapRecognizer(debugOwner: this), + (recognizer) => recognizer.onTap = onTap, + ), + }, + child: child, + ); +} diff --git a/app/lib/features/chat/workspace/chat_workspace.dart b/app/lib/features/chat/workspace/chat_workspace.dart index acd74157..461c9b80 100644 --- a/app/lib/features/chat/workspace/chat_workspace.dart +++ b/app/lib/features/chat/workspace/chat_workspace.dart @@ -7,6 +7,7 @@ import 'chat_workspace_controller.dart'; import 'chat_workspace_divider.dart'; import 'chat_workspace_geometry.dart'; import 'chat_mobile_panes.dart'; +import 'chat_pane_switch_motion.dart'; export 'chat_workspace_controller.dart'; export 'chat_workspace_geometry.dart' show ChatPaneDirection; @@ -44,9 +45,11 @@ class ChatWorkspace extends StatefulWidget { State createState() => _ChatWorkspaceState(); } -class _ChatWorkspaceState extends State { +class _ChatWorkspaceState extends State + with SingleTickerProviderStateMixin { final _focus = FocusNode(debugLabel: 'Chat workspace'); final Map _panes = {}; + late final ChatPaneSwitchMotion _motion; ChatWorkspaceGeometry? _geometry; String? _lastActiveId; String? _peekId; @@ -54,6 +57,7 @@ class _ChatWorkspaceState extends State { @override void initState() { super.initState(); + _motion = ChatPaneSwitchMotion(vsync: this); HardwareKeyboard.instance.addHandler(_key); } @@ -103,6 +107,7 @@ class _ChatWorkspaceState extends State { @override void dispose() { HardwareKeyboard.instance.removeHandler(_key); + _motion.dispose(); _focus.dispose(); for (final node in _panes.values) { node.dispose(); @@ -124,7 +129,8 @@ class _ChatWorkspaceState extends State { final collapsed = widget.singlePane || geometry.collapsed; final panes = widget.controller.panes; final activeId = widget.controller.activePaneId; - if (_lastActiveId != activeId) { + final switching = _lastActiveId != activeId; + if (switching) { _peekId = _lastActiveId; _lastActiveId = activeId; } @@ -136,76 +142,42 @@ class _ChatWorkspaceState extends State { size: constraints.biggest, ) : null; + if (mobile == null) { + _motion.rest(); + } else { + _motion.layout( + rects: mobile.rects, + size: constraints.biggest, + activeId: activeId!, + switching: switching, + animate: !MediaQuery.disableAnimationsOf(context), + ); + } final existing = panes.map((p) => p.id).toSet(); for (final id in _panes.keys.where((id) => !existing.contains(id)).toList()) { final node = _panes.remove(id)!; WidgetsBinding.instance.addPostFrameCallback((_) => node.dispose()); } + final ordered = mobile?.paintOrder(panes) ?? panes; return Stack( fit: StackFit.expand, clipBehavior: Clip.hardEdge, children: [ - for (final pane in panes) - Positioned.fromRect( + 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}'), - rect: - mobile?.rects[pane.id] ?? - (collapsed - ? Offset.zero & constraints.biggest - : geometry.panes[pane.id]!), - child: Offstage( - offstage: mobile != null - ? !mobile.rects.containsKey(pane.id) - : collapsed && - pane.id != widget.controller.activePaneId, - child: ExcludeFocus( - excluding: - collapsed && - pane.id != widget.controller.activePaneId, - child: ChatMobilePaneViewport( - peek: mobile?.peekId == pane.id, - active: pane.id == activeId, - animate: - mobile != null && - !MediaQuery.disableAnimationsOf(context), - contentHeight: mobile?.contentHeight, - label: widget.paneTitle?.call(pane) ?? pane.sessionId, - paneId: pane.id, - onActivate: () { - FocusManager.instance.primaryFocus?.unfocus(); - widget.controller.activate(pane.id); - }, - child: FocusScope( - node: _panes.putIfAbsent( - pane.id, - () => FocusScopeNode(debugLabel: pane.id), - ), - onFocusChange: (focused) { - if (focused && !widget.mobilePeeks) { - widget.controller.activate(pane.id); - } - }, - child: Listener( - behavior: HitTestBehavior.translucent, - onPointerDown: (_) => - widget.controller.activate(pane.id), - child: Semantics( - identifier: 'chat-pane-${pane.id}', - container: true, - sortKey: mobile == null - ? null - : const OrdinalSortKey(0), - child: widget.paneBuilder( - context, - pane, - pane.id == widget.controller.activePaneId, - ), - ), - ), - ), - ), - ), + 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) @@ -225,4 +197,58 @@ class _ChatWorkspaceState extends State { ), ), ); + + Widget _paneView( + ChatWorkspacePane pane, + ChatMobilePanes? mobile, + bool collapsed, + String? activeId, + ) { + final peek = mobile?.peekId == pane.id; + final active = pane.id == activeId; + return Offstage( + offstage: mobile != null + ? !mobile.rects.containsKey(pane.id) + : collapsed && !active, + child: ExcludeFocus( + excluding: collapsed && !active, + child: ChatMobilePaneViewport( + peek: peek, + active: active, + barReveal: peek ? _motion.recede : _motion.rise, + contentHeight: mobile?.contentHeight, + label: widget.paneTitle?.call(pane) ?? pane.sessionId, + paneId: pane.id, + onActivate: () { + FocusManager.instance.primaryFocus?.unfocus(); + widget.controller.activate(pane.id); + }, + child: FocusScope( + node: _panes.putIfAbsent( + pane.id, + () => FocusScopeNode(debugLabel: pane.id), + ), + onFocusChange: (focused) { + if (focused && !widget.mobilePeeks) { + widget.controller.activate(pane.id); + } + }, + child: Listener( + behavior: HitTestBehavior.translucent, + // A press in the peek scrolls or taps; the tap alone activates. + onPointerDown: peek + ? null + : (_) => widget.controller.activate(pane.id), + child: Semantics( + identifier: 'chat-pane-${pane.id}', + container: true, + sortKey: mobile == null ? null : const OrdinalSortKey(0), + child: widget.paneBuilder(context, pane, active), + ), + ), + ), + ), + ), + ); + } } diff --git a/app/test/features/chat/conversation/chat_pane_presentation_test.dart b/app/test/features/chat/conversation/chat_pane_presentation_test.dart new file mode 100644 index 00000000..1d6b84cd --- /dev/null +++ b/app/test/features/chat/conversation/chat_pane_presentation_test.dart @@ -0,0 +1,122 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_pane_presentation.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_transcript_scroll_view.dart'; + +import 'chat_thread_fixtures.dart'; + +/// The conversation's bar under a workspace presentation: shown for the +/// active chat, offstage for a resting peek (whose transcript starts at the +/// top), fading out in place while the chat is becoming the peek. + +Future _pump( + WidgetTester tester, { + required bool peek, + required double reveal, +}) async { + final gateway = FakeChatGateway(); + addTearDown(gateway.close); + gateway.history = (_) async => chatPage([ + for (var index = 0; index < 4; index++) + chatMessage( + 'm$index', + index.isEven ? 'user' : 'assistant', + parts: [ + {'type': 'text', 'text': 'Message $index'}, + ], + ), + ]); + gateway.subscribe = () async => const {}; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ChatPanePresentation( + peek: peek, + barReveal: AlwaysStoppedAnimation(reveal), + child: ChatThreadView( + gateway: gateway, + sessionId: 's', + title: 'Presented', + onBack: () {}, + ), + ), + ), + ), + ); + await tester.pumpAndSettle(); +} + +void main() { + final header = find.byKey(const Key('chat-thread-header')); + double listTop(WidgetTester tester) => tester + .widget( + find.byKey(const ValueKey('chat-thread-s')), + ) + .padding + .top; + + testWidgets('the active chat shows its bar and keeps the list clear', ( + tester, + ) async { + await _pump(tester, peek: false, reveal: 1); + expect(header, findsOneWidget); + expect(listTop(tester), greaterThan(8)); + expect(listTop(tester), closeTo(tester.getSize(header).height + 8, .5)); + }); + + testWidgets('a resting peek keeps its bar offstage and reads from the top', ( + tester, + ) async { + await _pump(tester, peek: true, reveal: 0); + expect(header, findsNothing); + expect( + find.byKey(const Key('chat-thread-header'), skipOffstage: false), + findsOneWidget, + ); + expect(listTop(tester), 8); + 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('without a presentation the bar is itself', (tester) async { + final gateway = FakeChatGateway(); + addTearDown(gateway.close); + gateway.history = (_) async => chatPage(const []); + gateway.subscribe = () async => const {}; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ChatThreadView( + gateway: gateway, + sessionId: 's', + title: 'Alone', + onBack: () {}, + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(header, findsOneWidget); + Widget? child; + tester + .element(find.byType(ChatPaneBarReveal)) + .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 5618cca5..f6909b91 100644 --- a/app/test/features/chat/workspace/chat_mobile_panes_test.dart +++ b/app/test/features/chat/workspace/chat_mobile_panes_test.dart @@ -1,13 +1,18 @@ +import 'dart:math' as math; + import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_pane_presentation.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_pane_switch_motion.dart'; import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; -import 'package:pipelines_app/features/chat/workspace/chat_mobile_panes.dart'; +import 'package:pipelines_app/ui/adaptive/segmented_bar_overlay.dart'; Widget _host( ChatWorkspaceController controller, { bool mobile = true, bool reduced = false, + ChatWorkspacePaneBuilder? paneBuilder, }) => MaterialApp( home: MediaQuery( data: MediaQueryData(disableAnimations: reduced), @@ -18,7 +23,9 @@ Widget _host( controller: controller, singlePane: mobile, mobilePeeks: mobile, - paneBuilder: (_, pane, active) => _Draft(key: ValueKey(pane.sessionId)), + paneBuilder: + paneBuilder ?? + (_, pane, active) => _Draft(key: ValueKey(pane.sessionId)), ), ), ), @@ -73,10 +80,68 @@ class _CountLayouts extends RenderProxyBox { } } +/// A conversation stand-in: a bar over a list that keeps clear of it, with a +/// button as its first row. +class _Thread extends StatelessWidget { + const _Thread({ + super.key, + required this.id, + required this.scroll, + required this.onPressed, + }); + final String id; + final ScrollController scroll; + final VoidCallback onPressed; + @override + Widget build(BuildContext context) => Material( + child: SegmentedBarOverlay( + bar: ChatPaneBarReveal( + bar: SizedBox(key: ValueKey('bar-$id'), height: 48), + ), + child: Builder( + builder: (context) => ListView( + controller: scroll, + padding: EdgeInsets.only(top: MediaQuery.paddingOf(context).top), + children: [ + SizedBox( + key: ValueKey('row-$id-0'), + height: 40, + child: TextButton( + key: ValueKey('button-$id'), + onPressed: onPressed, + child: const Text('Row'), + ), + ), + for (var row = 1; row < 40; row++) + SizedBox(key: ValueKey('row-$id-$row'), height: 40), + ], + ), + ), + ), + ); +} + +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')); void main() { testWidgets( - 'active chat promotes to top with small focus accent and stable thread layout', + 'active chat travels to the top on the switch curve with a stable thread layout', (tester) async { tester.view.physicalSize = const Size(560, 800); tester.view.devicePixelRatio = 1; @@ -103,9 +168,9 @@ void main() { ), 'Draft b', ); - final oldHeight = tester.getSize(_rect(first)).height; - expect(oldHeight, closeTo((800 - 6) * .2, 1)); - expect(tester.getSize(_rect(second)).height, closeTo((800 - 6) * .8, 1)); + 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( @@ -113,49 +178,54 @@ void main() { 'a', reason: 'tap activates immediately', ); + // The switch starts where the panes are: the tapped chat rises over + // the other, which stays put until covered. await tester.pump(); - expect(tester.getRect(_rect(first)).top, 0); - expect(tester.getSize(_rect(first)).height, closeTo((800 - 6) * .8, 1)); - expect(tester.getRect(_rect(second)).top, closeTo((800 - 6) * .8 + 6, 1)); + 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 accentFinder = find.byKey(ValueKey('chat-focus-accent-$first')); - final accent = - tester.widget(accentFinder).painter! - as ChatPaneFocusAccent; - expect(tester.getSize(accentFinder).height, 2); - expect(accent.progress.value, greaterThan(0)); - expect(accent.progress.value, lessThan(1)); + final firstTop = tester.getRect(_rect(first)).top; + expect(firstTop, closeTo(_peekTop * (1 - _rise(60)), 1)); + expect(tester.getRect(_rect(first)).bottom, 800); expect( - find.descendant( - of: find.byType(ChatWorkspace), - matching: find.byType(FadeTransition), - ), - findsNothing, + 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( - find.descendant( - of: find.byType(ChatWorkspace), - matching: find.byType(SlideTransition), - ), - findsNothing, + tester.getRect(_rect(first)).top, + closeTo(_peekTop * (1 - _rise(120)), 1), ); - // The short animation only composites retained content. In particular, - // neither transcript nor composer receives a new size on each tick. + expect(tester.getRect(_rect(second)).top, 0); + // The run only composites retained content: neither transcript nor + // composer receives a new size on any tick. expect( tester.getSize(find.byKey(const ValueKey('a'))).height, - closeTo((800 - 6) * .8, 1), + closeTo(_top, 1), ); expect( tester.getSize(find.byKey(const ValueKey('b'))).height, - closeTo((800 - 6) * .8, 1), + 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); await tester.pumpAndSettle(); expect(controller.activePane?.sessionId, 'a'); - expect(tester.getSize(_rect(first)).height, closeTo((800 - 6) * .8, 1)); 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'); @@ -171,26 +241,29 @@ void main() { .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(); - expect(tester.getRect(_rect(second)).top, 0); - await tester.pump(const Duration(milliseconds: 20)); - await tester.tap(find.byKey(ValueKey('chat-peek-$first'))); + 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 - .widget( - find.byKey(ValueKey('chat-focus-accent-$second')), - ) - .painter! - as ChatPaneFocusAccent) - .progress - .value, - 1, - ); + expect(tester.getRect(_rect(first)).top, closeTo(_peekTop, 1)); expect(controller.snapshot()!.encode(), saved); await tester.pumpWidget(_host(controller, mobile: false)); await tester.pumpAndSettle(); @@ -203,6 +276,7 @@ void main() { controller.dispose(); }, ); + testWidgets( 'three retained panes expose active+previous and honor reduced motion', (tester) async { @@ -219,17 +293,131 @@ void main() { final third = controller.activePaneId!; controller.activate(first); await tester.pump(); - expect(tester.getSize(_rect(first)).height, closeTo((800 - 6) * .8, 1)); + // 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(find.byKey(ValueKey('chat-focus-accent-$first')), findsNothing); + 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((800 - 6) * .8, 1)); + 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); + 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!; + controller.splitActive('b', ChatSplitAxis.horizontal); + final second = controller.activePaneId!; + Finder bar(String id) => find.byKey(ValueKey('bar-$id')); + await tester.pumpWidget( + _host( + controller, + paneBuilder: (_, pane, active) => _Thread( + key: ValueKey(pane.sessionId), + id: pane.sessionId, + scroll: scrolls[pane.sessionId]!, + onPressed: () => pressed.add(pane.sessionId), + ), + ), + ); + 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); + 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), + ); + 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, + ); + 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'))); + await tester.pump(); + expect(pressed, ['a']); + final semantics = tester.ensureSemantics(); + await tester.pump(); + final peek = find.byKey(ValueKey('chat-peek-$second')); + expect( + tester.getSemantics(peek), + matchesSemantics( + identifier: 'chat-peek-$second', + label: 'Expand b', + isButton: true, + hasTapAction: true, + ), + ); + semantics.dispose(); + await tester.pumpWidget(const SizedBox()); + controller.dispose(); + }, + ); }