Skip to content

Commit 1a2505e

Browse files
committed
Refine deprecations
1 parent 0e19c18 commit 1a2505e

File tree

8 files changed

+43
-36
lines changed

8 files changed

+43
-36
lines changed

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: 1 addition & 1 deletion
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,

package/cpp/api/JsiSkPictureRecorder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class JsiSkPictureRecorder
2828

2929
JSI_HOST_FUNCTION(beginRecording) {
3030
SkCanvas *canvas;
31-
if (count > 0) {
31+
if (count > 0 && !arguments[0].isUndefined()) {
3232
auto rect = JsiSkRect::fromValue(runtime, arguments[0]);
3333
SkRTreeFactory factory;
3434
canvas = getObject()->beginRecording(*rect, &factory);

package/src/renderer/Canvas.tsx

Lines changed: 30 additions & 23 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

1717
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,7 +109,7 @@ 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}
@@ -125,7 +132,7 @@ export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
125132
mode={mode}
126133
debug={debug}
127134
picture={picture}
128-
onSize={onSize}
135+
onLayout={onLayout}
129136
{...props}
130137
/>
131138
);

package/src/skia/core/Picture.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ export const createPicture = (
1111
cb: (canvas: SkCanvas) => void,
1212
rect?: SkRect | SkSize
1313
) => {
14+
"worklet";
1415
const recorder = Skia.PictureRecorder();
15-
const canvas = recorder.beginRecording(
16-
// eslint-disable-next-line no-nested-ternary
17-
rect
18-
? isRect(rect)
19-
? rect
20-
: Skia.XYWHRect(0, 0, rect.width, rect.height)
21-
: undefined
22-
);
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);
2321
cb(canvas);
2422
return recorder.finishRecordingAsPicture();
2523
};

package/src/skia/types/Rect.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface SkRect {
66
}
77

88
export const isRect = (def: unknown): def is SkRect => {
9+
"worklet";
910
if (typeof def === "object" && def !== null) {
1011
const rect = def as SkRect;
1112
return (

package/src/values/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const ValueApi = {
2626
* for animating Skia: https://shopify.github.io/react-native-skia/docs/animations/animations
2727
*/
2828
createValue<T>(initialValue: T): SkiaMutableValue<T> {
29+
deprecatedWarning();
2930
return SkiaValueApi.createValue(initialValue);
3031
},
3132
/**

0 commit comments

Comments
 (0)