Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Add default container implementation for widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Sep 16, 2024
1 parent 6ade5ff commit 6ba302f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/Model/User Interface/View/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
Expand Down
13 changes: 13 additions & 0 deletions Sources/Model/User Interface/View/Widget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -42,4 +49,10 @@ 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 {
""
}

}

0 comments on commit 6ba302f

Please sign in to comment.