Skip to content

Commit

Permalink
fix(android): 📺 makeImageFromView respect scroll offset (#2306)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodonisko authored Apr 2, 2024
1 parent e6e6add commit 4e63f30
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 1 deletion.
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 });
}}
>
<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 () => {
await testSnapshot("Snapshot5");
});
});

0 comments on commit 4e63f30

Please sign in to comment.