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 1 commit
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
14 changes: 11 additions & 3 deletions src/components/ImageView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ImageView extends PureComponent {
this.canUseTouchScreen = canUseTouchScreen();
this.state = {
containerHeight: 0,
containerWidth: 0,
isZoomed: false,
isDragging: false,
isMouseDown: false,
Expand Down Expand Up @@ -68,6 +69,7 @@ class ImageView extends PureComponent {
const isScreenWiderThanImage = (this.props.windowWidth / width) > 1;
const isScreenTallerThanImage = (this.props.windowHeight / height) > 1;
const aspect = width / height;
let scale = this.state.zoomScale;
if (aspect > 1 && !isScreenWiderThanImage) {
// In case Width fit Screen width and Height not fit the Screen height
const fitRate = this.props.windowWidth / width;
Expand All @@ -84,8 +86,13 @@ class ImageView extends PureComponent {
imgRight = imgLeft + (fitRate * width);
}

//In case image loading is delayed than onLayout callback of the root View caused internet speed
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment isn't exactly clear. Is it still relevant/needed?

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, my internet speed is not good sometimes. And I noticed that it has some issue because we would not load image correctly.
If you don't need this, I will remove.

if (scale == 0) {
scale = Math.min(this.state.containerWidth / width, this.state.containerHeight / height);
}

this.setState({
imgWidth: width, imgHeight: height, imageLeft: imgLeft, imageTop: imgTop, imageRight: imgRight, imageBottom: imgBottom,
imgWidth: width, zoomScale: scale, imgHeight: height, imageLeft: imgLeft, imageTop: imgTop, imageRight: imgRight, imageBottom: imgBottom,
});
}

Expand Down Expand Up @@ -168,6 +175,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 @@ -180,9 +188,9 @@ class ImageView extends PureComponent {
>
<Pressable
style={{
...getZoomSizingStyle(this.state.isZoomed, this.state.imgWidth, this.state.imgHeight, this.state.zoomScale, this.state.containerHeight),
...getZoomSizingStyle(this.state.isZoomed, this.state.imgWidth, this.state.imgHeight, this.state.zoomScale, this.state.containerHeight, this.state.containerWidth),
...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
36 changes: 26 additions & 10 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2297,29 +2297,45 @@ 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) {
if (zoomScale > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you please note what's going on in the UI in each of these 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.

I added the comments for each styling logic.
And noticed that my styling was not correct in the previous commit.
So, fixed it too. (when isZoom === false && zoomScale < 1)
Please review and let me know if something is still not clear.
Thank you

Copy link
Contributor

Choose a reason for hiding this comment

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

That's perfect. Thanks!

return {
height: `${imgHeight * zoomScale}px`,
width: `${imgWidth * zoomScale}px`,
};
}
return {
height: `${(imgHeight * zoomScale)}px`,
width: `${(imgWidth * zoomScale)}px`,
height: `${imgHeight}px`,
width: `${imgWidth}px`,
top,
left,
};
}
if (zoomScale > 1) {
return {
height: `${imgHeight}px`,
width: `${imgWidth}px`,
top,
left,
};
}

const top = `${(containerHeight - imgHeight) / 2}px`;
const left = `calc(50% - ${imgWidth / 2}px)`;
return {
height: `${imgHeight}px`,
width: `${imgWidth}px`,
top,
left,
height: `${imgHeight * zoomScale}px`,
width: `${imgWidth * zoomScale}px`,
};
}

Expand Down