diff --git a/Sources/Model/Extensions/Array.swift b/Sources/Model/Extensions/Array.swift index 6b970ff..146e0ae 100644 --- a/Sources/Model/Extensions/Array.swift +++ b/Sources/Model/Extensions/Array.swift @@ -76,7 +76,9 @@ extension Array: AnyView where Element == AnyView { modifiers: [(AnyView) -> AnyView], type: WidgetType.Type ) -> [ViewStorage] { - compactMap { $0.renderable(type: type, modifiers: modifiers) ? $0.storage(modifiers: [], type: type) : nil } + compactMap { view in + view.renderable(type: type, modifiers: modifiers) ? view.storage(modifiers: modifiers, type: type) : nil + } } } diff --git a/Sources/Model/User Interface/View/AnyView.swift b/Sources/Model/User Interface/View/AnyView.swift index 4372946..3212a44 100644 --- a/Sources/Model/User Interface/View/AnyView.swift +++ b/Sources/Model/User Interface/View/AnyView.swift @@ -83,6 +83,9 @@ extension AnyView { if let peer = modified as? Widget { return peer } + if let array = modified as? Body { + return Wrapper { array } + } return Wrapper { viewContent.map { $0.getModified(modifiers: modifiers) } } } diff --git a/Sources/View/StateWrapper.swift b/Sources/View/StateWrapper.swift index 3ab7300..726b18a 100644 --- a/Sources/View/StateWrapper.swift +++ b/Sources/View/StateWrapper.swift @@ -11,7 +11,7 @@ import Observation public struct StateWrapper: ConvenienceWidget { /// The content. - var content: () -> Body + var content: Body /// The state information (from properties with the `State` wrapper). var state: [String: StateProtocol] = [:] @@ -24,7 +24,7 @@ public struct StateWrapper: ConvenienceWidget { /// The debug tree's content. public var debugTreeContent: [(String, body: Body)] { - [("content", body: content())] + [("content", body: content)] } /// The identifier of the field storing whether to update the wrapper's content. @@ -33,7 +33,7 @@ public struct StateWrapper: ConvenienceWidget { /// Initialize a `StateWrapper`. /// - Parameter content: The view content. public init(@ViewBuilder content: @escaping () -> Body) { - self.content = content + self.content = content() } /// Initialize a `StateWrapper`. @@ -41,7 +41,7 @@ public struct StateWrapper: ConvenienceWidget { /// - content: The view content. /// - state: The state information. init(content: @escaping () -> Body, state: [String: StateProtocol]) { - self.content = content + self.content = content() self.state = state } @@ -68,11 +68,10 @@ public struct StateWrapper: ConvenienceWidget { property.value.content.storage.update = false } } - if let storage = storage.content[.mainContent]?.first { - content() - .widget(modifiers: modifiers) - .update(storage, modifiers: modifiers, updateProperties: updateProperties, type: type) + guard let storages = storage.content[.mainContent] else { + return } + content.update(storages, modifiers: modifiers, updateProperties: updateProperties, type: type) } /// Get a view storage. @@ -81,8 +80,8 @@ public struct StateWrapper: ConvenienceWidget { /// - type: The type of the widgets. /// - Returns: The view storage. public func container(modifiers: [(AnyView) -> AnyView], type: WidgetType.Type) -> ViewStorage { - let content = content().storage(modifiers: modifiers, type: type) - let storage = ViewStorage(content.pointer, content: [.mainContent: [content]]) + let content = content.storages(modifiers: modifiers, type: type) + let storage = ViewStorage(nil, content: [.mainContent: content]) storage.state = state observe(storage: storage) return storage @@ -92,7 +91,7 @@ public struct StateWrapper: ConvenienceWidget { /// - Parameter storage: The view storage func observe(storage: ViewStorage) { withObservationTracking { - _ = content().getDebugTree(parameters: true, type: AnyView.self) + _ = content.getDebugTree(parameters: true, type: AnyView.self) } onChange: { storage.fields[updateID] = true UpdateManager.updateViews() diff --git a/Tests/DemoApp/DemoApp.swift b/Tests/DemoApp/DemoApp.swift index fbac523..b0c3798 100644 --- a/Tests/DemoApp/DemoApp.swift +++ b/Tests/DemoApp/DemoApp.swift @@ -4,9 +4,13 @@ import SampleBackends struct DemoView: SimpleView { var view: Body { - Backend1.TestWidget1() - TestView() - testContent + Wrapper { + Backend1.TestWidget1() + Backend1.Button("") { + } + TestView() + testContent + } } @ViewBuilder @@ -20,7 +24,7 @@ struct DemoView: SimpleView { struct TestView: SimpleView { var view: Body { - [] + Backend2.TestWidget4() } } diff --git a/Tests/SampleBackends/Backend1.swift b/Tests/SampleBackends/Backend1.swift index 8219045..1e1d5b3 100644 --- a/Tests/SampleBackends/Backend1.swift +++ b/Tests/SampleBackends/Backend1.swift @@ -54,6 +54,28 @@ public enum Backend1 { } + public struct Button: BackendWidget { + + public init(_ label: String, action: @escaping () -> Void) { + } + + public var debugTreeContent: [(String, body: Body)] { + [] + } + + public var debugTreeParameters: [(String, value: any CustomStringConvertible)] { + [] + } + + public func container(modifiers: [(any AnyView) -> any AnyView], type: WidgetType.Type) -> ViewStorage { + .init(nil) + } + + public func update(_ storage: ViewStorage, modifiers: [(any AnyView) -> any AnyView], updateProperties: Bool, type: WidgetType.Type) { + } + + } + public protocol BackendWidget: Widget { } }