Skip to content

Commit f11683c

Browse files
committed
Expose typed action lambdas on registry props
1 parent 795ad88 commit f11683c

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Demo/swiftui/src/commonMain/kotlin/com/pureswift/swiftui/ComposableRegistry.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ class Props internal constructor(private val json: JsonObject) {
2020
fun bool(key: String): Boolean? = (json[key] as? JsonPrimitive)?.booleanOrNull
2121
/// A color passed from Swift as `PropValue.color(_:)` (an ARGB int).
2222
fun color(key: String): Color? = (json[key] as? JsonPrimitive)?.longOrNull?.let { Color(it.toInt()) }
23+
24+
// Actions: a `ComposableView(actions:)` entry arrives as a callback id; each
25+
// accessor returns a typed lambda that dispatches back to Swift, or null if
26+
// the key is absent. The factory never sees the id or the bridge.
27+
fun voidAction(key: String): (() -> Unit)? =
28+
(json[key] as? JsonPrimitive)?.longOrNull?.let { id -> { SwiftBridge.sink.invokeVoid(id) } }
29+
fun boolAction(key: String): ((Boolean) -> Unit)? =
30+
(json[key] as? JsonPrimitive)?.longOrNull?.let { id -> { v -> SwiftBridge.sink.invokeBool(id, v) } }
31+
fun doubleAction(key: String): ((Double) -> Unit)? =
32+
(json[key] as? JsonPrimitive)?.longOrNull?.let { id -> { v -> SwiftBridge.sink.invokeDouble(id, v) } }
33+
fun intAction(key: String): ((Int) -> Unit)? =
34+
(json[key] as? JsonPrimitive)?.longOrNull?.let { id -> { v -> SwiftBridge.sink.invokeInt(id, v) } }
35+
fun stringAction(key: String): ((String) -> Unit)? =
36+
(json[key] as? JsonPrimitive)?.longOrNull?.let { id -> { v -> SwiftBridge.sink.invokeString(id, v) } }
2337
}
2438

2539
/// The library's single extension point: Kotlin registers named composables;

0 commit comments

Comments
 (0)