macbackend/Sources/Core/View/VStack.swift
david-swift 3d2ef52cc8
Some checks failed
Deploy Docs / publish (push) Successful in 3m4s
SwiftLint / SwiftLint (push) Failing after 9s
Extract core target
2024-12-04 21:17:21 +01:00

72 lines
2.2 KiB
Swift

//
// VStack.swift
// MacBackend
//
// Created by david-swift on 23.08.23.
//
import SwiftUI
/// A `VStack` view.
public struct VStack: SwiftUIWidget, Wrapper {
/// The content view.
var content: Body
/// The wrapped views.
public var wrappedViews: [String: Meta.AnyView] {
content.enumerated().reduce(into: [:]) { partialResult, element in
partialResult["\(element.offset)"] = element.element
}
}
/// Initialize the ``VStack``.
/// - Parameter content: The content.
public init(@Meta.ViewBuilder content: @escaping () -> Body) {
self.content = content()
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
SwiftUI.VStack {
ForEach(properties.content.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}
}
/// 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 {
if content.count == 1, let storage = content.first?.storage(data: data, type: type) {
return storage
}
return internalContainer(data: data, type: type)
}
/// Update the stored content.
/// - Parameters:
/// - storage: The storage to update.
/// - data: Modify views before being updated
/// - updateProperties: Whether to update the view's properties.
/// - type: The view render data type.
public func update<Data>(
_ storage: ViewStorage,
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
if content.count == 1, let first = content.first {
first.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
} else {
internalUpdate(storage, data: data, updateProperties: updateProperties, type: type)
}
}
}