Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6326: fix bug reported at #6518 #6535

Merged
merged 5 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/components/ImageView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ImageView extends PureComponent {
this.canUseTouchScreen = canUseTouchScreen();
this.state = {
containerHeight: 0,
containerWidth: 0,
isZoomed: false,
isDragging: false,
isMouseDown: false,
Expand Down Expand Up @@ -85,9 +86,16 @@ class ImageView extends PureComponent {
imgRight = imgLeft + (fitRate * width);
}

this.setState({
imgWidth: width, imgHeight: height, imageLeft: imgLeft, imageTop: imgTop, imageRight: imgRight, imageBottom: imgBottom,
});
const newZoomScale = Math.min(this.state.containerWidth / width, this.state.containerHeight / height);
this.setState(prevState => ({
imgWidth: width,
zoomScale: prevState.zoomScale === 0 ? newZoomScale : prevState.zoomScale,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use the newZoomScale in all cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I was going to set zoomScale in the onLayout event of the parent view.
But while testing in my local, I noticed that it is sometimes 0 because image loading is slower than view loading.
So, reset with newZoomScale here.

imgHeight: height,
imageLeft: imgLeft,
imageTop: imgTop,
imageRight: imgRight,
imageBottom: imgBottom,
}));
}

/**
Expand Down Expand Up @@ -169,6 +177,7 @@ class ImageView extends PureComponent {
const scale = imageHeight && imageWidth ? Math.min(width / imageWidth, height / imageHeight) : 0;
this.setState({
containerHeight: height,
containerWidth: width,
zoomScale: scale,
});
}}
Expand All @@ -181,9 +190,10 @@ class ImageView extends PureComponent {
>
<Pressable
style={{
...StyleUtils.getZoomSizingStyle(this.state.isZoomed, this.state.imgWidth, this.state.imgHeight, this.state.zoomScale, this.state.containerHeight),
...StyleUtils.getZoomSizingStyle(this.state.isZoomed, this.state.imgWidth, this.state.imgHeight, this.state.zoomScale,
this.state.containerHeight, this.state.containerWidth),
...StyleUtils.getZoomCursorStyle(this.state.isZoomed, this.state.isDragging),
...this.state.isZoomed ? styles.pRelative : styles.pAbsolute,
...this.state.isZoomed && this.state.zoomScale > 1 ? styles.pRelative : styles.pAbsolute,
...styles.flex1,
}}
onPressIn={(e) => {
Expand Down
46 changes: 37 additions & 9 deletions src/styles/StyleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,57 @@ function getZoomCursorStyle(isZoomed, isDragging) {
* @param {Number} imgHeight
* @param {Number} zoomScale
* @param {Number} containerHeight
* @param {Number} containerWidth
* @return {Object}
*/
function getZoomSizingStyle(isZoomed, imgWidth, imgHeight, zoomScale, containerHeight) {
function getZoomSizingStyle(isZoomed, imgWidth, imgHeight, zoomScale, containerHeight, containerWidth) {
if (imgWidth === 0 || imgHeight === 0) {
return {
height: isZoomed ? '250%' : '100%',
width: isZoomed ? '250%' : '100%',
};
}
const top = `${Math.max((containerHeight - imgHeight) / 2, 0)}px`;
const left = `${Math.max((containerWidth - imgWidth) / 2, 0)}px`;

// Return different size and offset style based on zoomScale and isZoom.
if (isZoomed) {
// When both width and height are smaller than container(modal) size, set the height by multiplying zoomScale if it is zoomed in.
if (zoomScale > 1) {
return {
height: `${imgHeight * zoomScale}px`,
width: `${imgWidth * zoomScale}px`,
};
}

// If image height and width are bigger than container size, display image with original size because original size is bigger and position absolute.
return {
height: `${imgHeight}px`,
width: `${imgWidth}px`,
top,
left,
};
}

// If image is not zoomed in and image size is smaller than container size, display with original size based on offset and position absolute.
if (zoomScale > 1) {
return {
height: `${(imgHeight * zoomScale)}px`,
width: `${(imgWidth * zoomScale)}px`,
height: `${imgHeight}px`,
width: `${imgWidth}px`,
top,
left,
};
}

const top = `${(containerHeight - imgHeight) / 2}px`;
const left = `calc(50% - ${imgWidth / 2}px)`;
// If image is bigger than container size, display full image in the screen with scaled size (fit by container size) and position absolute.
// top, left offset should be different when displaying long or wide image.
const scaledTop = imgHeight > imgWidth ? 0 : `${Math.max((containerHeight - (imgHeight * zoomScale)) / 2, 0)}px`;
const scaledLeft = imgWidth > imgHeight ? 0 : `${Math.max((containerWidth - (imgWidth * zoomScale)) / 2, 0)}px`;
return {
height: `${imgHeight}px`,
width: `${imgWidth}px`,
top,
left,
height: `${imgHeight * zoomScale}px`,
width: `${imgWidth * zoomScale}px`,
top: scaledTop,
left: scaledLeft,
};
}

Expand Down