Add live quick switch swaps with perspective card stacking

This commit is contained in:
2026-11-07 19:16:57 +00:00
parent 5eb59a6813
commit 0da7442296
7 changed files with 421 additions and 77 deletions
@@ -0,0 +1,171 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pipelines_app/ui/motion/quick_switch_animation.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
import 'package:pipelines_app/features/chat/workspace/chat_switch_card.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;
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);
}
for (final style in [
QuickSwitchAnimation.swap,
QuickSwitchAnimation.cardStack,
]) {
testWidgets('${style.name} exchanges depth without recreating drafts', (
tester,
) async {
phone(tester);
SharedPreferences.setMockInitialValues({});
await QuickSwitchAnimationController.instance.setAnimation(style);
addTearDown(
() => QuickSwitchAnimationController.instance.setAnimation(
QuickSwitchAnimation.tape,
),
);
final controller = ChatWorkspaceController()..open('a');
final a = controller.activePaneId!;
controller.splitActive('b', ChatSplitAxis.horizontal);
final b = controller.activePaneId!;
await tester.pumpWidget(_host(controller));
await tester.pumpAndSettle();
final before = tester.state<_DraftState>(find.byKey(const ValueKey('b')));
before.controller.text = 'Keep my draft';
controller.activate(a);
await tester.pump();
await tester.pump(const Duration(milliseconds: 140));
expect(find.byKey(const ValueKey('chat-tape-exit')), findsNothing);
expect(find.byKey(const ValueKey('chat-swap-exit')), findsNothing);
Stack surface() => tester.widget<Stack>(
find.ancestor(of: rect(a), matching: find.byType(Stack)).first,
);
expect(surface().children.last.key, ValueKey('chat-workspace-$b'));
final card = tester.widget<Transform>(
find
.descendant(
of: find.byWidgetPredicate(
(w) => w is ChatSwitchCard && w.outgoing,
),
matching: find.byType(Transform),
)
.first,
);
expect(
card.transform.entry(3, 2) != 0,
style == QuickSwitchAnimation.cardStack,
);
await tester.pump(const Duration(milliseconds: 210));
expect(
surface().children.indexWhere(
(w) => w.key == ValueKey('chat-workspace-$b'),
),
lessThan(
surface().children.indexWhere(
(w) => w.key == ValueKey('chat-workspace-$a'),
),
),
);
await tester.pumpAndSettle();
expect(tester.state(find.byKey(const ValueKey('b'))), same(before));
expect(before.controller.text, 'Keep my draft');
expect(tester.getRect(rect(a)).top, 0);
expect(tester.getRect(rect(b)).top, _peekTop);
expect(find.byKey(const ValueKey('chat-swap-exit')), findsNothing);
await tester.pumpWidget(const SizedBox());
controller.dispose();
await QuickSwitchAnimationController.instance.setAnimation(
QuickSwitchAnimation.tape,
);
});
}
testWidgets('card stack cycles all added panes and settles reduced motion', (
tester,
) async {
phone(tester);
SharedPreferences.setMockInitialValues({});
await QuickSwitchAnimationController.instance.setAnimation(
QuickSwitchAnimation.cardStack,
);
addTearDown(
() => QuickSwitchAnimationController.instance.setAnimation(
QuickSwitchAnimation.tape,
),
);
final controller = ChatWorkspaceController()..open('a');
for (final id in ['b', 'c']) {
controller.splitActive(id, ChatSplitAxis.horizontal);
}
await tester.pumpWidget(_host(controller));
await tester.pumpAndSettle();
final states = {
for (final id in ['a', 'b', 'c'])
id: tester.state<_DraftState>(
find.byKey(ValueKey(id), skipOffstage: false),
),
};
for (final id in ['a', 'b', 'c', 'a']) {
final pane = controller.panes.firstWhere((p) => p.sessionId == id);
await tester.tap(find.byKey(ValueKey('chat-peek-${pane.id}')));
await tester.pumpAndSettle();
expect(controller.activePane!.sessionId, id);
for (final entry in states.entries) {
expect(
tester.state(find.byKey(ValueKey(entry.key), skipOffstage: false)),
same(entry.value),
);
}
}
await tester.pumpWidget(_host(controller, reduced: true));
controller.activate(
controller.panes.firstWhere((p) => p.sessionId == 'b').id,
);
await tester.pump();
expect(tester.getRect(rect(controller.activePaneId!)).top, 0);
expect(find.byKey(const ValueKey('chat-swap-exit')), findsNothing);
await tester.pumpWidget(const SizedBox());
controller.dispose();
});
}