From ff704e427a6e509fdd16a4a1b9c27b658153dd00 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 3 Jun 2024 02:20:15 +0800 Subject: [PATCH] Update VariadicView --- .../Core/View/ConditionalContent.swift | 4 +- .../Core/View/VariadicView/VariadicView.swift | 49 +++++++++++ .../VariadicView/VariadicView_Children.swift | 6 ++ .../VariadicView_ImplicitRoot.swift | 24 ++++++ .../View/VariadicView/VariadicView_Root.swift | 26 ++++++ .../VariadicView/VariadicView_ViewRoot.swift | 82 +++++++++++++++++++ .../View/ViewList/ViewListCountInputs.swift | 6 +- Sources/OpenSwiftUI/Core/View/ViewRoot.swift | 6 -- .../Stack/{HStack.swift => HVStack.swift} | 0 .../Stack/HVStackLayout.swift | 38 +++++++++ .../Stack/_HStackLayout.swift | 16 ---- .../OpenSwiftUI/View/View/_VariadicView.swift | 34 -------- 12 files changed, 230 insertions(+), 61 deletions(-) create mode 100644 Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView.swift create mode 100644 Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Children.swift create mode 100644 Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_ImplicitRoot.swift create mode 100644 Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Root.swift create mode 100644 Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_ViewRoot.swift rename Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/{HStack.swift => HVStack.swift} (100%) create mode 100644 Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/HVStackLayout.swift delete mode 100644 Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/_HStackLayout.swift delete mode 100644 Sources/OpenSwiftUI/View/View/_VariadicView.swift diff --git a/Sources/OpenSwiftUI/Core/View/ConditionalContent.swift b/Sources/OpenSwiftUI/Core/View/ConditionalContent.swift index b7d1d733..480e0aa0 100644 --- a/Sources/OpenSwiftUI/Core/View/ConditionalContent.swift +++ b/Sources/OpenSwiftUI/Core/View/ConditionalContent.swift @@ -30,9 +30,9 @@ extension _ConditionalContent: View, PrimitiveView where TrueContent: View, Fals public static func _makeView(view: _GraphValue, inputs: _ViewInputs) -> _ViewOutputs { if _SemanticFeature_v2.isEnable { - return makeImplicitRoot(view: view, inputs: inputs) + makeImplicitRoot(view: view, inputs: inputs) } else { - return AnyView._makeView( + AnyView._makeView( view: _GraphValue(ChildView(content: view.value)), inputs: inputs ) diff --git a/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView.swift b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView.swift new file mode 100644 index 00000000..16df934a --- /dev/null +++ b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView.swift @@ -0,0 +1,49 @@ +/// A type of structured content that is passed as an argument to a +/// `Root`'s result builder, creating a `Tree` that conditionally conforms +/// to protocols like `View`. +/// +/// For example, `View`s can be passed to a `Layout` result builder +/// creating a `View`: +/// +/// HStack { +/// Image(name: "envelope") +/// Text("Your time away request has been approved") +/// Spacer() +/// Text(timestamp, format: .dateTime).layoutPriority(1) +/// } +/// +public enum _VariadicView { + public typealias Root = _VariadicView_Root + public typealias ViewRoot = _VariadicView_ViewRoot + public typealias Children = _VariadicView_Children +// public typealias UnaryViewRoot = _VariadicView_UnaryViewRoot +// public typealias MultiViewRoot = _VariadicView_MultiViewRoot + + @frozen + public struct Tree { + public var root: Root + public var content: Content + @inlinable + init(root: Root, content: Content) { + self.root = root + self.content = content + } + + @inlinable public init(_ root: Root, @ViewBuilder content: () -> Content) { + self.root = root + self.content = content() + } + } +} + +extension _VariadicView_ViewRoot { + func bodyError() -> Never { + fatalError("body() should not be called on \(Self.self)") + } +} + +extension _VariadicView_ViewRoot where Body == Never { + public func body(children: _VariadicView.Children) -> Never { + bodyError() + } +} diff --git a/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Children.swift b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Children.swift new file mode 100644 index 00000000..b4609c77 --- /dev/null +++ b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Children.swift @@ -0,0 +1,6 @@ +// TODO + +/// An ad hoc collection of the children of a variadic view. +public struct _VariadicView_Children { + +} diff --git a/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_ImplicitRoot.swift b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_ImplicitRoot.swift new file mode 100644 index 00000000..d59e920b --- /dev/null +++ b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_ImplicitRoot.swift @@ -0,0 +1,24 @@ +// +// VariadicView_ImplicitRoot.swift +// OpenSwiftUI +// +// Audited for RELEASE_2021 +// Status: Complete + +protocol _VariadicView_AnyImplicitRoot { + func visitType(visitor: inout Visitor) +} + +protocol _VariadicView_ImplicitRootVisitor { + func visit(type: Root.Type) +} + +protocol _VariadicView_ImplicitRoot: _VariadicView_AnyImplicitRoot, _VariadicView_ViewRoot { + static var implicitRoot: Self { get } +} + +extension _VariadicView_ImplicitRoot { + func visitType(visitor: inout Visitor) { + visitor.visit(type: Self.self) + } +} diff --git a/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Root.swift b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Root.swift new file mode 100644 index 00000000..d4851377 --- /dev/null +++ b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Root.swift @@ -0,0 +1,26 @@ +// +// VariadicView_ImplicitRoot.swift +// OpenSwiftUI +// +// Audited for RELEASE_2021 +// Status: Complete + +/// A type that creates a `Tree`, managing content subtrees passed to a result builder. +/// +/// - SeeAlso: _VariadicView.Root. +public protocol _VariadicView_Root { + static var _viewListOptions: Int { get } +} + +extension _VariadicView_Root { + public static var _viewListOptions: Int { + 0 + } + + public static func _viewListCount( + inputs _: _ViewListCountInputs, + body _: (_ViewListCountInputs) -> Int? + ) -> Int? { + nil + } +} diff --git a/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_ViewRoot.swift b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_ViewRoot.swift new file mode 100644 index 00000000..ba04afea --- /dev/null +++ b/Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_ViewRoot.swift @@ -0,0 +1,82 @@ +// +// VariadicView_ImplicitRoot.swift +// OpenSwiftUI +// +// Audited for RELEASE_2021 +// Status: WIP + +internal import OpenGraphShims + +/// A type of root that creates a `View` when its result builder is invoked with +/// `View`. +/// - SeeAlso: _VariadicView.ViewRoot. +/// - Note: Requirements mirror `View`'s. +public protocol _VariadicView_ViewRoot: _VariadicView_Root { + associatedtype Body: View + + static func _makeView( + root: _GraphValue, + inputs: _ViewInputs, + body: (_Graph, _ViewInputs) -> _ViewListOutputs + ) -> _ViewOutputs + + static func _makeViewList( + root: _GraphValue, + inputs: _ViewListInputs, + body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs + ) -> _ViewListOutputs + + static func _viewListCount( + inputs: _ViewListCountInputs, + body: (_ViewListCountInputs) -> Int? + ) -> Int? + + @ViewBuilder + func body(children: _VariadicView.Children) -> Body +} + +extension _VariadicView_ViewRoot { + public static func _makeView( + root: _GraphValue, + inputs: _ViewInputs, + body: (_Graph, _ViewInputs) -> _ViewListOutputs + ) -> _ViewOutputs { + fatalError("TODO") + } + + public static func _makeViewList( + root: _GraphValue, + inputs: _ViewListInputs, + body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs + ) -> _ViewListOutputs { + fatalError("TODO") + } + + public static func _viewListCount(inputs: _ViewListCountInputs) -> Int? { + Body._viewListCount(inputs: inputs) + } +} + + +// MARK: - ViewRootBody + +private struct ViewRootBody { + @Attribute var root: Root + @Attribute var list: ViewList + let contentSubgraph: OGSubgraph +} + +extension _ViewInputs { + private struct ImplicitRootType: ViewInput { + static let defaultValue: _VariadicView_AnyImplicitRoot.Type = _VStackLayout.self + } +} + +extension View { + static func makeImplicitRoot(view: _GraphValue, inputs: _ViewInputs) -> _ViewOutputs { + // TODO + return .init() + } +} + +// TODO: ViewModifier diff --git a/Sources/OpenSwiftUI/Core/View/ViewList/ViewListCountInputs.swift b/Sources/OpenSwiftUI/Core/View/ViewList/ViewListCountInputs.swift index 7e41438e..10f64ea2 100644 --- a/Sources/OpenSwiftUI/Core/View/ViewList/ViewListCountInputs.swift +++ b/Sources/OpenSwiftUI/Core/View/ViewList/ViewListCountInputs.swift @@ -7,9 +7,9 @@ /// Input values to `View._viewListCount()`. public struct _ViewListCountInputs { - var customInputs : PropertyList - var options : _ViewListInputs.Options - var baseOptions : _GraphInputs.Options + var customInputs: PropertyList + var options: _ViewListInputs.Options + var baseOptions: _GraphInputs.Options subscript(_ type: Input.Type) -> Input.Value { get { customInputs[type] } diff --git a/Sources/OpenSwiftUI/Core/View/ViewRoot.swift b/Sources/OpenSwiftUI/Core/View/ViewRoot.swift index 21140d02..f6fc1bf6 100644 --- a/Sources/OpenSwiftUI/Core/View/ViewRoot.swift +++ b/Sources/OpenSwiftUI/Core/View/ViewRoot.swift @@ -6,9 +6,3 @@ // Status: WIP // ID: 00F12C0E37A19C593ECA0DBD3BE26541 -extension View { - static func makeImplicitRoot(view: _GraphValue, inputs: _ViewInputs) -> _ViewOutputs { - // TODO - return .init() - } -} diff --git a/Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/HStack.swift b/Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/HVStack.swift similarity index 100% rename from Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/HStack.swift rename to Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/HVStack.swift diff --git a/Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/HVStackLayout.swift b/Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/HVStackLayout.swift new file mode 100644 index 00000000..7a657dd6 --- /dev/null +++ b/Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/HVStackLayout.swift @@ -0,0 +1,38 @@ +import Foundation + +@frozen +public struct _HStackLayout { + public var alignment: VerticalAlignment + public var spacing: CGFloat? + + @inlinable + public init(alignment: VerticalAlignment = .center, spacing: CGFloat? = nil) { + self.alignment = alignment + self.spacing = spacing + } + + public typealias AnimatableData = EmptyAnimatableData + public typealias Body = Swift.Never +} + +@frozen +public struct _VStackLayout { + public var alignment: HorizontalAlignment + public var spacing: CGFloat? + + @inlinable + public init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil) { + self.alignment = alignment + self.spacing = spacing + } + + public typealias AnimatableData = EmptyAnimatableData + public typealias Body = Swift.Never +} + +extension _HStackLayout: _VariadicView_ImplicitRoot { + static var implicitRoot: _HStackLayout { _HStackLayout() } +} +extension _VStackLayout: _VariadicView_ImplicitRoot { + static var implicitRoot: _VStackLayout { _VStackLayout() } +} diff --git a/Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/_HStackLayout.swift b/Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/_HStackLayout.swift deleted file mode 100644 index e3a736a0..00000000 --- a/Sources/OpenSwiftUI/Layout/LayoutFundamentals/Stack/_HStackLayout.swift +++ /dev/null @@ -1,16 +0,0 @@ -import Foundation - -@frozen -public struct _HStackLayout { - public var alignment: VerticalAlignment - public var spacing: CGFloat? - - @inlinable - public init(alignment: VerticalAlignment = .center, spacing: CGFloat? = nil) { - self.alignment = alignment - self.spacing = spacing - } - - public typealias AnimatableData = EmptyAnimatableData - public typealias Body = Swift.Never -} diff --git a/Sources/OpenSwiftUI/View/View/_VariadicView.swift b/Sources/OpenSwiftUI/View/View/_VariadicView.swift deleted file mode 100644 index c19ae688..00000000 --- a/Sources/OpenSwiftUI/View/View/_VariadicView.swift +++ /dev/null @@ -1,34 +0,0 @@ -public enum _VariadicView { - @frozen - public struct Tree { - public var root: Root - public var content: Content - @inlinable - init(root: Root, content: Content) { - self.root = root - self.content = content - } - - @inlinable public init(_ root: Root, @ViewBuilder content: () -> Content) { - self.root = root - self.content = content() - } - } -} - -public protocol _VariadicView_Root { - static var _viewListOptions: Int { get } -} - -// FIXME -extension _VariadicView_Root { - public static var _viewListOptions: Int { - 0 - } -} - -protocol _VariadicView_ViewRoot: _VariadicView_Root, View { - associatedtype Body -} - -extension _HStackLayout: _VariadicView_Root {}