|
| 1 | +// |
| 2 | +// FocusState.swift |
| 3 | +// OpenSwiftUI |
| 4 | +// |
| 5 | +// Created by Kyle on 2024/2/1. |
| 6 | +// Lastest Version: iOS 15.5 |
| 7 | +// Status: WIP |
| 8 | +// ID: 274D264A38B51DC68ACC48A91353B7D0 |
| 9 | + |
| 10 | +@frozen |
| 11 | +@propertyWrapper |
| 12 | +public struct FocusState<Value>: DynamicProperty where Value: Hashable { |
| 13 | + @frozen |
| 14 | + @propertyWrapper |
| 15 | + public struct Binding { |
| 16 | + @OpenSwiftUI.Binding |
| 17 | + private var binding: Value |
| 18 | + |
| 19 | + init(binding: OpenSwiftUI.Binding<Value>) { |
| 20 | + _binding = binding |
| 21 | + } |
| 22 | + |
| 23 | + public var wrappedValue: Value { |
| 24 | + get { binding } |
| 25 | + nonmutating set { binding = newValue } |
| 26 | + } |
| 27 | + |
| 28 | + public var projectedValue: FocusState<Value>.Binding { |
| 29 | + self |
| 30 | + } |
| 31 | + |
| 32 | + var propertyID: ObjectIdentifier { |
| 33 | + if let location = _binding.location as? FocusStoreLocation<Value> { |
| 34 | + location.id |
| 35 | + } else { |
| 36 | + #if canImport(ObjectiveC) |
| 37 | + ObjectIdentifier(PrivateType.self) |
| 38 | + #else |
| 39 | + ObjectIdentifier(unsafeBitCast(0, to: AnyObject.self)) |
| 40 | + #endif |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private enum PrivateType {} |
| 45 | + } |
| 46 | + |
| 47 | + var value: Value |
| 48 | + var location: AnyLocation<Value>? |
| 49 | + var resetValue: Value |
| 50 | + public var wrappedValue: Value { |
| 51 | + get { |
| 52 | + getValue(forReading: true) |
| 53 | + } |
| 54 | + nonmutating set { |
| 55 | + guard let location else { |
| 56 | + return |
| 57 | + } |
| 58 | + location.set(newValue, transaction: Transaction()) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public var projectedValue: FocusState<Value>.Binding { |
| 63 | + let value = getValue(forReading: false) |
| 64 | + let binding: OpenSwiftUI.Binding<Value> |
| 65 | + if let location { |
| 66 | + binding = OpenSwiftUI.Binding(value: value, location: location) |
| 67 | + } else { |
| 68 | + Log.runtimeIssues("Accessing FocusState's value outside of the body of a View. This will result in a constant Binding of the initial value and will not update.") |
| 69 | + binding = .constant(value) |
| 70 | + } |
| 71 | + return Binding(binding: binding) |
| 72 | + } |
| 73 | + |
| 74 | + public static func _makeProperty<V>(in buffer: inout _DynamicPropertyBuffer, container: _GraphValue<V>, fieldOffset: Int, inputs: inout _GraphInputs) { |
| 75 | + // TODO |
| 76 | + } |
| 77 | + |
| 78 | + public init() where Value == Bool { |
| 79 | + value = false |
| 80 | + location = nil |
| 81 | + resetValue = false |
| 82 | + } |
| 83 | + |
| 84 | + public init<T>() where Value == T?, T: Hashable { |
| 85 | + value = nil |
| 86 | + location = nil |
| 87 | + resetValue = nil |
| 88 | + } |
| 89 | + |
| 90 | + private func getValue(forReading: Bool) -> Value { |
| 91 | + guard let location else { |
| 92 | + return value |
| 93 | + } |
| 94 | + if GraphHost.isUpdating { |
| 95 | + if forReading { |
| 96 | + location.wasRead = true |
| 97 | + } |
| 98 | + return value |
| 99 | + } else { |
| 100 | + return location.get() |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments