π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Refine gallery filmstrip interactions
ashil-pilotb20644b54097
diff --git a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dartindex f5e141feb1..4133eebe0f 100644--- a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart@@ -473,6 +473,10 @@ class _BodyState extends State<_Body> {previewFile,key: ObjectKey(previewFile),rawThumbnail: true,+ diskLoadDeferDuration:+ galleryThumbnailDiskLoadDeferDuration,+ serverLoadDeferDuration:+ galleryThumbnailServerLoadDeferDuration,shouldShowSyncStatus: false,shouldShowFavoriteIcon: false,shouldShowVideoOverlayIcon: false,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.dartindex bb8f4317e6..dca8ec1e31 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@@ -23,9 +23,11 @@ const _selectedThumbnailHeight = 43.0;const _thumbnailWidth = 29.0;const _thumbnailHeight = 35.0;const _cacheExtentInItems = 4;-const _selectionAnimationDuration = Duration(milliseconds: 120);const _scrollAnimationDuration = Duration(milliseconds: 180);-const _scrubThrottleDuration = Duration(milliseconds: 50);+const _maxFlingVelocity = 800.0;+// Evaluating a slightly faster platform simulation on a slower clock keeps the+// launch velocity unchanged while extending its distance and settling time.+const _ballisticTimeScale = 0.7;enum FileViewerFilmstripSelectionSource {tap,@@ -70,8 +72,6 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {late int _visualSelectedIndex;bool _isUserScrollSession = false;bool _alignmentScheduled = false;- Timer? _scrubThrottleTimer;- int? _pendingScrubIndex;@overridevoid initState() {@@ -80,6 +80,7 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {_scrollController = ScrollController(initialScrollOffset: _offsetForIndex(_visualSelectedIndex),);+ _scrollController.addListener(_onScrollOffsetChanged);}@override@@ -87,7 +88,9 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {super.didUpdateWidget(oldWidget);final selectedIndex = _clampIndex(widget.selectedIndex);if (!_isUserScrollSession) {- _visualSelectedIndex = selectedIndex;+ if (_visualSelectedIndex != selectedIndex) {+ _visualSelectedIndex = selectedIndex;+ }} else if (_visualSelectedIndex >= widget.itemCount) {_visualSelectedIndex = selectedIndex;}@@ -101,7 +104,7 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {@overridevoid dispose() {- _scrubThrottleTimer?.cancel();+ _scrollController.removeListener(_onScrollOffsetChanged);_scrollController.dispose();super.dispose();}@@ -151,6 +154,7 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {controller: _scrollController,clipBehavior: Clip.none,scrollDirection: Axis.horizontal,+ physics: const _FilmstripScrollPhysics(),itemCount: widget.itemCount,itemExtent: _itemExtent,findChildIndexCallback: widget.findChildIndexCallback,@@ -169,7 +173,6 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {}Widget _buildItem(BuildContext context, int index) {- final isSelected = index == _visualSelectedIndex;final item = GestureDetector(behavior: HitTestBehavior.opaque,onTap: () => _select(index, FileViewerFilmstripSelectionSource.tap),@@ -181,36 +184,54 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {maxWidth: _selectedThumbnailWidth,minHeight: 0,maxHeight: _selectedThumbnailHeight,- child: AnimatedContainer(- duration: _selectionAnimationDuration,- curve: Curves.easeOutCubic,- width: isSelected ? _selectedThumbnailWidth : _thumbnailWidth,- height: isSelected ? _selectedThumbnailHeight : _thumbnailHeight,- decoration: BoxDecoration(- borderRadius: BorderRadius.circular(2.5),- boxShadow: [- BoxShadow(- color: isSelected- ? const Color(0x33000000)- : const Color(0x24000000),- blurRadius: isSelected ? 3 : 2,- offset: const Offset(0, 1),- ),- ],- ),- child: ClipRRect(- borderRadius: BorderRadius.circular(2.5),- child: Stack(- fit: StackFit.expand,- children: [- widget.itemBuilder(context, index),- AnimatedContainer(- duration: _selectionAnimationDuration,- color: isSelected ? Colors.transparent : Colors.black26,+ child: AnimatedBuilder(+ animation: _scrollController,+ child: widget.itemBuilder(context, index),+ builder: (context, thumbnail) {+ final proximity = _centerProximity(index);+ final width =+ _thumbnailWidth ++ ((_selectedThumbnailWidth - _thumbnailWidth) * proximity);+ final height =+ _thumbnailHeight ++ ((_selectedThumbnailHeight - _thumbnailHeight) * proximity);+ return SizedBox(+ width: width,+ height: height,+ child: DecoratedBox(+ decoration: BoxDecoration(+ borderRadius: BorderRadius.circular(2.5),+ boxShadow: [+ BoxShadow(+ color: Color.lerp(+ const Color(0x24000000),+ const Color(0x33000000),+ proximity,+ )!,+ blurRadius: 2 + proximity,+ offset: const Offset(0, 1),+ ),+ ],),- ],- ),- ),+ child: ClipRRect(+ borderRadius: BorderRadius.circular(2.5),+ child: Stack(+ fit: StackFit.expand,+ children: [+ thumbnail!,+ ColoredBox(+ color: Color.lerp(+ Colors.black26,+ Colors.transparent,+ proximity,+ )!,+ ),+ ],+ ),+ ),+ ),+ );+ },),),),@@ -223,32 +244,19 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {if (notification is ScrollStartNotification &¬ification.dragDetails != null) {_isUserScrollSession = true;- _pendingScrubIndex = null;widget.onSelectionChanged(_visualSelectedIndex,FileViewerFilmstripSelectionSource.scrubStart,);}- if (_isUserScrollSession &&- (notification is ScrollUpdateNotification ||- notification is OverscrollNotification)) {- _queueNearestScrolledItem();- }-if (notification is ScrollEndNotification && _isUserScrollSession) {- _scrubThrottleTimer?.cancel();- _scrubThrottleTimer = null;- _pendingScrubIndex = null;final finalIndex = _nearestScrolledIndex();if (finalIndex != _visualSelectedIndex) {- unawaited(HapticFeedback.selectionClick());_select(finalIndex,FileViewerFilmstripSelectionSource.scrubPreview,align: false,- notifySelection: false,- triggerHaptic: false,);}_isUserScrollSession = false;@@ -261,32 +269,9 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {return false;}- void _queueNearestScrolledItem() {+ void _selectNearestScrolledItem() {final index = _nearestScrolledIndex();- if (index == _visualSelectedIndex) {- _pendingScrubIndex = null;- return;- }- if (index == _pendingScrubIndex) return;- _pendingScrubIndex = index;- if (_scrubThrottleTimer != null) return;- _flushPendingScrubSelection();- _startScrubThrottleWindow();- }-- void _startScrubThrottleWindow() {- _scrubThrottleTimer = Timer(_scrubThrottleDuration, () {- _scrubThrottleTimer = null;- if (!mounted || _pendingScrubIndex == null) return;- _flushPendingScrubSelection();- if (_isUserScrollSession) _startScrubThrottleWindow();- });- }-- void _flushPendingScrubSelection() {- final index = _pendingScrubIndex;- _pendingScrubIndex = null;- if (index == null) return;+ if (index == _visualSelectedIndex) return;_select(index,FileViewerFilmstripSelectionSource.scrubPreview,@@ -294,6 +279,10 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {);}+ void _onScrollOffsetChanged() {+ if (_isUserScrollSession) _selectNearestScrolledItem();+ }+int _nearestScrolledIndex() {if (!_scrollController.hasClients || widget.itemCount == 0) return 0;return _clampIndex((_scrollController.offset / _itemExtent).round());@@ -303,27 +292,39 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {int index,FileViewerFilmstripSelectionSource source, {bool align = true,- bool notifySelection = true,- bool triggerHaptic = true,}) {final clampedIndex = _clampIndex(index);if (clampedIndex == _visualSelectedIndex) {- if (notifySelection &&- clampedIndex != _clampIndex(widget.selectedIndex)) {- if (triggerHaptic) unawaited(HapticFeedback.selectionClick());+ if (clampedIndex != _clampIndex(widget.selectedIndex)) {+ unawaited(HapticFeedback.selectionClick());widget.onSelectionChanged(clampedIndex, source);}if (align) _alignToIndex(clampedIndex, animate: true);return;}setState(() => _visualSelectedIndex = clampedIndex);- if (notifySelection) {- if (triggerHaptic) unawaited(HapticFeedback.selectionClick());- widget.onSelectionChanged(clampedIndex, source);- }+ unawaited(HapticFeedback.selectionClick());+ widget.onSelectionChanged(clampedIndex, source);if (align) _alignToIndex(clampedIndex, animate: true);}+ double _centerProximity(int index) {+ var scrollOffset = _offsetForIndex(_visualSelectedIndex);+ if (_scrollController.hasClients) {+ final position = _scrollController.position;+ scrollOffset = position.hasContentDimensions+ ? position.pixels+ .clamp(position.minScrollExtent, position.maxScrollExtent)+ .toDouble()+ : position.pixels;+ }+ final linearProximity =+ (1 - ((scrollOffset - _offsetForIndex(index)).abs() / _itemExtent))+ .clamp(0.0, 1.0)+ .toDouble();+ return linearProximity * linearProximity * (3 - (2 * linearProximity));+ }+void _scheduleAlignment(int index, {required bool animate}) {if (widget.itemCount == 0 || _alignmentScheduled) return;_alignmentScheduled = true;@@ -360,3 +361,58 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {double _offsetForIndex(int index) => index * _itemExtent;}++class _FilmstripScrollPhysics extends ScrollPhysics {+ const _FilmstripScrollPhysics({super.parent});++ @override+ _FilmstripScrollPhysics applyTo(ScrollPhysics? ancestor) =>+ _FilmstripScrollPhysics(parent: buildParent(ancestor));++ @override+ double get maxFlingVelocity => _maxFlingVelocity;++ @override+ double carriedMomentum(double existingVelocity) => 0;++ @override+ Simulation? createBallisticSimulation(+ ScrollMetrics position,+ double velocity,+ ) {+ final cappedVelocity = velocity+ .clamp(-_maxFlingVelocity, _maxFlingVelocity)+ .toDouble();+ if (position.outOfRange ||+ cappedVelocity.abs() < toleranceFor(position).velocity) {+ return super.createBallisticSimulation(position, cappedVelocity);+ }+ final simulation = super.createBallisticSimulation(+ position,+ cappedVelocity / _ballisticTimeScale,+ );+ return simulation == null+ ? null+ : _FilmstripBallisticSimulation(+ simulation,+ timeScale: _ballisticTimeScale,+ );+ }+}++class _FilmstripBallisticSimulation extends Simulation {+ final Simulation _simulation;+ final double timeScale;++ _FilmstripBallisticSimulation(this._simulation, {required this.timeScale})+ : super(tolerance: _simulation.tolerance);++ @override+ double x(double time) => _simulation.x(time * timeScale);++ @override+ double dx(double time) => _simulation.dx(time * timeScale) * timeScale;++ @override+ bool isDone(double time) => _simulation.isDone(time * timeScale);+}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.dartindex d64dc09553..e9421fab44 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@@ -27,28 +27,8 @@ void main() {.dx,closeTo(33, 0.01),);- expect(- tester.getSize(- find- .descendant(- of: find.byKey(const ValueKey("filmstrip-test-item-7")),- matching: find.byType(AnimatedContainer),- )- .first,- ),- const Size(34, 43),- );- expect(- tester.getSize(- find- .descendant(- of: find.byKey(const ValueKey("filmstrip-test-item-8")),- matching: find.byType(AnimatedContainer),- )- .first,- ),- const Size(29, 35),- );+ expect(_thumbnailSize(tester, 7), const Size(34, 43));+ expect(_thumbnailSize(tester, 8), const Size(29, 35));final semanticsNode = tester.getSemantics(find.byKey(fileViewerFilmstripKey),);@@ -79,7 +59,9 @@ void main() {semantics.dispose();});- testWidgets("tapping a thumbnail selects and centers it", (tester) async {+ testWidgets("a tap selects immediately and keeps the size animation", (+ tester,+ ) async {final key = GlobalKey<_FilmstripHarnessState>();var hapticCount = 0;TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger@@ -104,6 +86,14 @@ void main() {index: 8,source: FileViewerFilmstripSelectionSource.tap,));+ expect(_thumbnailSize(tester, 8), const Size(29, 35));+ await tester.pump();+ expect(_thumbnailSize(tester, 8), const Size(29, 35));+ await tester.pump(const Duration(milliseconds: 1));+ await tester.pump(const Duration(milliseconds: 60));+ final transitioningSize = _thumbnailSize(tester, 8);+ expect(transitioningSize.width, inExclusiveRange(29, 34));+ expect(transitioningSize.height, inExclusiveRange(35, 43));await tester.pumpAndSettle();expect(key.currentState!.selections, [@@ -111,9 +101,10 @@ void main() {]);expect(hapticCount, 1);_expectCentered(tester, 8);+ expect(_thumbnailSize(tester, 8), const Size(34, 43));});- testWidgets("scrubbing previews and ticks each newly centered thumbnail", (+ testWidgets("sizes thumbnails continuously by their distance from center", (tester,) async {final key = GlobalKey<_FilmstripHarnessState>();@@ -134,15 +125,102 @@ void main() {);await tester.pumpAndSettle();+ final position = _filmstripPosition(tester);+ final initialOffset = position.pixels;+ final itemPitch =+ tester+ .getCenter(find.byKey(const ValueKey("filmstrip-test-item-8")))+ .dx -+ tester+ .getCenter(find.byKey(const ValueKey("filmstrip-test-item-7")))+ .dx;final gesture = await tester.startGesture(tester.getCenter(find.byKey(fileViewerFilmstripListKey)),);+ await gesture.moveBy(const Offset(-20, 0));+ await tester.pump();++ Future<void> moveTo(double itemFraction) async {+ final targetOffset = initialOffset + (itemPitch * itemFraction);+ await gesture.moveBy(Offset(position.pixels - targetOffset, 0));+ await tester.pump();+ }++ await moveTo(0.25);+ final outgoingAt25 = _thumbnailSize(tester, 7);+ final incomingAt25 = _thumbnailSize(tester, 8);+ expect(outgoingAt25.width, inExclusiveRange(29, 34));+ expect(incomingAt25.width, inExclusiveRange(29, outgoingAt25.width));+ expect(key.currentState!.selections.map((event) => event.source), [+ FileViewerFilmstripSelectionSource.scrubStart,+ ]);+ expect(hapticCount, 0);++ await moveTo(0.45);+ final outgoingAt45 = _thumbnailSize(tester, 7);+ final incomingAt45 = _thumbnailSize(tester, 8);+ expect(outgoingAt45.width, lessThan(outgoingAt25.width));+ expect(incomingAt45.width, greaterThan(incomingAt25.width));+ expect(hapticCount, 0);++ await moveTo(0.55);+ final outgoingAt55 = _thumbnailSize(tester, 7);+ final incomingAt55 = _thumbnailSize(tester, 8);+ expect(outgoingAt55.width, lessThan(outgoingAt45.width));+ expect(incomingAt55.width, greaterThan(incomingAt45.width));+ expect((outgoingAt55.width - outgoingAt45.width).abs(), lessThan(2));+ expect((incomingAt55.width - incomingAt45.width).abs(), lessThan(2));+ expect(key.currentState!.selections.last, (+ index: 8,+ source: FileViewerFilmstripSelectionSource.scrubPreview,+ ));+ expect(hapticCount, 1);++ await moveTo(0.75);+ expect(_thumbnailSize(tester, 7).width, lessThan(outgoingAt55.width));+ expect(_thumbnailSize(tester, 8).width, greaterThan(incomingAt55.width));+ expect(hapticCount, 1);+await tester.pump(const Duration(milliseconds: 200));+ await gesture.up();+ await tester.pumpAndSettle();+ expect(key.currentState!.selections.last, (+ index: 8,+ source: FileViewerFilmstripSelectionSource.scrubCommit,+ ));+ expect(hapticCount, 1);+ _expectCentered(tester, 8);+ });++ testWidgets("scrubbing previews and ticks each newly centered thumbnail", (+ tester,+ ) async {+ final key = GlobalKey<_FilmstripHarnessState>();+ var hapticCount = 0;+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .setMockMethodCallHandler(SystemChannels.platform, (call) async {+ if (call.method == "HapticFeedback.vibrate") hapticCount++;+ return null;+ });+ addTearDown(+ () => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .setMockMethodCallHandler(SystemChannels.platform, null),+ );+ await tester.pumpWidget(+ _TestApp(+ child: _FilmstripHarness(key: key, itemCount: 20, selectedIndex: 7),+ ),+ );+ await tester.pumpAndSettle();++ final gesture = await tester.startGesture(+ tester.getCenter(find.byKey(fileViewerFilmstripListKey)),+ );// The first move starts Flutter's drag recognizer; the second one scrolls.await gesture.moveBy(const Offset(-20, 0));await tester.pump();- await gesture.moveBy(const Offset(-72, 0));- await tester.pump(const Duration(milliseconds: 100));+ await gesture.moveBy(const Offset(-33, 0));+ await tester.pump();expect(key.currentState!.selections@@ -153,18 +231,43 @@ void main() {.length,1,);- final previewsBeforeUp = key.currentState!.selections+ var previewsBeforeUp = key.currentState!.selections.where((event) =>event.source == FileViewerFilmstripSelectionSource.scrubPreview,).toList();- expect(previewsBeforeUp, isNotEmpty);- expect(hapticCount, previewsBeforeUp.length);+ expect(previewsBeforeUp.map((event) => event.index), [8]);+ expect(hapticCount, 1);+ _expectNearestToStripCenter(tester, 8);++ await gesture.moveBy(const Offset(-33, 0));+ await tester.pump();++ previewsBeforeUp = key.currentState!.selections+ .where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubPreview,+ )+ .toList();+ expect(previewsBeforeUp.map((event) => event.index), [8, 9]);+ expect(hapticCount, 2);++ await gesture.moveBy(const Offset(33, 0));+ await tester.pump();++ previewsBeforeUp = key.currentState!.selections+ .where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubPreview,+ )+ .toList();+ expect(previewsBeforeUp.map((event) => event.index), [8, 9, 8]);+ expect(hapticCount, 3);+final previewIndex = previewsBeforeUp.last.index;_expectNearestToStripCenter(tester, previewIndex);- await tester.pump(const Duration(milliseconds: 200));await gesture.up();await tester.pumpAndSettle();@@ -185,6 +288,39 @@ void main() {_expectCentered(tester, previewIndex);});+ testWidgets("raises the fling cap and extends its coast", (tester) async {+ await tester.pumpWidget(+ const _TestApp(child: _FilmstripHarness(itemCount: 20, selectedIndex: 7)),+ );+ await tester.pumpAndSettle();++ final position = _filmstripPosition(tester);+ final physics = position.physics;+ expect(physics.maxFlingVelocity, 800);+ expect(physics.carriedMomentum(5000), 0);++ final cappedSimulation = physics.createBallisticSimulation(position, 5000);+ expect(cappedSimulation, isNotNull);+ expect(cappedSimulation!.dx(0).abs(), lessThanOrEqualTo(800.01));++ final tunedSimulation = physics.createBallisticSimulation(position, 500)!;+ final baselineSimulation = physics.parent!.createBallisticSimulation(+ position,+ 500,+ )!;+ expect(tunedSimulation.dx(0), closeTo(baselineSimulation.dx(0), 0.01));+ expect(+ tunedSimulation.dx(0.25).abs(),+ greaterThan(baselineSimulation.dx(0.25).abs()),+ );+ final tunedSettlingTime = _settlingTime(tunedSimulation, start: 0.25);+ final baselineSettlingTime = _settlingTime(baselineSimulation, start: 0.25);+ expect(+ tunedSettlingTime / baselineSettlingTime,+ inInclusiveRange(1.7, 2.0),+ );+ });+testWidgets("a drag that stays centered still ends its scrub session", (tester,) async {@@ -273,6 +409,31 @@ void _expectCentered(WidgetTester tester, int index) {expect(itemCenter.dx, closeTo(stripCenter.dx, 0.5));}+Size _thumbnailSize(WidgetTester tester, int index) {+ return tester.getSize(+ find.byKey(ValueKey("filmstrip-test-thumbnail-$index")),+ );+}++ScrollPosition _filmstripPosition(WidgetTester tester) {+ return tester+ .state<ScrollableState>(+ find.descendant(+ of: find.byKey(fileViewerFilmstripListKey),+ matching: find.byType(Scrollable),+ ),+ )+ .position;+}++double _settlingTime(Simulation simulation, {required double start}) {+ var time = start;+ while (!simulation.isDone(time) && time < 10) {+ time += 0.01;+ }+ return time;+}+void _expectNearestToStripCenter(WidgetTester tester, int index) {final stripCenter = tester.getCenter(find.byKey(fileViewerFilmstripListKey));final builtItems = find.byWidgetPredicate(@@ -348,8 +509,10 @@ class _FilmstripHarnessState extends State<_FilmstripHarness> {semanticLabel: "Photo chooser",semanticValueBuilder: (current, total) => "Photo $current of $total",itemKeyBuilder: (index) => ValueKey("filmstrip-test-item-$index"),- itemBuilder: (context, index) =>- ColoredBox(color: Color(0xFF000000 + index)),+ itemBuilder: (context, index) => ColoredBox(+ key: ValueKey("filmstrip-test-thumbnail-$index"),+ color: Color(0xFF000000 + index),+ ),onSelectionChanged: (index, source) {selections.add((index: index, source: source));if (source == FileViewerFilmstripSelectionSource.tap ||