Skip to content

Commit

Permalink
Update VariadicView (#95)
Browse files Browse the repository at this point in the history
* Update VariadicView

* Update HVStackLayout documentation

* Update VariadicView
  • Loading branch information
Kyle-Ye authored Aug 28, 2024
1 parent e5b5ce4 commit 36c266c
Show file tree
Hide file tree
Showing 17 changed files with 407 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUI/Core/View/ConditionalContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ extension _ConditionalContent: View, PrimitiveView where TrueContent: View, Fals

public static func _makeView(view: _GraphValue<Self>, 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
)
Expand Down
54 changes: 54 additions & 0 deletions Sources/OpenSwiftUI/Core/View/VariadicView/ForEach.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// ForEach.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

/// A structure that computes views on demand from an underlying collection of
/// identified data.
///
/// Use `ForEach` to provide views based on a
/// [RandomAccessCollection](https://developer.apple.com/documentation/swift/randomaccesscollection)
/// of some data type. Either the collection's elements must conform to
/// [Identifiable](https://developer.apple.com/documentation/swift/identifiable) or you
/// need to provide an `id` parameter to the `ForEach` initializer.
///
/// The following example creates a `NamedFont` type that conforms to
/// [Identifiable](https://developer.apple.com/documentation/swift/identifiable), and an
/// array of this type called `namedFonts`. A `ForEach` instance iterates
/// over the array, producing new ``Text`` instances that display examples
/// of each OpenSwiftUI ``Font`` style provided in the array.
///
/// private struct NamedFont: Identifiable {
/// let name: String
/// let font: Font
/// var id: String { name }
/// }
///
/// private let namedFonts: [NamedFont] = [
/// NamedFont(name: "Large Title", font: .largeTitle),
/// NamedFont(name: "Title", font: .title),
/// NamedFont(name: "Headline", font: .headline),
/// NamedFont(name: "Body", font: .body),
/// NamedFont(name: "Caption", font: .caption)
/// ]
///
/// var body: some View {
/// ForEach(namedFonts) { namedFont in
/// Text(namedFont.name)
/// .font(namedFont.font)
/// }
/// }
///
/// ![A vertically arranged stack of labels showing various standard fonts,
/// such as Large Title and Headline.](OpenSwiftUI-ForEach-fonts.png)
public struct ForEach<Data, ID, Content> where Data: RandomAccessCollection, ID: Hashable {

/// The collection of underlying identified data that OpenSwiftUI uses to create
/// views dynamically.
public var data: Data

/// A function to create content on demand using the underlying data.
public var content: (Data.Element) -> Content
}
49 changes: 49 additions & 0 deletions Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView.swift
Original file line number Diff line number Diff line change
@@ -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<Root: _VariadicView_Root, Content> {
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()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// VariadicView_Children.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: TODO
// ID: 52A2FFECFBCF37BFFEED558E33EBD1E3

internal import OpenGraphShims

/// An ad hoc collection of the children of a variadic view.
public struct _VariadicView_Children {
var list: ViewList
var contentSubgraph: OGSubgraph
}

extension _VariadicView_Children: RandomAccessCollection {
public struct Element: PrimitiveView, UnaryView, Identifiable {

// var view: _ViewList_View
var traits: ViewTraitCollection

public var id: AnyHashable {
// view.viewID
fatalError("TODO")

}
public func id<ID>(as _: ID.Type = ID.self) -> ID? where ID : Hashable {
fatalError("TODO")
}

public subscript<Trait: _ViewTraitKey>(key: Trait.Type) -> Trait.Value {
get { traits[key] }
set { traits[key] = newValue }
}

public static func _makeView(view: _GraphValue<_VariadicView_Children.Element>, inputs: _ViewInputs) -> _ViewOutputs {
fatalError("TODO")
}
}

public var startIndex: Int {
fatalError("TODO")

// get
}
public var endIndex: Int {
fatalError("TODO")

// get
}
public subscript(index: Int) -> _VariadicView_Children.Element {
fatalError("TODO")

// get
}
}

extension _VariadicView_Children {
private struct Child: Rule, AsyncAttribute {
typealias Value = ForEach<_VariadicView_Children, AnyHashable, _VariadicView_Children.Element>

@Attribute var children: _VariadicView_Children

var value: Value {
fatalError("TODO")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// VariadicView_ImplicitRoot.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Complete

package protocol _VariadicView_AnyImplicitRoot {
static func visitType<V>(visitor: inout V) where V: _VariadicView_ImplicitRootVisitor
}

package protocol _VariadicView_ImplicitRootVisitor {
mutating func visit<R>(type: R.Type) where R: _VariadicView_ImplicitRoot
}

package protocol _VariadicView_ImplicitRoot: _VariadicView_AnyImplicitRoot, _VariadicView_ViewRoot {
static var implicitRoot: Self { get }
}

extension _VariadicView_ImplicitRoot {
package func visitType<Visitor: _VariadicView_ImplicitRootVisitor>(visitor: inout Visitor) {
visitor.visit(type: Self.self)
}
}
30 changes: 30 additions & 0 deletions Sources/OpenSwiftUI/Core/View/VariadicView/VariadicView_Root.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// 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
}

package static var viewListOptions: _ViewListInputs.Options {
.init(rawValue: _viewListOptions)
}

public static func _viewListCount(
inputs _: _ViewListCountInputs,
body _: (_ViewListCountInputs) -> Int?
) -> Int? {
nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// VariadicView_ImplicitRoot.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// ID: 00F12C0E37A19C593ECA0DBD3BE26541

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<Self>,
inputs: _ViewInputs,
body: (_Graph, _ViewInputs) -> _ViewListOutputs
) -> _ViewOutputs

static func _makeViewList(
root: _GraphValue<Self>,
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<Self>,
inputs: _ViewInputs,
body: (_Graph, _ViewInputs) -> _ViewListOutputs
) -> _ViewOutputs {
fatalError("TODO")
}

public static func _makeViewList(
root: _GraphValue<Self>,
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<Root> {
@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<Self>, inputs: _ViewInputs) -> _ViewOutputs {
// TODO
return .init()
}
}

// TODO: ViewModifier
Original file line number Diff line number Diff line change
Expand Up @@ -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<Input: GraphInput>(_ type: Input.Type) -> Input.Value {
get { customInputs[type] }
Expand Down
11 changes: 7 additions & 4 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewListInputs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ public struct _ViewListInputs {
private var base: _GraphInputs
var implicitID: Int
var options: _ViewListInputs.Options
@OptionalAttribute
var traits: ViewTraitCollection?
@OptionalAttribute var traits: ViewTraitCollection?
var traitKeys: ViewTraitKeys?

struct Options: OptionSet {
let rawValue: Int
package struct Options: OptionSet {
package init(rawValue: Int) {
self.rawValue = rawValue
}

package let rawValue: Int
}

// MARK: - base
Expand Down
12 changes: 12 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewList_ID.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ID: 70E71091E926A1B09B75AAEB38F5AA3F

struct _ViewList_ID {
var _index: Int32
var implicitID: Int32
private var explicitIDs: [Explicit]
}

extension _ViewList_ID {
private struct Explicit {
}
}
9 changes: 9 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewList_View.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
internal import OpenGraphShims

struct _ViewList_View {
var elements: _ViewList_Elements
var id: _ViewList_ID
var index: Int
var count: Int
var contentSubgraph: OGSubgraph
}
6 changes: 0 additions & 6 deletions Sources/OpenSwiftUI/Core/View/ViewRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,3 @@
// Status: WIP
// ID: 00F12C0E37A19C593ECA0DBD3BE26541

extension View {
static func makeImplicitRoot(view: _GraphValue<Self>, inputs: _ViewInputs) -> _ViewOutputs {
// TODO
return .init()
}
}
Loading

0 comments on commit 36c266c

Please sign in to comment.