Add quick switch motion library with true deck exchanges
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user