Skip to content

Commit 981cdf5

Browse files
authored
Merge pull request #2048 from Shopify/stagger-deprecation
Refine deprecations
2 parents 9e7c601 + 1a2505e commit 981cdf5

File tree

13 files changed

+82
-238
lines changed

13 files changed

+82
-238
lines changed

docs/docs/shapes/pictures.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ import {
2525
BlendMode
2626
} from "@shopify/react-native-skia";
2727

28-
const size = 256;
29-
3028
export const HelloWorld = () => {
3129
// Create a picture
3230
const picture = useMemo(() => createPicture(
33-
{ width: size, height: size },
3431
(canvas) => {
32+
const size = 256;
3533
const r = 0.33 * size;
3634
const paint = Skia.Paint();
3735
paint.setBlendMode(BlendMode.Multiply);
@@ -73,7 +71,6 @@ import {
7371
export const PictureExample = () => {
7472
// Create picture
7573
const picture = useMemo(() => createPicture(
76-
{ width: 100, height: 100 },
7774
(canvas) => {
7875
const paint = Skia.Paint();
7976
paint.setColor(Skia.Color("pink"));
@@ -82,7 +79,8 @@ export const PictureExample = () => {
8279
const circlePaint = Skia.Paint();
8380
circlePaint.setColor(Skia.Color("orange"));
8481
canvas.drawCircle(50, 50, 50, circlePaint);
85-
}
82+
},
83+
{ width: 100, height: 100 },
8684
), []);
8785

8886
// Serialize the picture

example/src/Examples/API/Picture.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010

1111
// Create picture
1212
const picture = createPicture(
13-
{ x: 0, y: 0, width: 100, height: 100 },
1413
(canvas) => {
1514
const paint = Skia.Paint();
1615
paint.setColor(Skia.Color("pink"));
@@ -19,7 +18,8 @@ const picture = createPicture(
1918
const circlePaint = Skia.Paint();
2019
circlePaint.setColor(Skia.Color("orange"));
2120
canvas.drawCircle(50, 50, 50, circlePaint);
22-
}
21+
},
22+
{ x: 0, y: 0, width: 100, height: 100 }
2323
);
2424

2525
export const PictureExample = () => {

example/src/Examples/API/PictureView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const PictureViewExample = () => {
1010
// Create picture
1111
const picture = useMemo(
1212
() =>
13-
createPicture({ x: 0, y: 0, width: 100, height: 100 }, (canvas) => {
13+
createPicture((canvas) => {
1414
const paint = Skia.Paint();
1515
paint.setColor(Skia.Color("pink"));
1616
canvas.drawRect({ x: 0, y: 0, width: 100, height: 100 }, paint);

example/src/Examples/API/Touch.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import { StyleSheet, View } from "react-native";
3-
import type { SkPicture, SkSize } from "@shopify/react-native-skia";
3+
import type { SkPicture } from "@shopify/react-native-skia";
44
import {
55
Group,
66
Fill,
@@ -38,11 +38,11 @@ paint.setColor(Skia.Color("cyan"));
3838
paint.setStyle(PaintStyle.Stroke);
3939
paint.setStrokeWidth(10);
4040

41-
const drawTouches = (touches: TouchData[], size: SkSize, colors: string[]) => {
41+
const drawTouches = (touches: TouchData[], colors: string[]) => {
4242
"worklet";
4343
const recorder = Skia.PictureRecorder();
4444
const canvas = recorder.beginRecording(
45-
Skia.XYWHRect(0, 0, size.width, size.height)
45+
Skia.XYWHRect(0, 0, 2_000_000, 2_000_000)
4646
);
4747
touches.forEach((touch) => {
4848
const p = paint.copy();
@@ -54,16 +54,15 @@ const drawTouches = (touches: TouchData[], size: SkSize, colors: string[]) => {
5454

5555
export const Touch = () => {
5656
const picture = useSharedValue<SkPicture>(emptyPicture);
57-
const size = useSharedValue({ width: 0, height: 0 });
5857
const colors = useSharedValue<string[]>([]);
5958
const gesture = Gesture.Native()
6059
.onTouchesDown((event) => {
6160
const start = Math.round(Math.random() * 4);
6261
colors.value = Colors.slice(start, start + event.allTouches.length);
63-
picture.value = drawTouches(event.allTouches, size.value, colors.value);
62+
picture.value = drawTouches(event.allTouches, colors.value);
6463
})
6564
.onTouchesMove((event) => {
66-
picture.value = drawTouches(event.allTouches, size.value, colors.value);
65+
picture.value = drawTouches(event.allTouches, colors.value);
6766
})
6867
.onTouchesUp(() => {
6968
picture.value = emptyPicture;
@@ -72,7 +71,7 @@ export const Touch = () => {
7271
<View style={styles.container}>
7372
<Title>Touch handling</Title>
7473
<View style={{ flex: 1 }}>
75-
<Canvas style={styles.container} onSize={size}>
74+
<Canvas style={styles.container}>
7675
<Fill color="white" />
7776
<Group style="stroke" strokeWidth={8}>
7877
<Picture picture={picture} />

package/cpp/api/JsiSkPictureRecorder.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ class JsiSkPictureRecorder
2727
context, std::make_shared<SkPictureRecorder>()) {}
2828

2929
JSI_HOST_FUNCTION(beginRecording) {
30-
auto rect = JsiSkRect::fromValue(runtime, arguments[0]);
31-
SkRTreeFactory factory;
32-
auto canvas = getObject()->beginRecording(*rect, &factory);
30+
SkCanvas *canvas;
31+
if (count > 0 && !arguments[0].isUndefined()) {
32+
auto rect = JsiSkRect::fromValue(runtime, arguments[0]);
33+
SkRTreeFactory factory;
34+
canvas = getObject()->beginRecording(*rect, &factory);
35+
} else {
36+
SkISize size = SkISize::Make(2'000'000, 2'000'000);
37+
SkRect rect = SkRect::Make(size);
38+
canvas = getObject()->beginRecording(rect, nullptr);
39+
}
3340
return jsi::Object::createFromHostObject(
3441
runtime, std::make_shared<JsiSkCanvas>(getContext(), canvas));
3542
}

package/src/renderer/Canvas.tsx

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, {
44
useMemo,
55
forwardRef,
66
useRef,
7-
useLayoutEffect,
87
} from "react";
98
import type {
109
RefObject,
@@ -13,13 +12,13 @@ import type {
1312
ForwardedRef,
1413
FunctionComponent,
1514
} from "react";
15+
import type { LayoutChangeEvent } from "react-native";
1616

17-
import { SkiaDomView, SkiaView } from "../views";
17+
import { SkiaDomView, SkiaPictureView } from "../views";
1818
import { Skia } from "../skia/Skia";
1919
import type { TouchHandler, SkiaBaseViewProps } from "../views";
2020
import type { SkiaValue } from "../values/types";
2121
import { JsiDrawingContext } from "../dom/types";
22-
import { useValue } from "../values";
2322

2423
import { SkiaRoot } from "./Reconciler";
2524
import { NATIVE_DOM } from "./HostComponents";
@@ -33,34 +32,42 @@ export interface CanvasProps extends SkiaBaseViewProps {
3332
onTouch?: TouchHandler;
3433
}
3534

36-
const useOnSizeEvent = (resultValue: SkiaBaseViewProps["onSize"]) => {
37-
const onSize = useValue({
38-
width: 0,
39-
height: 0,
40-
});
35+
const useOnSizeEvent = (
36+
resultValue: SkiaBaseViewProps["onSize"],
37+
onLayout?: (event: LayoutChangeEvent) => void
38+
) => {
39+
return useCallback(
40+
(event: LayoutChangeEvent) => {
41+
if (onLayout) {
42+
onLayout(event);
43+
}
44+
const { width, height } = event.nativeEvent.layout;
4145

42-
useLayoutEffect(() => {
43-
if (!resultValue) {
44-
return;
45-
}
46-
return onSize.addListener((newValue) => {
4746
if (isValue(resultValue)) {
48-
resultValue.current = newValue;
49-
} else {
50-
resultValue.value = newValue;
47+
resultValue.current = { width, height };
48+
} else if (resultValue) {
49+
resultValue.value = { width, height };
5150
}
52-
});
53-
}, [resultValue, onSize]);
54-
55-
return onSize;
51+
},
52+
[onLayout, resultValue]
53+
);
5654
};
5755

5856
export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
5957
(
60-
{ children, style, debug, mode, onTouch, onSize: _onSize, ...props },
58+
{
59+
children,
60+
style,
61+
debug,
62+
mode,
63+
onTouch,
64+
onSize: _onSize,
65+
onLayout: _onLayout,
66+
...props
67+
},
6168
forwardedRef
6269
) => {
63-
const onSize = useOnSizeEvent(_onSize);
70+
const onLayout = useOnSizeEvent(_onSize, _onLayout);
6471
const innerRef = useCanvasRef();
6572
const ref = useCombinedRefs(forwardedRef, innerRef);
6673
const redraw = useCallback(() => {
@@ -102,26 +109,30 @@ export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
102109
style={style}
103110
root={root.dom}
104111
onTouch={onTouch}
105-
onSize={onSize}
112+
onLayout={onLayout}
106113
mode={mode}
107114
debug={debug}
108115
{...props}
109116
/>
110117
);
111118
} else {
119+
// This is for debugging
120+
const recorder = Skia.PictureRecorder();
121+
const canvas = recorder.beginRecording(
122+
Skia.XYWHRect(0, 0, 2_000_000, 2_000_000)
123+
);
124+
const ctx = new JsiDrawingContext(Skia, canvas);
125+
root.dom.render(ctx);
126+
const picture = recorder.finishRecordingAsPicture();
112127
return (
113-
<SkiaView
128+
<SkiaPictureView
114129
// eslint-disable-next-line @typescript-eslint/no-explicit-any
115130
ref={ref as any}
116131
style={style}
117132
mode={mode}
118133
debug={debug}
119-
onSize={onSize}
120-
onDraw={(canvas, info) => {
121-
onTouch && onTouch(info.touches);
122-
const ctx = new JsiDrawingContext(Skia, canvas);
123-
root.dom.render(ctx);
124-
}}
134+
picture={picture}
135+
onLayout={onLayout}
125136
{...props}
126137
/>
127138
);

package/src/renderer/__tests__/Picture.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const CheckPicture = ({}: EmptyProps) => {
1717
}, [Skia]);
1818
const picture = useMemo(
1919
() =>
20-
createPicture(Skia.XYWHRect(0, 0, r * 2, r * 2), (canvas) => {
20+
createPicture((canvas) => {
2121
const paint = Skia.Paint();
2222
paint.setColor(Skia.Color(color));
2323
canvas.drawCircle(r, r, r, paint);
@@ -45,11 +45,11 @@ const CheckPicture2 = ({}: EmptyProps) => {
4545
}, [Skia]);
4646
const picture = useMemo(
4747
() =>
48-
createPicture(Skia.XYWHRect(0, 0, r * 2, r * 2), (canvas) => {
48+
createPicture((canvas) => {
4949
const paint = Skia.Paint();
5050
paint.setColor(Skia.Color(color));
5151
canvas.drawCircle(r, r, r, paint);
52-
}),
52+
}, Skia.XYWHRect(0, 0, r * 2, r * 2)),
5353
// eslint-disable-next-line react-hooks/exhaustive-deps
5454
[]
5555
);

package/src/renderer/__tests__/examples/Breathe.spec.tsx

Lines changed: 0 additions & 83 deletions
This file was deleted.

package/src/skia/core/Picture.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import { isRect, type SkCanvas, type SkRect, type SkSize } from "../types";
88
* @returns SkPicture
99
*/
1010
export const createPicture = (
11-
rect: SkRect | SkSize,
12-
cb: (canvas: SkCanvas) => void
11+
cb: (canvas: SkCanvas) => void,
12+
rect?: SkRect | SkSize
1313
) => {
14+
"worklet";
1415
const recorder = Skia.PictureRecorder();
15-
const canvas = recorder.beginRecording(
16-
isRect(rect) ? rect : Skia.XYWHRect(0, 0, rect.width, rect.height)
17-
);
16+
let bounds: undefined | SkRect;
17+
if (rect) {
18+
bounds = isRect(rect) ? rect : Skia.XYWHRect(0, 0, rect.width, rect.height);
19+
}
20+
const canvas = recorder.beginRecording(bounds);
1821
cb(canvas);
1922
return recorder.finishRecordingAsPicture();
2023
};

package/src/skia/types/Picture/PictureRecorder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface SkPictureRecorder {
99
*
1010
* @param bounds - a rect to cull the results.
1111
*/
12-
beginRecording(bounds: SkRect): SkCanvas;
12+
beginRecording(bounds?: SkRect): SkCanvas;
1313

1414
/**
1515
* Returns the captured draw commands as a picture and invalidates the canvas returned earlier.

0 commit comments

Comments
 (0)