[Photos] Fix auto scaling on loading final image (#749)

## Description

When an image is zoomed in, there were cases where when then final image
is loaded and rendered on screen, the image gets zoomed even more.

#### Case 1
Double tapping twice to zoom and then the final image is loaded.

##### Before


https://github.com/ente-io/ente/assets/77285023/7d9acb0f-2849-4ffb-863d-cab61ef4bd16

##### After


https://github.com/ente-io/ente/assets/77285023/89fb1238-1444-4681-a94f-7a58679f9350



#### Case 2
When screen is pressed or dragged (any contact with screen).

##### Before


https://github.com/ente-io/ente/assets/77285023/3aefdec5-3bc3-42ef-9442-592442273569

##### After


https://github.com/ente-io/ente/assets/77285023/7b458184-5abe-4983-ba21-f38080cc8bee

## Tests

Tested for regressions. Found one, which it not quite a regression as
the issue was already present but reproducible in a different flow. It
happens too fast for screen recording to capture it.

Not a blocker as it doesn't affect usability in anyway and since this
fix (auto scaling on final image loading) is an important fix.
This commit is contained in:
Vishnu Mohandas 2024-03-08 17:05:24 +05:30 committed by GitHub
commit c75e45897c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -41,8 +41,7 @@ class ZoomableImage extends StatefulWidget {
State<ZoomableImage> createState() => _ZoomableImageState();
}
class _ZoomableImageState extends State<ZoomableImage>
with SingleTickerProviderStateMixin {
class _ZoomableImageState extends State<ZoomableImage> {
late Logger _logger;
late EnteFile _photo;
ImageProvider? _imageProvider;
@ -54,6 +53,7 @@ class _ZoomableImageState extends State<ZoomableImage>
ValueChanged<PhotoViewScaleState>? _scaleStateChangedCallback;
bool _isZooming = false;
PhotoViewController _photoViewController = PhotoViewController();
final _scaleStateController = PhotoViewScaleStateController();
@override
void initState() {
@ -74,6 +74,7 @@ class _ZoomableImageState extends State<ZoomableImage>
@override
void dispose() {
_photoViewController.dispose();
_scaleStateController.dispose();
super.dispose();
}
@ -90,8 +91,10 @@ class _ZoomableImageState extends State<ZoomableImage>
content = PhotoViewGestureDetectorScope(
axis: Axis.vertical,
child: PhotoView(
key: ValueKey(_loadedFinalImage),
imageProvider: _imageProvider,
controller: _photoViewController,
scaleStateController: _scaleStateController,
scaleStateChangedCallback: _scaleStateChangedCallback,
minScale: widget.shouldCover
? PhotoViewComputedScale.covered
@ -272,15 +275,13 @@ class _ZoomableImageState extends State<ZoomableImage>
final scale = _photoViewController.scale! /
(finalImageInfo.image.width / prevImageInfo.image.width);
final currentPosition = _photoViewController.value.position;
final positionScaleFactor = 1 / scale;
final newPosition = currentPosition.scale(
positionScaleFactor,
positionScaleFactor,
);
_photoViewController = PhotoViewController(
initialPosition: newPosition,
initialPosition: currentPosition,
initialScale: scale,
);
// Fix for auto-zooming when final image is loaded after double tapping
//twice.
_scaleStateController.scaleState = PhotoViewScaleState.zoomedIn;
}
final bool canUpdateMetadata = _photo.canEditMetaInfo;
// forcefully get finalImageInfo is dimensions are not available in metadata