Add default container implementation for widgets

This commit is contained in:
david-swift 2024-09-16 07:15:56 +02:00
parent 6ade5ffa0d
commit 17718471e9
2 changed files with 29 additions and 0 deletions

View File

@ -176,6 +176,21 @@ public enum UpdateStrategy {
extension Widget {
/// The view storage.
/// - Parameters:
/// - data: Modify views before being updated.
/// - type: The view render data type.
/// - Returns: The view storage.
public func container<Data>(
data: WidgetData,
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let storage = ViewStorage(initializeWidget())
initProperties(storage, data: data, type: type)
update(storage, data: data, updateProperties: true, type: type)
return storage
}
/// Update the stored content.
/// - Parameters:
/// - storage: The storage to update.

View File

@ -34,6 +34,13 @@ public protocol Widget: AnyView {
type: Data.Type
) where Data: ViewRenderData
/// Get the widget.
/// - Returns: The widget.
///
/// Define this function only if you do not define ``Widget/container(data:type:)``.
/// Otherwise, it will not have an effect.
func initializeWidget() -> Any
}
/// Extend the widget type.
@ -42,4 +49,11 @@ extension Widget {
/// A widget's view is empty.
public var viewContent: Body { [] }
/// Print a warning if the widget does not set this function but it gets accessed.
/// - Returns: A dummy pointer.
public func initializeWidget() -> Any {
print("Warning: Define initialize widget function or container function for \(Self.self)")
return ""
}
}