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

fix(android): 📺 makeImageFromView respect scroll offset #2306

Merged
merged 11 commits into from
Apr 2, 2024
68 changes: 68 additions & 0 deletions example/src/Tests/Screens/Snapshot5.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from "react";
import { ScrollView, StyleSheet, View } from "react-native";

export const Snapshot5 = () => {
return (
<View style={{ flex: 1 }}>
<View style={styles.view}>
<Component />
</View>
</View>
);
};

const Component = () => {
return (
<>
<ScrollView
style={styles.scrollview}
ref={(ref) => {
ref?.scrollTo({ y: 200 });
}}
>
<View style={[styles.verticalBox, { backgroundColor: "cyan" }]} />
<View style={[styles.verticalBox, { backgroundColor: "red" }]} />
<View style={[styles.verticalBox, { backgroundColor: "green" }]} />
</ScrollView>

<ScrollView
style={styles.scrollViewHorizontal}
horizontal
ref={(ref) => {
ref?.scrollTo({ x: 200 });
}}
>
<View style={[styles.horizontalBox, { backgroundColor: "cyan" }]} />
<View style={[styles.horizontalBox, { backgroundColor: "red" }]} />
<View style={[styles.horizontalBox, { backgroundColor: "green" }]} />
</ScrollView>
</>
);
};

const styles = StyleSheet.create({
view: {
flex: 1,
backgroundColor: "yellow",
},
scrollview: {
padding: 14,
paddingTop: 100,
maxHeight: 150,
},
scrollViewHorizontal: {
padding: 14,
paddingTop: 100,
maxHeight: 150,
},
verticalBox: {
flex: 1,
minHeight: 500,
borderWidth: 5,
},
horizontalBox: {
width: 200,
height: "100%",
borderWidth: 5,
},
});
2 changes: 2 additions & 0 deletions example/src/Tests/Screens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { Snapshot1 } from "./Snapshot1";
import { Snapshot2 } from "./Snapshot2";
import { Snapshot3 } from "./Snapshot3";
import { Snapshot4 } from "./Snapshot4";
import { Snapshot5 } from "./Snapshot5";

export const Screens: Record<string, FC> = {
Snapshot1,
Snapshot2,
Snapshot3,
Snapshot4,
Snapshot5,
};
68 changes: 68 additions & 0 deletions fabricexample/src/Tests/Screens/Snapshot5.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from "react";
import { ScrollView, StyleSheet, View } from "react-native";

export const Snapshot5 = () => {
return (
<View style={{ flex: 1 }}>
<View style={styles.view}>
<Component />
</View>
</View>
);
};

const Component = () => {
return (
<>
<ScrollView
style={styles.scrollview}
ref={(ref) => {
ref?.scrollTo({ y: 200 });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wcandillon Maybe remove these refs in all Snapshots since they doesn't work anyway because snapshot is taken before it scrolls?

Copy link
Contributor

Choose a reason for hiding this comment

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

we do wait before taking the screenshot but it is possible that on CI it is too slow to work. Locally I was getting a consistent result. If I see that it is unstable on CI, I will indeed change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

You seem to be pretty sure that the test will be flaky there. I should probably be very careful there :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, I would prefer slightly less cover in tests than flakines :-)

}}
>
<View style={[styles.verticalBox, { backgroundColor: "cyan" }]} />
<View style={[styles.verticalBox, { backgroundColor: "red" }]} />
<View style={[styles.verticalBox, { backgroundColor: "green" }]} />
</ScrollView>

<ScrollView
style={styles.scrollViewHorizontal}
horizontal
ref={(ref) => {
ref?.scrollTo({ x: 200 });
}}
>
<View style={[styles.horizontalBox, { backgroundColor: "cyan" }]} />
<View style={[styles.horizontalBox, { backgroundColor: "red" }]} />
<View style={[styles.horizontalBox, { backgroundColor: "green" }]} />
</ScrollView>
</>
);
};

const styles = StyleSheet.create({
view: {
flex: 1,
backgroundColor: "yellow",
},
scrollview: {
padding: 14,
paddingTop: 100,
maxHeight: 150,
},
scrollViewHorizontal: {
padding: 14,
paddingTop: 100,
maxHeight: 150,
},
verticalBox: {
flex: 1,
minHeight: 500,
borderWidth: 5,
},
horizontalBox: {
width: 200,
height: "100%",
borderWidth: 5,
},
});
2 changes: 2 additions & 0 deletions fabricexample/src/Tests/Screens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { Snapshot1 } from "./Snapshot1";
import { Snapshot2 } from "./Snapshot2";
import { Snapshot3 } from "./Snapshot3";
import { Snapshot4 } from "./Snapshot4";
import { Snapshot5 } from "./Snapshot5";

export const Screens: Record<string, FC> = {
Snapshot1,
Snapshot2,
Snapshot3,
Snapshot4,
Snapshot5,
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.UIManagerModule;
Expand Down Expand Up @@ -70,6 +71,17 @@ private static void renderViewToCanvas(Canvas canvas, View view, Paint paint, fl
canvas.save();
applyTransformations(canvas, view);

// If the view is a ScrollView or similar, clip to its bounds
if (view instanceof ScrollView) {
ScrollView scrollView = (ScrollView) view;
int clipLeft = scrollView.getScrollX();
int clipTop = scrollView.getScrollY();
int clipRight = clipLeft + scrollView.getWidth();
int clipBottom = clipTop + scrollView.getHeight();

canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
}

if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
drawBackgroundIfPresent(canvas, view, combinedOpacity);
Expand Down Expand Up @@ -166,7 +178,10 @@ private static void drawSurfaceView(Canvas canvas, SurfaceView sv, Paint paint,
private static void applyTransformations(final Canvas c, @NonNull final View view) {
final Matrix matrix = view.getMatrix();
final Matrix translateMatrix = new Matrix();
translateMatrix.setTranslate(view.getLeft() + view.getPaddingLeft(), view.getTop() + view.getPaddingTop());

translateMatrix.setTranslate(view.getLeft() + view.getPaddingLeft() - view.getScrollX(),
view.getTop() + view.getPaddingTop() - view.getScrollY());

c.concat(translateMatrix);
c.concat(matrix);
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions package/src/renderer/__tests__/e2e/Snapshot.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ describe("Snapshot", () => {
itRunsE2eOnly("should respect overflow: hidden", async () => {
await testSnapshot("Snapshot4");
});
itRunsE2eOnly("should respect ScrollView offset and padding", async () => {
wcandillon marked this conversation as resolved.
Show resolved Hide resolved
await testSnapshot("Snapshot5");
});
});
Loading