Skip to content

Commit e16ee45

Browse files
authored
Add ViewList and ViewTrait (#90)
* Add ViewTraitCollection implementation * Add _ViewListOutputs related interface
1 parent 3725a67 commit e16ee45

20 files changed

+278
-16
lines changed

Sources/OpenSwiftUI/Core/View/TODO/_ViewListCountInputs.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.

Sources/OpenSwiftUI/Core/View/TODO/_ViewListInputs.swift

Lines changed: 0 additions & 2 deletions
This file was deleted.

Sources/OpenSwiftUI/Core/View/TODO/_ViewListOutputs.swift

Lines changed: 0 additions & 2 deletions
This file was deleted.

Sources/OpenSwiftUI/Core/View/ViewInputs.swift renamed to Sources/OpenSwiftUI/Core/View/View/ViewInputs.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//
2+
// ViewInputs.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
18
internal import OpenGraphShims
29

310
public struct _ViewInputs {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// ViewListCountInputs.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
8+
public struct _ViewListCountInputs {
9+
var customInputs : PropertyList
10+
var options : _ViewListInputs.Options
11+
var baseOptions : _GraphInputs.Options
12+
13+
subscript<Input: GraphInput>(_ type: Input.Type) -> Input.Value {
14+
get { customInputs[type] }
15+
set { customInputs[type] = newValue }
16+
}
17+
18+
mutating func popLast<Input: GraphInput, Value>(_ type: Input.Type) -> Value? where Input.Value == [Value] {
19+
var values = self[type]
20+
guard let value = values.popLast() else {
21+
return nil
22+
}
23+
self[type] = values
24+
return value
25+
}
26+
27+
mutating func append<Input: GraphInput, Value>(_ value: Value, to type: Input.Type) where Input.Value == [Value] {
28+
var values = self[type]
29+
values.append(value)
30+
self[type] = values
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// ViewListInputs.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
8+
internal import OpenGraphShims
9+
10+
public struct _ViewListInputs {
11+
var base: _GraphInputs
12+
var implicitID: Int
13+
var options: _ViewListInputs.Options
14+
@OptionalAttribute
15+
var traits: ViewTraitCollection?
16+
var traitKeys: ViewTraitKeys?
17+
18+
struct Options: OptionSet {
19+
let rawValue: Int
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// ViewListOutputs.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
8+
internal import OpenGraphShims
9+
10+
public struct _ViewListOutputs {
11+
var views: Views
12+
var nextImplicitID: Int
13+
var staticCount: Int?
14+
15+
enum Views {
16+
case staticList(_ViewList_Elements)
17+
case dynamicList(Attribute<ViewList>, ListModifier?)
18+
}
19+
20+
class ListModifier {
21+
init() {}
22+
23+
func apply(to: inout ViewList) {
24+
// TODO
25+
}
26+
}
27+
}
28+
29+
// TODO
30+
protocol ViewList {
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// ViewList_Elements.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
8+
protocol _ViewList_Elements {
9+
var count: Int { get }
10+
func makeElements(
11+
from: inout Int,
12+
inputs: _ViewInputs,
13+
indirectMap: _ViewList_IndirectMap?,
14+
body: (_ViewInputs, (_ViewInputs) -> _ViewOutputs) -> (_ViewOutputs?, Bool)
15+
) -> (_ViewOutputs?, Bool)
16+
func tryToReuseElement(at: Int, by: _ViewList_Elements, at: Int, indirectMap: _ViewList_IndirectMap, testOnly: Bool) -> Bool
17+
func retain() -> () -> Void
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// ViewList_IndirectMap.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
// ID: 70E71091E926A1B09B75AAEB38F5AA3F
8+
9+
internal import OpenGraphShims
10+
11+
final class _ViewList_IndirectMap {
12+
let subgraph: OGSubgraph
13+
#if canImport(Darwin)
14+
private var map: [OGAttribute: OGAttribute]
15+
#endif
16+
17+
init(subgraph: OGSubgraph) {
18+
self.subgraph = subgraph
19+
#if canImport(Darwin)
20+
self.map = [:]
21+
#endif
22+
}
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// ViewList_Subgraph.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
// ID: 70E71091E926A1B09B75AAEB38F5AA3F
8+
9+
internal import OpenGraphShims
10+
11+
class _ViewList_Subgraph {
12+
let subgraph: OGSubgraph
13+
private var refcount : UInt32
14+
15+
init(subgraph: OGSubgraph) {
16+
self.subgraph = subgraph
17+
self.refcount = 1 // TODO
18+
}
19+
20+
func invalidate() {}
21+
}
22+
23+
extension _ViewList_Subgraph {
24+
var isValid: Bool {
25+
guard refcount > 0 else {
26+
return false
27+
}
28+
return subgraph.isValid
29+
}
30+
31+
func retain() {
32+
refcount &+= 1
33+
}
34+
35+
func release(isInserted: Bool) {
36+
refcount &-= 1
37+
guard refcount == 0 else {
38+
return
39+
}
40+
invalidate()
41+
guard subgraph.isValid else {
42+
return
43+
}
44+
subgraph.willInvalidate(isInserted: isInserted)
45+
subgraph.invalidate()
46+
}
47+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// ViewTraitCollection.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
// ID: 9929B476764059557433A108298EE66F
8+
9+
struct ViewTraitCollection {
10+
private var storage: [any AnyViewTrait]
11+
12+
private struct AnyTrait<Key: _ViewTraitKey>: AnyViewTrait {
13+
var value: Key.Value
14+
15+
init(value: Key.Value) {
16+
self.value = value
17+
}
18+
19+
var id: ObjectIdentifier { ObjectIdentifier(Key.self) }
20+
21+
subscript<V>() -> V {
22+
get { value as! V }
23+
set { value = newValue as! Key.Value }
24+
}
25+
}
26+
27+
subscript<Key: _ViewTraitKey>(_: Key.Type) -> Key.Value {
28+
get {
29+
value(for: Key.self)
30+
}
31+
set {
32+
if let index = storage.firstIndex(where: { $0.id == ObjectIdentifier(Key.self) }) {
33+
storage[index][] = newValue
34+
} else {
35+
storage.append(AnyTrait<Key>(value: newValue))
36+
}
37+
}
38+
}
39+
40+
func value<Key: _ViewTraitKey>(for _: Key.Type, defaultValue: Key.Value = Key.defaultValue) -> Key.Value {
41+
storage.first { $0.id == ObjectIdentifier(Key.self) }?[] ?? defaultValue
42+
}
43+
44+
mutating func setValueIfUnset<Key: _ViewTraitKey>(_ value: Key.Value, for _: Key.Type) {
45+
guard !storage.contains(where: { $0.id == ObjectIdentifier(Key.self) }) else {
46+
return
47+
}
48+
storage.append(AnyTrait<Key>(value: value))
49+
}
50+
51+
// func insertInteraction(for: OnInsertInteraction.Strategy) -> OnInsertInteraction? {
52+
// fatalError("TODO")
53+
// }
54+
//
55+
// var optionalTransition: AnyTransition? {
56+
// fatalError("TODO")
57+
// }
58+
}
59+
60+
private protocol AnyViewTrait {
61+
var id: ObjectIdentifier { get }
62+
subscript<V>() -> V { get set }
63+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// ViewTraitKey.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: Complete
7+
8+
protocol _ViewTraitKey {
9+
associatedtype Value
10+
static var defaultValue: Value { get }
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// ViewTraitKeys.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: Complete
7+
8+
struct ViewTraitKeys {
9+
var types: Set<ObjectIdentifier>
10+
var isDataDependent: Bool
11+
12+
mutating func insert<Key: _ViewTraitKey>(_ type: Key.Type) {
13+
types.insert(ObjectIdentifier(type))
14+
}
15+
16+
func contains<Key: _ViewTraitKey>(_ type: Key.Type) -> Bool {
17+
types.contains(ObjectIdentifier(type))
18+
}
19+
}

Sources/OpenSwiftUI/View/Animation/TODO/AnimatableModifier.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension AnimatableModifier {
1515
}
1616

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

@@ -25,6 +25,6 @@ extension ViewModifier where Self: Animatable {
2525
}
2626

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

Sources/OpenSwiftUI/View/Animation/TODO/_AnimatableView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension _AnimatableView {
1515
}
1616

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

@@ -25,6 +25,6 @@ extension View where Self: Animatable {
2525
}
2626

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

Sources/OpenSwiftUI/View/View/AnyView.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public struct AnyView: PrimitiveView {
7474
}
7575

7676
public static func _makeViewList(view: _GraphValue<Self>, inputs: _ViewListInputs) -> _ViewListOutputs {
77-
// TODO
78-
.init()
77+
fatalError("TODO")
7978
}
8079
}
8180

Sources/OpenSwiftUI/View/View/EmptyView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct EmptyView: PrimitiveView {
1313
}
1414

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

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

0 commit comments

Comments
 (0)