π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Polish gallery filmstrip lifecycle
ashil-pilot9e4eb8ddc2ee
diff --git a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dartindex 4133eebe0f..fe6275fa97 100644--- a/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/detail_page.dart@@ -38,6 +38,7 @@ import "package:photos/ui/tools/editor/video_editor_page.dart";import "package:photos/ui/viewer/file/file_app_bar.dart";import "package:photos/ui/viewer/file/file_bottom_bar.dart";import "package:photos/ui/viewer/file/file_viewer_filmstrip.dart";+import "package:photos/ui/viewer/file/file_viewer_filmstrip_preview_handoff.dart";import 'package:photos/ui/viewer/file/file_widget.dart';import "package:photos/ui/viewer/file/ocr/inline_text_detection.dart";import "package:photos/ui/viewer/file/panorama_viewer_screen.dart";@@ -180,7 +181,7 @@ class _BodyState extends State<_Body> {List<EnteFile>? _files;late PageController _pageController;final _selectedIndexNotifier = ValueNotifier(0);- final _filmstripPreviewIndexNotifier = ValueNotifier<int?>(null);+ final _filmstripPreviewHandoff = FileViewerFilmstripPreviewHandoff();final _inlineTextDetectionController = InlineTextDetectionController();bool _isFirstOpened = true;bool isGuestView = false;@@ -195,7 +196,6 @@ class _BodyState extends State<_Body> {ValueNotifier<double>? _bottomControlsAdditionalInsetNotifier;bool _isFilmstripScrubbing = false;bool _didPauseVideoForFilmstripScrub = false;- int? _pendingFilmstripCommitIndex;@overridevoid initState() {@@ -262,7 +262,7 @@ class _BodyState extends State<_Body> {_captionUpdatedSubscription.cancel();_pageController.dispose();_selectedIndexNotifier.dispose();- _filmstripPreviewIndexNotifier.dispose();+ _filmstripPreviewHandoff.dispose();_qrHelper?.dispose();_playbackSpeed.dispose();super.dispose();@@ -310,7 +310,7 @@ class _BodyState extends State<_Body> {appBar: PreferredSize(preferredSize: const Size.fromHeight(80),child: ValueListenableBuilder<int?>(- valueListenable: _filmstripPreviewIndexNotifier,+ valueListenable: _filmstripPreviewHandoff,builder: (context, previewIndex, child) =>AbsorbPointer(absorbing: previewIndex != null, child: child),child: ValueListenableBuilder(@@ -456,7 +456,7 @@ class _BodyState extends State<_Body> {),if (_shouldShowFilmstrip)ValueListenableBuilder<int?>(- valueListenable: _filmstripPreviewIndexNotifier,+ valueListenable: _filmstripPreviewHandoff,builder: (context, previewIndex, _) {final previewFile = previewIndex == null? null@@ -469,18 +469,8 @@ class _BodyState extends State<_Body> {child: ColoredBox(key: fileViewerFilmstripPreviewKey,color: Colors.black,- child: ThumbnailWidget(- previewFile,- key: ObjectKey(previewFile),- rawThumbnail: true,- diskLoadDeferDuration:- galleryThumbnailDiskLoadDeferDuration,- serverLoadDeferDuration:- galleryThumbnailServerLoadDeferDuration,- shouldShowSyncStatus: false,- shouldShowFavoriteIcon: false,- shouldShowVideoOverlayIcon: false,- shouldShowLivePhotoOverlay: false,+ child: _GalleryFilmstripThumbnail(+ file: previewFile,fit: BoxFit.contain,),),@@ -490,7 +480,7 @@ class _BodyState extends State<_Body> {),if (widget.config.mode != DetailPageMode.minimalistic)ValueListenableBuilder<int?>(- valueListenable: _filmstripPreviewIndexNotifier,+ valueListenable: _filmstripPreviewHandoff,builder: (context, previewIndex, child) => AbsorbPointer(// The toolbar stays visible, but must not act on the old// committed file while another file is being previewed.@@ -638,9 +628,18 @@ class _BodyState extends State<_Body> {} else {_selectedIndexNotifier.value = index;}- if (_pendingFilmstripCommitIndex == index) {- _clearFilmstripPreview();- }+ _filmstripPreviewHandoff.onPageChanged(+ index: index,+ fileIdentity: file,+ onPagePainted: () {+ if (!mounted ||+ _selectedIndexNotifier.value != index ||+ !identical(_fileAt(index), file)) {+ return;+ }+ _clearFilmstripPreview();+ },+ );if (!_isFilmstripScrubbing) {_runSelectedFileSideEffects(file);}@@ -701,7 +700,7 @@ class _BodyState extends State<_Body> {case FileViewerFilmstripSelectionSource.scrubPreview:_isFilmstripScrubbing = true;if (index == _selectedIndexNotifier.value) {- _filmstripPreviewIndexNotifier.value = null;+ _filmstripPreviewHandoff.clear();break;}if (!_didPauseVideoForFilmstripScrub) {@@ -711,36 +710,37 @@ class _BodyState extends State<_Body> {Bus.instance.fire(PauseVideoEvent(fileTag: selectedFile!.tag));}}- if (_filmstripPreviewIndexNotifier.value != index) {- _filmstripPreviewIndexNotifier.value = index;- }+ _filmstripPreviewHandoff.showPreview(index);break;case FileViewerFilmstripSelectionSource.scrubCommit:_isFilmstripScrubbing = false;- if (!_pageController.hasClients) {- _clearFilmstripPreview();- return;- }- if (index == _selectedIndexNotifier.value) {- _clearFilmstripPreview();- return;- }- _pendingFilmstripCommitIndex = index;- if (_filmstripPreviewIndexNotifier.value != index) {- _filmstripPreviewIndexNotifier.value = index;- }- _pageController.jumpToPage(index);+ _beginFilmstripHandoff(index, file);break;}}+ void _beginFilmstripHandoff(int index, EnteFile file) {+ if (!_pageController.hasClients) {+ _clearFilmstripPreview();+ return;+ }+ if (index == _selectedIndexNotifier.value) {+ if (!_filmstripPreviewHandoff.isPendingCommit(+ index: index,+ fileIdentity: file,+ )) {+ _clearFilmstripPreview();+ }+ return;+ }+ _filmstripPreviewHandoff.beginCommit(index: index, fileIdentity: file);+ _pageController.jumpToPage(index);+ }+void _clearFilmstripPreview() {_isFilmstripScrubbing = false;_didPauseVideoForFilmstripScrub = false;- _pendingFilmstripCommitIndex = null;- if (_filmstripPreviewIndexNotifier.value != null) {- _filmstripPreviewIndexNotifier.value = null;- }+ _filmstripPreviewHandoff.clear();}void _runSelectedFileSideEffects(EnteFile file) {@@ -933,7 +933,7 @@ class _BodyState extends State<_Body> {final inset = _shouldShowFilmstrip? kFileViewerFilmstripAdditionalBottomInset: 0.0;- if (notifier.value != inset) notifier.value = inset;+ notifier.value = inset;}EnteFile? _fileAt(int index) {@@ -1143,19 +1143,7 @@ class _GalleryFileViewerFilmstripOverlay extends StatelessWidget {return Stack(fit: StackFit.expand,children: [- ThumbnailWidget(- file,- key: ObjectKey(file),- rawThumbnail: true,- diskLoadDeferDuration: galleryThumbnailDiskLoadDeferDuration,- serverLoadDeferDuration:- galleryThumbnailServerLoadDeferDuration,- shouldShowSyncStatus: false,- shouldShowFavoriteIcon: false,- shouldShowVideoOverlayIcon: false,- shouldShowLivePhotoOverlay: false,- fit: BoxFit.cover,- ),+ _GalleryFilmstripThumbnail(file: file, fit: BoxFit.cover),if (file.fileType == FileType.video)const Center(child: Icon(@@ -1175,6 +1163,29 @@ class _GalleryFileViewerFilmstripOverlay extends StatelessWidget {}}+class _GalleryFilmstripThumbnail extends StatelessWidget {+ final EnteFile file;+ final BoxFit fit;++ const _GalleryFilmstripThumbnail({required this.file, required this.fit});++ @override+ Widget build(BuildContext context) {+ return ThumbnailWidget(+ file,+ key: ObjectKey(file),+ rawThumbnail: true,+ diskLoadDeferDuration: galleryThumbnailDiskLoadDeferDuration,+ serverLoadDeferDuration: galleryThumbnailServerLoadDeferDuration,+ shouldShowSyncStatus: false,+ shouldShowFavoriteIcon: false,+ shouldShowVideoOverlayIcon: false,+ shouldShowLivePhotoOverlay: false,+ fit: fit,+ );+ }+}+// This widget returns Positioned, so its parent must be a Stack.class _GallerySocialOverlay extends StatelessWidget {final EnteFile file;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 dc952e4635..09a8b33a54 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@@ -18,10 +18,9 @@ const kFileViewerFilmstripAdditionalBottomInset =kFileViewerFilmstripHeight + kFileViewerFilmstripGap + 6.0;const _itemExtent = 33.0;-const _selectedThumbnailWidth = 34.0;-const _selectedThumbnailHeight = 43.0;-const _thumbnailWidth = 29.0;-const _thumbnailHeight = 35.0;+const _selectedThumbnailSize = Size(34, 43);+const _thumbnailSize = Size(29, 35);+const _thumbnailBorderRadius = BorderRadius.all(Radius.circular(2.5));const _cacheExtentInItems = 4;const _scrollAnimationDuration = Duration(milliseconds: 180);const _maxFlingVelocity = 800.0;@@ -36,6 +35,8 @@ enum FileViewerFilmstripSelectionSource {scrubCommit,}+enum _AlignmentState { idle, scheduled, deferred }+typedef FileViewerFilmstripSelectionCallback =void Function(int index, FileViewerFilmstripSelectionSource source);typedef FileViewerFilmstripSemanticValueBuilder =@@ -69,18 +70,21 @@ 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;+ late final ValueNotifier<int> _focusedIndexNotifier;+ final Set<int> _activePointerIds = {};+ bool _isScrubbing = false;+ _AlignmentState _alignmentState = _AlignmentState.idle;++ int get _focusedIndex => _focusedIndexNotifier.value;++ bool get _isInteracting => _isScrubbing || _activePointerIds.isNotEmpty;@overridevoid initState() {super.initState();- _visualSelectedIndex = _clampIndex(widget.selectedIndex);+ _focusedIndexNotifier = ValueNotifier(_clampIndex(widget.selectedIndex));_scrollController = ScrollController(- initialScrollOffset: _offsetForIndex(_visualSelectedIndex),+ initialScrollOffset: _offsetForIndex(_focusedIndex),);_scrollController.addListener(_onScrollOffsetChanged);}@@ -89,18 +93,15 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {void didUpdateWidget(covariant FileViewerFilmstrip oldWidget) {super.didUpdateWidget(oldWidget);final selectedIndex = _clampIndex(widget.selectedIndex);- if (!_isUserScrollSession) {- if (_visualSelectedIndex != selectedIndex) {- _visualSelectedIndex = selectedIndex;- }- } else if (_visualSelectedIndex >= widget.itemCount) {- _visualSelectedIndex = selectedIndex;+ final itemCountChanged = widget.itemCount != oldWidget.itemCount;+ final shouldSyncFocus = !_isScrubbing || _focusedIndex >= widget.itemCount;+ final focusChanged = shouldSyncFocus && _focusedIndex != selectedIndex;+ if (focusChanged) {+ _focusedIndexNotifier.value = selectedIndex;}- if (!_isUserScrollSession &&- (selectedIndex != _clampIndex(oldWidget.selectedIndex) ||- widget.itemCount != oldWidget.itemCount)) {- _scheduleAlignment(animate: true);+ if (!_isScrubbing && (focusChanged || itemCountChanged)) {+ _scheduleCentering();}}@@ -108,6 +109,7 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {void dispose() {_scrollController.removeListener(_onScrollOffsetChanged);_scrollController.dispose();+ _focusedIndexNotifier.dispose();super.dispose();}@@ -117,29 +119,8 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {return const SizedBox.shrink();}- final selectedIndex = _clampIndex(_visualSelectedIndex);- return Semantics(- container: true,- label: widget.semanticLabel,- value: widget.semanticValueBuilder(selectedIndex + 1, widget.itemCount),- increasedValue: selectedIndex < widget.itemCount - 1- ? widget.semanticValueBuilder(selectedIndex + 2, widget.itemCount)- : null,- decreasedValue: selectedIndex > 0- ? widget.semanticValueBuilder(selectedIndex, widget.itemCount)- : null,- onIncrease: selectedIndex < widget.itemCount - 1- ? () => _select(- selectedIndex + 1,- FileViewerFilmstripSelectionSource.tap,- )- : null,- onDecrease: selectedIndex > 0- ? () => _select(- selectedIndex - 1,- FileViewerFilmstripSelectionSource.tap,- )- : null,+ return ValueListenableBuilder<int>(+ valueListenable: _focusedIndexNotifier,child: ExcludeSemantics(child: LayoutBuilder(builder: (context, constraints) {@@ -148,7 +129,7 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {(constraints.maxWidth - _itemExtent) / 2,);return Listener(- onPointerDown: (event) => _activePointers.add(event.pointer),+ onPointerDown: (event) => _activePointerIds.add(event.pointer),onPointerUp: _onPointerReleased,onPointerCancel: _onPointerReleased,child: NotificationListener<ScrollNotification>(@@ -178,6 +159,36 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {},),),+ builder: (context, focusedIndex, child) {+ final selectedIndex = _clampIndex(focusedIndex);+ return Semantics(+ container: true,+ label: widget.semanticLabel,+ value: widget.semanticValueBuilder(+ selectedIndex + 1,+ widget.itemCount,+ ),+ increasedValue: selectedIndex < widget.itemCount - 1+ ? widget.semanticValueBuilder(selectedIndex + 2, widget.itemCount)+ : null,+ decreasedValue: selectedIndex > 0+ ? widget.semanticValueBuilder(selectedIndex, widget.itemCount)+ : null,+ onIncrease: selectedIndex < widget.itemCount - 1+ ? () => _select(+ selectedIndex + 1,+ FileViewerFilmstripSelectionSource.tap,+ )+ : null,+ onDecrease: selectedIndex > 0+ ? () => _select(+ selectedIndex - 1,+ FileViewerFilmstripSelectionSource.tap,+ )+ : null,+ child: child,+ );+ },);}@@ -190,26 +201,24 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {height: kFileViewerFilmstripHeight,child: OverflowBox(minWidth: 0,- maxWidth: _selectedThumbnailWidth,+ maxWidth: _selectedThumbnailSize.width,minHeight: 0,- maxHeight: _selectedThumbnailHeight,+ maxHeight: _selectedThumbnailSize.height,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,+ final size = Size.lerp(+ _thumbnailSize,+ _selectedThumbnailSize,+ proximity,+ )!;+ return SizedBox.fromSize(+ size: size,child: DecoratedBox(decoration: BoxDecoration(- borderRadius: BorderRadius.circular(2.5),+ borderRadius: _thumbnailBorderRadius,boxShadow: [BoxShadow(color: Color.lerp(@@ -223,7 +232,7 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {],),child: ClipRRect(- borderRadius: BorderRadius.circular(2.5),+ borderRadius: _thumbnailBorderRadius,child: Stack(fit: StackFit.expand,children: [@@ -252,53 +261,49 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {bool _onScrollNotification(ScrollNotification notification) {if (notification is ScrollStartNotification &¬ification.dragDetails != null) {- _isUserScrollSession = true;+ _isScrubbing = true;widget.onSelectionChanged(- _visualSelectedIndex,+ _focusedIndex,FileViewerFilmstripSelectionSource.scrubStart,);}- if (notification is ScrollEndNotification && _isUserScrollSession) {- final finalIndex = _nearestScrolledIndex();- if (finalIndex != _visualSelectedIndex) {- _select(+ if (notification is ScrollEndNotification) {+ if (_isScrubbing) {+ final finalIndex = _syncFocusToScrollPosition();+ _isScrubbing = false;+ widget.onSelectionChanged(finalIndex,- FileViewerFilmstripSelectionSource.scrubPreview,- align: false,+ FileViewerFilmstripSelectionSource.scrubCommit,);}- _isUserScrollSession = false;- widget.onSelectionChanged(- finalIndex,- FileViewerFilmstripSelectionSource.scrubCommit,- );- _scheduleAlignment(animate: true);+ _scheduleCentering();}return false;}- void _selectNearestScrolledItem() {+ int _syncFocusToScrollPosition() {final index = _nearestScrolledIndex();- if (index == _visualSelectedIndex) return;- _select(- index,- FileViewerFilmstripSelectionSource.scrubPreview,- align: false,- );+ if (index != _focusedIndex) {+ _select(+ index,+ FileViewerFilmstripSelectionSource.scrubPreview,+ center: false,+ );+ }+ return index;}void _onScrollOffsetChanged() {- if (_isUserScrollSession) _selectNearestScrolledItem();+ if (_isScrubbing) _syncFocusToScrollPosition();}void _onPointerReleased(PointerEvent event) {- _activePointers.remove(event.pointer);- if (_activePointers.isEmpty &&- !_isUserScrollSession &&- _alignmentDeferred) {- _alignmentDeferred = false;- _scheduleAlignment(animate: true);+ _activePointerIds.remove(event.pointer);+ if (_activePointerIds.isEmpty &&+ !_isScrubbing &&+ _alignmentState == _AlignmentState.deferred) {+ _scheduleCentering();}}@@ -310,25 +315,24 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {void _select(int index,FileViewerFilmstripSelectionSource source, {- bool align = true,+ bool center = true,}) {final clampedIndex = _clampIndex(index);- if (clampedIndex == _visualSelectedIndex) {- if (clampedIndex != _clampIndex(widget.selectedIndex)) {- unawaited(HapticFeedback.selectionClick());- widget.onSelectionChanged(clampedIndex, source);- }- if (align) _alignToIndex(clampedIndex, animate: true);- return;+ final focusChanged = clampedIndex != _focusedIndex;+ final shouldNotify =+ focusChanged || clampedIndex != _clampIndex(widget.selectedIndex);+ if (focusChanged) {+ _focusedIndexNotifier.value = clampedIndex;+ }+ if (shouldNotify) {+ unawaited(HapticFeedback.selectionClick());+ widget.onSelectionChanged(clampedIndex, source);}- setState(() => _visualSelectedIndex = clampedIndex);- unawaited(HapticFeedback.selectionClick());- widget.onSelectionChanged(clampedIndex, source);- if (align) _alignToIndex(clampedIndex, animate: true);+ if (center) _animateToIndex(clampedIndex);}double _centerProximity(int index) {- var scrollOffset = _offsetForIndex(_visualSelectedIndex);+ var scrollOffset = _offsetForIndex(_focusedIndex);if (_scrollController.hasClients) {final position = _scrollController.position;scrollOffset = position.hasContentDimensions@@ -344,39 +348,36 @@ class _FileViewerFilmstripState extends State<FileViewerFilmstrip> {return linearProximity * linearProximity * (3 - (2 * linearProximity));}- void _scheduleAlignment({required bool animate}) {- if (widget.itemCount == 0 || _alignmentScheduled) return;- _alignmentScheduled = true;+ void _scheduleCentering() {+ if (widget.itemCount == 0 || _alignmentState == _AlignmentState.scheduled) {+ return;+ }+ _alignmentState = _AlignmentState.scheduled;WidgetsBinding.instance.addPostFrameCallback((_) {- _alignmentScheduled = false;if (!mounted) return;- if (_activePointers.isNotEmpty || _isUserScrollSession) {- _alignmentDeferred = true;+ if (_isInteracting) {+ _alignmentState = _AlignmentState.deferred;return;}- _alignmentDeferred = false;- _alignToIndex(_visualSelectedIndex, animate: animate);+ _alignmentState = _AlignmentState.idle;+ _animateToIndex(_focusedIndex);});}- void _alignToIndex(int index, {required bool animate}) {+ void _animateToIndex(int index) {if (widget.itemCount == 0 || !_scrollController.hasClients) return;final position = _scrollController.position;final target = _offsetForIndex(_clampIndex(index),).clamp(position.minScrollExtent, position.maxScrollExtent);if ((position.pixels - target).abs() < 0.5) return;- if (animate) {- unawaited(- _scrollController.animateTo(- target,- duration: _scrollAnimationDuration,- curve: Curves.easeOutCubic,- ),- );- } else {- _scrollController.jumpTo(target);- }+ unawaited(+ _scrollController.animateTo(+ target,+ duration: _scrollAnimationDuration,+ curve: Curves.easeOutCubic,+ ),+ );}int _clampIndex(int index) {@@ -432,17 +433,19 @@ class _FilmstripScrollPhysics extends ScrollPhysics {class _FilmstripBallisticSimulation extends Simulation {final Simulation _simulation;- final double timeScale;+ final double _timeScale;- _FilmstripBallisticSimulation(this._simulation, {required this.timeScale})- : super(tolerance: _simulation.tolerance);+ _FilmstripBallisticSimulation(this._simulation, {required double timeScale})+ : assert(timeScale > 0 && timeScale <= 1),+ _timeScale = timeScale,+ super(tolerance: _simulation.tolerance);@override- double x(double time) => _simulation.x(time * timeScale);+ double x(double time) => _simulation.x(time * _timeScale);@override- double dx(double time) => _simulation.dx(time * timeScale) * timeScale;+ double dx(double time) => _simulation.dx(time * _timeScale) * _timeScale;@override- bool isDone(double time) => _simulation.isDone(time * timeScale);+ bool isDone(double time) => _simulation.isDone(time * _timeScale);}diff --git a/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip_preview_handoff.dart b/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip_preview_handoff.dartnew file mode 100644index 0000000000..43d7a61650--- /dev/null+++ b/mobile/apps/photos/lib/ui/viewer/file/file_viewer_filmstrip_preview_handoff.dart@@ -0,0 +1,82 @@+import "package:flutter/widgets.dart";++/// Keeps the filmstrip preview visible until the committed page has painted.+///+/// Each pending clear is tied to both a generation and the file object so a+/// late callback cannot clear a newer preview after rapid navigation or a list+/// mutation.+class FileViewerFilmstripPreviewHandoff extends ValueNotifier<int?> {+ final void Function(VoidCallback callback) _scheduleAfterNextFramePaint;++ int _generation = 0;+ int? _pendingCommitIndex;+ Object? _pendingFileIdentity;+ bool _isDisposed = false;++ FileViewerFilmstripPreviewHandoff({+ void Function(VoidCallback callback)? scheduleAfterNextFramePaint,+ }) : _scheduleAfterNextFramePaint =+ scheduleAfterNextFramePaint ?? _afterNextFramePaint,+ super(null);++ void showPreview(int index) {+ _invalidatePendingCommit();+ value = index;+ }++ void beginCommit({required int index, required Object fileIdentity}) {+ _generation++;+ _pendingCommitIndex = index;+ _pendingFileIdentity = fileIdentity;+ value = index;+ }++ bool isPendingCommit({required int index, required Object fileIdentity}) =>+ _matchesPendingCommit(index, fileIdentity);++ void onPageChanged({+ required int index,+ required Object fileIdentity,+ required VoidCallback onPagePainted,+ }) {+ if (!_matchesPendingCommit(index, fileIdentity)) return;+ final generation = _generation;+ _scheduleAfterNextFramePaint(() {+ if (_isDisposed ||+ generation != _generation ||+ !_matchesPendingCommit(index, fileIdentity)) {+ return;+ }+ onPagePainted();+ });+ }++ void clear() {+ _invalidatePendingCommit();+ value = null;+ }++ void _invalidatePendingCommit() {+ _generation++;+ _pendingCommitIndex = null;+ _pendingFileIdentity = null;+ }++ bool _matchesPendingCommit(int index, Object fileIdentity) =>+ value == index &&+ _pendingCommitIndex == index &&+ identical(_pendingFileIdentity, fileIdentity);++ @override+ void dispose() {+ _isDisposed = true;+ _invalidatePendingCommit();+ super.dispose();+ }+}++void _afterNextFramePaint(VoidCallback callback) {+ WidgetsBinding.instance.scheduleFrameCallback((_) {+ WidgetsBinding.instance.addPostFrameCallback((_) => callback());+ });+}diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_control/gallery_video_controls.dart b/mobile/apps/photos/lib/ui/viewer/file/video_control/gallery_video_controls.dartindex ce5ee856d3..b0e8f86abf 100644--- a/mobile/apps/photos/lib/ui/viewer/file/video_control/gallery_video_controls.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_control/gallery_video_controls.dart@@ -22,6 +22,32 @@ ValueListenable<double> galleryBottomControlsAdditionalInsetListenable(const AlwaysStoppedAnimation(0);}+// This widget returns Positioned, so its parent must be a Stack.+class GalleryBottomControlsPositioned extends StatelessWidget {+ final double bottom;+ final Widget child;++ const GalleryBottomControlsPositioned({+ required this.bottom,+ required this.child,+ super.key,+ });++ @override+ Widget build(BuildContext context) {+ return ValueListenableBuilder<double>(+ valueListenable: galleryBottomControlsAdditionalInsetListenable(context),+ builder: (context, additionalBottomInset, child) => Positioned(+ left: 0,+ right: 0,+ bottom: bottom + additionalBottomInset,+ child: child!,+ ),+ child: child,+ );+ }+}+class EqualHeightSliderTrackShape extends RoundedRectSliderTrackShape {const EqualHeightSliderTrackShape();@@ -137,13 +163,9 @@ class VideoProgressRow extends StatelessWidget {}}-double videoStreamControlBottomInset(- bool hasCaption, {- double additionalBottomInset = 0,-}) {+double videoStreamControlBottomInset(bool hasCaption) {return kVideoProgressBottomInset +kVideoProgressHeight +- additionalBottomInset +(hasCaption ? kVideoCaptionGap + kVideoCaptionLineHeight : 0) +8;}diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit_common.dart b/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit_common.dartindex 0f121fc23e..80659568ee 100644--- a/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit_common.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit_common.dart@@ -208,20 +208,8 @@ class _VideoWidgetState extends State<VideoWidget> {),widget.isFromMemories? const SizedBox.shrink()- : ValueListenableBuilder<double>(- valueListenable:- galleryBottomControlsAdditionalInsetListenable(- context,- ),- builder: (context, additionalBottomInset, child) =>- Positioned(- bottom:- kVideoProgressRowBottomInset +- additionalBottomInset,- right: 0,- left: 0,- child: child!,- ),+ : GalleryBottomControlsPositioned(+ bottom: kVideoProgressRowBottomInset,child: IgnorePointer(ignoring: !value,child: SafeArea(@@ -237,21 +225,10 @@ class _VideoWidgetState extends State<VideoWidget> {),widget.isFromMemories? const SizedBox.shrink()- : ValueListenableBuilder<double>(- valueListenable:- galleryBottomControlsAdditionalInsetListenable(- context,- ),- builder: (context, additionalBottomInset, child) =>- Positioned(- bottom: videoStreamControlBottomInset(- widget.file.caption?.isNotEmpty ?? false,- additionalBottomInset: additionalBottomInset,- ),- right: 0,- left: 0,- child: child!,- ),+ : GalleryBottomControlsPositioned(+ bottom: videoStreamControlBottomInset(+ widget.file.caption?.isNotEmpty ?? false,+ ),child: SafeArea(top: false,left: false,diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dart b/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dartindex 8e23e79ee3..7fde332da4 100644--- a/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/video_widget_native.dart@@ -470,21 +470,8 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>Positioned.fill(child: _getLoadingWidget()),widget.isFromMemories? const SizedBox.shrink()- : ValueListenableBuilder<double>(- valueListenable:- galleryBottomControlsAdditionalInsetListenable(- context,- ),- builder:- (context, additionalBottomInset, child) =>- Positioned(- bottom:- kVideoProgressRowBottomInset +- additionalBottomInset,- right: 0,- left: 0,- child: child!,- ),+ : GalleryBottomControlsPositioned(+ bottom: kVideoProgressRowBottomInset,child: SafeArea(top: false,left: false,@@ -501,27 +488,10 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>),widget.isFromMemories? const SizedBox.shrink()- : ValueListenableBuilder<double>(- valueListenable:- galleryBottomControlsAdditionalInsetListenable(- context,- ),- builder:- (- context,- additionalBottomInset,- child,- ) => Positioned(- bottom: videoStreamControlBottomInset(- widget.file.caption?.isNotEmpty ??- false,- additionalBottomInset:- additionalBottomInset,- ),- right: 0,- left: 0,- child: child!,- ),+ : GalleryBottomControlsPositioned(+ bottom: videoStreamControlBottomInset(+ widget.file.caption?.isNotEmpty ?? false,+ ),child: SafeArea(top: false,left: false,diff --git a/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_preview_handoff_test.dart b/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_preview_handoff_test.dartnew file mode 100644index 0000000000..31929b3007--- /dev/null+++ b/mobile/apps/photos/test/ui/viewer/file/file_viewer_filmstrip_preview_handoff_test.dart@@ -0,0 +1,170 @@+import "package:flutter/material.dart";+import "package:flutter_test/flutter_test.dart";+import "package:photos/ui/viewer/file/file_viewer_filmstrip_preview_handoff.dart";++const _previewKey = ValueKey<String>("filmstrip-preview-handoff-test");+const _targetPageKey = ValueKey<String>("filmstrip-target-page-test");++void main() {+ testWidgets("keeps the preview through the committed page's first paint", (+ tester,+ ) async {+ final handoff = FileViewerFilmstripPreviewHandoff();+ final committedPage = ValueNotifier(0);+ final targetPainter = _CountingPainter();+ addTearDown(handoff.dispose);+ addTearDown(committedPage.dispose);++ await tester.pumpWidget(+ _HandoffHarness(+ handoff: handoff,+ committedPage: committedPage,+ targetPainter: targetPainter,+ ),+ );++ handoff.showPreview(1);+ await tester.pump();+ expect(find.byKey(_previewKey), findsOneWidget);+ expect(targetPainter.paintCount, 0);++ final targetFile = Object();+ handoff.beginCommit(index: 1, fileIdentity: targetFile);+ WidgetsBinding.instance.addPostFrameCallback((_) {+ committedPage.value = 1;+ handoff.onPageChanged(+ index: 1,+ fileIdentity: targetFile,+ onPagePainted: handoff.clear,+ );+ });+ tester.binding.scheduleFrame();++ expect(handoff.value, 1);++ await tester.pump();+ expect(committedPage.value, 1);+ expect(targetPainter.paintCount, 0);+ expect(find.byKey(_previewKey), findsOneWidget);++ await tester.pump();++ expect(find.byKey(_targetPageKey), findsOneWidget);+ expect(targetPainter.paintCount, greaterThan(0));+ expect(find.byKey(_previewKey), findsOneWidget);+ expect(handoff.value, isNull);++ await tester.pump();+ expect(find.byKey(_previewKey), findsNothing);+ });++ test("a stale page-change callback cannot clear a newer preview", () {+ VoidCallback? scheduledCallback;+ final handoff = FileViewerFilmstripPreviewHandoff(+ scheduleAfterNextFramePaint: (callback) => scheduledCallback = callback,+ );+ addTearDown(handoff.dispose);+ var pagePaintedCount = 0;++ final staleFile = Object();+ handoff.beginCommit(index: 1, fileIdentity: staleFile);+ handoff.onPageChanged(+ index: 1,+ fileIdentity: staleFile,+ onPagePainted: () {+ pagePaintedCount++;+ handoff.clear();+ },+ );+ handoff.showPreview(2);+ scheduledCallback!();++ expect(pagePaintedCount, 0);+ expect(handoff.value, 2);+ });++ test("a page callback must match the committed file identity", () {+ VoidCallback? scheduledCallback;+ final handoff = FileViewerFilmstripPreviewHandoff(+ scheduleAfterNextFramePaint: (callback) => scheduledCallback = callback,+ );+ addTearDown(handoff.dispose);++ final committedFile = Object();+ final replacementFile = Object();+ handoff.beginCommit(index: 1, fileIdentity: committedFile);+ expect(+ handoff.isPendingCommit(index: 1, fileIdentity: committedFile),+ isTrue,+ );+ expect(+ handoff.isPendingCommit(index: 1, fileIdentity: replacementFile),+ isFalse,+ );+ handoff.onPageChanged(+ index: 1,+ fileIdentity: replacementFile,+ onPagePainted: handoff.clear,+ );++ expect(scheduledCallback, isNull);+ expect(handoff.value, 1);+ });+}++class _HandoffHarness extends StatelessWidget {+ final FileViewerFilmstripPreviewHandoff handoff;+ final ValueNotifier<int> committedPage;+ final CustomPainter targetPainter;++ const _HandoffHarness({+ required this.handoff,+ required this.committedPage,+ required this.targetPainter,+ });++ @override+ Widget build(BuildContext context) => MaterialApp(+ home: Scaffold(+ body: Stack(+ fit: StackFit.expand,+ children: [+ ValueListenableBuilder<int>(+ valueListenable: committedPage,+ builder: (context, page, _) => page == 1+ ? CustomPaint(+ key: _targetPageKey,+ painter: targetPainter,+ child: const ColoredBox(color: Colors.blue),+ )+ : const ColoredBox(color: Colors.black),+ ),+ ValueListenableBuilder<int?>(+ valueListenable: handoff,+ builder: (context, previewIndex, _) => previewIndex == null+ ? const SizedBox.shrink()+ : Positioned.fill(+ child: ColoredBox(+ key: _previewKey,+ color: Colors.grey,+ child: Center(child: Text("Preview $previewIndex")),+ ),+ ),+ ),+ ],+ ),+ ),+ );+}++class _CountingPainter extends CustomPainter {+ int paintCount = 0;++ @override+ void paint(Canvas canvas, Size size) {+ paintCount++;+ }++ @override+ bool shouldRepaint(covariant _CountingPainter oldDelegate) => false;+}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 484f691ca4..cda75b3be8 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@@ -10,69 +10,68 @@ void main() {) async {final key = GlobalKey<_FilmstripHarnessState>();final semantics = tester.ensureSemantics();+ try {+ await tester.pumpWidget(+ _TestApp(+ child: _FilmstripHarness(key: key, itemCount: 20, selectedIndex: 7),+ ),+ );+ await tester.pumpAndSettle();- await tester.pumpWidget(- _TestApp(- child: _FilmstripHarness(key: key, itemCount: 20, selectedIndex: 7),- ),- );- await tester.pumpAndSettle();-- _expectCentered(tester, 7);- expect(tester.getSize(find.byKey(fileViewerFilmstripListKey)).height, 45);- expect(- tester.getCenter(find.byKey(const ValueKey("filmstrip-test-item-8"))).dx -- tester- .getCenter(find.byKey(const ValueKey("filmstrip-test-item-7")))- .dx,- closeTo(33, 0.01),- );- expect(_thumbnailSize(tester, 7), const Size(34, 43));- expect(_thumbnailSize(tester, 8), const Size(29, 35));- final semanticsNode = tester.getSemantics(- find.byKey(fileViewerFilmstripKey),- );- final semanticsData = semanticsNode.getSemanticsData();- expect(semanticsData.label, "Photo chooser");- expect(semanticsData.value, "Photo 8 of 20");- expect(semanticsData.hasAction(SemanticsAction.increase), isTrue);- expect(semanticsData.hasAction(SemanticsAction.decrease), isTrue);-- semanticsNode.owner!.performAction(- semanticsNode.id,- SemanticsAction.increase,- );- await tester.pumpAndSettle();- expect(key.currentState!.selections.last.index, 8);- _expectCentered(tester, 8);-- final updatedSemanticsNode = tester.getSemantics(- find.byKey(fileViewerFilmstripKey),- );- updatedSemanticsNode.owner!.performAction(- updatedSemanticsNode.id,- SemanticsAction.decrease,- );- await tester.pumpAndSettle();- expect(key.currentState!.selections.last.index, 7);- _expectCentered(tester, 7);- semantics.dispose();+ _expectCentered(tester, 7);+ expect(tester.getSize(find.byKey(fileViewerFilmstripListKey)).height, 45);+ expect(+ tester+ .getCenter(find.byKey(const ValueKey("filmstrip-test-item-8")))+ .dx -+ tester+ .getCenter(find.byKey(const ValueKey("filmstrip-test-item-7")))+ .dx,+ closeTo(33, 0.01),+ );+ expect(_thumbnailSize(tester, 7), const Size(34, 43));+ expect(_thumbnailSize(tester, 8), const Size(29, 35));+ final semanticsNode = tester.getSemantics(+ find.byKey(fileViewerFilmstripKey),+ );+ final semanticsData = semanticsNode.getSemanticsData();+ expect(semanticsData.label, "Photo chooser");+ expect(semanticsData.value, "Photo 8 of 20");+ expect(semanticsData.hasAction(SemanticsAction.increase), isTrue);+ expect(semanticsData.hasAction(SemanticsAction.decrease), isTrue);++ semanticsNode.owner!.performAction(+ semanticsNode.id,+ SemanticsAction.increase,+ );+ await tester.pumpAndSettle();+ expect(key.currentState!.selections.last.index, 8);+ _expectCentered(tester, 8);++ final updatedSemanticsNode = tester.getSemantics(+ find.byKey(fileViewerFilmstripKey),+ );+ expect(updatedSemanticsNode.getSemanticsData().value, "Photo 9 of 20");+ updatedSemanticsNode.owner!.performAction(+ updatedSemanticsNode.id,+ SemanticsAction.decrease,+ );+ await tester.pumpAndSettle();+ expect(key.currentState!.selections, [+ (index: 8, source: FileViewerFilmstripSelectionSource.tap),+ (index: 7, source: FileViewerFilmstripSelectionSource.tap),+ ]);+ _expectCentered(tester, 7);+ } finally {+ semantics.dispose();+ }});testWidgets("a tap selects immediately and keeps the size animation", (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),- );+ final haptics = _installHapticSpy();await tester.pumpWidget(_TestApp(child: _FilmstripHarness(key: key, itemCount: 20, selectedIndex: 7),@@ -86,7 +85,6 @@ 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));@@ -99,7 +97,7 @@ void main() {expect(key.currentState!.selections, [(index: 8, source: FileViewerFilmstripSelectionSource.tap),]);- expect(hapticCount, 1);+ expect(haptics.count, 1);_expectCentered(tester, 8);expect(_thumbnailSize(tester, 8), const Size(34, 43));});@@ -108,16 +106,7 @@ void main() {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),- );+ final haptics = _installHapticSpy();await tester.pumpWidget(_TestApp(child: _FilmstripHarness(key: key, itemCount: 20, selectedIndex: 7),@@ -154,14 +143,14 @@ void main() {expect(key.currentState!.selections.map((event) => event.source), [FileViewerFilmstripSelectionSource.scrubStart,]);- expect(hapticCount, 0);+ expect(haptics.count, 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);+ expect(haptics.count, 0);await moveTo(0.55);final outgoingAt55 = _thumbnailSize(tester, 7);@@ -174,12 +163,12 @@ void main() {index: 8,source: FileViewerFilmstripSelectionSource.scrubPreview,));- expect(hapticCount, 1);+ expect(haptics.count, 1);await moveTo(0.75);expect(_thumbnailSize(tester, 7).width, lessThan(outgoingAt55.width));expect(_thumbnailSize(tester, 8).width, greaterThan(incomingAt55.width));- expect(hapticCount, 1);+ expect(haptics.count, 1);await tester.pump(const Duration(milliseconds: 200));await gesture.up();@@ -188,24 +177,54 @@ void main() {index: 8,source: FileViewerFilmstripSelectionSource.scrubCommit,));- expect(hapticCount, 1);+ expect(haptics.count, 1);_expectCentered(tester, 8);});- testWidgets("scrubbing previews and ticks each newly centered thumbnail", (+ testWidgets("scrub focus changes do not rebuild itemBuilder children", (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),+ final buildCounts = <int, int>{};+ await tester.pumpWidget(+ _TestApp(+ child: _FilmstripHarness(+ key: key,+ itemCount: 20,+ selectedIndex: 7,+ onItemBuild: (index) {+ buildCounts.update(index, (count) => count + 1, ifAbsent: () => 1);+ },+ ),+ ),+ );+ await tester.pumpAndSettle();+ final initialBuilds = {7: buildCounts[7]!, 8: buildCounts[8]!};++ final gesture = await tester.startGesture(+ tester.getCenter(find.byKey(fileViewerFilmstripListKey)),);+ await gesture.moveBy(const Offset(-20, 0));+ await tester.pump();+ await gesture.moveBy(const Offset(-33, 0));+ await tester.pump();++ expect(key.currentState!.selections.last, (+ index: 8,+ source: FileViewerFilmstripSelectionSource.scrubPreview,+ ));+ expect(buildCounts[7], initialBuilds[7]);+ expect(buildCounts[8], initialBuilds[8]);++ await gesture.up();+ await tester.pumpAndSettle();+ });++ testWidgets("scrubbing previews and ticks each newly centered thumbnail", (+ tester,+ ) async {+ final key = GlobalKey<_FilmstripHarnessState>();+ final haptics = _installHapticSpy();await tester.pumpWidget(_TestApp(child: _FilmstripHarness(key: key, itemCount: 20, selectedIndex: 7),@@ -238,7 +257,7 @@ void main() {).toList();expect(previewsBeforeUp.map((event) => event.index), [8]);- expect(hapticCount, 1);+ expect(haptics.count, 1);_expectNearestToStripCenter(tester, 8);await gesture.moveBy(const Offset(-33, 0));@@ -251,7 +270,7 @@ void main() {).toList();expect(previewsBeforeUp.map((event) => event.index), [8, 9]);- expect(hapticCount, 2);+ expect(haptics.count, 2);await gesture.moveBy(const Offset(33, 0));await tester.pump();@@ -263,7 +282,7 @@ void main() {).toList();expect(previewsBeforeUp.map((event) => event.index), [8, 9, 8]);- expect(hapticCount, 3);+ expect(haptics.count, 3);final previewIndex = previewsBeforeUp.last.index;_expectNearestToStripCenter(tester, previewIndex);@@ -271,7 +290,7 @@ void main() {await gesture.up();await tester.pumpAndSettle();- expect(hapticCount, previewsBeforeUp.length);+ expect(haptics.count, previewsBeforeUp.length);expect(key.currentState!.selections.last, (index: previewIndex,source: FileViewerFilmstripSelectionSource.scrubCommit,@@ -288,7 +307,9 @@ void main() {_expectCentered(tester, previewIndex);});- testWidgets("raises the fling cap and extends its coast", (tester) async {+ testWidgets("caps fling velocity at 800 and extends Android coast", (+ tester,+ ) async {await tester.pumpWidget(const _TestApp(child: _FilmstripHarness(itemCount: 20, selectedIndex: 7)),);@@ -297,7 +318,6 @@ void main() {final position = _filmstripPosition(tester);final physics = position.physics;expect(physics.maxFlingVelocity, 800);- expect(physics.carriedMomentum(500), greaterThan(0));final cappedSimulation = physics.createBallisticSimulation(position, 5000);expect(cappedSimulation, isNotNull);@@ -366,6 +386,91 @@ void main() {expect(repeatedLaunchVelocity.abs(), lessThanOrEqualTo(800.01));});+ testWidgets("a fling previews during its coast and commits once at rest", (+ tester,+ ) async {+ final key = GlobalKey<_FilmstripHarnessState>();+ final haptics = _installHapticSpy();+ await tester.pumpWidget(+ _TestApp(+ child: _FilmstripHarness(key: key, itemCount: 100, selectedIndex: 50),+ ),+ );+ await tester.pumpAndSettle();++ await tester.fling(+ find.byKey(fileViewerFilmstripListKey),+ const Offset(-80, 0),+ 400,+ );+ await tester.pump();++ final position = _filmstripPosition(tester);+ final previewsAtLaunch = key.currentState!.selections+ .where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubPreview,+ )+ .length;+ expect(position.isScrollingNotifier.value, isTrue);+ expect(+ key.currentState!.selections.where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubCommit,+ ),+ isEmpty,+ );++ await tester.pump(const Duration(milliseconds: 150));+ expect(position.isScrollingNotifier.value, isTrue);+ expect(+ key.currentState!.selections+ .where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubPreview,+ )+ .length,+ greaterThan(previewsAtLaunch),+ );+ expect(+ key.currentState!.selections.where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubCommit,+ ),+ isEmpty,+ );++ await tester.pumpAndSettle();++ final selections = key.currentState!.selections;+ final previews = selections+ .where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubPreview,+ )+ .toList();+ final commits = selections+ .where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubCommit,+ )+ .toList();+ expect(+ selections+ .where(+ (event) =>+ event.source == FileViewerFilmstripSelectionSource.scrubStart,+ )+ .length,+ 1,+ );+ expect(commits, hasLength(1));+ expect(selections.last, commits.single);+ expect(commits.single.index, previews.last.index);+ expect(haptics.count, previews.length);+ _expectCentered(tester, commits.single.index);+ });+testWidgets("holding a coasting strip does not start a center snap", (tester,) async {@@ -400,16 +505,7 @@ void main() {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),- );+ final haptics = _installHapticSpy();await tester.pumpWidget(_TestApp(child: _FilmstripHarness(key: key, itemCount: 20, selectedIndex: 7),@@ -429,7 +525,7 @@ void main() {(index: 7, source: FileViewerFilmstripSelectionSource.scrubStart),(index: 7, source: FileViewerFilmstripSelectionSource.scrubCommit),]);- expect(hapticCount, 0);+ expect(haptics.count, 0);_expectCentered(tester, 7);});@@ -437,16 +533,7 @@ void main() {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),- );+ final haptics = _installHapticSpy();await tester.pumpWidget(_TestApp(child: _FilmstripHarness(key: key, itemCount: 20, selectedIndex: 7),@@ -471,8 +558,7 @@ void main() {key.currentState!.update(itemCount: 5, selectedIndex: 2);await tester.pumpAndSettle();_expectCentered(tester, 2);- expect(tester.takeException(), isNull);- expect(hapticCount, 0);+ expect(haptics.count, 0);});}@@ -501,11 +587,20 @@ ScrollPosition _filmstripPosition(WidgetTester tester) {.position;}+_HapticSpy _installHapticSpy() {+ final spy = _HapticSpy()..install();+ addTearDown(spy.uninstall);+ return spy;+}+double _settlingTime(Simulation simulation, {required double start}) {var time = start;while (!simulation.isDone(time) && time < 10) {time += 0.01;}+ if (!simulation.isDone(time)) {+ fail("Simulation did not settle within 10 seconds");+ }return time;}@@ -549,13 +644,32 @@ class _TestApp extends StatelessWidget {}}+class _HapticSpy {+ int count = 0;++ void install() {+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .setMockMethodCallHandler(SystemChannels.platform, (call) async {+ if (call.method == "HapticFeedback.vibrate") count++;+ return null;+ });+ }++ void uninstall() {+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger+ .setMockMethodCallHandler(SystemChannels.platform, null);+ }+}+class _FilmstripHarness extends StatefulWidget {final int itemCount;final int selectedIndex;+ final ValueChanged<int>? onItemBuild;const _FilmstripHarness({required this.itemCount,required this.selectedIndex,+ this.onItemBuild,super.key,});@@ -584,10 +698,13 @@ 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(- key: ValueKey("filmstrip-test-thumbnail-$index"),- color: Color(0xFF000000 + index),- ),+ itemBuilder: (context, index) {+ widget.onItemBuild?.call(index);+ return ColoredBox(+ key: ValueKey("filmstrip-test-thumbnail-$index"),+ color: Color(0xFF000000 + index),+ );+ },onSelectionChanged: (index, source) {selections.add((index: index, source: source));if (source == FileViewerFilmstripSelectionSource.tap ||