diff --git a/Sources/Adwaita/Model/Data Flow/Binding.swift b/Sources/Adwaita/Model/Data Flow/Binding.swift index 925384d..5484fae 100644 --- a/Sources/Adwaita/Model/Data Flow/Binding.swift +++ b/Sources/Adwaita/Model/Data Flow/Binding.swift @@ -38,6 +38,7 @@ /// } /// ``` @propertyWrapper +@dynamicMemberLookup public struct Binding { /// The value. @@ -64,6 +65,17 @@ public struct Binding { /// The closure for settings the value. private let setValue: (Value) -> Void + /// Get a property of any content of a `Binding` as a `Binding`. + /// - Parameter dynamicMember: The path to the member. + /// - Returns: The binding. + public subscript(dynamicMember keyPath: WritableKeyPath) -> Binding { + .init { + wrappedValue[keyPath: keyPath] + } set: { newValue in + wrappedValue[keyPath: keyPath] = newValue + } + } + /// Initialize a property that is bound from a parent view. /// - Parameters: /// - get: The closure for getting the value. diff --git a/user-manual/Basics/CreatingViews.md b/user-manual/Basics/CreatingViews.md index d9fbfe7..f5f6ada 100644 --- a/user-manual/Basics/CreatingViews.md +++ b/user-manual/Basics/CreatingViews.md @@ -131,6 +131,13 @@ struct ChangeTextView: View { } ``` +If you have a more complex type and want to pass a property of the type as a binding, +you can just access the property on the binding. + +```swift +HelloView(text: $complexType.text) +``` + Whenever you modify a state property (directly or indirectly through bindings), the user interface gets automatically updated to reflect that change.