Skip to content

Commit 444cc72

Browse files
committed
Use enum identity on RN Web
1 parent 7dd903c commit 444cc72

15 files changed

+90
-69
lines changed

package/src/skia/web/Host.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ export abstract class HostObject<T, N extends string> extends BaseHostObject<
4141
}
4242
}
4343

44-
export const getCkEnum = (e: EmbindEnum, v: number): EmbindEnumEntity =>
44+
export const getEnum = (e: EmbindEnum, v: number): EmbindEnumEntity =>
4545
Object.values(e).find(({ value }) => value === v);
46-
export const ckEnum = (value: number): EmbindEnumEntity => ({ value });
46+
4747
export const optEnum = (
48+
e: EmbindEnum,
4849
value: number | undefined
4950
): EmbindEnumEntity | undefined =>
50-
value === undefined ? undefined : { value };
51+
value === undefined ? undefined : getEnum(e, value);

package/src/skia/web/JsiSkCanvas.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {
2525
SkVertices,
2626
} from "../types";
2727

28-
import { ckEnum, getCkEnum, HostObject } from "./Host";
28+
import { getEnum, HostObject } from "./Host";
2929
import { JsiSkPaint } from "./JsiSkPaint";
3030
import { JsiSkRect } from "./JsiSkRect";
3131
import { JsiSkRRect } from "./JsiSkRRect";
@@ -113,8 +113,8 @@ export class JsiSkCanvas
113113
JsiSkImage.fromValue(img),
114114
left,
115115
top,
116-
ckEnum(fm),
117-
ckEnum(mm),
116+
getEnum(this.CanvasKit.FilterMode, fm),
117+
getEnum(this.CanvasKit.MipmapMode, mm),
118118
paint ? JsiSkPaint.fromValue(paint) : paint
119119
);
120120
}
@@ -130,7 +130,7 @@ export class JsiSkCanvas
130130
JsiSkImage.fromValue(img),
131131
Array.from(JsiSkRect.fromValue(this.CanvasKit, center)),
132132
JsiSkRect.fromValue(this.CanvasKit, dest),
133-
ckEnum(filter),
133+
getEnum(this.CanvasKit.FilterMode, filter),
134134
paint ? JsiSkPaint.fromValue(paint) : paint
135135
);
136136
}
@@ -165,8 +165,8 @@ export class JsiSkCanvas
165165
JsiSkImage.fromValue(img),
166166
JsiSkRect.fromValue(this.CanvasKit, src),
167167
JsiSkRect.fromValue(this.CanvasKit, dest),
168-
ckEnum(fm),
169-
ckEnum(mm),
168+
getEnum(this.CanvasKit.FilterMode, fm),
169+
getEnum(this.CanvasKit.MipmapMode, mm),
170170
paint ? JsiSkPaint.fromValue(paint) : paint
171171
);
172172
}
@@ -186,7 +186,7 @@ export class JsiSkCanvas
186186
drawVertices(verts: SkVertices, mode: BlendMode, paint: SkPaint) {
187187
this.ref.drawVertices(
188188
JsiSkVertices.fromValue(verts),
189-
ckEnum(mode),
189+
getEnum(this.CanvasKit.BlendMode, mode),
190190
JsiSkPaint.fromValue(paint)
191191
);
192192
}
@@ -202,7 +202,7 @@ export class JsiSkCanvas
202202
cubics.map(({ x, y }) => [x, y]).flat(),
203203
colors,
204204
texs ? texs.flatMap((p) => Array.from(JsiSkPoint.fromValue(p))) : texs,
205-
mode ? ckEnum(mode) : null,
205+
mode ? getEnum(this.CanvasKit.BlendMode, mode) : null,
206206
paint ? JsiSkPaint.fromValue(paint) : undefined
207207
);
208208
}
@@ -213,7 +213,7 @@ export class JsiSkCanvas
213213

214214
drawPoints(mode: PointMode, points: SkPoint[], paint: SkPaint) {
215215
this.ref.drawPoints(
216-
ckEnum(mode),
216+
getEnum(this.CanvasKit.PointMode, mode),
217217
points.map(({ x, y }) => [x, y]).flat(),
218218
JsiSkPaint.fromValue(paint)
219219
);
@@ -341,29 +341,36 @@ export class JsiSkCanvas
341341
}
342342

343343
drawColor(color: SkColor, blendMode?: BlendMode) {
344-
this.ref.drawColor(color, blendMode ? ckEnum(blendMode) : undefined);
344+
this.ref.drawColor(
345+
color,
346+
blendMode ? getEnum(this.CanvasKit.BlendMode, blendMode) : undefined
347+
);
345348
}
346349

347350
clear(color: SkColor) {
348351
this.ref.clear(color);
349352
}
350353

