diff --git a/app/lib/ui/motion/fade_through_stack.dart b/app/lib/ui/motion/fade_through_stack.dart index 04998f7b..5b425b44 100644 --- a/app/lib/ui/motion/fade_through_stack.dart +++ b/app/lib/ui/motion/fade_through_stack.dart @@ -94,55 +94,45 @@ class _FadeThroughStackState extends State Widget _page(int index, bool reduced) { final outgoing = index == _outgoingIndex; final active = index == _visibleIndex; - final shown = reduced || active || outgoing; + final shown = active || outgoing; final progress = _controller.value; - final opacity = outgoing - ? 1 - - CurvedAnimation( - parent: _controller, - curve: const Interval( - 0, - Motion.tabFadeThroughIncomingAt, - curve: Motion.curve, - ), - ).value - : active - ? CurvedAnimation( - parent: _controller, - curve: const Interval( - Motion.tabFadeThroughIncomingAt, - 1, - curve: Motion.curve, - ), - ).value - : 0.0; + const split = Motion.tabFadeThroughIncomingAt; + // Curves evaluated directly: a CurvedAnimation built per frame would + // register a listener on the controller every frame. + final out = const Interval( + 0, + split, + curve: Motion.curve, + ).transform(progress); + final into = const Interval( + split, + 1, + curve: Motion.curve, + ).transform(progress); + const shrink = 1 - Motion.tabFadeThroughScale; + final opacity = outgoing ? 1 - out : (active ? into : 0.0); final scale = outgoing - ? 1 - - (Motion.tabFadeThroughScale - 1).abs() * - (progress / Motion.tabFadeThroughIncomingAt).clamp(0.0, 1.0) - : active - ? Motion.tabFadeThroughScale + - (1 - Motion.tabFadeThroughScale) * - ((progress - Motion.tabFadeThroughIncomingAt) / - (1 - Motion.tabFadeThroughIncomingAt)) - .clamp(0.0, 1.0) - : 1.0; - final transformed = Transform.scale( - scale: scale, - child: Opacity(opacity: opacity, child: widget.children[index]), - ); - final page = active - ? Semantics(container: true, explicitChildNodes: true, child: transformed) - : ExcludeFocus( - child: ExcludeSemantics(child: transformed), - ); + ? 1 - shrink * out + : (active ? Motion.tabFadeThroughScale + shrink * into : 1.0); + // The same widget structure for every page whatever its state, so a + // page's element (and its scroll position, controllers and text) is kept + // across switches. return Offstage( offstage: !shown, child: TickerMode( enabled: active, child: IgnorePointer( ignoring: !active, - child: page, + child: ExcludeFocus( + excluding: !active, + child: ExcludeSemantics( + excluding: !active, + child: Transform.scale( + scale: scale, + child: Opacity(opacity: opacity, child: widget.children[index]), + ), + ), + ), ), ), ); diff --git a/app/test/application/attention_badge_wiring_test.dart b/app/test/application/attention_badge_wiring_test.dart index 107efc18..2ee9e9d4 100644 --- a/app/test/application/attention_badge_wiring_test.dart +++ b/app/test/application/attention_badge_wiring_test.dart @@ -58,7 +58,9 @@ void main() { await tester.pump(); expect(provider.value.uri.path, '/pipelines'); await waitForRequest(tester, channel, 'list_pipelines'); - expect(find.text('Nightly build'), findsNothing); + // The Runs page may still be fading out, but it is no longer the + // active page: nothing on it takes a tap. + expect(find.text('Nightly build').hitTestable(), findsNothing); channel.incoming.add( jsonEncode({'type': 'run_event', 'run_id': 1, 'status': 'waiting'}), diff --git a/app/test/ui/motion/fade_through_stack_test.dart b/app/test/ui/motion/fade_through_stack_test.dart new file mode 100644 index 00000000..053c4846 --- /dev/null +++ b/app/test/ui/motion/fade_through_stack_test.dart @@ -0,0 +1,132 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/motion/fade_through_stack.dart'; +import 'package:pipelines_app/ui/motion/motion.dart'; + +class _Page extends StatefulWidget { + const _Page(this.name); + final String name; + @override + State<_Page> createState() => _PageState(); +} + +class _PageState extends State<_Page> { + final controller = TextEditingController(); + @override + void dispose() { + controller.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) => Column( + children: [ + Text('page ${widget.name}'), + TextField(key: Key('field ${widget.name}'), controller: controller), + ElevatedButton( + key: Key('button ${widget.name}'), + onPressed: () => tapped.add(widget.name), + child: Text('tap ${widget.name}'), + ), + ], + ); +} + +final tapped = []; + +Widget _host(int index, {bool reduced = false}) => MaterialApp( + home: MediaQuery( + data: MediaQueryData(disableAnimations: reduced), + child: Scaffold( + body: FadeThroughStack( + index: index, + children: const [_Page('a'), _Page('b'), _Page('c')], + ), + ), + ), +); + +double _opacityOf(WidgetTester tester, String name) => tester + .widget( + find.ancestor( + of: find.text('page $name'), + matching: find.byType(Opacity), + ), + ) + .opacity; + +bool _onstage(WidgetTester tester, String name) => !tester + .widget( + find + .ancestor( + of: find.text('page $name', skipOffstage: false), + matching: find.byType(Offstage, skipOffstage: false), + ) + .first, + ) + .offstage; + +void main() { + setUp(tapped.clear); + + testWidgets('fades the old page out, then the new page in', (tester) async { + await tester.pumpWidget(_host(0)); + await tester.pumpWidget(_host(1)); + final total = Motion.tabFadeThrough; + await tester.pump(total * 0.2); + expect(_opacityOf(tester, 'a'), inExclusiveRange(0.0, 1.0)); + expect(_opacityOf(tester, 'b'), 0.0); + await tester.pump(total * 0.5); + expect(_opacityOf(tester, 'a'), 0.0); + expect(_opacityOf(tester, 'b'), inExclusiveRange(0.0, 1.0)); + await tester.pump(total); + expect(_opacityOf(tester, 'b'), 1.0); + expect(_onstage(tester, 'a'), isFalse); + }); + + testWidgets('a page keeps its state across switches', (tester) async { + await tester.pumpWidget(_host(0)); + await tester.enterText(find.byKey(const Key('field a')), 'draft'); + final before = tester.state(find.byType(_Page).first); + await tester.pumpWidget(_host(1)); + await tester.pump(Motion.tabFadeThrough * 2); + await tester.pumpWidget(_host(0)); + await tester.pump(Motion.tabFadeThrough * 2); + expect(tester.state(find.byType(_Page).first), same(before)); + expect(find.text('draft'), findsOneWidget); + }); + + testWidgets('only the active page takes taps', (tester) async { + await tester.pumpWidget(_host(0)); + await tester.pumpWidget(_host(1)); + await tester.pump(Motion.tabFadeThrough * 0.2); + // The outgoing page is still visible but must not take the tap; it + // reaches the active page's button underneath instead. + await tester.tap(find.byKey(const Key('button a')), warnIfMissed: false); + expect(tapped, isNot(contains('a'))); + expect(tapped, ['b']); + }); + + testWidgets('rapid switching ends with only the last page shown', ( + tester, + ) async { + await tester.pumpWidget(_host(0)); + await tester.pumpWidget(_host(1)); + await tester.pump(const Duration(milliseconds: 30)); + await tester.pumpWidget(_host(2)); + await tester.pump(const Duration(milliseconds: 30)); + await tester.pumpWidget(_host(0)); + await tester.pump(Motion.tabFadeThrough * 2); + expect(_onstage(tester, 'a'), isTrue); + expect(_onstage(tester, 'b'), isFalse); + expect(_onstage(tester, 'c'), isFalse); + expect(_opacityOf(tester, 'a'), 1.0); + }); + + testWidgets('reduced motion switches instantly', (tester) async { + await tester.pumpWidget(_host(0, reduced: true)); + await tester.pumpWidget(_host(1, reduced: true)); + await tester.pump(); + expect(_opacityOf(tester, 'b'), 1.0); + }); +}