Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix video zoom gesture timing and extract ZoomableVideoViewer

ashilkn8e82e9046d5b
diff --git a/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart b/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart
index a8b731b5ed..9e198998ad 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/video_widget_media_kit.dart
@@ -26,6 +26,7 @@ import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/notification/toast.dart";
import "package:photos/ui/viewer/file/video_widget_media_kit_common.dart"
as common;
+import "package:photos/ui/viewer/file/zoomable_video_viewer.dart";
import "package:photos/utils/dialog_util.dart";
import "package:photos/utils/file_util.dart";
@@ -208,7 +209,7 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>
void _onZoomChanged() {
final scale = _transformationController.value.getMaxScaleOnAxis();
- final isZoomed = scale > 1.01;
+ final isZoomed = scale > kZoomThreshold;
if (_isZooming != isZoomed) {
_isZooming = isZoomed;
widget.shouldDisableScroll?.call(isZoomed);
@@ -237,6 +238,7 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>
controller!,
widget.playbackCallback,
transformationController: _transformationController,
+ shouldDisableScroll: widget.shouldDisableScroll,
isFromMemories: widget.isFromMemories,
onStreamChange: widget.onStreamChange,
isPreviewPlayer: widget.selectedPreview,
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.dart
index b6a90bf72b..d41bd4dffe 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
@@ -9,6 +9,7 @@ import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/actions/file/file_actions.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/viewer/file/video_stream_change.dart";
+import "package:photos/ui/viewer/file/zoomable_video_viewer.dart";
import "package:photos/utils/standalone/date_time.dart";
import "package:photos/utils/standalone/debouncer.dart";
@@ -17,6 +18,7 @@ class VideoWidget extends StatefulWidget {
final VideoController controller;
final FullScreenRequestCallback? playbackCallback;
final TransformationController? transformationController;
+ final Function(bool)? shouldDisableScroll;
final bool isFromMemories;
final void Function() onStreamChange;
final bool isPreviewPlayer;
@@ -27,6 +29,7 @@ class VideoWidget extends StatefulWidget {
this.playbackCallback, {
super.key,
this.transformationController,
+ this.shouldDisableScroll,
required this.isFromMemories,
// ignore: unused_element
required this.onStreamChange,
@@ -103,10 +106,9 @@ class _VideoWidgetState extends State<VideoWidget> {
children: [
// Video layer with zoom support
widget.transformationController != null
- ? InteractiveViewer(
+ ? ZoomableVideoViewer(
transformationController: widget.transformationController!,
- maxScale: 4.0,
- minScale: 1.0,
+ shouldDisableScroll: widget.shouldDisableScroll,
child: videoWidget,
)
: videoWidget,
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.dart
index b51148cdc6..f40fa21434 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
@@ -30,6 +30,7 @@ import "package:photos/ui/viewer/file/native_video_player_controls/play_pause_bu
import "package:photos/ui/viewer/file/native_video_player_controls/seek_bar.dart";
import "package:photos/ui/viewer/file/thumbnail_widget.dart";
import "package:photos/ui/viewer/file/video_stream_change.dart";
+import "package:photos/ui/viewer/file/zoomable_video_viewer.dart";
import "package:photos/utils/dialog_util.dart";
import "package:photos/utils/exif_util.dart";
import "package:photos/utils/file_util.dart";
@@ -260,7 +261,7 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>
void _onZoomChanged() {
final scale = _transformationController.value.getMaxScaleOnAxis();
- final isZoomed = scale > 1.01;
+ final isZoomed = scale > kZoomThreshold;
if (_isZooming != isZoomed) {
_isZooming = isZoomed;
widget.shouldDisableScroll?.call(isZoomed);
@@ -304,10 +305,9 @@ class _VideoWidgetNativeState extends State<VideoWidgetNative>
: Stack(
key: const ValueKey("video_ready"),
children: [
- InteractiveViewer(
+ ZoomableVideoViewer(
transformationController: _transformationController,
- maxScale: 4.0,
- minScale: 1.0,
+ shouldDisableScroll: widget.shouldDisableScroll,
child: Center(
child: AspectRatio(
aspectRatio: aspectRatio ?? 1,
diff --git a/mobile/apps/photos/lib/ui/viewer/file/zoomable_video_viewer.dart b/mobile/apps/photos/lib/ui/viewer/file/zoomable_video_viewer.dart
new file mode 100644
index 0000000000..15f4a56f2c
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/viewer/file/zoomable_video_viewer.dart
@@ -0,0 +1,76 @@
+import "package:flutter/widgets.dart";
+
+/// Threshold for considering the view as "zoomed in".
+/// Using 1.01 instead of 1.0 to account for floating-point precision.
+const double kZoomThreshold = 1.01;
+
+/// A widget that wraps [InteractiveViewer] with multi-touch detection.
+///
+/// This widget solves a gesture arena race condition: when pinch-to-zoom
+/// is attempted on a video inside a PageView, the PageView's drag gesture
+/// recognizer can claim single-finger touches before InteractiveViewer's
+/// scale gesture recognizer detects the second finger.
+///
+/// By using a low-level [Listener], we detect multi-touch at the pointer
+/// level (before gesture arena processing) and immediately disable the
+/// parent PageView's scrolling.
+class ZoomableVideoViewer extends StatefulWidget {
+ final TransformationController transformationController;
+ final Function(bool)? shouldDisableScroll;
+ final Widget child;
+
+ const ZoomableVideoViewer({
+ super.key,
+ required this.transformationController,
+ this.shouldDisableScroll,
+ required this.child,
+ });
+
+ @override
+ State<ZoomableVideoViewer> createState() => _ZoomableVideoViewerState();
+}
+
+class _ZoomableVideoViewerState extends State<ZoomableVideoViewer> {
+ int _activePointers = 0;
+
+ void _onPointerDown(PointerDownEvent _) {
+ _activePointers++;
+ if (_activePointers >= 2) {
+ widget.shouldDisableScroll?.call(true);
+ }
+ }
+
+ void _onPointerUp(PointerUpEvent _) {
+ _activePointers = (_activePointers - 1).clamp(0, 99);
+ _maybeReenableScroll();
+ }
+
+ void _onPointerCancel(PointerCancelEvent _) {
+ _activePointers = (_activePointers - 1).clamp(0, 99);
+ _maybeReenableScroll();
+ }
+
+ void _maybeReenableScroll() {
+ if (_activePointers < 2) {
+ final scale = widget.transformationController.value.getMaxScaleOnAxis();
+ if (scale <= kZoomThreshold) {
+ widget.shouldDisableScroll?.call(false);
+ }
+ }
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Listener(
+ onPointerDown: _onPointerDown,
+ onPointerUp: _onPointerUp,
+ onPointerCancel: _onPointerCancel,
+ child: InteractiveViewer(
+ transformationController: widget.transformationController,
+ maxScale: 4.0,
+ minScale: 1.0,
+ child: widget.child,
+ ),
+ );
+ }
+}