Quick switch: a new request retargets the switch in flight instead of waiting

Peek taps, chords and programmatic activations during a mobile quick
switch start the next switch at once from what is on screen. Swap styles
continue from each card's current rect, pose, opacity and bar reveal;
the tape takes a fresh picture of the frame above the incoming chat and
continues its loop. The incoming chat keeps its speed and brakes once.
The arriving peek takes pointers while it moves; leaving cards do not.
The capture kit's film step gains a taps option for rapid-tap reviews.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 15:20:47 +00:00
parent daf290e39e
commit fd5d4b783c
14 changed files with 714 additions and 82 deletions
@@ -3,52 +3,92 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import '../../../ui/motion/quick_switch_animation.dart';
import '../../../ui/motion/quick_switch_effect.dart';
import 'chat_switch_ease.dart';
part 'chat_pane_switch_start.dart';
/// A vertical tape or a live card exchange, selected once for each transition.
/// Tape uses an outgoing picture; swaps move the retained chats themselves.
/// Every live transcript exists once and keeps its real visible viewport.
///
/// A switch requested while another is moving starts at once from what is on
/// screen: swaps take each card's current rectangle, pose, opacity and bar as
/// their start; the tape takes a fresh picture of the current frame above the
/// incoming chat. The incoming chat keeps its speed, then brakes once.
class ChatPaneSwitchMotion {
ChatPaneSwitchMotion({required TickerProvider vsync})
: _controller = AnimationController(
vsync: vsync,
duration: duration,
value: 1,
);
) {
_controller.addStatusListener(_settled);
}
static const Duration duration = Duration(milliseconds: 440);
static const Curve curve = _TapeEase();
static const Curve curve = ChatSwitchEase();
final AnimationController _controller;
Animation<double> get animation => _controller.view;
final Map<String, Animation<double>> _reveals = {};
Curve _curve = curve;
Map<String, Rect> _from = const {};
Map<String, Rect> _to = const {};
Map<String, double> _barFrom = const {};
Map<String, double> _barTo = const {};
Map<String, double> _opacityFrom = const {};
Map<String, double> _opacityTo = const {};
/// Interrupted cards' departure from the new path, decaying to nothing.
Map<String, Rect> _residual = const {};
Map<String, QuickSwitchPose> _carried = const {};
Map<String, Size> _viewports = const {};
/// Retained cards leaving the screen besides the previous active chat.
Set<String> _leaving = const {};
Size? _size;
String? _activeId;
String? _outgoingId;
String? _enteringPeekId;
/// The tape's picture region: everything above the incoming chat.
Rect? outgoingSourceRect;
QuickSwitchAnimation _style = QuickSwitchAnimation.tape;
QuickSwitchAnimation get style => _style;
bool get isTape => _style == QuickSwitchAnimation.tape;
double get progress => _progress;
bool get outgoingOnTop => quickSwitchOutgoingOnTop(_style, _progress);
bool isRetainedOutgoing(String id) =>
isAnimating && !isTape && id == _outgoingId;
bool _retained(String id) =>
!isTape && (id == _outgoingId || _leaving.contains(id));
bool isRetainedOutgoing(String id) => isAnimating && _retained(id);
String? get topPaneId => outgoingOnTop ? _outgoingId : _activeId;
double opacityFor(String id) =>
isAnimating && !isTape && id == _enteringPeekId && id != _outgoingId
? _progress
: 1;
double opacityFor(String id) {
if (!isAnimating || isTape) return 1;
final to = _opacityTo[id] ?? 1;
// The chosen chat turns opaque, and cards leaving fade, within the first
// third: interrupted exchanges never stack several translucent cards.
// The arriving peek fades in under the moving cards across the whole run.
final early = id == _activeId || to == 0;
final t = early ? (_progress * 3).clamp(0.0, 1.0) : _progress;
return lerpDouble(_opacityFrom[id] ?? 1, to, t)!;
}
bool get isAnimating => _controller.isAnimating;
double get _progress => curve.transform(_controller.value);
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)!;
/// The card's paint-only pose, including any pose it carried into this
/// transition from an interrupted one.
QuickSwitchPose poseFor(String id, {required bool outgoing}) {
if (!isAnimating) return const QuickSwitchPose();
final pose = quickSwitchPose(_style, _progress, outgoing: outgoing);
final carried = _carried[id];
return carried == null ? pose : pose.carry(carried, 1 - _progress);
}
void layout({
required Map<String, Rect> rects,
required Size size,
@@ -66,25 +106,13 @@ class ChatPaneSwitchMotion {
_activeId != activeId;
_size = size;
if (travel) {
_style = style;
_outgoingId = _activeId;
outgoingSourceRect = _to[_outgoingId];
_enteringPeekId = rects.keys.firstWhere((id) => id != activeId);
final peek = rects[_enteringPeekId]!;
_from = {..._to, activeId: _to[activeId] ?? peek};
_to = rects;
_barFrom = {
for (final id in {...rects.keys, _outgoingId!})
id: !isTape && id == _outgoingId ? 1 : 0,
};
_barTo = {for (final id in rects.keys) id: id == activeId ? 1 : 0};
_activeId = activeId;
_controller.forward(from: 0);
_start(rects, size, activeId, style);
} else if (!mapEquals(rects, _to) || !animate) {
_from = _to = rects;
_activeId = activeId;
_outgoingId = _enteringPeekId = null;
_enteringPeekId = _outgoingId = null;
outgoingSourceRect = null;
_clearTransition();
_barFrom = _barTo = {
for (final id in rects.keys) id: id == activeId ? 1 : 0,
};
@@ -92,60 +120,89 @@ class ChatPaneSwitchMotion {
}
}
void _clearTransition() {
_curve = curve;
_leaving = const {};
_residual = const {};
_carried = const {};
_viewports = const {};
_opacityFrom = _opacityTo = const {};
}
void _settled(AnimationStatus status) {
if (status != AnimationStatus.completed) return;
_outgoingId = null;
_leaving = const {};
_residual = const {};
_carried = const {};
}
void rest() {
if (_to.isEmpty && _size == null) return;
_from = _to = const {};
_barFrom = _barTo = const {};
_size = null;
_activeId = _outgoingId = _enteringPeekId = null;
_clearTransition();
outgoingSourceRect = null;
_controller.value = 1;
}
/// Stable content constraints for paint-only movement during a switch.
/// The outgoing card keeps its source viewport; incoming/preview cards use
/// their destination viewport. At rest this is the actual visible rectangle.
Rect? viewportRectFor(String id) =>
isRetainedOutgoing(id) ? outgoingSourceRect : _to[id] ?? rectFor(id);
/// Stable content size for paint-only movement during a swap. At rest this
/// is the actual visible rectangle.
Size? viewportFor(String id) =>
(isAnimating ? _viewports[id] : null) ??
_to[id]?.size ??
rectFor(id)?.size;
Rect? rectFor(String id) {
Rect? rectFor(String id) => _rectAt(id, _progress);
Rect? _rectAt(String id, double p) {
final to = _to[id];
if (isRetainedOutgoing(id)) return exitingRect;
if (to == null) return null;
if (isTape && _enteringPeekId == id && outgoingSourceRect != null) {
// The next pane in the ring enters from below with a true small
// viewport. With two panes this is the outgoing chat; with more it is
// the next added pane. The old active owns only the snapshot top exit.
Rect rect;
if (_retained(id)) {
final from = _from[id];
if (from == null) return null;
final target = to ?? _to[_enteringPeekId] ?? from;
rect = quickSwitchRect(_style, p, from, target, outgoing: true);
} else if (to == null) {
return null;
} else if (isTape && _enteringPeekId == id && outgoingSourceRect != null) {
// The next pane in the ring follows the incoming chat up from below
// with a true small viewport. With two panes this is the outgoing chat;
// with more it is the next added pane. The old active owns only the
// picture's top exit.
final lead = _rectAt(_activeId!, p)!;
final gap = to.top - _to[_activeId]!.bottom;
return to.translate(0, (_size!.height + gap - to.top) * (1 - _progress));
rect = to.translate(0, lead.bottom + gap - to.top);
} else {
rect = quickSwitchRect(_style, p, _from[id] ?? to, to, outgoing: false);
}
return quickSwitchRect(
_style,
_progress,
_from[id] ?? to,
to,
outgoing: false,
final residual = _residual[id];
if (residual == null) return rect;
final w = 1 - p;
return Rect.fromLTRB(
rect.left + residual.left * w,
rect.top + residual.top * w,
rect.right + residual.right * w,
rect.bottom + residual.bottom * w,
);
}
/// The tape's picture, moving up ahead of the incoming chat.
Rect? get exitingRect {
final source = outgoingSourceRect;
final incoming = _from[_activeId];
if (!isAnimating || source == null || incoming == null) return null;
if (isTape) return source.translate(0, -incoming.top * _progress);
final target = _to[_enteringPeekId] ?? source;
return quickSwitchRect(_style, _progress, source, target, outgoing: true);
if (!isAnimating || !isTape || source == null || incoming == null) {
return null;
}
return source.translate(0, -incoming.top * _progress);
}
void dispose() => _controller.dispose();
}
/// Quintic easing has zero velocity and acceleration at both ends: the tape
/// picks up speed gradually and brakes without a sudden snap near the finish.
class _TapeEase extends Curve {
const _TapeEase();
@override
double transformInternal(double t) => t * t * t * (t * (6 * t - 15) + 10);
void dispose() {
_controller.removeStatusListener(_settled);
_controller.dispose();
}
}
class _PaneReveal extends Animation<double> {
@@ -0,0 +1,140 @@
part of 'chat_pane_switch_motion.dart';
/// Starting a switch, from rest or from wherever an interrupted one has
/// brought the cards.
extension _SwitchStart on ChatPaneSwitchMotion {
void _start(
Map<String, Rect> rects,
Size size,
String activeId,
QuickSwitchAnimation style,
) {
// What is on screen now: the resting layout, or an interrupted switch.
final p = _progress;
final interrupted = isAnimating;
final presented = {
..._to.keys,
if (interrupted) ...{..._leaving, ?_outgoingId}.where(_retained),
};
final rectNow = {for (final id in presented) id: ?_rectAt(id, p)};
final opacityNow = {for (final id in presented) id: opacityFor(id)};
final revealNow = {for (final id in presented) id: _reveal(id)};
final poseNow = {
for (final id in presented)
id: poseFor(id, outgoing: isRetainedOutgoing(id)),
};
final viewportNow = {
for (final id in presented)
id: ?(interrupted ? _viewports[id] : null) ?? _to[id]?.size,
};
final speed = interrupted ? _speed(activeId) : null;
final previous = _activeId!;
_style = style;
_outgoingId = previous;
_activeId = activeId;
_enteringPeekId = rects.keys.firstWhere((id) => id != activeId);
final peek = rects[_enteringPeekId]!;
_to = rects;
_clearTransition();
if (isTape) {
// The picture shows the current frame above the incoming chat; its live
// tree continues from where it is (or the peek's place if unseen).
final lead = rectNow[activeId] ?? peek;
final gap = peek.top - rects[activeId]!.bottom;
_from = {activeId: lead};
outgoingSourceRect = Rect.fromLTRB(
0,
0,
size.width,
(lead.top - gap).clamp(0.0, size.height),
);
_barFrom = {
for (final id in {...rects.keys, previous}) id: 0,
};
_barTo = {for (final id in rects.keys) id: id == activeId ? 1 : 0};
} else {
_leaving = {
for (final id in presented)
if (!rects.containsKey(id) && id != previous) id,
};
final moving = {...rects.keys, previous, ..._leaving};
_from = {
for (final id in moving)
id: rectNow[id] ?? (id == activeId ? peek : rects[id]!),
};
outgoingSourceRect = _from[previous];
_opacityFrom = {
for (final id in moving)
id: presented.contains(id) ? opacityNow[id]! : 0,
};
_opacityTo = {
for (final id in moving)
id: rects.containsKey(id) || id == previous ? 1 : 0,
};
_barFrom = {for (final id in moving) id: revealNow[id] ?? 0};
_barTo = {
for (final id in moving)
id: rects.containsKey(id)
? (id == activeId ? 1 : 0)
: revealNow[id] ?? 0,
};
// Growing cards lay out at their destination; shrinking ones keep the
// viewport they have until they settle into the small preview.
_viewports = {
for (final id in moving)
id:
_SwitchStart._tallerOf(viewportNow[id], rects[id]?.size) ??
_from[id]!.size,
};
_carried = {
for (final MapEntry(key: id, value: pose) in poseNow.entries)
if (!pose.isRest) id: pose,
};
// A fresh switch starts on its style's own path (a deck's next card
// already waits full-size behind the front one); an interrupted one
// eases each card from where it is onto the new path.
_residual = {
if (interrupted)
for (final id in moving)
if ((rectNow[id], _rectAt(id, 0)) case (
final now?,
final start?,
) when now != start)
id: Rect.fromLTRB(
now.left - start.left,
now.top - start.top,
now.right - start.right,
now.bottom - start.bottom,
),
};
}
if (speed != null) _curve = _launch(activeId, speed);
_controller.forward(from: 0);
}
/// The incoming chat's current edge velocities, per unit of the clock.
(double, double)? _speed(String id) {
const dt = 1 / 120;
final t = _controller.value.clamp(0.0, 1 - dt);
final a = _rectAt(id, _curve.transform(t));
final b = _rectAt(id, _curve.transform(t + dt));
if (a == null || b == null) return null;
return ((b.top - a.top) / dt, (b.bottom - a.bottom) / dt);
}
/// An ease whose starting slope keeps [speed] along the new path; zero when
/// the chat reverses, so a reversal starts from rest.
Curve _launch(String id, (double, double) speed) {
final start = _rectAt(id, 0), end = _rectAt(id, 1);
if (start == null || end == null) return ChatPaneSwitchMotion.curve;
final dTop = end.top - start.top, dBottom = end.bottom - start.bottom;
final length = dTop * dTop + dBottom * dBottom;
if (length < 1) return ChatPaneSwitchMotion.curve;
final slope = (speed.$1 * dTop + speed.$2 * dBottom) / length;
return slope > .01 ? ChatSwitchEase(slope) : ChatPaneSwitchMotion.curve;
}
static Size? _tallerOf(Size? a, Size? b) =>
a == null || (b != null && b.height > a.height) ? b : a;
}
@@ -9,19 +9,26 @@ class ChatSwitchCard extends StatelessWidget {
super.key,
required this.motion,
required this.outgoing,
this.paneId,
required this.child,
});
final ChatPaneSwitchMotion motion;
final bool outgoing;
/// A live pane's id: its pose includes one carried from an interrupted
/// switch. The tape's picture has none.
final String? paneId;
final Widget child;
@override
Widget build(BuildContext context) {
final pose = quickSwitchPose(
motion.style,
motion.isAnimating ? motion.progress : 1,
outgoing: outgoing,
);
final pose = paneId != null
? motion.poseFor(paneId!, outgoing: outgoing)
: quickSwitchPose(
motion.style,
motion.isAnimating ? motion.progress : 1,
outgoing: outgoing,
);
final shadow = Theme.of(context).shadowColor;
final surface = Theme.of(context).colorScheme.surface;
return LayoutBuilder(
@@ -0,0 +1,20 @@
import 'package:flutter/animation.dart';
/// The quick switch's quintic ease. It always brakes to a stop with zero
/// velocity and acceleration. A fresh switch also starts from rest; a switch
/// that retargets one already in flight starts at [launch], the slope that
/// keeps the incoming chat moving at its current speed instead of stalling.
///
/// f(t) = s·t + a·t³ + b·t⁴ + c·t⁵ with f(1) = 1 and f'(1) = f''(1) = 0.
/// Slopes above two would overshoot, so [launch] is clamped to [0, 2].
class ChatSwitchEase extends Curve {
const ChatSwitchEase([this.launch = 0]);
final double launch;
@override
double transformInternal(double t) {
final s = launch.clamp(0.0, 2.0);
final a = 10 - 6 * s, b = 8 * s - 15, c = 6 - 3 * s;
return t * (s + t * t * (a + t * (b + c * t)));
}
}
@@ -270,13 +270,9 @@ class _ChatWorkspaceState extends State<ChatWorkspace>
},
};
_reportMoveTargets(moveTargets);
// Finish one joined loop before taking the latest requested role.
// Repeated taps/chords never tear the tape or duplicate a live chat.
final activeId =
_motion.isAnimating &&
panes.any((pane) => pane.id == _lastActiveId)
? _lastActiveId
: widget.controller.activePaneId;
// A new request retargets a switch in flight from what is on
// screen (see ChatPaneSwitchMotion); it never waits for the loop.
final activeId = widget.controller.activePaneId;
final switching = _lastActiveId != activeId;
if (switching) {
_lastActiveId = activeId;
@@ -87,12 +87,11 @@ Widget chatWorkspaceSurface({
? Offset.zero & size
: move.rectFor(pane.id, geometry.panes[pane.id]!)),
child: IgnorePointer(
ignoring:
motion.isRetainedOutgoing(pane.id) ||
(mobile != null
? !mobile.rects.containsKey(pane.id) &&
!motion.isRetainedOutgoing(pane.id)
: collapsed && pane.id != controller.activePaneId),
// Pointers follow the requested layout, even mid-switch: the
// arriving peek takes the next tap; a card on its way off does not.
ignoring: mobile != null
? !mobile.rects.containsKey(pane.id)
: collapsed && pane.id != controller.activePaneId,
child: Opacity(
opacity:
mobile != null &&
@@ -103,6 +102,7 @@ Widget chatWorkspaceSurface({
child: ChatSwitchCard(
motion: motion,
outgoing: motion.isRetainedOutgoing(pane.id),
paneId: pane.id,
child: chatCenterStageLayer(
strip: stage.stripFor(pane.id),
covered: stage.stage?.strips.contains(pane.id) ?? false,
@@ -111,7 +111,7 @@ Widget chatWorkspaceSurface({
: () => stageStrip(pane),
view: ChatMotionViewport(
size: mobile != null && motion.isAnimating && !motion.isTape
? motion.viewportRectFor(pane.id)?.size
? motion.viewportFor(pane.id)
: stage.viewportFor(pane.id),
child: child!,
),
@@ -18,6 +18,31 @@ class QuickSwitchPose {
final Offset translation;
final double scale, rotationX, rotationY, rotationZ, depth, opacity, shadow;
/// This pose plus [weight] of [residual]'s departure from rest. A switch
/// that interrupts another carries the card's current pose and lets it
/// decay, so the card neither snaps flat nor jumps to the new path.
QuickSwitchPose carry(QuickSwitchPose residual, double weight) =>
QuickSwitchPose(
translation: translation + residual.translation * weight,
scale: scale * (1 + (residual.scale - 1) * weight),
rotationX: rotationX + residual.rotationX * weight,
rotationY: rotationY + residual.rotationY * weight,
rotationZ: rotationZ + residual.rotationZ * weight,
depth: depth + residual.depth * weight,
opacity: opacity * (1 - (1 - residual.opacity) * weight),
shadow: (shadow + residual.shadow * weight).clamp(0.0, 1.0),
);
bool get isRest =>
translation == Offset.zero &&
scale == 1 &&
rotationX == 0 &&
rotationY == 0 &&
rotationZ == 0 &&
depth == 0 &&
opacity == 1 &&
shadow == 0;
Matrix4 transform(Size size) {
// A flat card at constant depth projects to a uniform 2D scale. Keep it
// affine so backdrop filters do not enter the renderer's 3D path.