Skip to content

Commit

Permalink
Add basic display list + String support (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye authored Oct 22, 2024
1 parent fc6ad55 commit 8ca3584
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// DisplayList+String.swift
// OpenSwiftUICore
//
// Audited for RELEASE_2024
// Status: WIP
// ID: 11125C146A81D1913BFBD53B89D010C6

extension DisplayList.Item {
// TODO
var features: DisplayList.Features { [] }
var properties: DisplayList.Properties { [] }

fileprivate func print(into printer: inout SExpPrinter) {
printer.push("item")
if identity.value != .zero {
printer.print("#:identity \(identity.value)", newline: false)
}
printer.print("#:version \(version.value)", newline: false)
if features.contains(.required) {
printer.print("#:required true", newline: false)
}
if features.contains(.views) {
printer.print("#:views true", newline: false)
}
printer.print("(frame (\(position.x) \(position.y); \(size.width) \(size.height)))")
switch value {
case .empty:
break
case let .content(content):
printer.print("(content-seed \(content.seed.value))")
switch content.value {
case let .placeholder(id: identity):
printer.print("(placeholder \(identity))")
default:
// TOOD
break
}
default:
// TODO
break
}
printer.pop()
}

fileprivate func printMinimally(into printer: inout SExpPrinter) {
printer.push("I:\(identity.value)")
switch value {
case .empty:
break
case let .content(content):
switch content.value {
case let .placeholder(id: identity):
printer.print("@\(identity))")
default:
// TOOD
break
}
default:
// TODO
break
}
printer.pop()
}
}


extension DisplayList: CustomStringConvertible {
public var description: String {
var printer = SExpPrinter(tag: "display-list", singleLine: false)
for item in items {
item.print(into: &printer)
}
return printer.end()
}

package var minimalDescription: String {
var printer = SExpPrinter(tag: "DL", singleLine: true)
for item in items {
item.printMinimally(into: &printer)
}
return printer.end()
}
}
extension DisplayList.Item: CustomStringConvertible {
package var description: String {
var printer = SExpPrinter(tag: "display-list-item", singleLine: false)
print(into: &printer)
return printer.end()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,29 @@ package struct DisplayList: Equatable {
}

package init(_ item: Item) {
fatalError("TODO")
switch item.value {
case .empty:
items = []
features = []
properties = []
default:
items = [item]
features = item.features
properties = item.properties
}
}

// TO BE VERIFIED
package init(_ items: [Item]) {
guard !items.isEmpty else {
self.init()
return
var features: Features = []
var properties: Properties = []
for item in items {
features.formUnion(item.features)
properties.formUnion(item.properties)
}
fatalError("TODO")
self.items = items
self.features = features
self.properties = properties
}

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

package struct Key : PreferenceKey {
package struct Key: PreferenceKey {
package static let _includesRemovedValues: Bool = true
package static let defaultValue = DisplayList()
package static func reduce(value: inout DisplayList, nextValue: () -> DisplayList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// DisplayList+StringTests.swift
// OpenSwiftUICoreTests

import OpenSwiftUICore
import Testing

struct DisplayList_StringTests {
@Test
func plain() {
let emptyList = DisplayList()
#expect(emptyList.description == "(display-list)")
#expect(emptyList.minimalDescription == "(DL)")
}

@Test
func emptyItem() {
let item = DisplayList.Item(.empty, frame: .zero, identity: .init(decodedValue: 1), version: .init(decodedValue: 0))
let d = item.description
print(d)

#expect(item.description == """
(display-list-item
(item #:identity 1 #:version 0
(frame (0.0 0.0; 0.0 0.0))))
""")

let list = DisplayList(item)
#expect(list.description == "(display-list)")
#expect(list.minimalDescription == "(DL)")

let list2 = DisplayList([item])
#expect(list2.description == """
(display-list
(item #:identity 1 #:version 0
(frame (0.0 0.0; 0.0 0.0))))
""")
#expect(list2.minimalDescription == "(DL(I:1))")
}

func placeholdItem() {
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))
let expectedDescription = """
(display-list
(item #:identity 1 #:version 0 #:views true
(frame (0.0 0.0; 0.0 0.0))
(content-seed 4)
(placeholder #2)))
"""

let expectedMinimalDescription = """
(DL(I:1 @#2))
"""


let list = DisplayList(item)
#expect(list.description == expectedDescription)
#expect(list.minimalDescription == expectedMinimalDescription)

let list2 = DisplayList([item])
#expect(list2.description == expectedDescription)
#expect(list2.minimalDescription == expectedMinimalDescription)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import OpenSwiftUICore
import Testing

@_spi(Debug) import OpenGraphShims

struct DisplayList_StableIdentityTests {
@Test
func formUnion() {
Expand Down

0 comments on commit 8ca3584

Please sign in to comment.