Skip to content

Commit 8ca3584

Browse files
authored
Add basic display list + String support (#146)
1 parent fc6ad55 commit 8ca3584

File tree

8 files changed

+176
-8
lines changed

8 files changed

+176
-8
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// DisplayList+String.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for RELEASE_2024
6+
// Status: WIP
7+
// ID: 11125C146A81D1913BFBD53B89D010C6
8+
9+
extension DisplayList.Item {
10+
// TODO
11+
var features: DisplayList.Features { [] }
12+
var properties: DisplayList.Properties { [] }
13+
14+
fileprivate func print(into printer: inout SExpPrinter) {
15+
printer.push("item")
16+
if identity.value != .zero {
17+
printer.print("#:identity \(identity.value)", newline: false)
18+
}
19+
printer.print("#:version \(version.value)", newline: false)
20+
if features.contains(.required) {
21+
printer.print("#:required true", newline: false)
22+
}
23+
if features.contains(.views) {
24+
printer.print("#:views true", newline: false)
25+
}
26+
printer.print("(frame (\(position.x) \(position.y); \(size.width) \(size.height)))")
27+
switch value {
28+
case .empty:
29+
break
30+
case let .content(content):
31+
printer.print("(content-seed \(content.seed.value))")
32+
switch content.value {
33+
case let .placeholder(id: identity):
34+
printer.print("(placeholder \(identity))")
35+
default:
36+
// TOOD
37+
break
38+
}
39+
default:
40+
// TODO
41+
break
42+
}
43+
printer.pop()
44+
}
45+
46+
fileprivate func printMinimally(into printer: inout SExpPrinter) {
47+
printer.push("I:\(identity.value)")
48+
switch value {
49+
case .empty:
50+
break
51+
case let .content(content):
52+
switch content.value {
53+
case let .placeholder(id: identity):
54+
printer.print("@\(identity))")
55+
default:
56+
// TOOD
57+
break
58+
}
59+
default:
60+
// TODO
61+
break
62+
}
63+
printer.pop()
64+
}
65+
}
66+
67+
68+
extension DisplayList: CustomStringConvertible {
69+
public var description: String {
70+
var printer = SExpPrinter(tag: "display-list", singleLine: false)
71+
for item in items {
72+
item.print(into: &printer)
73+
}
74+
return printer.end()
75+
}
76+
77+
package var minimalDescription: String {
78+
var printer = SExpPrinter(tag: "DL", singleLine: true)
79+
for item in items {
80+
item.printMinimally(into: &printer)
81+
}
82+
return printer.end()
83+
}
84+
}
85+
extension DisplayList.Item: CustomStringConvertible {
86+
package var description: String {
87+
var printer = SExpPrinter(tag: "display-list-item", singleLine: false)
88+
print(into: &printer)
89+
return printer.end()
90+
}
91+
}

Sources/OpenSwiftUICore/Render/DisplayList.swift renamed to Sources/OpenSwiftUICore/Render/DisplayList/DisplayList.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,29 @@ package struct DisplayList: Equatable {
6363
}
6464

6565
package init(_ item: Item) {
66-
fatalError("TODO")
66+
switch item.value {
67+
case .empty:
68+
items = []
69+
features = []
70+
properties = []
71+
default:
72+
items = [item]
73+
features = item.features
74+
properties = item.properties
75+
}
6776
}
6877

78+
// TO BE VERIFIED
6979
package init(_ items: [Item]) {
70-
guard !items.isEmpty else {
71-
self.init()
72-
return
80+
var features: Features = []
81+
var properties: Properties = []
82+
for item in items {
83+
features.formUnion(item.features)
84+
properties.formUnion(item.properties)
7385
}
74-
fatalError("TODO")
86+
self.items = items
87+
self.features = features
88+
self.properties = properties
7589
}
7690

7791
package mutating func append(_ item: Item) {
@@ -264,7 +278,7 @@ extension DisplayList {
264278
package static let screencaptureProhibited = Properties(rawValue: 1 << 7)
265279
}
266280

267-
package struct Key : PreferenceKey {
281+
package struct Key: PreferenceKey {
268282
package static let _includesRemovedValues: Bool = true
269283
package static let defaultValue = DisplayList()
270284
package static func reduce(value: inout DisplayList, nextValue: () -> DisplayList) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// DisplayList+StringTests.swift
3+
// OpenSwiftUICoreTests
4+
5+
import OpenSwiftUICore
6+
import Testing
7+
8+
struct DisplayList_StringTests {
9+
@Test
10+
func plain() {
11+
let emptyList = DisplayList()
12+
#expect(emptyList.description == "(display-list)")
13+
#expect(emptyList.minimalDescription == "(DL)")
14+
}
15+
16+
@Test
17+
func emptyItem() {
18+
let item = DisplayList.Item(.empty, frame: .zero, identity: .init(decodedValue: 1), version: .init(decodedValue: 0))
19+
let d = item.description
20+
print(d)
21+
22+
#expect(item.description == """
23+
(display-list-item
24+
(item #:identity 1 #:version 0
25+
(frame (0.0 0.0; 0.0 0.0))))
26+
""")
27+
28+
let list = DisplayList(item)
29+
#expect(list.description == "(display-list)")
30+
#expect(list.minimalDescription == "(DL)")
31+
32+
let list2 = DisplayList([item])
33+
#expect(list2.description == """
34+
(display-list
35+
(item #:identity 1 #:version 0
36+
(frame (0.0 0.0; 0.0 0.0))))
37+
""")
38+
#expect(list2.minimalDescription == "(DL(I:1))")
39+
}
40+
41+
func placeholdItem() {
42+
let item = DisplayList.Item(.content(.init(.placeholder(id: .init(decodedValue: 2)), seed: .init(decodedValue: 4))), frame: .zero, identity: .init(decodedValue: 1), version: .init(decodedValue: 0))
43+
let expectedDescription = """
44+
(display-list
45+
(item #:identity 1 #:version 0 #:views true
46+
(frame (0.0 0.0; 0.0 0.0))
47+
(content-seed 4)
48+
(placeholder #2)))
49+
"""
50+
51+
let expectedMinimalDescription = """
52+
(DL(I:1 @#2))
53+
"""
54+
55+
56+
let list = DisplayList(item)
57+
#expect(list.description == expectedDescription)
58+
#expect(list.minimalDescription == expectedMinimalDescription)
59+
60+
let list2 = DisplayList([item])
61+
#expect(list2.description == expectedDescription)
62+
#expect(list2.minimalDescription == expectedMinimalDescription)
63+
}
64+
65+
}

Tests/OpenSwiftUICoreTests/Render/DisplayList_StableIdentityTests.swift renamed to Tests/OpenSwiftUICoreTests/Render/DisplayList/DisplayList_StableIdentityTests.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import OpenSwiftUICore
66
import Testing
77

8-
@_spi(Debug) import OpenGraphShims
9-
108
struct DisplayList_StableIdentityTests {
119
@Test
1210
func formUnion() {

0 commit comments

Comments
 (0)