|
1 | 1 | //
|
2 |
| -// GraphInput.swift |
3 |
| -// OpenSwiftUI |
| 2 | +// GraphInputs.swift |
| 3 | +// OpenSwiftUICore |
4 | 4 | //
|
5 | 5 | // Audited for RELEASE_2021
|
6 | 6 | // Status: Complete
|
7 | 7 |
|
8 |
| -protocol GraphInput: PropertyKey {} |
| 8 | +package protocol GraphInput: PropertyKey { |
| 9 | + static var isTriviallyReusable: Swift.Bool { get } |
| 10 | + static func makeReusable(indirectMap: IndirectAttributeMap, value: inout Value) |
| 11 | + static func tryToReuse(_ value: Value, by other: Value, indirectMap: IndirectAttributeMap, testOnly: Bool) -> Bool |
| 12 | +} |
| 13 | + |
| 14 | +internal import OpenGraphShims |
| 15 | + |
| 16 | +public struct _GraphInputs { |
| 17 | + var customInputs: PropertyList |
| 18 | + var time: Attribute<Time> |
| 19 | + var cachedEnvironment: MutableBox<CachedEnvironment> |
| 20 | + var phase: Attribute<_GraphInputs.Phase> |
| 21 | + var transaction: Attribute<Transaction> |
| 22 | + private var changedDebugProperties: _ViewDebug.Properties |
| 23 | + private var options: _GraphInputs.Options |
| 24 | + #if canImport(Darwin) // FIXME: See #39 |
| 25 | + var mergedInputs: Set<AnyAttribute> |
| 26 | + #endif |
| 27 | + |
| 28 | + #if canImport(Darwin) |
| 29 | + init(customInputs: PropertyList = PropertyList(), |
| 30 | + time: Attribute<Time>, |
| 31 | + cachedEnvironment: MutableBox<CachedEnvironment>, |
| 32 | + phase: Attribute<Phase>, |
| 33 | + transaction: Attribute<Transaction>, |
| 34 | + changedDebugProperties: _ViewDebug.Properties = [], |
| 35 | + options: _GraphInputs.Options = [], |
| 36 | + mergedInputs: Set<AnyAttribute> = []) { |
| 37 | + self.customInputs = customInputs |
| 38 | + self.time = time |
| 39 | + self.cachedEnvironment = cachedEnvironment |
| 40 | + self.phase = phase |
| 41 | + self.transaction = transaction |
| 42 | + self.changedDebugProperties = changedDebugProperties |
| 43 | + self.options = options |
| 44 | + self.mergedInputs = mergedInputs |
| 45 | + } |
| 46 | + #else // FIXME: See #39 |
| 47 | + init(customInputs: PropertyList = PropertyList(), |
| 48 | + time: Attribute<Time>, |
| 49 | + cachedEnvironment: MutableBox<CachedEnvironment>, |
| 50 | + phase: Attribute<Phase>, |
| 51 | + transaction: Attribute<Transaction>, |
| 52 | + changedDebugProperties: _ViewDebug.Properties = [], |
| 53 | + options: _GraphInputs.Options = []/*, |
| 54 | + mergedInputs: Set<AnyAttribute> = []*/) { |
| 55 | + self.customInputs = customInputs |
| 56 | + self.time = time |
| 57 | + self.cachedEnvironment = cachedEnvironment |
| 58 | + self.phase = phase |
| 59 | + self.transaction = transaction |
| 60 | + self.changedDebugProperties = changedDebugProperties |
| 61 | + self.options = options |
| 62 | + /* self.mergedInputs = mergedInputs */ |
| 63 | + } |
| 64 | + #endif |
| 65 | + |
| 66 | + subscript<Input: GraphInput>(_ type: Input.Type) -> Input.Value { |
| 67 | + get { customInputs[type] } |
| 68 | + set { customInputs[type] = newValue } |
| 69 | + } |
| 70 | + |
| 71 | + // MARK: - cachedEnvironment |
| 72 | + |
| 73 | + @inline(__always) |
| 74 | + func detechedEnvironmentInputs() -> Self { |
| 75 | + var newInputs = self |
| 76 | + newInputs.cachedEnvironment = MutableBox(cachedEnvironment.wrappedValue) |
| 77 | + return newInputs |
| 78 | + } |
| 79 | + |
| 80 | + @inline(__always) |
| 81 | + mutating func updateCachedEnvironment(_ box: MutableBox<CachedEnvironment>) { |
| 82 | + cachedEnvironment = box |
| 83 | + changedDebugProperties.insert(.environment) |
| 84 | + } |
| 85 | + |
| 86 | + // MARK: - changedDebugProperties |
| 87 | + |
| 88 | + @inline(__always) |
| 89 | + func withEmptyChangedDebugPropertiesInputs<R>(_ body: (_GraphInputs) -> R) -> R { |
| 90 | + var inputs = self |
| 91 | + inputs.changedDebugProperties = [] |
| 92 | + return body(inputs) |
| 93 | + } |
| 94 | + |
| 95 | + // MARK: - options |
| 96 | + |
| 97 | + @inline(__always) |
| 98 | + var enableLayout: Bool { |
| 99 | + get { options.contains(.enableLayout) } |
| 100 | + // TODO: setter |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +extension _GraphInputs { |
| 105 | + struct Phase: Equatable { |
| 106 | + var value: UInt32 |
| 107 | + |
| 108 | + @inline(__always) |
| 109 | + var seed: UInt32 { |
| 110 | + get { value >> 1 } |
| 111 | + // TODO |
| 112 | + // set |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +extension _GraphInputs { |
| 118 | + struct Options: OptionSet { |
| 119 | + let rawValue: UInt32 |
| 120 | + |
| 121 | + static var enableLayout: Options { Options(rawValue: 1 << 1) } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +extension _GraphInputs { |
| 126 | + typealias ConstantID = Int |
| 127 | + |
| 128 | + func intern<Value>(_ value: Value, id: ConstantID) -> Attribute<Value> { |
| 129 | + cachedEnvironment.wrappedValue.intern(value, id: id.internID) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +extension _GraphInputs.ConstantID { |
| 134 | + @inline(__always) |
| 135 | + var internID: Self { self & 0x1 } |
| 136 | +} |
| 137 | + |
| 138 | +/// Protocol for modifiers that only modify their children's inputs. |
| 139 | +public protocol _GraphInputsModifier { |
| 140 | + static func _makeInputs(modifier: _GraphValue<Self>, inputs: inout _GraphInputs) |
| 141 | +} |
0 commit comments