351354
clipPath(path: SkPath, op: ClipOp, doAntiAlias: boolean) {
352-
this.ref.clipPath(JsiSkPath.fromValue(path), ckEnum(op), doAntiAlias);
355+
this.ref.clipPath(
356+
JsiSkPath.fromValue(path),
357+
getEnum(this.CanvasKit.PathOp, op),
358+
doAntiAlias
359+
);
353360
}
354361

355362
clipRect(rect: SkRect, op: ClipOp, doAntiAlias: boolean) {
356363
this.ref.clipRect(
357364
JsiSkRect.fromValue(this.CanvasKit, rect),
358-
ckEnum(op),
365+
getEnum(this.CanvasKit.PathOp, op),
359366
doAntiAlias
360367
);
361368
}
362369

363370
clipRRect(rrect: SkRRect, op: ClipOp, doAntiAlias: boolean) {
364371
this.ref.clipRRect(
365372
JsiSkRRect.fromValue(this.CanvasKit, rrect),
366-
ckEnum(op),
373+
getEnum(this.CanvasKit.PathOp, op),
367374
doAntiAlias
368375
);
369376
}
@@ -381,8 +388,8 @@ export class JsiSkCanvas
381388
width: imageInfo.width,
382389
height: imageInfo.height,
383390
colorSpace: this.CanvasKit.ColorSpace.SRGB,
384-
alphaType: getCkEnum(this.CanvasKit.AlphaType, imageInfo.alphaType),
385-
colorType: getCkEnum(this.CanvasKit.ColorType, imageInfo.colorType),
391+
alphaType: getEnum(this.CanvasKit.AlphaType, imageInfo.alphaType),
392+
colorType: getEnum(this.CanvasKit.ColorType, imageInfo.colorType),
386393
};
387394
return this.ref.readPixels(srcX, srcY, pxInfo);
388395
}

package/src/skia/web/JsiSkColorFilterFactory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
BlendMode,
99
} from "../types";
1010

11-
import { ckEnum, Host } from "./Host";
11+
import { getEnum, Host } from "./Host";
1212
import { JsiSkColorFilter } from "./JsiSkColorFilter";
1313

1414
export class JsiSkColorFilterFactory
@@ -29,7 +29,10 @@ export class JsiSkColorFilterFactory
2929
MakeBlend(color: SkColor, mode: BlendMode) {
3030
return new JsiSkColorFilter(
3131
this.CanvasKit,
32-
this.CanvasKit.ColorFilter.MakeBlend(color, ckEnum(mode))
32+
this.CanvasKit.ColorFilter.MakeBlend(
33+
color,
34+
getEnum(this.CanvasKit.BlendMode, mode)
35+
)
3336
);
3437
}
3538

package/src/skia/web/JsiSkFont.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
SkTypeface,
1111
} from "../types";
1212

13-
import { HostObject, NotImplementedOnRNWeb, ckEnum } from "./Host";
13+
import { HostObject, NotImplementedOnRNWeb, getEnum } from "./Host";
1414
import { JsiSkPaint } from "./JsiSkPaint";
1515
import { JsiSkPoint } from "./JsiSkPoint";
1616
import { JsiSkRect } from "./JsiSkRect";
@@ -100,15 +100,15 @@ export class JsiSkFont extends HostObject<Font, "Font"> implements SkFont {
100100
}
101101

102102
setEdging(edging: FontEdging) {
103-
this.ref.setEdging(ckEnum(edging));
103+
this.ref.setEdging(getEnum(this.CanvasKit.FontEdging, edging));
104104
}
105105

106106
setEmbeddedBitmaps(embeddedBitmaps: boolean) {
107107
this.ref.setEmbeddedBitmaps(embeddedBitmaps);
108108
}
109109

110110
setHinting(hinting: FontHinting) {
111-
this.ref.setHinting(ckEnum(hinting));
111+
this.ref.setHinting(getEnum(this.CanvasKit.FontHinting, hinting));
112112
}
113113

114114
setLinearMetrics(linearMetrics: boolean) {

package/src/skia/web/JsiSkImage.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
ImageInfo,
1616
} from "../types";
1717

18-
import { ckEnum, getCkEnum, HostObject } from "./Host";
18+
import { getEnum, HostObject } from "./Host";
1919
import { JsiSkMatrix } from "./JsiSkMatrix";
2020
import { JsiSkShader } from "./JsiSkShader";
2121

