diff --git a/mobile/lib/features/channels/me/solo_working_dots.dart b/mobile/lib/features/channels/me/solo_working_dots.dart index 661e5aca5d..a259d3c875 100644 --- a/mobile/lib/features/channels/me/solo_working_dots.dart +++ b/mobile/lib/features/channels/me/solo_working_dots.dart @@ -1,9 +1,15 @@ +import 'dart:async'; import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; /// Desktop's three quiet opacity pulses. The containing row owns semantics. +/// +/// Every visible row steps from one shared clock at about 12 fps instead of +/// animating at display rate. On Home each frame costs the app, backboardd +/// and the kernel far more than painting the dots does, so the frame rate is +/// the cost; sharing the clock also keeps rows in phase in the same frames. class SoloWorkingDots extends HookWidget { const SoloWorkingDots({super.key, required this.color}); @@ -12,55 +18,102 @@ class SoloWorkingDots extends HookWidget { @override Widget build(BuildContext context) { final reducedMotion = MediaQuery.disableAnimationsOf(context); - final animation = useAnimationController( - duration: const Duration(milliseconds: 1400), - ); + final animate = TickerMode.valuesOf(context).enabled && !reducedMotion; + // A per-row revision, not the shared step: a row that rejoins while + // another row kept the clock running must still rebuild on its next tick. + final revision = useState(0); useEffect(() { - if (reducedMotion) { - animation.stop(); - } else { - animation.repeat(); - } - return animation.stop; - }, [animation, reducedMotion]); - + if (!animate) return null; + void onStep() => revision.value++; + _PulseClock.instance.add(onStep); + return () => _PulseClock.instance.remove(onStep); + }, [animate]); + final phase = _PulseClock.instance.step / _PulseClock.stepsPerCycle; return ExcludeSemantics( child: RepaintBoundary( - child: AnimatedBuilder( - animation: animation, - builder: (context, _) => SizedBox( - height: 20, - child: Row( - mainAxisSize: MainAxisSize.min, - children: [ - for (var index = 0; index < 3; index++) ...[ - if (index > 0) const SizedBox(width: 2), - Opacity( - opacity: reducedMotion - ? 1 - : 0.35 + - 0.65 * - (1 - - math.cos( - 2 * - math.pi * - (animation.value - index / 7), - )) / - 2, - child: DecoratedBox( - decoration: BoxDecoration( - color: color, - shape: BoxShape.circle, - ), - child: const SizedBox.square(dimension: 4), + child: SizedBox( + height: 20, + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + for (var index = 0; index < 3; index++) ...[ + if (index > 0) const SizedBox(width: 2), + Opacity( + opacity: reducedMotion + ? 1 + : 0.35 + + 0.65 * + (1 - + math.cos( + 2 * math.pi * (phase - index / 7), + )) / + 2, + child: DecoratedBox( + decoration: BoxDecoration( + color: color, + shape: BoxShape.circle, ), + child: const SizedBox.square(dimension: 4), ), - ], + ), ], - ), + ], ), ), ), ); } } + +/// Steps the 1400 ms pulse in 17 steps (about 12 fps) while any dots are +/// visible and the app is in the foreground. +class _PulseClock { + static final instance = _PulseClock(); + static const stepsPerCycle = 17; + static const _period = Duration(microseconds: 1400000 ~/ stepsPerCycle); + + final _listeners = {}; + int step = 0; + Timer? _timer; + AppLifecycleListener? _lifecycle; + + void add(VoidCallback listener) { + _listeners.add(listener); + _sync(); + } + + void remove(VoidCallback listener) { + _listeners.remove(listener); + _sync(); + } + + void _sync() { + if (_listeners.isEmpty) { + _lifecycle?.dispose(); + _lifecycle = null; + } else { + _lifecycle ??= AppLifecycleListener(onStateChange: (_) => _sync()); + } + final state = WidgetsBinding.instance.lifecycleState; + final foreground = + state == null || + state == AppLifecycleState.resumed || + state == AppLifecycleState.inactive; + if (_listeners.isNotEmpty && foreground) { + _timer ??= Timer.periodic(_period, (_) { + step = (step + 1) % stepsPerCycle; + for (final listener in List.of(_listeners)) { + listener(); + } + }); + } else { + _timer?.cancel(); + _timer = null; + } + } +} + +/// Whether the shared pulse timer exists, for tests. +@visibleForTesting +bool get debugSoloWorkingDotsClockRunning => + _PulseClock.instance._timer != null; diff --git a/mobile/test/features/channels/me/solo_working_dots_test.dart b/mobile/test/features/channels/me/solo_working_dots_test.dart new file mode 100644 index 0000000000..83e978cbfc --- /dev/null +++ b/mobile/test/features/channels/me/solo_working_dots_test.dart @@ -0,0 +1,90 @@ +import 'package:buzz/features/channels/me/solo_working_dots.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + List opacities(WidgetTester tester, [int row = 0]) => tester + .widgetList( + find.descendant( + of: find.byType(SoloWorkingDots).at(row), + matching: find.byType(Opacity), + ), + ) + .map((dot) => dot.opacity) + .toList(); + + Widget dots({bool ticking = true, int rows = 1}) => Directionality( + textDirection: TextDirection.ltr, + child: TickerMode( + enabled: ticking, + child: Column( + children: [ + for (var i = 0; i < rows; i++) + const SoloWorkingDots(color: Color(0xFF000000)), + ], + ), + ), + ); + + testWidgets('rows step together at about 12 fps with no frames between', ( + tester, + ) async { + await tester.pumpWidget(dots(rows: 2)); + expect(debugSoloWorkingDotsClockRunning, isTrue); + final start = opacities(tester); + expect(tester.binding.hasScheduledFrame, isFalse); + + // Check before pumping: pump draws any frame scheduled in the interval. + await tester.binding.delayed(const Duration(milliseconds: 80)); + expect(tester.binding.hasScheduledFrame, isFalse); + await tester.pump(); + expect(opacities(tester), start); + + await tester.binding.delayed(const Duration(milliseconds: 3)); + expect(tester.binding.hasScheduledFrame, isTrue); + await tester.pump(); + final next = opacities(tester); + expect(next, isNot(start)); + expect(opacities(tester, 1), next); + expect(tester.binding.hasScheduledFrame, isFalse); + + await tester.pumpWidget(const SizedBox()); + expect(debugSoloWorkingDotsClockRunning, isFalse); + }); + + testWidgets('the clock stops while TickerMode is off and resumes after', ( + tester, + ) async { + await tester.pumpWidget(dots(ticking: false)); + expect(debugSoloWorkingDotsClockRunning, isFalse); + final start = opacities(tester); + await tester.pump(const Duration(seconds: 1)); + expect(opacities(tester), start); + expect(tester.binding.hasScheduledFrame, isFalse); + + await tester.pumpWidget(dots()); + expect(debugSoloWorkingDotsClockRunning, isTrue); + await tester.pump(const Duration(milliseconds: 83)); + expect(opacities(tester), isNot(start)); + + await tester.pumpWidget(dots(ticking: false)); + expect(debugSoloWorkingDotsClockRunning, isFalse); + }); + + testWidgets('the clock stops while the app is in the background', ( + tester, + ) async { + await tester.pumpWidget(dots()); + void go(AppLifecycleState state) => + tester.binding.handleAppLifecycleStateChanged(state); + go(AppLifecycleState.inactive); + expect(debugSoloWorkingDotsClockRunning, isTrue); + go(AppLifecycleState.hidden); + expect(debugSoloWorkingDotsClockRunning, isFalse); + go(AppLifecycleState.inactive); + go(AppLifecycleState.resumed); + expect(debugSoloWorkingDotsClockRunning, isTrue); + await tester.pumpWidget(const SizedBox()); + expect(debugSoloWorkingDotsClockRunning, isFalse); + }); +}