Add quick switch motion library with true deck exchanges

This commit is contained in:
2026-11-07 19:36:48 +00:00
parent fc4aa1652f
commit 3b44c49679
7 changed files with 277 additions and 44 deletions
@@ -1,8 +1,8 @@
import 'dart:math' as math;
import 'dart:ui' show lerpDouble;
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import '../../../ui/motion/quick_switch_animation.dart';
import '../../../ui/motion/quick_switch_effect.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.
@@ -33,7 +33,7 @@ class ChatPaneSwitchMotion {
QuickSwitchAnimation get style => _style;
bool get isTape => _style == QuickSwitchAnimation.tape;
double get progress => _progress;
bool get outgoingOnTop => !isTape && _progress < .5;
bool get outgoingOnTop => quickSwitchOutgoingOnTop(_style, _progress);
bool isRetainedOutgoing(String id) =>
isAnimating && !isTape && id == _outgoingId;
String? get topPaneId => outgoingOnTop ? _outgoingId : _activeId;
@@ -102,6 +102,12 @@ class ChatPaneSwitchMotion {
_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);
Rect? rectFor(String id) {
final to = _to[id];
if (isRetainedOutgoing(id)) return exitingRect;
@@ -113,7 +119,13 @@ class ChatPaneSwitchMotion {
final gap = to.top - _to[_activeId]!.bottom;
return to.translate(0, (_size!.height + gap - to.top) * (1 - _progress));
}
return Rect.lerp(_from[id] ?? to, to, _progress);
return quickSwitchRect(
_style,
_progress,
_from[id] ?? to,
to,
outgoing: false,
);
}
Rect? get exitingRect {
@@ -122,13 +134,7 @@ class ChatPaneSwitchMotion {
if (!isAnimating || source == null || incoming == null) return null;
if (isTape) return source.translate(0, -incoming.top * _progress);
final target = _to[_enteringPeekId] ?? source;
final rect = Rect.lerp(source, target, _progress)!;
if (_style == QuickSwitchAnimation.swap) return rect;
final lift = math.sin(math.pi * _progress);
return rect.translate(
-source.width * .18 * lift,
-source.height * .08 * lift,
);
return quickSwitchRect(_style, _progress, source, target, outgoing: true);
}
void dispose() => _controller.dispose();
@@ -1,8 +1,7 @@
import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../../../ui/motion/quick_switch_animation.dart';
import '../../../ui/motion/quick_switch_effect.dart';
import 'chat_pane_switch_motion.dart';
/// Perspective affects paint only. The retained chat keeps its real viewport
@@ -20,33 +19,33 @@ class ChatSwitchCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
final lift =
motion.isAnimating && motion.style == QuickSwitchAnimation.cardStack
? math.sin(math.pi * motion.progress)
: 0.0;
final direction = outgoing ? -1.0 : 1.0;
final transform = Matrix4.identity()
..setEntry(3, 2, .0012 * lift)
..translateByDouble(0, 0, -(outgoing ? 90 : 40) * lift, 1)
..rotateY(direction * .22 * lift)
..rotateZ(direction * .035 * lift);
final pose = quickSwitchPose(
motion.style,
motion.isAnimating ? motion.progress : 1,
outgoing: outgoing,
);
final shadow = usesCupertinoHost(context)
? CupertinoColors.black
: Theme.of(context).shadowColor;
return Transform(
alignment: Alignment.center,
transform: transform,
child: DecoratedBox(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: shadow.withValues(alpha: .24 * lift),
blurRadius: 22 * lift,
offset: Offset(0, 8 * lift),
return LayoutBuilder(
builder: (context, constraints) => Opacity(
opacity: pose.opacity,
child: Transform(
alignment: Alignment.center,
transform: pose.transform(constraints.biggest),
child: DecoratedBox(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: shadow.withValues(alpha: .24 * pose.shadow),
blurRadius: 22 * pose.shadow,
offset: Offset(0, 8 * pose.shadow),
),
],
),
],
child: child,
),
),
child: child,
),
);
}
@@ -28,6 +28,7 @@ class ChatWorkspace extends StatefulWidget {
this.shortcutsEnabled = true,
this.singlePane = false,
this.mobilePeeks = false,
this.animationStyle,
this.paneTitle,
this.onVisibleSessionIdsChanged,
});
@@ -38,6 +39,7 @@ class ChatWorkspace extends StatefulWidget {
/// Keeps the retained layout while presenting only the active chat.
final bool singlePane;
final bool mobilePeeks;
final QuickSwitchAnimation? animationStyle;
final String Function(ChatWorkspacePane)? paneTitle;
final ValueChanged<Set<String>>? onVisibleSessionIdsChanged;
@@ -187,7 +189,8 @@ class _ChatWorkspaceState extends State<ChatWorkspace>
if (mobile != null &&
switching &&
animate &&
QuickSwitchAnimationController.instance.value ==
(widget.animationStyle ??
QuickSwitchAnimationController.instance.value) ==
QuickSwitchAnimation.tape) {
// The top exit requires a fresh picture. If painting is not ready,
// settle safely instead of moving the only live tree offscreen.
@@ -202,7 +205,9 @@ class _ChatWorkspaceState extends State<ChatWorkspace>
activeId: activeId!,
switching: switching,
animate: animate,
style: QuickSwitchAnimationController.instance.value,
style:
widget.animationStyle ??
QuickSwitchAnimationController.instance.value,
);
}
_reportVisible({
+14 -3
View File
@@ -7,9 +7,20 @@ enum QuickSwitchAnimation {
tape('Tape', 'Chats travel together in a continuous vertical loop.'),
swap('Swap', 'Chats slide past each other into their new positions.'),
cardStack(
'Card stack',
'The front card lifts and settles behind the next chat.',
);
'Card deck',
'Lift the front card, slide it aside and tuck it behind.',
),
deckSlide(
'Deck slide',
'Slide the front card clear of the deck and behind it.',
),
fan('Fan', 'Cards fan apart with a gentle planar rotation.'),
slide('Slide', 'Cards sweep sideways in opposite directions.'),
verticalPush('Push', 'Cards push past each other vertically.'),
fade('Crossfade', 'The old view fades as the next chat takes its place.'),
zoom('Zoom', 'The old chat recedes while the incoming view grows forward.'),
flip('Flip', 'Cards turn edge-on with perspective before exchanging places.'),
rolodex('Rolodex', 'The previous tilted perspective exchange.');
const QuickSwitchAnimation(this.label, this.description);
final String label;
+136
View File
@@ -0,0 +1,136 @@
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
import 'quick_switch_animation.dart';
/// A paint-only pose shared by the live workspace and the Settings preview.
/// Translation is a fraction of the card's current viewport, not screen pixels.
class QuickSwitchPose {
const QuickSwitchPose({
this.translation = Offset.zero,
this.scale = 1,
this.rotationX = 0,
this.rotationY = 0,
this.rotationZ = 0,
this.depth = 0,
this.opacity = 1,
this.shadow = 0,
});
final Offset translation;
final double scale, rotationX, rotationY, rotationZ, depth, opacity, shadow;
Matrix4 transform(Size size) => Matrix4.identity()
..setEntry(3, 2, rotationX != 0 || rotationY != 0 || depth != 0 ? .0012 : 0)
..translateByDouble(
translation.dx * size.width,
translation.dy * size.height,
depth,
1,
)
..rotateX(rotationX)
..rotateY(rotationY)
..rotateZ(rotationZ)
..scaleByDouble(scale, scale, scale, 1);
}
bool quickSwitchOutgoingOnTop(QuickSwitchAnimation style, double progress) =>
style != QuickSwitchAnimation.tape && progress < .5;
/// Decks clear the front card sideways before changing its stacking order.
/// Its full-height viewport stays intact on the way out; only the return to
/// the small preview resizes it. No hinge rotation is used for a deck.
Rect quickSwitchRect(
QuickSwitchAnimation style,
double progress,
Rect from,
Rect to, {
required bool outgoing,
}) {
final t = progress.clamp(0.0, 1.0);
final deck =
style == QuickSwitchAnimation.cardStack ||
style == QuickSwitchAnimation.deckSlide;
final travel = deck
? outgoing
? ((t - .45) / .55).clamp(0.0, 1.0)
: (t / .8).clamp(0.0, 1.0)
: t;
return Rect.lerp(from, to, travel)!;
}
QuickSwitchPose quickSwitchPose(
QuickSwitchAnimation style,
double progress, {
required bool outgoing,
}) {
final t = progress.clamp(0.0, 1.0);
final wave = t == 0 || t == 1 ? 0.0 : math.sin(math.pi * t);
final direction = outgoing ? -1.0 : 1.0;
switch (style) {
case QuickSwitchAnimation.tape:
case QuickSwitchAnimation.swap:
return const QuickSwitchPose();
case QuickSwitchAnimation.cardStack:
return QuickSwitchPose(
translation: Offset(
outgoing ? -1.15 * wave : .04 * wave,
outgoing ? -.06 * wave : 0,
),
scale: outgoing ? 1 + .025 * wave : 1 - .035 * wave,
rotationZ: outgoing ? -.045 * wave : 0,
depth: (outgoing && t < .5 ? -18 : 18) * wave,
shadow: outgoing ? wave : .4 * wave,
);
case QuickSwitchAnimation.deckSlide:
return QuickSwitchPose(
translation: Offset(outgoing ? 1.15 * wave : -.03 * wave, 0),
scale: 1 - (outgoing ? .025 : .04) * wave,
depth: 18 * wave,
shadow: .7 * wave,
);
case QuickSwitchAnimation.fan:
return QuickSwitchPose(
translation: Offset(direction * .38 * wave, -.08 * wave),
rotationZ: direction * (outgoing ? .24 : .12) * wave,
scale: 1 - .035 * wave,
shadow: wave,
);
case QuickSwitchAnimation.slide:
return QuickSwitchPose(
translation: Offset(direction * (outgoing ? .95 : .38) * wave, 0),
shadow: .3 * wave,
);
case QuickSwitchAnimation.verticalPush:
return QuickSwitchPose(
translation: Offset(0, direction * (outgoing ? .48 : .15) * wave),
);
case QuickSwitchAnimation.fade:
return QuickSwitchPose(
opacity: outgoing ? (2 * t - 1).abs() : 1 - .55 * wave,
);
case QuickSwitchAnimation.zoom:
return QuickSwitchPose(
scale: 1 + (outgoing ? -.22 : .08) * wave,
opacity: 1 - (outgoing ? .35 : .12) * wave,
shadow: .6 * wave,
);
case QuickSwitchAnimation.flip:
final angle = direction * math.pi * (outgoing ? .85 : .45) * wave;
return QuickSwitchPose(
rotationY: angle,
opacity: math.cos(angle).clamp(0.0, 1.0),
depth: 60 * wave,
shadow: wave,
);
case QuickSwitchAnimation.rolodex:
return QuickSwitchPose(
translation: Offset(
outgoing ? -.18 * wave : 0,
outgoing ? -.08 * wave : 0,
),
rotationY: direction * .22 * wave,
rotationZ: direction * .035 * wave,
depth: -(outgoing ? 90 : 40) * wave,
shadow: wave,
);
}
}
@@ -50,10 +50,9 @@ void main() {
addTearDown(tester.view.reset);
}
for (final style in [
QuickSwitchAnimation.swap,
QuickSwitchAnimation.cardStack,
]) {
for (final style in QuickSwitchAnimation.values.where(
(style) => style != QuickSwitchAnimation.tape,
)) {
testWidgets('${style.name} exchanges depth without recreating drafts', (
tester,
) async {
@@ -94,7 +93,12 @@ void main() {
);
expect(
card.transform.entry(3, 2) != 0,
style == QuickSwitchAnimation.cardStack,
{
QuickSwitchAnimation.cardStack,
QuickSwitchAnimation.deckSlide,
QuickSwitchAnimation.flip,
QuickSwitchAnimation.rolodex,
}.contains(style),
);
await tester.pump(const Duration(milliseconds: 210));
expect(
@@ -0,0 +1,72 @@
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/motion/quick_switch_animation.dart';
import 'package:pipelines_app/ui/motion/quick_switch_effect.dart';
void main() {
const full = Rect.fromLTWH(0, 0, 560, 635);
const peek = Rect.fromLTWH(0, 641, 560, 159);
test('both deck styles leave the front before changing stacking order', () {
for (final style in [
QuickSwitchAnimation.cardStack,
QuickSwitchAnimation.deckSlide,
]) {
expect(
quickSwitchRect(style, .3, full, peek, outgoing: true).size,
full.size,
);
expect(quickSwitchOutgoingOnTop(style, .49), isTrue);
expect(quickSwitchOutgoingOnTop(style, .5), isFalse);
final pose = quickSwitchPose(style, .5, outgoing: true);
expect(pose.translation.dx.abs(), greaterThan(1));
expect(pose.rotationX, 0);
expect(pose.rotationY, 0);
expect(pose.rotationZ.abs(), lessThan(math.pi / 12));
expect(quickSwitchRect(style, 1, full, peek, outgoing: true), peek);
}
});
test('every style settles without residual transforms or missing cards', () {
for (final style in QuickSwitchAnimation.values) {
for (final outgoing in [true, false]) {
for (final t in [0.0, 1.0]) {
final pose = quickSwitchPose(style, t, outgoing: outgoing);
expect(pose.translation, Offset.zero, reason: '$style at $t');
expect(pose.scale, 1, reason: '$style at $t');
expect(pose.opacity, 1, reason: '$style at $t');
expect(
pose.transform(full.size),
Matrix4.identity(),
reason: '$style at $t',
);
}
}
}
});
test('all choices have finite bounded geometry throughout a transition', () {
for (final style in QuickSwitchAnimation.values) {
for (final outgoing in [true, false]) {
for (var i = 0; i <= 100; i++) {
final pose = quickSwitchPose(style, i / 100, outgoing: outgoing);
expect(pose.opacity, inInclusiveRange(0, 1));
expect(pose.scale, greaterThan(0));
expect(
pose.transform(full.size).storage.every((v) => v.isFinite),
isTrue,
);
final rect = quickSwitchRect(
style,
i / 100,
full,
peek,
outgoing: outgoing,
);
expect(rect.height, inInclusiveRange(peek.height, full.height));
}
}
}
});
}