Merge branch 'fix/mobile-motion'

This commit is contained in:
2026-11-07 02:47:45 +00:00
11 changed files with 409 additions and 458 deletions
@@ -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<Offset>(
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<Offset>(
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),
),
),
),
),
@@ -1072,7 +1072,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
),
);
final page = FloatingBarOverlay(
bar: composerBar,
bar: ChatPaneBarReveal(bar: composerBar),
child: SegmentedBarOverlay(
bar: ChatPaneBarReveal(bar: _header(context, inShellBar)),
child: NotificationListener<ScrollMetricsNotification>(
@@ -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,
),
),
),
@@ -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<double> 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;
}
@@ -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<double> recede;
/// Ticks through both phases, for rebuilding positions.
Animation<double> get animation => _controller.view;
final Map<String, Animation<double>> _reveals = {};
Map<String, Rect> _from = const {};
Map<String, Rect> _to = const {};
String? _activeId;
Map<String, double> _barFrom = const {};
Map<String, double> _barTo = const {};
Size? _size;
bool get isAnimating => _controller.isAnimating;
double get _progress => curve.transform(_controller.value);
Animation<double> 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<String, Rect> 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<double> {
_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);
}
@@ -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<ChatWorkspace>
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<ChatWorkspace>
@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<ChatWorkspace>
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<ChatWorkspace>
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<ChatWorkspace>
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: () {