This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1771847
commit 6289e51
Showing
3 changed files
with
158 additions
and
44 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Sources/Model/User Interface/View/Properties/BindingProperty.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// Property.swift | ||
// Meta | ||
// | ||
// Created by david-swift on 16.09.24. | ||
// | ||
|
||
/// Assign an observing and updating closure to a widget's binding property. | ||
/// | ||
/// This will be used if you do not provide a custom ``Widget/update(_:data:updateProperties:type:)`` method | ||
/// or call the ``Widget/updateProperties(_:updateProperties:)`` method in your custom update method. | ||
@propertyWrapper | ||
public struct BindingProperty<Value, Pointer>: BindingPropertyProtocol { | ||
|
||
/// The wrapped binding. | ||
public var wrappedValue: Binding<Value> | ||
/// Observe the UI element. | ||
var observe: (Pointer, Binding<Value>, ViewStorage) -> Void | ||
/// Set the UI element's property. | ||
var setValue: (Pointer, Value, ViewStorage) -> Void | ||
|
||
/// Initialize a property. | ||
/// - Parameters: | ||
/// - wrappedValue: The wrapped value. | ||
/// - getProperty: Get the property from the UI. | ||
/// - setProperty: The function applying the property to the UI. | ||
/// - pointer: The type of the pointer. | ||
public init( | ||
wrappedValue: Binding<Value>, | ||
observe: @escaping (Pointer, Binding<Value>, ViewStorage) -> Void, | ||
set setValue: @escaping (Pointer, Value, ViewStorage) -> Void, | ||
pointer: Pointer.Type | ||
) { | ||
self.wrappedValue = wrappedValue | ||
self.observe = observe | ||
self.setValue = setValue | ||
} | ||
|
||
/// Initialize a property. | ||
/// - Parameters: | ||
/// - wrappedValue: The wrapped value. | ||
/// - getProperty: Get the property from the UI. | ||
/// - setProperty: The function applying the property to the UI. | ||
/// - pointer: The type of the pointer. | ||
public init( | ||
wrappedValue: Binding<Value>, | ||
observe: @escaping (Pointer, Binding<Value>) -> Void, | ||
set setValue: @escaping (Pointer, Value) -> Void, | ||
pointer: Pointer.Type | ||
) { | ||
self.init( | ||
wrappedValue: wrappedValue, | ||
observe: { pointer, value, _ in observe(pointer, value) }, | ||
set: { pointer, value, _ in setValue(pointer, value) }, | ||
pointer: pointer | ||
) | ||
} | ||
|
||
} | ||
|
||
/// The binding property protocol. | ||
protocol BindingPropertyProtocol { | ||
|
||
/// The binding's wrapped value. | ||
associatedtype Value | ||
/// The storage's pointer. | ||
associatedtype Pointer | ||
|
||
/// The wrapped value. | ||
var wrappedValue: Binding<Value> { get } | ||
/// Observe the property. | ||
var observe: (Pointer, Binding<Value>, ViewStorage) -> Void { get } | ||
/// Set the property. | ||
var setValue: (Pointer, Value, ViewStorage) -> Void { get } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Sources/Model/User Interface/View/Properties/ViewProperty.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// Property.swift | ||
// Meta | ||
// | ||
// Created by david-swift on 16.09.24. | ||
// | ||
|
||
/// Assign an updating closure to a widget's property. | ||
/// | ||
/// This will be used if you do not provide a custom ``Widget/update(_:data:updateProperties:type:)`` method | ||
/// or call the ``Widget/updateProperties(_:updateProperties:)`` method in your custom update method. | ||
@propertyWrapper | ||
public struct ViewProperty<Pointer, ViewPointer>: ViewPropertyProtocol { | ||
|
||
/// The wrapped value. | ||
public var wrappedValue: Body = [] | ||
/// Set the view. | ||
var setView: (Pointer, ViewPointer) -> Void | ||
|
||
/// Initialize a property. | ||
/// - Parameters: | ||
/// - setView: Set the view. | ||
/// - pointer: The pointer type of the parent view (usually a concrete view type). | ||
/// - subview: The pointer type of the child view (usually a protocol, view class, or similar). | ||
public init( | ||
set setView: @escaping (Pointer, ViewPointer) -> Void, | ||
pointer: Pointer.Type, | ||
subview: ViewPointer.Type | ||
) { | ||
self.setView = setView | ||
} | ||
|
||
} | ||
|
||
/// The view property protocol. | ||
/// | ||
/// Do not use for wrapper widgets. | ||
protocol ViewPropertyProtocol { | ||
|
||
/// The type of the view's pointer. | ||
associatedtype Pointer | ||
/// The type of the view's content. | ||
associatedtype ViewPointer | ||
|
||
/// The wrapped value. | ||
var wrappedValue: Body { get } | ||
/// Set the view. | ||
var setView: (Pointer, ViewPointer) -> Void { get } | ||
|
||
} |