Skip to content

Commit 9752f62

Browse files
committed
Add images to SwiftUI.
1 parent de5cd69 commit 9752f62

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

packages/backend/src/swiftui/builderImpl/swiftuiColor.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ export const swiftUISolidColor = (fill: Paint): string => {
1111
return "";
1212
};
1313

14-
/**
15-
* Retrieve the SOLID color for SwiftUI when existent, otherwise ""
16-
*/
17-
export const swiftuiColorFromFills = (
14+
export const swiftuiSolidColor = (
1815
fills: ReadonlyArray<Paint> | PluginAPI["mixed"]
1916
): string => {
2017
const fill = retrieveTopFill(fills);
@@ -24,9 +21,8 @@ export const swiftuiColorFromFills = (
2421
const opacity = fill.opacity ?? 1.0;
2522
return swiftuiColor(fill.color, opacity);
2623
} else if (fill?.type === "GRADIENT_LINEAR") {
27-
return swiftuiGradient(fill);
24+
return swiftuiRGBAColor(fill.gradientStops[0].color);
2825
} else if (fill?.type === "IMAGE") {
29-
// placeholder for the image. Apparently SwiftUI doesn't support Image.network(...).
3026
return swiftuiColor(
3127
{
3228
r: 0.5,
@@ -40,6 +36,27 @@ export const swiftuiColorFromFills = (
4036
return "";
4137
};
4238

39+
export const swiftuiBackground = (
40+
node: SceneNode,
41+
fills: ReadonlyArray<Paint> | PluginAPI["mixed"]
42+
): string => {
43+
const fill = retrieveTopFill(fills);
44+
45+
if (fill && fill.type === "SOLID") {
46+
// opacity should only be null on set, not on get. But better be prevented.
47+
const opacity = fill.opacity ?? 1.0;
48+
return swiftuiColor(fill.color, opacity);
49+
} else if (fill?.type === "GRADIENT_LINEAR") {
50+
return swiftuiGradient(fill);
51+
} else if (fill?.type === "IMAGE") {
52+
return `AsyncImage(url: URL(string: "https://via.placeholder.com/${node.width.toFixed(
53+
0
54+
)}x${node.height.toFixed(0)}"))`;
55+
}
56+
57+
return "";
58+
};
59+
4360
export const swiftuiGradient = (fill: GradientPaint): string => {
4461
const direction = gradientDirection(gradientAngle(fill));
4562

@@ -74,6 +91,8 @@ const gradientDirection = (angle: number): string => {
7491
}
7592
};
7693

94+
export const swiftuiRGBAColor = (color: RGBA) => swiftuiColor(color, color.a);
95+
7796
export const swiftuiColor = (color: RGB, opacity: number): string => {
7897
// Using Color.black.opacity() is not reccomended, as per:
7998
// https://stackoverflow.com/a/56824114/4418073

packages/backend/src/swiftui/swiftuiDefaultBuilder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
swiftuiBorder,
55
swiftuiCornerRadius,
66
} from "./builderImpl/swiftuiBorder";
7-
import { swiftuiColorFromFills } from "./builderImpl/swiftuiColor";
7+
import { swiftuiBackground } from "./builderImpl/swiftuiColor";
88
import { swiftuiPadding } from "./builderImpl/swiftuiPadding";
99
import { swiftuiSize } from "./builderImpl/swiftuiSize";
1010

@@ -104,9 +104,9 @@ export class SwiftuiDefaultBuilder {
104104

105105
shapeBackground(node: SceneNode): this {
106106
if ("fills" in node) {
107-
const fillColor = swiftuiColorFromFills(node.fills);
108-
if (fillColor) {
109-
this.pushModifier([`background`, fillColor]);
107+
const background = swiftuiBackground(node, node.fills);
108+
if (background) {
109+
this.pushModifier([`background`, background]);
110110
}
111111
}
112112
return this;

packages/backend/src/swiftui/swiftuiTextBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { swiftuiSize } from "./builderImpl/swiftuiSize";
99
import { globalTextStyleSegments } from "../altNodes/altConversion";
1010
import { SwiftUIElement } from "./builderImpl/swiftuiParser";
1111
import { parseTextAsCode } from "../flutter/flutterTextBuilder";
12-
import { swiftuiColorFromFills } from "./builderImpl/swiftuiColor";
12+
import { swiftuiSolidColor } from "./builderImpl/swiftuiColor";
1313

1414
export class SwiftuiTextBuilder extends SwiftuiDefaultBuilder {
1515
modifiers: string[] = [];
@@ -41,7 +41,7 @@ export class SwiftuiTextBuilder extends SwiftuiDefaultBuilder {
4141
}
4242

4343
textColor(fills: Paint[]): string {
44-
const fillColor = swiftuiColorFromFills(fills);
44+
const fillColor = swiftuiSolidColor(fills);
4545
if (fillColor) {
4646
return fillColor;
4747
}

0 commit comments

Comments
 (0)