Skip to content

armgit / ente

Public Imported from GitHub
0

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

Custom ScrollPhysics to avoid extreme overscroll when using scrollbar

ashilkn401c8e160ae9
diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart b/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart
index c96989942a..e46324246c 100644
--- a/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart
+++ b/mobile/apps/photos/lib/ui/viewer/gallery/gallery.dart
@@ -1,4 +1,5 @@
import 'dart:async';
+import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
@@ -491,7 +492,7 @@ class GalleryState extends State<Gallery> {
clipBehavior: Clip.none,
children: [
CustomScrollView(
- physics: const BouncingScrollPhysics(),
+ physics: const ExponentialBouncingScrollPhysics(),
controller: _scrollController,
slivers: [
SliverToBoxAdapter(
@@ -794,3 +795,40 @@ class GalleryIndexUpdatedEvent {
GalleryIndexUpdatedEvent(this.tag, this.index);
}
+
+/// Scroll physics similar to [BouncingScrollPhysics] but with exponentially
+/// increasing friction when scrolling out of bounds.
+///
+/// This creates a stronger resistance to overscrolling the further you go
+/// past the scroll boundary.
+class ExponentialBouncingScrollPhysics extends BouncingScrollPhysics {
+ const ExponentialBouncingScrollPhysics({
+ this.frictionExponent = 7.0,
+ super.decelerationRate,
+ super.parent,
+ });
+
+ /// The exponent used in the friction calculation.
+ ///
+ /// A higher value will result in a more rapid increase in friction as the
+ /// user overscrolls. Defaults to 7.0.
+ final double frictionExponent;
+
+ @override
+ ExponentialBouncingScrollPhysics applyTo(ScrollPhysics? ancestor) {
+ return ExponentialBouncingScrollPhysics(
+ parent: buildParent(ancestor),
+ decelerationRate: decelerationRate,
+ frictionExponent: frictionExponent,
+ );
+ }
+
+ @override
+ double frictionFactor(double overscrollFraction) {
+ final double baseFactor = switch (decelerationRate) {
+ ScrollDecelerationRate.fast => 0.26,
+ ScrollDecelerationRate.normal => 0.52,
+ };
+ return baseFactor * math.exp(-overscrollFraction * frictionExponent);
+ }
+}