Skip to content

Commit

Permalink
Refine deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon committed Dec 12, 2023
1 parent 0e19c18 commit 1a2505e
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 36 deletions.
4 changes: 2 additions & 2 deletions example/src/Examples/API/Picture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {

// Create picture
const picture = createPicture(
{ x: 0, y: 0, width: 100, height: 100 },
(canvas) => {
const paint = Skia.Paint();
paint.setColor(Skia.Color("pink"));
Expand All @@ -19,7 +18,8 @@ const picture = createPicture(
const circlePaint = Skia.Paint();
circlePaint.setColor(Skia.Color("orange"));
canvas.drawCircle(50, 50, 50, circlePaint);
}
},
{ x: 0, y: 0, width: 100, height: 100 }
);

export const PictureExample = () => {
Expand Down
2 changes: 1 addition & 1 deletion example/src/Examples/API/PictureView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const PictureViewExample = () => {
// Create picture
const picture = useMemo(
() =>
createPicture({ x: 0, y: 0, width: 100, height: 100 }, (canvas) => {
createPicture((canvas) => {
const paint = Skia.Paint();
paint.setColor(Skia.Color("pink"));
canvas.drawRect({ x: 0, y: 0, width: 100, height: 100 }, paint);
Expand Down
2 changes: 1 addition & 1 deletion example/src/Examples/API/Touch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { StyleSheet, View } from "react-native";
import type { SkPicture, SkSize } from "@shopify/react-native-skia";
import type { SkPicture } from "@shopify/react-native-skia";
import {
Group,
Fill,
Expand Down
2 changes: 1 addition & 1 deletion package/cpp/api/JsiSkPictureRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class JsiSkPictureRecorder

JSI_HOST_FUNCTION(beginRecording) {
SkCanvas *canvas;
if (count > 0) {
if (count > 0 && !arguments[0].isUndefined()) {
auto rect = JsiSkRect::fromValue(runtime, arguments[0]);
SkRTreeFactory factory;
canvas = getObject()->beginRecording(*rect, &factory);
Expand Down
53 changes: 30 additions & 23 deletions package/src/renderer/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React, {
useMemo,
forwardRef,
useRef,
useLayoutEffect,
} from "react";
import type {
RefObject,
Expand All @@ -13,13 +12,13 @@ import type {
ForwardedRef,
FunctionComponent,
} from "react";
import type { LayoutChangeEvent } from "react-native";

import { SkiaDomView, SkiaPictureView } from "../views";
import { Skia } from "../skia/Skia";
import type { TouchHandler, SkiaBaseViewProps } from "../views";
import type { SkiaValue } from "../values/types";
import { JsiDrawingContext } from "../dom/types";
import { useValue } from "../values";

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

const useOnSizeEvent = (resultValue: SkiaBaseViewProps["onSize"]) => {
const onSize = useValue({
width: 0,
height: 0,
});
const useOnSizeEvent = (
resultValue: SkiaBaseViewProps["onSize"],
onLayout?: (event: LayoutChangeEvent) => void
) => {
return useCallback(
(event: LayoutChangeEvent) => {
if (onLayout) {
onLayout(event);
}
const { width, height } = event.nativeEvent.layout;

useLayoutEffect(() => {
if (!resultValue) {
return;
}
return onSize.addListener((newValue) => {
if (isValue(resultValue)) {
resultValue.current = newValue;
} else {
resultValue.value = newValue;
resultValue.current = { width, height };
} else if (resultValue) {
resultValue.value = { width, height };
}
});
}, [resultValue, onSize]);

return onSize;
},
[onLayout, resultValue]
);
};

export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
(
{ children, style, debug, mode, onTouch, onSize: _onSize, ...props },
{
children,
style,
debug,
mode,
onTouch,
onSize: _onSize,
onLayout: _onLayout,
...props
},
forwardedRef
) => {
const onSize = useOnSizeEvent(_onSize);
const onLayout = useOnSizeEvent(_onSize, _onLayout);
const innerRef = useCanvasRef();
const ref = useCombinedRefs(forwardedRef, innerRef);
const redraw = useCallback(() => {
Expand Down Expand Up @@ -102,7 +109,7 @@ export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
style={style}
root={root.dom}
onTouch={onTouch}
onSize={onSize}
onLayout={onLayout}
mode={mode}
debug={debug}
{...props}
Expand All @@ -125,7 +132,7 @@ export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
mode={mode}
debug={debug}
picture={picture}
onSize={onSize}
onLayout={onLayout}
{...props}
/>
);
Expand Down
14 changes: 6 additions & 8 deletions package/src/skia/core/Picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ export const createPicture = (
cb: (canvas: SkCanvas) => void,
rect?: SkRect | SkSize
) => {
"worklet";
const recorder = Skia.PictureRecorder();
const canvas = recorder.beginRecording(
// eslint-disable-next-line no-nested-ternary
rect
? isRect(rect)
? rect
: Skia.XYWHRect(0, 0, rect.width, rect.height)
: undefined
);
let bounds: undefined | SkRect;
if (rect) {
bounds = isRect(rect) ? rect : Skia.XYWHRect(0, 0, rect.width, rect.height);
}
const canvas = recorder.beginRecording(bounds);
cb(canvas);
return recorder.finishRecordingAsPicture();
};
1 change: 1 addition & 0 deletions package/src/skia/types/Rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface SkRect {
}

export const isRect = (def: unknown): def is SkRect => {
"worklet";
if (typeof def === "object" && def !== null) {
const rect = def as SkRect;
return (
Expand Down
1 change: 1 addition & 0 deletions package/src/values/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const ValueApi = {
* for animating Skia: https://shopify.github.io/react-native-skia/docs/animations/animations
*/
createValue<T>(initialValue: T): SkiaMutableValue<T> {
deprecatedWarning();
return SkiaValueApi.createValue(initialValue);
},
/**
Expand Down

0 comments on commit 1a2505e

Please sign in to comment.