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:
@@ -138,7 +138,7 @@ void main() {
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'tape exits above incoming, wraps below, queues reversal and retains drafts',
|
||||
'tape exits above incoming, wraps below, retargets reversal and retains drafts',
|
||||
(tester) async {
|
||||
phone(tester);
|
||||
final controller = ChatWorkspaceController()..open('a');
|
||||
@@ -176,10 +176,19 @@ void main() {
|
||||
tester.getSize(find.byKey(const ValueKey('a'))).height,
|
||||
closeTo(incoming.height, .5),
|
||||
);
|
||||
// Reversing mid-loop starts at once: the arriving chat keeps its place
|
||||
// and a fresh picture of the frame above it continues the exit.
|
||||
controller.activate(b);
|
||||
await tester.pump();
|
||||
expect(tester.getRect(rect(a)), incoming);
|
||||
expect(tester.getRect(rect(b)), wrapped);
|
||||
expect(tester.getRect(rect(b)).top, closeTo(wrapped.top, .5));
|
||||
final picture = tester.getRect(
|
||||
find.byKey(const ValueKey('chat-tape-exit')),
|
||||
);
|
||||
expect(picture.top, closeTo(0, .5));
|
||||
expect(picture.bottom + 6, closeTo(wrapped.top, .5));
|
||||
expect(tester.getRect(rect(a)).top, greaterThanOrEqualTo(800));
|
||||
await tester.pump(ChatPaneSwitchMotion.duration ~/ 8);
|
||||
expect(tester.getRect(rect(b)).top, lessThan(wrapped.top - 1));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.getRect(rect(a)).top, _peekTop);
|
||||
expect(tester.getRect(rect(b)).top, 0);
|
||||
|
||||
@@ -0,0 +1,330 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_pane_switch_motion.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_switch_card.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_switch_ease.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
|
||||
import 'package:pipelines_app/ui/motion/quick_switch_animation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
Widget _host(ChatWorkspaceController controller, {bool reduced = false}) =>
|
||||
MaterialApp(
|
||||
home: MediaQuery(
|
||||
data: MediaQueryData(disableAnimations: reduced),
|
||||
child: ChatWorkspace(
|
||||
controller: controller,
|
||||
singlePane: true,
|
||||
mobilePeeks: true,
|
||||
paneBuilder: (_, pane, active) =>
|
||||
_Draft(key: ValueKey(pane.sessionId)),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
class _Draft extends StatefulWidget {
|
||||
const _Draft({super.key});
|
||||
@override
|
||||
State<_Draft> createState() => _DraftState();
|
||||
}
|
||||
|
||||
class _DraftState extends State<_Draft> {
|
||||
final controller = TextEditingController();
|
||||
@override
|
||||
void dispose() {
|
||||
controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
Material(child: TextField(controller: controller));
|
||||
}
|
||||
|
||||
const _peekTop = (800 - 6) * .8 + 6;
|
||||
const _step = Duration(milliseconds: 16);
|
||||
Finder _rect(String pane) => find.byKey(ValueKey('chat-workspace-$pane'));
|
||||
|
||||
void main() {
|
||||
void phone(WidgetTester tester) {
|
||||
tester.view.physicalSize = const Size(560, 800);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
tester.platformDispatcher.accessibilityFeaturesTestValue =
|
||||
const FakeAccessibilityFeatures(disableAnimations: false);
|
||||
addTearDown(tester.platformDispatcher.clearAccessibilityFeaturesTestValue);
|
||||
addTearDown(tester.view.reset);
|
||||
}
|
||||
|
||||
Future<ChatWorkspaceController> open(
|
||||
WidgetTester tester,
|
||||
QuickSwitchAnimation style,
|
||||
List<String> ids,
|
||||
) async {
|
||||
phone(tester);
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
await QuickSwitchAnimationController.instance.setAnimation(style);
|
||||
addTearDown(
|
||||
() => QuickSwitchAnimationController.instance.setAnimation(
|
||||
QuickSwitchAnimation.tape,
|
||||
),
|
||||
);
|
||||
final controller = ChatWorkspaceController()..open(ids.first);
|
||||
for (final id in ids.skip(1)) {
|
||||
controller.splitActive(id, ChatSplitAxis.horizontal);
|
||||
}
|
||||
controller.activate(controller.panes.first.id);
|
||||
await tester.pumpWidget(_host(controller));
|
||||
await tester.pumpAndSettle();
|
||||
return controller;
|
||||
}
|
||||
|
||||
String paneOf(ChatWorkspaceController controller, String session) =>
|
||||
controller.panes.firstWhere((p) => p.sessionId == session).id;
|
||||
|
||||
bool ignoring(WidgetTester tester, String pane) => tester
|
||||
.widget<IgnorePointer>(
|
||||
find
|
||||
.descendant(of: _rect(pane), matching: find.byType(IgnorePointer))
|
||||
.first,
|
||||
)
|
||||
.ignoring;
|
||||
|
||||
/// A point where the card is uncovered on screen now, through its
|
||||
/// paint-only pose: where a person would tap the arriving peek.
|
||||
Offset? uncovered(WidgetTester tester, String pane) {
|
||||
final card = find.byWidgetPredicate(
|
||||
(w) => w is ChatSwitchCard && w.paneId == pane,
|
||||
);
|
||||
final box = tester.renderObject<RenderBox>(
|
||||
find.descendant(of: card, matching: find.byType(DecoratedBox)).first,
|
||||
);
|
||||
for (var y = box.size.height - 4; y > 0; y -= 4) {
|
||||
for (final x in [.5, .25, .75]) {
|
||||
final point = box.localToGlobal(Offset(box.size.width * x, y));
|
||||
if (point.dy < 0 || point.dy >= 800 || point.dx < 0) continue;
|
||||
if (point.dx >= 560) continue;
|
||||
final hits = tester.hitTestOnBinding(point).path;
|
||||
if (hits.any((entry) => entry.target == box)) return point;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, Rect> presented(WidgetTester tester, Iterable<String> panes) => {
|
||||
for (final pane in panes)
|
||||
if (tester
|
||||
.widget<Offstage>(
|
||||
find
|
||||
.descendant(of: _rect(pane), matching: find.byType(Offstage))
|
||||
.first,
|
||||
)
|
||||
.offstage
|
||||
.isFalse)
|
||||
pane: tester.getRect(_rect(pane)),
|
||||
};
|
||||
|
||||
Future<void> expectSettled(
|
||||
WidgetTester tester,
|
||||
ChatWorkspaceController controller,
|
||||
Map<String, _DraftState> drafts,
|
||||
) async {
|
||||
await tester.pumpAndSettle();
|
||||
final active = controller.activePaneId!;
|
||||
expect(tester.getRect(_rect(active)).top, 0);
|
||||
final visible = presented(tester, controller.panes.map((p) => p.id));
|
||||
expect(visible.length, 2, reason: 'one active chat and one peek');
|
||||
final peek = visible.keys.firstWhere((id) => id != active);
|
||||
expect(visible[peek]!.top, closeTo(_peekTop, .01));
|
||||
expect(ignoring(tester, active), isFalse);
|
||||
expect(ignoring(tester, peek), isFalse);
|
||||
for (final pane in controller.panes) {
|
||||
// Every chat is one retained live tree, never a second copy.
|
||||
expect(
|
||||
find.byKey(ValueKey(pane.sessionId), skipOffstage: false),
|
||||
findsOneWidget,
|
||||
);
|
||||
final state = drafts[pane.sessionId]!;
|
||||
expect(
|
||||
tester.state(find.byKey(ValueKey(pane.sessionId), skipOffstage: false)),
|
||||
same(state),
|
||||
);
|
||||
expect(state.controller.text, 'Draft ${pane.sessionId}');
|
||||
}
|
||||
expect(find.byKey(const ValueKey('chat-tape-exit')), findsNothing);
|
||||
}
|
||||
|
||||
for (final style in [
|
||||
QuickSwitchAnimation.tape,
|
||||
QuickSwitchAnimation.swap,
|
||||
QuickSwitchAnimation.arcSwap,
|
||||
QuickSwitchAnimation.cardStack,
|
||||
QuickSwitchAnimation.fade,
|
||||
]) {
|
||||
testWidgets('${style.name}: rapid peek taps retarget without waiting', (
|
||||
tester,
|
||||
) async {
|
||||
final controller = await open(tester, style, ['a', 'b', 'c', 'd']);
|
||||
final drafts = {
|
||||
for (final id in ['a', 'b', 'c', 'd'])
|
||||
id: tester.state<_DraftState>(
|
||||
find.byKey(ValueKey(id), skipOffstage: false),
|
||||
)..controller.text = 'Draft $id',
|
||||
};
|
||||
final ids = [for (final pane in controller.panes) pane.id];
|
||||
// Five taps, each on the peek arriving from the previous one.
|
||||
for (var tap = 0; tap < 5; tap++) {
|
||||
final target = ids[(ids.indexOf(controller.activePaneId!) + 1) % 4];
|
||||
final animating = tap > 0;
|
||||
if (animating) {
|
||||
// The arriving peek takes pointers while it is still moving.
|
||||
expect(ignoring(tester, target), isFalse);
|
||||
}
|
||||
// A deck card may still be sliding back from aside: tap as soon as
|
||||
// any of it shows.
|
||||
var point = uncovered(tester, target);
|
||||
for (var wait = 0; point == null && wait < 30; wait++) {
|
||||
await tester.pump(_step);
|
||||
point = uncovered(tester, target);
|
||||
}
|
||||
if (point == null) fail('$target never showed');
|
||||
final before = presented(tester, ids);
|
||||
await tester.tapAt(point);
|
||||
expect(controller.activePaneId, target, reason: 'tap ${tap + 1}');
|
||||
await tester.pump();
|
||||
final now = presented(tester, ids);
|
||||
if (animating) {
|
||||
// Motion continues from where the cards are: no jump.
|
||||
final continuing = style == QuickSwitchAnimation.tape
|
||||
? [target]
|
||||
: before.keys.where(now.containsKey);
|
||||
for (final pane in continuing) {
|
||||
expect(now[pane]!.top, closeTo(before[pane]!.top, 1), reason: pane);
|
||||
expect(
|
||||
now[pane]!.height,
|
||||
closeTo(before[pane]!.height, 1),
|
||||
reason: pane,
|
||||
);
|
||||
}
|
||||
if (style == QuickSwitchAnimation.tape) {
|
||||
final picture = tester.getRect(
|
||||
find.byKey(const ValueKey('chat-tape-exit')),
|
||||
);
|
||||
expect(picture.top, closeTo(0, .5));
|
||||
expect(picture.bottom + 6, closeTo(now[target]!.top, 1));
|
||||
}
|
||||
}
|
||||
// The new switch starts at once: the chosen chat already rises (or,
|
||||
// on a fresh deck, already waits full-size behind the front card).
|
||||
await tester.pump(_step * 2);
|
||||
if (now[target]!.top > 0) {
|
||||
expect(
|
||||
tester.getRect(_rect(target)).top,
|
||||
lessThan(now[target]!.top - .5),
|
||||
);
|
||||
}
|
||||
// Every pane on screen is the requested pair or a card on its way
|
||||
// off, which never takes a pointer.
|
||||
for (final pane in presented(tester, ids).keys) {
|
||||
final requested =
|
||||
pane == target ||
|
||||
pane == ids[(ids.indexOf(target) + 1) % ids.length];
|
||||
expect(ignoring(tester, pane), !requested, reason: pane);
|
||||
}
|
||||
await tester.pump(ChatPaneSwitchMotion.duration * .4);
|
||||
}
|
||||
expect(controller.activePaneId, ids[(0 + 5) % 4]);
|
||||
await expectSettled(tester, controller, drafts);
|
||||
// Settled pointers are valid: the peek opens with one more tap.
|
||||
final peek = ids[(ids.indexOf(controller.activePaneId!) + 1) % 4];
|
||||
await tester.tap(find.byKey(ValueKey('chat-peek-$peek')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(controller.activePaneId, peek);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
controller.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
for (final style in [
|
||||
QuickSwitchAnimation.tape,
|
||||
QuickSwitchAnimation.swap,
|
||||
QuickSwitchAnimation.deckSlide,
|
||||
]) {
|
||||
testWidgets('${style.name}: a programmatic reversal starts immediately', (
|
||||
tester,
|
||||
) async {
|
||||
final controller = await open(tester, style, ['a', 'b']);
|
||||
final drafts = {
|
||||
for (final id in ['a', 'b'])
|
||||
id: tester.state<_DraftState>(find.byKey(ValueKey(id)))
|
||||
..controller.text = 'Draft $id',
|
||||
};
|
||||
final a = paneOf(controller, 'a'), b = paneOf(controller, 'b');
|
||||
controller.activate(b);
|
||||
await tester.pump();
|
||||
await tester.pump(ChatPaneSwitchMotion.duration * .3);
|
||||
final before = presented(tester, [a, b]);
|
||||
controller.activate(a);
|
||||
await tester.pump();
|
||||
final now = presented(tester, [a, b]);
|
||||
if (style == QuickSwitchAnimation.tape) {
|
||||
// a's live tree was arriving from below as the next peek.
|
||||
expect(now[a]!.top, closeTo(before[a]!.top, 1));
|
||||
} else {
|
||||
for (final pane in [a, b]) {
|
||||
expect(now[pane]!.top, closeTo(before[pane]!.top, 1));
|
||||
}
|
||||
}
|
||||
await tester.pump(ChatPaneSwitchMotion.duration * .1);
|
||||
// A deck's incoming card already waits full-size behind the front one.
|
||||
if (now[a]!.top > 0) {
|
||||
expect(tester.getRect(_rect(a)).top, lessThan(now[a]!.top));
|
||||
}
|
||||
await expectSettled(tester, controller, drafts);
|
||||
expect(controller.activePaneId, a);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
controller.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('reduced motion still settles each request at once', (
|
||||
tester,
|
||||
) async {
|
||||
final controller = await open(tester, QuickSwitchAnimation.tape, [
|
||||
'a',
|
||||
'b',
|
||||
'c',
|
||||
]);
|
||||
await tester.pumpWidget(_host(controller, reduced: true));
|
||||
for (final session in ['b', 'c', 'a']) {
|
||||
controller.activate(paneOf(controller, session));
|
||||
await tester.pump();
|
||||
expect(tester.getRect(_rect(controller.activePaneId!)).top, 0);
|
||||
expect(find.byKey(const ValueKey('chat-tape-exit')), findsNothing);
|
||||
}
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
controller.dispose();
|
||||
});
|
||||
|
||||
test('the retarget ease launches at its slope and brakes to rest', () {
|
||||
for (final slope in [0.0, .8, 2.0]) {
|
||||
final ease = ChatSwitchEase(slope);
|
||||
expect(ease.transform(0), 0);
|
||||
expect(ease.transform(1), 1);
|
||||
expect(ease.transform(1e-4) / 1e-4, closeTo(slope, 1e-2));
|
||||
var last = 0.0;
|
||||
for (var i = 1; i <= 100; i++) {
|
||||
final value = ease.transform(i / 100);
|
||||
expect(value, greaterThanOrEqualTo(last));
|
||||
expect(value, lessThanOrEqualTo(1));
|
||||
last = value;
|
||||
}
|
||||
}
|
||||
expect(
|
||||
const ChatSwitchEase().transform(.3),
|
||||
ChatPaneSwitchMotion.curve.transform(.3),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
extension on bool {
|
||||
bool get isFalse => !this;
|
||||
}
|
||||
Reference in New Issue
Block a user