Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Fix repeated filmstrip flings

ashil-pilot2ff19afd10cd
diff --git a/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip.dart b/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip.dart
index dca8ec1e31..dc952e4635 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip.dart
@@ -70,8 +70,10 @@ class FileViewerFilmstrip extends StatefulWidget {
class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {
late final ScrollController _scrollController;
late int _visualSelectedIndex;
+ final Set<int> _activePointers = {};
bool _isUserScrollSession = false;
bool _alignmentScheduled = false;
+ bool _alignmentDeferred = false;
@override
void initState() {
@@ -98,7 +100,7 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {
if (!_isUserScrollSession &&
(selectedIndex != _clampIndex(oldWidget.selectedIndex) ||
widget.itemCount != oldWidget.itemCount)) {
- _scheduleAlignment(selectedIndex, animate: true);
+ _scheduleAlignment(animate: true);
}
}
@@ -145,24 +147,31 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {
0.0,
(constraints.maxWidth - _itemExtent) / 2,
);
- return NotificationListener<ScrollNotification>(
- onNotification: _onScrollNotification,
- child: SizedBox(
- height: kFileViewerFilmstripHeight,
- child: ListView.builder(
- key: fileViewerFilmstripListKey,
- controller: _scrollController,
- clipBehavior: Clip.none,
- scrollDirection: Axis.horizontal,
- physics: const _FilmstripScrollPhysics(),
- itemCount: widget.itemCount,
- itemExtent: _itemExtent,
- findChildIndexCallback: widget.findChildIndexCallback,
- padding: EdgeInsets.symmetric(horizontal: horizontalPadding),
- cacheExtent: _itemExtent * _cacheExtentInItems,
- addAutomaticKeepAlives: false,
- addSemanticIndexes: false,
- itemBuilder: (context, index) => _buildItem(context, index),
+ return Listener(
+ onPointerDown: (event) => _activePointers.add(event.pointer),
+ onPointerUp: _onPointerReleased,
+ onPointerCancel: _onPointerReleased,
+ child: NotificationListener<ScrollNotification>(
+ onNotification: _onScrollNotification,
+ child: SizedBox(
+ height: kFileViewerFilmstripHeight,
+ child: ListView.builder(
+ key: fileViewerFilmstripListKey,
+ controller: _scrollController,
+ clipBehavior: Clip.none,
+ scrollDirection: Axis.horizontal,
+ physics: const _FilmstripScrollPhysics(),
+ itemCount: widget.itemCount,
+ itemExtent: _itemExtent,
+ findChildIndexCallback: widget.findChildIndexCallback,
+ padding: EdgeInsets.symmetric(
+ horizontal: horizontalPadding,
+ ),
+ cacheExtent: _itemExtent * _cacheExtentInItems,
+ addAutomaticKeepAlives: false,
+ addSemanticIndexes: false,
+ itemBuilder: (context, index) => _buildItem(context, index),
+ ),
),
),
);
@@ -264,7 +273,7 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {
finalIndex,
FileViewerFilmstripSelectionSource.scrubCommit,
);
- _scheduleAlignment(_visualSelectedIndex, animate: true);
+ _scheduleAlignment(animate: true);
}
return false;
}
@@ -283,6 +292,16 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {
if (_isUserScrollSession) _selectNearestScrolledItem();
}
+ void _onPointerReleased(PointerEvent event) {
+ _activePointers.remove(event.pointer);
+ if (_activePointers.isEmpty &&
+ !_isUserScrollSession &&
+ _alignmentDeferred) {
+ _alignmentDeferred = false;
+ _scheduleAlignment(animate: true);
+ }
+ }
+
int _nearestScrolledIndex() {
if (!_scrollController.hasClients || widget.itemCount == 0) return 0;
return _clampIndex((_scrollController.offset / _itemExtent).round());
@@ -325,12 +344,18 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {
return linearProximity * linearProximity * (3 - (2 * linearProximity));
}
- void _scheduleAlignment(int index, {required bool animate}) {
+ void _scheduleAlignment({required bool animate}) {
if (widget.itemCount == 0 || _alignmentScheduled) return;
_alignmentScheduled = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
_alignmentScheduled = false;
- if (mounted) _alignToIndex(index, animate: animate);
+ if (!mounted) return;
+ if (_activePointers.isNotEmpty || _isUserScrollSession) {
+ _alignmentDeferred = true;
+ return;
+ }
+ _alignmentDeferred = false;
+ _alignToIndex(_visualSelectedIndex, animate: animate);
});
}
@@ -373,7 +398,12 @@ class _FilmstripScrollPhysics extends ScrollPhysics {
double get maxFlingVelocity => _maxFlingVelocity;
@override
- double carriedMomentum(double existingVelocity) => 0;
+ double carriedMomentum(double existingVelocity) {
+ final momentum = const BouncingScrollPhysics().carriedMomentum(
+ existingVelocity,
+ );
+ return momentum.clamp(-_maxFlingVelocity, _maxFlingVelocity).toDouble();
+ }
@override
Simulation? createBallisticSimulation(
diff --git a/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_test.dart b/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_test.dart
index e9421fab44..484f691ca4 100644
--- a/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_test.dart
+++ b/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_test.dart
@@ -297,7 +297,7 @@ void main() {
final position = _filmstripPosition(tester);
final physics = position.physics;
expect(physics.maxFlingVelocity, 800);
- expect(physics.carriedMomentum(5000), 0);
+ expect(physics.carriedMomentum(500), greaterThan(0));
final cappedSimulation = physics.createBallisticSimulation(position, 5000);
expect(cappedSimulation, isNotNull);
@@ -321,6 +321,81 @@ void main() {
);
});
+ testWidgets("a second fling keeps moving while the strip is coasting", (
+ tester,
+ ) async {
+ await tester.pumpWidget(
+ const _TestApp(
+ child: _FilmstripHarness(itemCount: 100, selectedIndex: 50),
+ ),
+ );
+ await tester.pumpAndSettle();
+
+ final list = find.byKey(fileViewerFilmstripListKey);
+ final position = _filmstripPosition(tester);
+ const motion = Offset(-60, 0);
+ const speed = 400.0;
+ await tester.fling(list, motion, speed);
+ await tester.pump();
+ final firstLaunchVelocity = position.activity!.velocity;
+ await tester.pump(const Duration(milliseconds: 10));
+ final coastingVelocity = position.activity!.velocity;
+ expect(position.isScrollingNotifier.value, isTrue);
+ expect(coastingVelocity.sign, firstLaunchVelocity.sign);
+ expect(
+ coastingVelocity.abs(),
+ inExclusiveRange(0, firstLaunchVelocity.abs()),
+ );
+
+ await tester.fling(
+ list,
+ motion,
+ speed,
+ // Run the replacement gesture without an intermediate frame so the
+ // interrupted fling's queued alignment races with its new ballistic.
+ frameInterval: const Duration(seconds: 1),
+ );
+ await tester.pump();
+
+ final repeatedLaunchVelocity = position.activity!.velocity;
+ expect(repeatedLaunchVelocity.sign, firstLaunchVelocity.sign);
+ expect(
+ repeatedLaunchVelocity.abs(),
+ greaterThan(firstLaunchVelocity.abs() * 1.05),
+ );
+ expect(repeatedLaunchVelocity.abs(), lessThanOrEqualTo(800.01));
+ });
+
+ testWidgets("holding a coasting strip does not start a center snap", (
+ tester,
+ ) async {
+ final key = GlobalKey<_FilmstripHarnessState>();
+ await tester.pumpWidget(
+ _TestApp(
+ child: _FilmstripHarness(key: key, itemCount: 100, selectedIndex: 50),
+ ),
+ );
+ await tester.pumpAndSettle();
+
+ final list = find.byKey(fileViewerFilmstripListKey);
+ final position = _filmstripPosition(tester);
+ await tester.fling(list, const Offset(-50, 0), 400);
+ await tester.pump();
+ await tester.pump(const Duration(milliseconds: 10));
+ expect(position.isScrollingNotifier.value, isTrue);
+
+ final gesture = await tester.startGesture(tester.getCenter(list));
+ final heldOffset = position.pixels;
+ await tester.pump();
+ await tester.pump(const Duration(milliseconds: 30));
+
+ expect(position.pixels, closeTo(heldOffset, 0.01));
+
+ await gesture.cancel();
+ await tester.pumpAndSettle();
+ _expectCentered(tester, key.currentState!.selectedIndex);
+ });
+
testWidgets("a drag that stays centered still ends its scrub session", (
tester,
) async {