david-swift 443b942c46
All checks were successful
Deploy Docs / publish (push) Successful in 49s
SwiftLint / SwiftLint (push) Successful in 3s
Explicitly set view context in view properties
2024-10-14 16:09:53 +02:00

55 lines
1.6 KiB
Swift

//
// 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, ViewContext>: ViewPropertyProtocol where ViewContext: ViewRenderData {
/// 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).
/// - context: The view render data type.
public init(
set setView: @escaping (Pointer, ViewPointer) -> Void,
pointer: Pointer.Type,
subview: ViewPointer.Type,
context: ViewContext.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 view render data type.
associatedtype ViewContext: ViewRenderData
/// The wrapped value.
var wrappedValue: Body { get }
/// Set the view.
var setView: (Pointer, ViewPointer) -> Void { get }
}