perf(library): remove per-frame scrollbar hide timers

This commit is contained in:
zarzet committed 2026-10-22 02:43:04 +07:00
1 parent 78fd9ce5cb
commit 060d71a44e
2 files changed
+102 -12

No files matched your search

+29 -12
View File
@@ -73,7 +73,7 @@ class _LibraryFastScrollerState extends State<LibraryFastScroller> {
}
void _onOuterPositionChanged() {
if (mounted) setState(() {});
if (mounted && _long && widget.topPadding > 0) setState(() {});
}
@override
@@ -100,12 +100,23 @@ class _LibraryFastScrollerState extends State<LibraryFastScroller> {
}
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<LibraryFastScroller> {
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<LibraryFastScroller> {
_dragging = false;
_show();
});
_scheduleHide();
}
@override
+73
View File
@@ -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<AnimatedOpacity>(
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 {