@@ -77,10 +77,10 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
7777
return new JsiSkShader(
7878
this.CanvasKit,
7979
this.ref.makeShaderOptions(
80-
ckEnum(tx),
81-
ckEnum(ty),
82-
ckEnum(fm),
83-
ckEnum(mm),
80+
getEnum(this.CanvasKit.TileMode, tx),
81+
getEnum(this.CanvasKit.TileMode, ty),
82+
getEnum(this.CanvasKit.FilterMode, fm),
83+
getEnum(this.CanvasKit.MipmapMode, mm),
8484
localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined
8585
)
8686
);
@@ -96,8 +96,8 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
9696
return new JsiSkShader(
9797
this.CanvasKit,
9898
this.ref.makeShaderCubic(
99-
ckEnum(tx),
100-
ckEnum(ty),
99+
getEnum(this.CanvasKit.TileMode, tx),
100+
getEnum(this.CanvasKit.TileMode, ty),
101101
B,
102102
C,
103103
localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined
@@ -108,9 +108,12 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
108108
encodeToBytes(fmt?: ImageFormat, quality?: number) {
109109
let result: Uint8Array | null;
110110
if (fmt && quality) {
111-
result = this.ref.encodeToBytes(ckEnum(fmt), quality);
111+
result = this.ref.encodeToBytes(
112+
getEnum(this.CanvasKit.ImageFormat, fmt),
113+
quality
114+
);
112115
} else if (fmt) {
113-
result = this.ref.encodeToBytes(ckEnum(fmt));
116+
result = this.ref.encodeToBytes(getEnum(this.CanvasKit.ImageFormat, fmt));
114117
} else {
115118
result = this.ref.encodeToBytes();
116119
}
@@ -131,11 +134,11 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
131134
colorSpace: this.CanvasKit.ColorSpace.SRGB,
132135
width: imageInfo?.width ?? info.width,
133136
height: imageInfo?.height ?? info.height,
134-
alphaType: getCkEnum(
137+
alphaType: getEnum(
135138
this.CanvasKit.AlphaType,
136139
(imageInfo ?? info).alphaType
137140
),
138-
colorType: getCkEnum(
141+
colorType: getEnum(
139142
this.CanvasKit.ColorType,
140143
(imageInfo ?? info).colorType
141144
),

package/src/skia/web/JsiSkImageFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CanvasKit } from "canvaskit-wasm";
33
import type { SkData, ImageInfo, SkImage } from "../types";
44
import type { ImageFactory } from "../types/Image/ImageFactory";
55

6-
import { Host, ckEnum } from "./Host";
6+
import { Host, getEnum } from "./Host";
77
import { JsiSkImage } from "./JsiSkImage";
88
import { JsiSkData } from "./JsiSkData";
99

@@ -33,9 +33,9 @@ export class JsiSkImageFactory extends Host implements ImageFactory {
3333
// see toSkImageInfo() from canvaskit
3434
const image = this.CanvasKit.MakeImage(
3535
{
36-
alphaType: ckEnum(info.alphaType),
36+
alphaType: getEnum(this.CanvasKit.AlphaType, info.alphaType),
3737
colorSpace: this.CanvasKit.ColorSpace.SRGB,
38-
colorType: ckEnum(info.colorType),
38+
colorType: getEnum(this.CanvasKit.ColorType, info.colorType),
3939
height: info.height,
4040
width: info.width,
4141
},

package/src/skia/web/JsiSkImageFilterFactory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
TileMode,
1414
} from "../types";
1515

16-
import { Host, NotImplementedOnRNWeb, ckEnum } from "./Host";
16+
import { Host, NotImplementedOnRNWeb, getEnum } from "./Host";
1717
import { JsiSkImageFilter } from "./JsiSkImageFilter";
1818
import { JsiSkColorFilter } from "./JsiSkColorFilter";
1919

@@ -42,8 +42,8 @@ export class JsiSkImageFilterFactory
4242
const inputFilter =
4343
input === null ? null : JsiSkImageFilter.fromValue<ImageFilter>(input);
4444
const filter = this.CanvasKit.ImageFilter.MakeDisplacementMap(
45-
ckEnum(channelX),
46-
ckEnum(channelY),
45+
getEnum(this.CanvasKit.ColorChannel, channelX),
46+
getEnum(this.CanvasKit.ColorChannel, channelY),
4747
scale,
4848
JsiSkImageFilter.fromValue(in1),
4949
inputFilter
@@ -69,7 +69,7 @@ export class JsiSkImageFilterFactory
6969
this.CanvasKit.ImageFilter.MakeBlur(
7070
sigmaX,
7171
sigmaY,
72-
ckEnum(mode),
72+
getEnum(this.CanvasKit.TileMode, mode),
7373
input === null ? null : JsiSkImageFilter.fromValue(input)
7474
)
7575
);
@@ -199,7 +199,7 @@ export class JsiSkImageFilterFactory
199199
);
200200
}
201201
const filter = this.CanvasKit.ImageFilter.MakeBlend(
202-
ckEnum(mode),
202+
getEnum(this.CanvasKit.BlendMode, mode),
203203
JsiSkImageFilter.fromValue(background),
204204
inputFilter
205205
);

package/src/skia/web/JsiSkMaskFilterFactory.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CanvasKit } from "canvaskit-wasm";
33
import type { BlurStyle } from "../types";
44
import type { MaskFilterFactory } from "../types/MaskFilter";
55

6-
import { Host, ckEnum } from "./Host";
6+
import { Host, getEnum } from "./Host";
77
import { JsiSkMaskFilter } from "./JsiSkMaskFilter";
88

99
export class JsiSkMaskFilterFactory extends Host implements MaskFilterFactory {
@@ -14,7 +14,11 @@ export class JsiSkMaskFilterFactory extends Host implements MaskFilterFactory {
1414
MakeBlur(style: BlurStyle, sigma: number, respectCTM: boolean) {
1515
return new JsiSkMaskFilter(
1616
this.CanvasKit,
17-
this.CanvasKit.MaskFilter.MakeBlur(ckEnum(style), sigma, respectCTM)
17+
this.CanvasKit.MaskFilter.MakeBlur(
18+
getEnum(this.CanvasKit.BlurStyle, style),
19+
sigma,
20+
respectCTM
21+
)
1822
);
1923
}
2024
}

package/src/skia/web/JsiSkPaint.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
SkPathEffect,
1515
} from "../types";
1616

17-
import { HostObject, ckEnum } from "./Host";
17+
import { HostObject, getEnum } from "./Host";
1818
import { JsiSkColorFilter } from "./JsiSkColorFilter";
1919
import { JsiSkImageFilter } from "./JsiSkImageFilter";
2020
import { JsiSkMaskFilter } from "./JsiSkMaskFilter";
@@ -75,7 +75,7 @@ export class JsiSkPaint extends HostObject<Paint, "Paint"> implements SkPaint {
7575
}
7676

7777
setBlendMode(blendMode: BlendMode) {
78-
this.ref.setBlendMode(ckEnum(blendMode));
78+
this.ref.setBlendMode(getEnum(this.CanvasKit.BlendMode, blendMode));
7979
}
8080

8181
setColor(color: SkColor) {
@@ -103,11 +103,11 @@ export class JsiSkPaint extends HostObject<Paint, "Paint"> implements SkPaint {
103103
}
104104

105105
setStrokeCap(cap: StrokeCap) {
106-
this.ref.setStrokeCap(ckEnum(cap));
106+
this.ref.setStrokeCap(getEnum(this.CanvasKit.StrokeCap, cap));
107107
}
108108

109109
setStrokeJoin(join: StrokeJoin) {
110-
this.ref.setStrokeJoin(ckEnum(join));
110+
this.ref.setStrokeJoin(getEnum(this.CanvasKit.StrokeJoin, join));
111111
}
112112

113113
setStrokeMiter(limit: number) {

package/src/skia/web/JsiSkPath.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
StrokeOpts,
1414
} from "../types";
1515

16-
import { ckEnum, HostObject, optEnum } from "./Host";
16+
import { getEnum, HostObject, optEnum } from "./Host";
1717
import { JsiSkPoint } from "./JsiSkPoint";
1818
import { JsiSkRect } from "./JsiSkRect";
1919
import { JsiSkRRect } from "./JsiSkRRect";
@@ -149,7 +149,7 @@ export class JsiSkPath extends HostObject<Path, "Path"> implements SkPath {
149149
}
150150

151151
setFillType(fill: FillType) {
152-
this.ref.setFillType(ckEnum(fill));
152+
this.ref.setFillType(getEnum(this.CanvasKit.FillType, fill));
153153
return this;
154154
}
155155

@@ -167,8 +167,8 @@ export class JsiSkPath extends HostObject<Path, "Path"> implements SkPath {
167167
// eslint-disable-next-line camelcase
168168
miter_limit: opts.width,
169169
precision: opts.width,
170-
join: optEnum(opts.join),
171-
cap: optEnum(opts.cap),
170+
join: optEnum(this.CanvasKit.StrokeJoin, opts.join),
171+
cap: optEnum(this.CanvasKit.StrokeCap, opts.cap),
172172
}
173173
);
174174
return result === null ? result : this;
@@ -315,7 +315,10 @@ export class JsiSkPath extends HostObject<Path, "Path"> implements SkPath {
315315
}
316316

317317
op(path: SkPath, op: PathOp) {
318-
return this.ref.op(JsiSkPath.fromValue(path), ckEnum(op));
318+
return this.ref.op(
319+
JsiSkPath.fromValue(path),
320+
getEnum(this.CanvasKit.PathOp, op)
321+
);
319322
}
320323

321324
simplify() {

0 commit comments

Comments
 (0)