From 049a5514323a8869697e4ce6b67bf2b50b3740a4 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 7 Nov 2026 02:43:00 +0000 Subject: [PATCH] Add device-wide animation speed setting --- app/lib/application/bootstrap.dart | 3 + .../settings/settings_animation_section.dart | 90 ++++++++++++++++ .../features/settings/settings_screen.dart | 8 ++ app/lib/ui/motion/animation_speed.dart | 97 +++++++++++++++++ app/lib/ui/motion/motion.dart | 3 + app/notes/animation-speed.md | 16 +++ .../settings_animation_section_test.dart | 58 ++++++++++ app/test/ui/motion/animation_speed_test.dart | 100 ++++++++++++++++++ 8 files changed, 375 insertions(+) create mode 100644 app/lib/features/settings/settings_animation_section.dart create mode 100644 app/lib/ui/motion/animation_speed.dart create mode 100644 app/notes/animation-speed.md create mode 100644 app/test/features/settings/settings_animation_section_test.dart create mode 100644 app/test/ui/motion/animation_speed_test.dart diff --git a/app/lib/application/bootstrap.dart b/app/lib/application/bootstrap.dart index 3e8b016f..bbbba8c8 100644 --- a/app/lib/application/bootstrap.dart +++ b/app/lib/application/bootstrap.dart @@ -8,6 +8,7 @@ import 'server_endpoint.dart'; import '../features/settings/settings_store.dart'; import '../ui/theme/app_font_controller.dart'; import '../ui/theme/app_font_licenses.dart'; +import '../ui/motion/animation_speed.dart'; /// Native builds must supply their server explicitly; there is no baked host. const configuredServerUrl = String.fromEnvironment('PIPELINES_SERVER_URL'); @@ -55,10 +56,12 @@ Future runPipelinesApp({ registerAppFontLicenses(); // The device's chosen font loads beside the session; a slow download does // not hold the first frame, and applies when it lands. + final animationSpeed = AnimationSpeedController.instance.restore(); final font = AppFontController.instance.restore(wait: appFontStartupWait); // A phone behind the web login reconnects with its kept session. await ServerSession.load(); await font; + await animationSpeed; final server = await resolveLaunchServer( base: base, configured: configured, diff --git a/app/lib/features/settings/settings_animation_section.dart b/app/lib/features/settings/settings_animation_section.dart new file mode 100644 index 00000000..35269e83 --- /dev/null +++ b/app/lib/features/settings/settings_animation_section.dart @@ -0,0 +1,90 @@ +import 'dart:async'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import '../../ui/motion/animation_speed.dart'; +import 'settings_group.dart'; +import 'settings_theme.dart'; + +class SettingsAnimationSection extends StatelessWidget { + const SettingsAnimationSection({super.key, required this.controller}); + final AnimationSpeedController controller; + + @override + Widget build(BuildContext context) => ListenableBuilder( + listenable: controller, + builder: (context, _) { + final cupertino = usesCupertinoControls(context); + final speed = controller.speed; + final label = '${speed.toStringAsFixed(1)}×'; + final reset = speed == 1 ? null : () => unawaited(controller.reset()); + return SettingsGroup( + title: 'Animation speed', + footer: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + const Text( + 'Saved on this device. Higher is faster. Default 1.0×. Respects reduced motion.', + ), + if (controller.saveFailed) + Text( + 'Could not save; applies until reload.', + style: TextStyle(color: settingsErrorColor(context)), + ), + ], + ), + children: [ + SettingsGroupItem( + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Wrap( + alignment: WrapAlignment.spaceBetween, + crossAxisAlignment: WrapCrossAlignment.center, + children: [ + Text(label, style: settingsTextStyle(context, title: true)), + cupertino + ? CupertinoButton( + onPressed: reset, + child: const Text('Reset'), + ) + : TextButton( + onPressed: reset, + child: const Text('Reset'), + ), + ], + ), + Semantics( + label: 'Animation speed', + child: cupertino + ? CupertinoSlider( + value: speed, + min: minAnimationSpeed, + max: maxAnimationSpeed, + divisions: 15, + onChanged: controller.setSpeed, + onChangeEnd: (_) => unawaited(controller.save()), + ) + : Slider( + value: speed, + min: minAnimationSpeed, + max: maxAnimationSpeed, + divisions: 15, + label: label, + semanticFormatterCallback: (value) => + '${value.toStringAsFixed(1)} times', + onChanged: controller.setSpeed, + onChangeEnd: (_) => unawaited(controller.save()), + ), + ), + const Wrap( + alignment: WrapAlignment.spaceBetween, + children: [Text('0.5× · Slower'), Text('2.0× · Faster')], + ), + ], + ), + ), + ], + ); + }, + ); +} diff --git a/app/lib/features/settings/settings_screen.dart b/app/lib/features/settings/settings_screen.dart index 77a5dee4..5ebe3584 100644 --- a/app/lib/features/settings/settings_screen.dart +++ b/app/lib/features/settings/settings_screen.dart @@ -11,6 +11,8 @@ import 'default_endpoint.dart'; import 'settings_controller.dart'; import 'settings_controls.dart'; import 'settings_font_section.dart'; +import 'settings_animation_section.dart'; +import '../../ui/motion/animation_speed.dart'; import '../../ui/theme/app_font_controller.dart'; import 'settings_group.dart'; import 'storage/settings_storage_section.dart'; @@ -58,10 +60,12 @@ class SettingsScreen extends StatefulWidget { this.deliveryService, this.deliveryServiceController, this.fonts, + this.animationSpeed, }); /// The device's interface font; defaults to the app's shared controller. final AppFontController? fonts; + final AnimationSpeedController? animationSpeed; /// Android background delivery; hidden when absent or unsupported. final NotificationServiceBridge? deliveryService; @@ -204,6 +208,10 @@ class _SettingsScreenState extends State { ), _single('Server', SettingsHealthSection(gateway: widget.client)), // A device-local choice: a browser and a phone keep their own. + SettingsAnimationSection( + controller: + widget.animationSpeed ?? AnimationSpeedController.instance, + ), SettingsFontSection( controller: widget.fonts ?? AppFontController.instance, ), diff --git a/app/lib/ui/motion/animation_speed.dart b/app/lib/ui/motion/animation_speed.dart new file mode 100644 index 00000000..a0264509 --- /dev/null +++ b/app/lib/ui/motion/animation_speed.dart @@ -0,0 +1,97 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +const animationSpeedKey = 'pipelines.animation-speed.v1'; +const minAnimationSpeed = 0.5; +const maxAnimationSpeed = 2.0; + +abstract interface class AnimationSpeedStore { + Future load(); + Future save(double speed); +} + +class SharedAnimationSpeedStore implements AnimationSpeedStore { + const SharedAnimationSpeedStore(); + @override + Future load() async { + try { + return (await SharedPreferences.getInstance()).getDouble( + animationSpeedKey, + ); + } catch (_) { + return null; + } + } + + @override + Future save(double speed) async { + try { + return await (await SharedPreferences.getInstance()).setDouble( + animationSpeedKey, + speed, + ); + } catch (_) { + return false; + } + } +} + +/// A device-local animation clock. Larger factors play animations faster. +/// +/// Flutter divides scheduler frame timestamps by timeDilation, including +/// native routes, implicit animations and physics. Its setter resets the epoch +/// to preserve continuity for in-flight animations. Values below one are valid. +/// Durations in Motion must NOT also be scaled. Real timers, networking and +/// retry delays keep wall-clock time; existing reduced-motion handling remains. +class AnimationSpeedController extends ChangeNotifier { + AnimationSpeedController({AnimationSpeedStore? store}) + : _store = store ?? const SharedAnimationSpeedStore(); + static final instance = AnimationSpeedController(); + final AnimationSpeedStore _store; + double _speed = 1; + int _revision = 0; + bool _disposed = false; + bool saveFailed = false; + double get speed => _speed; + + Future restore() async { + final revision = _revision; + final value = await _store.load(); + if (_disposed || + revision != _revision || + value == null || + !value.isFinite) { + return; + } + setSpeed(value); + } + + void setSpeed(double value) { + if (_disposed || !value.isFinite) return; + _revision++; + _speed = value.clamp(minAnimationSpeed, maxAnimationSpeed); + saveFailed = false; + timeDilation = 1 / _speed; + notifyListeners(); + } + + Future save() async { + final revision = _revision; + final saved = await _store.save(_speed); + if (_disposed || revision != _revision) return; + saveFailed = !saved; + notifyListeners(); + } + + Future reset() async { + setSpeed(1); + await save(); + } + + @override + void dispose() { + _disposed = true; + super.dispose(); + } +} diff --git a/app/lib/ui/motion/motion.dart b/app/lib/ui/motion/motion.dart index 9de23d1b..3982b836 100644 --- a/app/lib/ui/motion/motion.dart +++ b/app/lib/ui/motion/motion.dart @@ -1,5 +1,8 @@ import 'package:flutter/widgets.dart'; +/// Durations stay unscaled: AnimationSpeedController adjusts the global +/// scheduler clock once, including native routes and implicit animations. +/// /// Short, quiet motion shared by the app: opening and closing sections, /// counts and statuses changing, live items arriving. Every duration drops /// to zero when the system asks for reduced motion. diff --git a/app/notes/animation-speed.md b/app/notes/animation-speed.md new file mode 100644 index 00000000..fd0f32ee --- /dev/null +++ b/app/notes/animation-speed.md @@ -0,0 +1,16 @@ +# Animation speed + +Settings offers a device-local 0.5×–2.0× animation speed slider in 0.1 steps, +with normal speed at 1.0× and a Reset action. It applies immediately and persists +at the end of a drag. A failed save is reported without hiding the active choice. + +`AnimationSpeedController` restores at bootstrap before the first app frame and +sets Flutter scheduler `timeDilation = 1 / speed`. Scheduler timestamps scale +all ticker-based animations, native routes, implicit transitions and springs. +Do not divide Motion durations again. `Timer`/`Future` delays, networking and +retry clocks remain real time. Existing reduce-motion paths remain authoritative. +The factor is not part of server/workspace settings and survives server changes. + +Checks: watchdog motion tests and native Material/Cupertino slider interaction +tests; clean analyzer; fixture web build and Settings browser interaction captures. +No device verification was performed. diff --git a/app/test/features/settings/settings_animation_section_test.dart b/app/test/features/settings/settings_animation_section_test.dart new file mode 100644 index 00000000..fc0e9dc3 --- /dev/null +++ b/app/test/features/settings/settings_animation_section_test.dart @@ -0,0 +1,58 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/settings/settings_animation_section.dart'; +import 'package:pipelines_app/ui/motion/animation_speed.dart'; + +class _Store implements AnimationSpeedStore { + double? saved; + @override + Future load() async => saved; + @override + Future save(double speed) async { + saved = speed; + return true; + } +} + +void main() { + tearDown(() => timeDilation = 1); + for (final cupertino in [false, true]) { + testWidgets( + 'native slider updates global speed and Reset restores normal ($cupertino)', + (tester) async { + final store = _Store(); + final controller = AnimationSpeedController(store: store); + final section = SettingsAnimationSection(controller: controller); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: section)) + : MaterialApp(home: Scaffold(body: section)), + ); + if (cupertino) { + final slider = tester.widget( + find.byType(CupertinoSlider), + ); + slider.onChanged!(1.8); + slider.onChangeEnd!(1.8); + } else { + final slider = tester.widget(find.byType(Slider)); + slider.onChanged!(1.8); + slider.onChangeEnd!(1.8); + } + await tester.pump(); + expect(find.text('1.8×'), findsWidgets); + expect(timeDilation, closeTo(1 / 1.8, 0.001)); + expect(store.saved, 1.8); + await tester.tap(find.text('Reset')); + await tester.pump(); + expect(controller.speed, 1); + expect(timeDilation, 1); + expect(store.saved, 1); + expect(tester.takeException(), isNull); + controller.dispose(); + }, + ); + } +} diff --git a/app/test/ui/motion/animation_speed_test.dart b/app/test/ui/motion/animation_speed_test.dart new file mode 100644 index 00000000..83305363 --- /dev/null +++ b/app/test/ui/motion/animation_speed_test.dart @@ -0,0 +1,100 @@ +import 'dart:async'; +import 'package:flutter/scheduler.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/motion/animation_speed.dart'; +import 'package:pipelines_app/ui/motion/motion.dart'; + +class _Store implements AnimationSpeedStore { + double? value; + bool fail = false; + Completer? pending; + @override + Future load() async => pending == null ? value : pending!.future; + @override + Future save(double value) async { + this.value = value; + return !fail; + } +} + +void main() { + tearDown(() => timeDilation = 1); + test( + 'device preference restores speed, clamps values, resets and reports save failure', + () async { + final store = _Store()..value = 2; + final controller = AnimationSpeedController(store: store); + await controller.restore(); + expect(controller.speed, 2); + expect(timeDilation, 0.5); + controller.setSpeed(0.1); + expect(controller.speed, 0.5); + expect(timeDilation, 2); + await controller.save(); + expect(store.value, 0.5); + store.fail = true; + await controller.save(); + expect(controller.saveFailed, isTrue); + store.fail = false; + await controller.reset(); + expect(timeDilation, 1); + expect(store.value, 1); + expect(controller.saveFailed, isFalse); + controller.dispose(); + }, + ); + test( + 'late restore cannot overwrite a newer choice and invalid values are ignored', + () async { + final store = _Store()..pending = Completer(); + final controller = AnimationSpeedController(store: store); + final restore = controller.restore(); + controller.setSpeed(1.5); + store.pending!.complete(0.5); + await restore; + expect(controller.speed, 1.5); + controller.setSpeed(double.nan); + expect(controller.speed, 1.5); + controller.dispose(); + }, + ); + testWidgets( + 'animation clock changes ticker speed while wall timers and reduced motion stay independent', + (tester) async { + final controller = AnimationSpeedController(store: _Store()); + await tester.pumpWidget(const SizedBox()); + final animation = AnimationController( + vsync: tester, + duration: const Duration(seconds: 1), + ); + + controller.setSpeed(2); + animation.forward(); + await tester.pump(); + var elapsed = false; + final timer = Timer( + const Duration(milliseconds: 250), + () => elapsed = true, + ); + await tester.pump(const Duration(milliseconds: 250)); + expect(animation.value, closeTo(0.5, 0.02)); + expect(elapsed, isTrue); + timer.cancel(); + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(disableAnimations: true), + child: Builder( + builder: (context) { + expect(Motion.of(context, Motion.standard), Duration.zero); + return const SizedBox(); + }, + ), + ), + ); + animation.dispose(); + controller.dispose(); + timeDilation = 1; + }, + ); +}