Skip to content

Commit 36c266c

Browse files
authored
Update VariadicView (#95)
* Update VariadicView * Update HVStackLayout documentation * Update VariadicView
1 parent e5b5ce4 commit 36c266c

17 files changed

+407
-66
lines changed

Sources/OpenSwiftUI/Core/View/ConditionalContent.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ extension _ConditionalContent: View, PrimitiveView where TrueContent: View, Fals
3030

3131
public static func _makeView(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
3232
if _SemanticFeature_v2.isEnable {
33-
return makeImplicitRoot(view: view, inputs: inputs)
33+
makeImplicitRoot(view: view, inputs: inputs)
3434
} else {
35-
return AnyView._makeView(
35+
AnyView._makeView(
3636
view: _GraphValue(ChildView(content: view.value)),
3737
inputs: inputs
3838
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// ForEach.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
8+
/// A structure that computes views on demand from an underlying collection of
9+
/// identified data.
10+
///
11+
/// Use `ForEach` to provide views based on a
12+
/// [RandomAccessCollection](https://developer.apple.com/documentation/swift/randomaccesscollection)
13+
/// of some data type. Either the collection's elements must conform to
14+
/// [Identifiable](https://developer.apple.com/documentation/swift/identifiable) or you
15+
/// need to provide an `id` parameter to the `ForEach` initializer.
16+
///
17+
/// The following example creates a `NamedFont` type that conforms to
18+
/// [Identifiable](https://developer.apple.com/documentation/swift/identifiable), and an
19+
/// array of this type called `namedFonts`. A `ForEach` instance iterates
20+
/// over the array, producing new ``Text`` instances that display examples
21+
/// of each OpenSwiftUI ``Font`` style provided in the array.
22+
///
23+
/// private struct NamedFont: Identifiable {
24+
/// let name: String
25+
/// let font: Font
26+
/// var id: String { name }
27+
/// }
28+
///
29+
/// private let namedFonts: [NamedFont] = [
30+
/// NamedFont(name: "Large Title", font: .largeTitle),
31+
/// NamedFont(name: "Title", font: .title),
32+
/// NamedFont(name: "Headline", font: .headline),
33+
/// NamedFont(name: "Body", font: .body),
34+
/// NamedFont(name: "Caption", font: .caption)
35+
/// ]
36+
///
37+
/// var body: some View {
38+
/// ForEach(namedFonts) { namedFont in
39+
/// Text(namedFont.name)
40+
/// .font(namedFont.font)
41+
/// }
42+
/// }
43+
///
44+
/// ![A vertically arranged stack of labels showing various standard fonts,
45+
/// such as Large Title and Headline.](OpenSwiftUI-ForEach-fonts.png)
46+
public struct ForEach<Data, ID, Content> where Data: RandomAccessCollection, ID: Hashable {
47+
48+
/// The collection of underlying identified data that OpenSwiftUI uses to create
49+
/// views dynamically.
50+
public var data: Data
51+
52+
/// A function to create content on demand using the underlying data.
53+
public var content: (Data.Element) -> Content
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// A type of structured content that is passed as an argument to a
2+
/// `Root`'s result builder, creating a `Tree` that conditionally conforms
3+
/// to protocols like `View`.
4+
///
5+
/// For example, `View`s can be passed to a `Layout` result builder
6+
/// creating a `View`:
7+
///
8+
/// HStack {
9+
/// Image(name: "envelope")
10+
/// Text("Your time away request has been approved")
11+
/// Spacer()
12+
/// Text(timestamp, format: .dateTime).layoutPriority(1)
13+
/// }
14+
///
15+
public enum _VariadicView {
16+
public typealias Root = _VariadicView_Root
17+
public typealias ViewRoot = _VariadicView_ViewRoot
18+
public typealias Children = _VariadicView_Children
19+
// public typealias UnaryViewRoot = _VariadicView_UnaryViewRoot
20+
// public typealias MultiViewRoot = _VariadicView_MultiViewRoot
21+
22+
@frozen
23+
public struct Tree<Root: _VariadicView_Root, Content> {
24+
public var root: Root
25+
public var content: Content
26+
@inlinable
27+
init(root: Root, content: Content) {
28+
self.root = root
29+
self.content = content
30+
}
31+
32+
@inlinable public init(_ root: Root, @ViewBuilder content: () -> Content) {
33+
self.root = root
34+
self.content = content()
35+
}
36+
}
37+
}
38+
39+
extension _VariadicView_ViewRoot {
40+
func bodyError() -> Never {
41+
fatalError("body() should not be called on \(Self.self)")
42+
}
43+
}
44+
45+
extension _VariadicView_ViewRoot where Body == Never {
46+
public func body(children: _VariadicView.Children) -> Never {
47+
bodyError()
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// VariadicView_Children.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: TODO
7+
// ID: 52A2FFECFBCF37BFFEED558E33EBD1E3
8+
9+
internal import OpenGraphShims
10+
11+
/// An ad hoc collection of the children of a variadic view.
12+
public struct _VariadicView_Children {
13+
var list: ViewList
14+
var contentSubgraph: OGSubgraph
15+
}
16+
17+
extension _VariadicView_Children: RandomAccessCollection {
18+
public struct Element: PrimitiveView, UnaryView, Identifiable {
19+
20+
// var view: _ViewList_View
21+
var traits: ViewTraitCollection
22+
23+
public var id: AnyHashable {
24+
// view.viewID
25+
fatalError("TODO")
26+
27+
}
28+
public func id<ID>(as _: ID.Type = ID.self) -> ID? where ID : Hashable {
29+
fatalError("TODO")
30+
}
31+
32+
public subscript<Trait: _ViewTraitKey>(key: Trait.Type) -> Trait.Value {
33+
get { traits[key] }
34+
set { traits[key] = newValue }
35+
}
36+
37+
public static func _makeView(view: _GraphValue<_VariadicView_Children.Element>, inputs: _ViewInputs) -> _ViewOutputs {
38+
fatalError("TODO")
39+
}
40+
}
41+
42+
public var startIndex: Int {
43+
fatalError("TODO")
44+
45+
// get
46+
}
47+
public var endIndex: Int {
48+
fatalError("TODO")
49+
50+
// get
51+
}
52+
public subscript(index: Int) -> _VariadicView_Children.Element {
53+
fatalError("TODO")
54+
55+
// get
56+
}
57+
}
58+
59+
extension _VariadicView_Children {
60+
private struct Child: Rule, AsyncAttribute {
61+
typealias Value = ForEach<_VariadicView_Children, AnyHashable, _VariadicView_Children.Element>
62+
63+
@Attribute var children: _VariadicView_Children
64+
65+
var value: Value {
66+
fatalError("TODO")
67+
}
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// VariadicView_ImplicitRoot.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: Complete
7+
8+
package protocol _VariadicView_AnyImplicitRoot {
9+
static func visitType<V>(visitor: inout V) where V: _VariadicView_ImplicitRootVisitor
10+
}
11+
12+
package protocol _VariadicView_ImplicitRootVisitor {
13+
mutating func visit<R>(type: R.Type) where R: _VariadicView_ImplicitRoot
14+
}
15+
16+
package protocol _VariadicView_ImplicitRoot: _VariadicView_AnyImplicitRoot, _VariadicView_ViewRoot {
17+
static var implicitRoot: Self { get }
18+
}
19+
20+
extension _VariadicView_ImplicitRoot {
21+
package func visitType<Visitor: _VariadicView_ImplicitRootVisitor>(visitor: inout Visitor) {
22+
visitor.visit(type: Self.self)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// VariadicView_ImplicitRoot.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: Complete
7+
8+
/// A type that creates a `Tree`, managing content subtrees passed to a result builder.
9+
///
10+
/// - SeeAlso: _VariadicView.Root.
11+
public protocol _VariadicView_Root {
12+
static var _viewListOptions: Int { get }
13+
}
14+
15+
extension _VariadicView_Root {
16+
public static var _viewListOptions: Int {
17+
0
18+
}
19+
20+
package static var viewListOptions: _ViewListInputs.Options {
21+
.init(rawValue: _viewListOptions)
22+
}
23+
24+
public static func _viewListCount(
25+
inputs _: _ViewListCountInputs,
26+
body _: (_ViewListCountInputs) -> Int?
27+
) -> Int? {
28+
nil
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// VariadicView_ImplicitRoot.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
// ID: 00F12C0E37A19C593ECA0DBD3BE26541
8+
9+
internal import OpenGraphShims
10+
11+
/// A type of root that creates a `View` when its result builder is invoked with
12+
/// `View`.
13+
/// - SeeAlso: _VariadicView.ViewRoot.
14+
/// - Note: Requirements mirror `View`'s.
15+
public protocol _VariadicView_ViewRoot: _VariadicView_Root {
16+
associatedtype Body: View
17+
18+
static func _makeView(
19+
root: _GraphValue<Self>,
20+
inputs: _ViewInputs,
21+
body: (_Graph, _ViewInputs) -> _ViewListOutputs
22+
) -> _ViewOutputs
23+
24+
static func _makeViewList(
25+
root: _GraphValue<Self>,
26+
inputs: _ViewListInputs,
27+
body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs
28+
) -> _ViewListOutputs
29+
30+
static func _viewListCount(
31+
inputs: _ViewListCountInputs,
32+
body: (_ViewListCountInputs) -> Int?
33+
) -> Int?
34+
35+
@ViewBuilder
36+
func body(children: _VariadicView.Children) -> Body
37+
}
38+
39+
extension _VariadicView_ViewRoot {
40+
public static func _makeView(
41+
root: _GraphValue<Self>,
42+
inputs: _ViewInputs,
43+
body: (_Graph, _ViewInputs) -> _ViewListOutputs
44+
) -> _ViewOutputs {
45+
fatalError("TODO")
46+
}
47+
48+
public static func _makeViewList(
49+
root: _GraphValue<Self>,
50+
inputs: _ViewListInputs,
51+
body: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs
52+
) -> _ViewListOutputs {
53+
fatalError("TODO")
54+
}
55+
56+
public static func _viewListCount(inputs: _ViewListCountInputs) -> Int? {
57+
Body._viewListCount(inputs: inputs)
58+
}
59+
}
60+
61+
62+
// MARK: - ViewRootBody
63+
64+
private struct ViewRootBody<Root> {
65+
@Attribute var root: Root
66+
@Attribute var list: ViewList
67+
let contentSubgraph: OGSubgraph
68+
}
69+
70+
extension _ViewInputs {
71+
private struct ImplicitRootType: ViewInput {
72+
static let defaultValue: _VariadicView_AnyImplicitRoot.Type = _VStackLayout.self
73+
}
74+
}
75+
76+
extension View {
77+
static func makeImplicitRoot(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
78+
// TODO
79+
return .init()
80+
}
81+
}
82+
83+
// TODO: ViewModifier

Sources/OpenSwiftUI/Core/View/ViewList/ViewListCountInputs.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
/// Input values to `View._viewListCount()`.
99
public struct _ViewListCountInputs {
10-
var customInputs : PropertyList
11-
var options : _ViewListInputs.Options
12-
var baseOptions : _GraphInputs.Options
10+
var customInputs: PropertyList
11+
var options: _ViewListInputs.Options
12+
var baseOptions: _GraphInputs.Options
1313

1414
subscript<Input: GraphInput>(_ type: Input.Type) -> Input.Value {
1515
get { customInputs[type] }

Sources/OpenSwiftUI/Core/View/ViewList/ViewListInputs.swift

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ public struct _ViewListInputs {
1212
private var base: _GraphInputs
1313
var implicitID: Int
1414
var options: _ViewListInputs.Options
15-
@OptionalAttribute
16-
var traits: ViewTraitCollection?
15+
@OptionalAttribute var traits: ViewTraitCollection?
1716
var traitKeys: ViewTraitKeys?
1817

19-
struct Options: OptionSet {
20-
let rawValue: Int
18+
package struct Options: OptionSet {
19+
package init(rawValue: Int) {
20+
self.rawValue = rawValue
21+
}
22+
23+
package let rawValue: Int
2124
}
2225

2326
// MARK: - base
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ID: 70E71091E926A1B09B75AAEB38F5AA3F
2+
3+
struct _ViewList_ID {
4+
var _index: Int32
5+
var implicitID: Int32
6+
private var explicitIDs: [Explicit]
7+
}
8+
9+
extension _ViewList_ID {
10+
private struct Explicit {
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
internal import OpenGraphShims
2+
3+
struct _ViewList_View {
4+
var elements: _ViewList_Elements
5+
var id: _ViewList_ID
6+
var index: Int
7+
var count: Int
8+
var contentSubgraph: OGSubgraph
9+
}

Sources/OpenSwiftUI/Core/View/ViewRoot.swift

-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,3 @@
66
// Status: WIP
77
// ID: 00F12C0E37A19C593ECA0DBD3BE26541
88

9-
extension View {
10-
static func makeImplicitRoot(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
11-
// TODO
12-
return .init()
13-
}
14-
}

0 commit comments

Comments
 (0)