diff --git a/Sources/Model/User Interface/View/Property.swift b/Sources/Model/User Interface/View/Property.swift index dc49a5b..ddea3ac 100644 --- a/Sources/Model/User Interface/View/Property.swift +++ b/Sources/Model/User Interface/View/Property.swift @@ -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: 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. diff --git a/Sources/Model/User Interface/View/Widget.swift b/Sources/Model/User Interface/View/Widget.swift index 31199f1..97b129c 100644 --- a/Sources/Model/User Interface/View/Widget.swift +++ b/Sources/Model/User Interface/View/Widget.swift @@ -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 "" + } + }