From 060d71a44eedbbedfca6a4ba65f61beffb404ffe Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:43:04 +0700 Subject: [PATCH] perf(library): remove per-frame scrollbar hide timers --- lib/widgets/library_fast_scroller.dart | 41 ++++++++++----- test/library_fast_scroller_test.dart | 73 ++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 12 deletions(-) diff --git a/lib/widgets/library_fast_scroller.dart b/lib/widgets/library_fast_scroller.dart index a5bf5fbe1..e0f663d30 100644 --- a/lib/widgets/library_fast_scroller.dart +++ b/lib/widgets/library_fast_scroller.dart @@ -73,7 +73,7 @@ class _LibraryFastScrollerState extends State { } void _onOuterPositionChanged() { - if (mounted) setState(() {}); + if (mounted && _long && widget.topPadding > 0) setState(() {}); } @override @@ -100,12 +100,23 @@ class _LibraryFastScrollerState extends State { } void _readMetrics(ScrollMetrics metrics, {bool show = false}) { - setState(() { - _pixels = metrics.pixels; - _maxExtent = metrics.maxScrollExtent; - _viewport = metrics.viewportDimension; - if (show) _show(); - }); + final wasLong = _long; + final changed = + _pixels != metrics.pixels || + _maxExtent != metrics.maxScrollExtent || + _viewport != metrics.viewportDimension; + final wasVisible = _visible; + _pixels = metrics.pixels; + _maxExtent = metrics.maxScrollExtent; + _viewport = metrics.viewportDimension; + if (show && _long) _show(); + if (!_long) { + _visible = false; + _hideTimer?.cancel(); + } + if ((changed && (wasLong || _long)) || wasVisible != _visible) { + setState(() {}); + } } bool _onMetricsNotification(ScrollMetricsNotification notification) { @@ -123,17 +134,22 @@ class _LibraryFastScrollerState extends State { if (metrics.axis != Axis.vertical) return false; _attachPosition(notification.context); _readMetrics(metrics, show: notification is ScrollUpdateNotification); + if (notification is ScrollStartNotification) _hideTimer?.cancel(); + if (notification is ScrollEndNotification) _scheduleHide(); return false; } void _show() { _visible = true; _hideTimer?.cancel(); - if (!_dragging) { - _hideTimer = Timer(const Duration(milliseconds: 1500), () { - if (mounted && !_dragging) setState(() => _visible = false); - }); - } + } + + void _scheduleHide() { + if (!_long || !_visible || _dragging) return; + _hideTimer?.cancel(); + _hideTimer = Timer(const Duration(milliseconds: 1500), () { + if (mounted && !_dragging) setState(() => _visible = false); + }); } void _startDrag(DragStartDetails details) { @@ -187,6 +203,7 @@ class _LibraryFastScrollerState extends State { _dragging = false; _show(); }); + _scheduleHide(); } @override diff --git a/test/library_fast_scroller_test.dart b/test/library_fast_scroller_test.dart index 8d9f56756..8a9cf8ebe 100644 --- a/test/library_fast_scroller_test.dart +++ b/test/library_fast_scroller_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mornye/widgets/library_fast_scroller.dart'; @@ -149,6 +151,77 @@ void main() { expect(thumb(), findsNothing); }); + testWidgets('scrolling keeps the thumb visible without per-frame timers', ( + tester, + ) async { + final controller = ScrollController(); + addTearDown(controller.dispose); + await tester.pumpWidget(host(controller: controller)); + var hideTimers = 0; + double opacity() => tester + .widget( + find.descendant(of: thumb(), matching: find.byType(AnimatedOpacity)), + ) + .opacity; + + await runZoned( + () async { + final gesture = await tester.startGesture(const Offset(200, 450)); + for (var index = 0; index < 100; index++) { + await gesture.moveBy(const Offset(0, -2)); + await tester.pump(const Duration(milliseconds: 16)); + } + expect(controller.offset, greaterThan(100)); + expect(opacity(), 1); + expect(hideTimers, 0); + + await tester.pump(const Duration(seconds: 2)); + expect(opacity(), 1); + await gesture.up(); + await tester.pump(); + expect(hideTimers, 1); + expect(opacity(), 1); + await tester.pump(const Duration(milliseconds: 1400)); + expect(opacity(), 1); + await tester.pump(const Duration(milliseconds: 100)); + expect(opacity(), 0); + await tester.pumpAndSettle(); + }, + zoneSpecification: ZoneSpecification( + createTimer: (self, parent, zone, duration, callback) { + if (duration == const Duration(milliseconds: 1500)) hideTimers++; + return parent.createTimer(zone, duration, callback); + }, + ), + ); + expect(tester.takeException(), isNull); + }); + + testWidgets('short lists allocate no thumb hide timers while scrolling', ( + tester, + ) async { + final controller = ScrollController(); + addTearDown(controller.dispose); + await tester.pumpWidget(host(controller: controller, itemCount: 20)); + var hideTimers = 0; + await runZoned( + () async { + for (var index = 1; index <= 30; index++) { + controller.jumpTo(index * 10); + await tester.pump(); + } + }, + zoneSpecification: ZoneSpecification( + createTimer: (self, parent, zone, duration, callback) { + if (duration == const Duration(milliseconds: 1500)) hideTimers++; + return parent.createTimer(zone, duration, callback); + }, + ), + ); + expect(hideTimers, 0); + expect(thumb(), findsNothing); + }); + testWidgets('thumb and date bubble stay clear of the pinned Library header', ( tester, ) async {