Skip to content

Commit

Permalink
Add ViewList and ViewTrait (#90)
Browse files Browse the repository at this point in the history
* Add ViewTraitCollection implementation

* Add _ViewListOutputs related interface
  • Loading branch information
Kyle-Ye committed May 4, 2024
1 parent 3725a67 commit e16ee45
Show file tree
Hide file tree
Showing 20 changed files with 278 additions and 16 deletions.
5 changes: 0 additions & 5 deletions Sources/OpenSwiftUI/Core/View/TODO/_ViewListCountInputs.swift

This file was deleted.

2 changes: 0 additions & 2 deletions Sources/OpenSwiftUI/Core/View/TODO/_ViewListInputs.swift

This file was deleted.

2 changes: 0 additions & 2 deletions Sources/OpenSwiftUI/Core/View/TODO/_ViewListOutputs.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//
// ViewInputs.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

internal import OpenGraphShims

public struct _ViewInputs {
Expand Down
32 changes: 32 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewListCountInputs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ViewListCountInputs.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

public struct _ViewListCountInputs {
var customInputs : PropertyList
var options : _ViewListInputs.Options
var baseOptions : _GraphInputs.Options

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

mutating func popLast<Input: GraphInput, Value>(_ type: Input.Type) -> Value? where Input.Value == [Value] {
var values = self[type]
guard let value = values.popLast() else {
return nil
}
self[type] = values
return value
}

mutating func append<Input: GraphInput, Value>(_ value: Value, to type: Input.Type) where Input.Value == [Value] {
var values = self[type]
values.append(value)
self[type] = values
}
}
21 changes: 21 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewListInputs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ViewListInputs.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

internal import OpenGraphShims

public struct _ViewListInputs {
var base: _GraphInputs
var implicitID: Int
var options: _ViewListInputs.Options
@OptionalAttribute
var traits: ViewTraitCollection?
var traitKeys: ViewTraitKeys?

struct Options: OptionSet {
let rawValue: Int
}
}
31 changes: 31 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewListOutputs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// ViewListOutputs.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

internal import OpenGraphShims

public struct _ViewListOutputs {
var views: Views
var nextImplicitID: Int
var staticCount: Int?

enum Views {
case staticList(_ViewList_Elements)
case dynamicList(Attribute<ViewList>, ListModifier?)
}

class ListModifier {
init() {}

func apply(to: inout ViewList) {
// TODO
}
}
}

// TODO
protocol ViewList {
}
18 changes: 18 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewList_Elements.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// ViewList_Elements.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

protocol _ViewList_Elements {
var count: Int { get }
func makeElements(
from: inout Int,
inputs: _ViewInputs,
indirectMap: _ViewList_IndirectMap?,
body: (_ViewInputs, (_ViewInputs) -> _ViewOutputs) -> (_ViewOutputs?, Bool)
) -> (_ViewOutputs?, Bool)
func tryToReuseElement(at: Int, by: _ViewList_Elements, at: Int, indirectMap: _ViewList_IndirectMap, testOnly: Bool) -> Bool
func retain() -> () -> Void
}
23 changes: 23 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewList_IndirectMap.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ViewList_IndirectMap.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// ID: 70E71091E926A1B09B75AAEB38F5AA3F

internal import OpenGraphShims

final class _ViewList_IndirectMap {
let subgraph: OGSubgraph
#if canImport(Darwin)
private var map: [OGAttribute: OGAttribute]
#endif

init(subgraph: OGSubgraph) {
self.subgraph = subgraph
#if canImport(Darwin)
self.map = [:]
#endif
}
}
47 changes: 47 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewList/ViewList_Subgraph.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// ViewList_Subgraph.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// ID: 70E71091E926A1B09B75AAEB38F5AA3F

internal import OpenGraphShims

class _ViewList_Subgraph {
let subgraph: OGSubgraph
private var refcount : UInt32

init(subgraph: OGSubgraph) {
self.subgraph = subgraph
self.refcount = 1 // TODO
}

func invalidate() {}
}

extension _ViewList_Subgraph {
var isValid: Bool {
guard refcount > 0 else {
return false
}
return subgraph.isValid
}

func retain() {
refcount &+= 1
}

func release(isInserted: Bool) {
refcount &-= 1
guard refcount == 0 else {
return
}
invalidate()
guard subgraph.isValid else {
return
}
subgraph.willInvalidate(isInserted: isInserted)
subgraph.invalidate()
}
}
63 changes: 63 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewTrait/ViewTraitCollection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// ViewTraitCollection.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// ID: 9929B476764059557433A108298EE66F

struct ViewTraitCollection {
private var storage: [any AnyViewTrait]

private struct AnyTrait<Key: _ViewTraitKey>: AnyViewTrait {
var value: Key.Value

init(value: Key.Value) {
self.value = value
}

var id: ObjectIdentifier { ObjectIdentifier(Key.self) }

subscript<V>() -> V {
get { value as! V }
set { value = newValue as! Key.Value }
}
}

subscript<Key: _ViewTraitKey>(_: Key.Type) -> Key.Value {
get {
value(for: Key.self)
}
set {
if let index = storage.firstIndex(where: { $0.id == ObjectIdentifier(Key.self) }) {
storage[index][] = newValue
} else {
storage.append(AnyTrait<Key>(value: newValue))
}
}
}

func value<Key: _ViewTraitKey>(for _: Key.Type, defaultValue: Key.Value = Key.defaultValue) -> Key.Value {
storage.first { $0.id == ObjectIdentifier(Key.self) }?[] ?? defaultValue
}

mutating func setValueIfUnset<Key: _ViewTraitKey>(_ value: Key.Value, for _: Key.Type) {
guard !storage.contains(where: { $0.id == ObjectIdentifier(Key.self) }) else {
return
}
storage.append(AnyTrait<Key>(value: value))
}

// func insertInteraction(for: OnInsertInteraction.Strategy) -> OnInsertInteraction? {
// fatalError("TODO")
// }
//
// var optionalTransition: AnyTransition? {
// fatalError("TODO")
// }
}

private protocol AnyViewTrait {
var id: ObjectIdentifier { get }
subscript<V>() -> V { get set }
}
11 changes: 11 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewTrait/ViewTraitKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// ViewTraitKey.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Complete

protocol _ViewTraitKey {
associatedtype Value
static var defaultValue: Value { get }
}
19 changes: 19 additions & 0 deletions Sources/OpenSwiftUI/Core/View/ViewTrait/ViewTraitKeys.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// ViewTraitKeys.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Complete

struct ViewTraitKeys {
var types: Set<ObjectIdentifier>
var isDataDependent: Bool

mutating func insert<Key: _ViewTraitKey>(_ type: Key.Type) {
types.insert(ObjectIdentifier(type))
}

func contains<Key: _ViewTraitKey>(_ type: Key.Type) -> Bool {
types.contains(ObjectIdentifier(type))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension AnimatableModifier {
}

public static func _makeViewList(modifier _: _GraphValue<Self>, inputs _: _ViewListInputs, body _: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs) -> _ViewListOutputs {
.init()
fatalError("TODO")
}
}

Expand All @@ -25,6 +25,6 @@ extension ViewModifier where Self: Animatable {
}

public static func _makeViewList(modifier _: _GraphValue<Self>, inputs _: _ViewListInputs, body _: @escaping (_Graph, _ViewListInputs) -> _ViewListOutputs) -> _ViewListOutputs {
.init()
fatalError("TODO")
}
}
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUI/View/Animation/TODO/_AnimatableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension _AnimatableView {
}

public static func _makeViewList(view _: _GraphValue<Self>, inputs _: _ViewListInputs) -> _ViewListOutputs {
.init()
fatalError("TODO")
}
}

Expand All @@ -25,6 +25,6 @@ extension View where Self: Animatable {
}

public static func _makeViewList(view _: _GraphValue<Self>, inputs _: _ViewListInputs) -> _ViewListOutputs {
.init()
fatalError("TODO")
}
}
3 changes: 1 addition & 2 deletions Sources/OpenSwiftUI/View/View/AnyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public struct AnyView: PrimitiveView {
}

public static func _makeViewList(view: _GraphValue<Self>, inputs: _ViewListInputs) -> _ViewListOutputs {
// TODO
.init()
fatalError("TODO")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/View/View/EmptyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct EmptyView: PrimitiveView {
}

public static func _makeViewList(view: _GraphValue<EmptyView>, inputs: _ViewListInputs) -> _ViewListOutputs {
.init() // FIXME
fatalError("TODO")
}

public static func _viewListCount(inputs: _ViewListCountInputs) -> Int? {
Expand Down

0 comments on commit e16ee45

Please sign in to comment.