Skip to content

Commit e667bcf

Browse files
committed
feat: add round rect in SwiftUI Skia
1 parent d761463 commit e667bcf

File tree

10 files changed

+65
-0
lines changed

10 files changed

+65
-0
lines changed

SoftSkiaSwift/RustXcframework.xcframework/ios-arm64/Headers/soft-skia-swift.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void __swift_bridge__$SoftSkia$set_rect_attr(void* self, uintptr_t id, uint32_t
1818
void __swift_bridge__$SoftSkia$set_circle_attr(void* self, uintptr_t id, uint32_t cx, uint32_t cy, uint32_t r, void* style, void* color);
1919
void __swift_bridge__$SoftSkia$set_line_attr(void* self, uintptr_t id, struct __private__FfiSlice p1, struct __private__FfiSlice p2, struct __private__OptionU32 stroke_width, void* color);
2020
void __swift_bridge__$SoftSkia$set_points_attr(void* self, uintptr_t id, struct __private__FfiSlice points, struct __private__OptionU32 stroke_width, void* style, void* color);
21+
void __swift_bridge__$SoftSkia$set_round_rect_attr(void* self, uintptr_t id, uint32_t x, uint32_t y, uint32_t r, uint32_t width, uint32_t height, void* style, void* color);
2122
void* __swift_bridge__$SoftSkia$to_base64(void* self);
2223

2324

SoftSkiaSwift/RustXcframework.xcframework/ios-arm64_x86_64-simulator/Headers/soft-skia-swift.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void __swift_bridge__$SoftSkia$set_rect_attr(void* self, uintptr_t id, uint32_t
1818
void __swift_bridge__$SoftSkia$set_circle_attr(void* self, uintptr_t id, uint32_t cx, uint32_t cy, uint32_t r, void* style, void* color);
1919
void __swift_bridge__$SoftSkia$set_line_attr(void* self, uintptr_t id, struct __private__FfiSlice p1, struct __private__FfiSlice p2, struct __private__OptionU32 stroke_width, void* color);
2020
void __swift_bridge__$SoftSkia$set_points_attr(void* self, uintptr_t id, struct __private__FfiSlice points, struct __private__OptionU32 stroke_width, void* style, void* color);
21+
void __swift_bridge__$SoftSkia$set_round_rect_attr(void* self, uintptr_t id, uint32_t x, uint32_t y, uint32_t r, uint32_t width, uint32_t height, void* style, void* color);
2122
void* __swift_bridge__$SoftSkia$to_base64(void* self);
2223

2324

SoftSkiaSwift/RustXcframework.xcframework/macos-arm64_x86_64/Headers/soft-skia-swift.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void __swift_bridge__$SoftSkia$set_rect_attr(void* self, uintptr_t id, uint32_t
1818
void __swift_bridge__$SoftSkia$set_circle_attr(void* self, uintptr_t id, uint32_t cx, uint32_t cy, uint32_t r, void* style, void* color);
1919
void __swift_bridge__$SoftSkia$set_line_attr(void* self, uintptr_t id, struct __private__FfiSlice p1, struct __private__FfiSlice p2, struct __private__OptionU32 stroke_width, void* color);
2020
void __swift_bridge__$SoftSkia$set_points_attr(void* self, uintptr_t id, struct __private__FfiSlice points, struct __private__OptionU32 stroke_width, void* style, void* color);
21+
void __swift_bridge__$SoftSkia$set_round_rect_attr(void* self, uintptr_t id, uint32_t x, uint32_t y, uint32_t r, uint32_t width, uint32_t height, void* style, void* color);
2122
void* __swift_bridge__$SoftSkia$to_base64(void* self);
2223

2324

SoftSkiaSwift/Sources/SoftSkiaSwift/soft-skia-swift.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ extension SoftSkiaRefMut {
4444
__swift_bridge__$SoftSkia$set_points_attr(ptr, id, points.toFfiSlice(), stroke_width.intoFfiRepr(), { let rustString = style.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), { let rustString = color.intoRustString(); rustString.isOwned = false; return rustString.ptr }())
4545
}
4646

47+
public func set_round_rect_attr<GenericIntoRustString: IntoRustString>(_ id: UInt, _ x: UInt32, _ y: UInt32, _ r: UInt32, _ width: UInt32, _ height: UInt32, _ style: GenericIntoRustString, _ color: GenericIntoRustString) {
48+
__swift_bridge__$SoftSkia$set_round_rect_attr(ptr, id, x, y, r, width, height, { let rustString = style.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), { let rustString = color.intoRustString(); rustString.isOwned = false; return rustString.ptr }())
49+
}
50+
4751
public func to_base64() -> RustString {
4852
RustString(ptr: __swift_bridge__$SoftSkia$to_base64(ptr))
4953
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import SwiftUI
2+
3+
@available(macOS 10.15, *)
4+
public struct RoundRect<Content: View>: View {
5+
var x: UInt32
6+
var y: UInt32
7+
var r: UInt32
8+
var width: UInt32
9+
var height: UInt32
10+
var style: String
11+
var color: String
12+
let content: Content
13+
@EnvironmentObject var ffi: FFI;
14+
15+
public init(x: UInt32, y: UInt32, r: UInt32, width: UInt32, height: UInt32, style: String, color: String, @ViewBuilder builder: () -> Content) {
16+
self.x = x
17+
self.y = y
18+
self.r = r
19+
self.width = width
20+
self.height = height
21+
self.style = style
22+
self.color = color
23+
self.content = builder()
24+
}
25+
26+
public var body: some View {
27+
ZStack {
28+
content
29+
}.onAppear{
30+
let id = AutoIncrementID.id();
31+
self.ffi.soft.create(UInt(id))
32+
self.ffi.soft.set_round_rect_attr(UInt(id), self.x, self.y, self.r, self.width, self.height, self.style, self.color)
33+
self.ffi.willChange();
34+
}
35+
}
36+
}

examples/first-example/first-example/ContentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct ContentView: View {
1717
SwiftUISkia.Circle(cx: 205, cy: 45, r: 30, style: "stroke", color: "purple") {}
1818
SwiftUISkia.Line(p1: [133, 44], p2: [160, 188], strokeWidth: 4, color: "black") {}
1919
SwiftUISkia.Points(points: [ [300, 200], [305, 200], [310, 210], [290, 310]], strokeWidth: 2, style: "stroke", color: "green") {}
20+
SwiftUISkia.RoundRect(x: 40, y: 300, r: 4, width: 60, height: 80, style: "fill", color: "orange") {}
2021
}
2122
}
2223
}

generated/soft-skia-swift/soft-skia-swift.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void __swift_bridge__$SoftSkia$set_rect_attr(void* self, uintptr_t id, uint32_t
1818
void __swift_bridge__$SoftSkia$set_circle_attr(void* self, uintptr_t id, uint32_t cx, uint32_t cy, uint32_t r, void* style, void* color);
1919
void __swift_bridge__$SoftSkia$set_line_attr(void* self, uintptr_t id, struct __private__FfiSlice p1, struct __private__FfiSlice p2, struct __private__OptionU32 stroke_width, void* color);
2020
void __swift_bridge__$SoftSkia$set_points_attr(void* self, uintptr_t id, struct __private__FfiSlice points, struct __private__OptionU32 stroke_width, void* style, void* color);
21+
void __swift_bridge__$SoftSkia$set_round_rect_attr(void* self, uintptr_t id, uint32_t x, uint32_t y, uint32_t r, uint32_t width, uint32_t height, void* style, void* color);
2122
void* __swift_bridge__$SoftSkia$to_base64(void* self);
2223

2324

generated/soft-skia-swift/soft-skia-swift.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ extension SoftSkiaRefMut {
4343
__swift_bridge__$SoftSkia$set_points_attr(ptr, id, points.toFfiSlice(), stroke_width.intoFfiRepr(), { let rustString = style.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), { let rustString = color.intoRustString(); rustString.isOwned = false; return rustString.ptr }())
4444
}
4545

46+
public func set_round_rect_attr<GenericIntoRustString: IntoRustString>(_ id: UInt, _ x: UInt32, _ y: UInt32, _ r: UInt32, _ width: UInt32, _ height: UInt32, _ style: GenericIntoRustString, _ color: GenericIntoRustString) {
47+
__swift_bridge__$SoftSkia$set_round_rect_attr(ptr, id, x, y, r, width, height, { let rustString = style.intoRustString(); rustString.isOwned = false; return rustString.ptr }(), { let rustString = color.intoRustString(); rustString.isOwned = false; return rustString.ptr }())
48+
}
49+
4650
public func to_base64() -> RustString {
4751
RustString(ptr: __swift_bridge__$SoftSkia$to_base64(ptr))
4852
}

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use soft_skia::shape::Rect;
77
use soft_skia::shape::Circle;
88
use soft_skia::shape::Line;
99
use soft_skia::shape::Points;
10+
use soft_skia::shape::RoundRect;
1011
use soft_skia::shape::Shapes;
1112
use soft_skia::shape::Pixmap;
1213
use soft_skia::shape::ColorU8;
@@ -27,6 +28,7 @@ mod ffi {
2728
fn set_circle_attr(&mut self, id: usize, cx: u32, cy: u32, r: u32, style: String, color: String);
2829
fn set_line_attr(&mut self, id: usize, p1: &[u32], p2: &[u32], stroke_width: Option<u32>, color: String);
2930
fn set_points_attr(&mut self, id: usize, points: &[u32], stroke_width: Option<u32>, style: String, color: String);
31+
fn set_round_rect_attr(&mut self, id: usize, x: u32, y: u32, r: u32, width: u32, height: u32, style: String, color: String);
3032

3133
fn to_base64(&mut self) -> String;
3234
}
@@ -109,6 +111,20 @@ impl SoftSkia {
109111
}))
110112
}
111113

114+
pub fn set_round_rect_attr(&mut self, id: usize, x: u32, y: u32, r: u32, width: u32, height: u32, style: String, color: String) {
115+
let style = parse_style(Some(style));
116+
let color = parse_color(Some(color));
117+
self.instance.set_shape_to_child(id, Shapes::RR(RoundRect {
118+
x,
119+
y,
120+
r,
121+
width,
122+
height,
123+
style,
124+
color,
125+
}))
126+
}
127+
112128
pub fn to_base64(&mut self) -> String {
113129
to_base64(&mut self.instance.tree)
114130
}

0 commit comments

Comments
 (0)