3
3
// OpenSwiftUICore
4
4
//
5
5
// Audited for iOS 18.0
6
- // Status: TODO
6
+ // Status: Complete
7
7
// ID: 83E729E7BD00420AB79EFD8DF557072A (SwiftUI)
8
8
// ID: 0CBA6217BE011883F496E97230B6CF8F (SwiftUICore)
9
9
@@ -87,28 +87,92 @@ public struct EnvironmentValues: CustomStringConvertible {
87
87
public init ( ) {
88
88
_plist = PropertyList ( )
89
89
tracker = nil
90
- // TODO: CoreGlue .shared
90
+ CoreGlue2 . shared. configureDefaultEnvironment ( & self )
91
91
}
92
92
93
+ /// Creates an environment values instance with the specified property list.
94
+ ///
95
+ /// - Parameter plist: The property list to initialize the environment values with.
93
96
package init ( _ plist: PropertyList ) {
94
97
_plist = plist
95
98
tracker = nil
96
99
}
97
100
101
+ /// Creates an environment values instance with the specified property list and tracker.
102
+ ///
103
+ /// - Parameters:
104
+ /// - plist: The property list to initialize the environment values with.
105
+ /// - tracker: The tracker to monitor property list changes.
98
106
package init ( _ plist: PropertyList , tracker: PropertyList . Tracker ) {
99
- // FIXME
107
+ tracker . initializeValues ( from : plist )
100
108
self . _plist = plist
101
109
self . tracker = tracker
102
110
}
103
111
104
- // FIXME
112
+ /// The underlying property list that stores environment values.
105
113
package var plist : PropertyList {
106
- get { _plist }
114
+ get {
115
+ _plist
116
+ }
107
117
set {
118
+ guard _plist. id != newValue. id else {
119
+ return
120
+ }
121
+ if let tracker {
122
+ tracker. invalidateAllValues ( from: _plist, to: newValue)
123
+ }
108
124
_plist = newValue
109
125
}
110
126
}
111
127
128
+ /// Returns a new environment values instance without a tracker.
129
+ ///
130
+ /// - Returns: An environment values instance with the same property list but no tracker.
131
+ package func removingTracker( ) -> EnvironmentValues {
132
+ EnvironmentValues ( _plist)
133
+ }
134
+
135
+ /// Adds dependencies from another tracker to this environment's tracker.
136
+ ///
137
+ /// - Parameter other: The tracker whose dependencies should be added.
138
+ package func addDependencies( from other: PropertyList . Tracker ) {
139
+ guard let tracker else { return }
140
+ tracker. formUnion ( other)
141
+ }
142
+
143
+ /// Retrieves a value using a secondary lookup mechanism.
144
+ ///
145
+ /// - Parameter key: The lookup key type.
146
+ /// - Returns: The value associated with the key's primary type.
147
+ package func valueWithSecondaryLookup< Lookup> ( _ key: Lookup . Type ) -> Lookup . Primary . Value where Lookup: PropertyKeyLookup {
148
+ if let tracker {
149
+ tracker. valueWithSecondaryLookup ( _plist, secondaryLookupHandler: key)
150
+ } else {
151
+ _plist. valueWithSecondaryLookup ( key)
152
+ }
153
+ }
154
+
155
+ /// Sets a value for the specified environment key.
156
+ ///
157
+ /// - Parameters:
158
+ /// - value: The value to set.
159
+ /// - key: The environment key type.
160
+ package mutating func setValue< K> ( _ value: K . Value , for key: K . Type ) where K: EnvironmentKey {
161
+ _set ( value, for: key)
162
+ }
163
+
164
+ private mutating func _set< K> ( _ value: K . Value , for key: K . Type ) where K: EnvironmentKey {
165
+ let oldPlist = _plist
166
+ _plist [ EnvironmentPropertyKey< K> . self ] = value
167
+ if let tracker {
168
+ tracker. invalidateValue (
169
+ for: EnvironmentPropertyKey< K> . self ,
170
+ from: oldPlist,
171
+ to: plist
172
+ )
173
+ }
174
+ }
175
+
112
176
/// Accesses the environment value associated with a custom key.
113
177
///
114
178
/// Create custom environment values by defining a key
@@ -148,17 +212,17 @@ public struct EnvironmentValues: CustomStringConvertible {
148
212
}
149
213
}
150
214
set {
151
- _plist [ EnvironmentPropertyKey< K> . self ] = newValue
152
- if let tracker {
153
- tracker. invalidateValue (
154
- for: EnvironmentPropertyKey< K> . self ,
155
- from: _plist,
156
- to: _plist
157
- )
158
- }
215
+ setValue ( newValue, for: key)
159
216
}
160
217
}
161
218
219
+ /// Accesses the environment value associated with a derived environment key.
220
+ ///
221
+ /// Derived environment keys calculate their values based on other environment values.
222
+ /// This subscript provides read-only access to these derived values.
223
+ ///
224
+ /// - Parameter key: The derived environment key type.
225
+ /// - Returns: The value associated with the key.
162
226
package subscript< K> ( key: K . Type ) -> K . Value where K: DerivedEnvironmentKey {
163
227
if let tracker {
164
228
tracker. derivedValue ( _plist, for: DerivedEnvironmentPropertyKey< K> . self )
@@ -175,12 +239,25 @@ public struct EnvironmentValues: CustomStringConvertible {
175
239
@available ( * , unavailable)
176
240
extension EnvironmentValues : Sendable { }
177
241
178
- private struct EnvironmentPropertyKey < EnvKey: EnvironmentKey > : PropertyKey {
179
- static var defaultValue : EnvKey . Value { EnvKey . defaultValue }
242
+ /// A property key that provides access to environment values.
243
+ ///
244
+ /// This type bridges between the `EnvironmentKey` protocol and the internal `PropertyKey` system.
245
+ private struct EnvironmentPropertyKey < Key> : PropertyKey where Key: EnvironmentKey {
246
+ /// The default value for this property key, obtained from the environment key.
247
+ static var defaultValue : Key . Value {
248
+ Key . defaultValue
249
+ }
180
250
}
181
251
182
- private struct DerivedEnvironmentPropertyKey < EnvKey: DerivedEnvironmentKey > : DerivedPropertyKey {
183
- static func value( in plist: PropertyList ) -> EnvKey . Value {
184
- EnvKey . value ( in: EnvironmentValues ( plist) )
252
+ /// A property key that provides access to derived environment values.
253
+ ///
254
+ /// This type bridges between the `DerivedEnvironmentKey` protocol and the internal `DerivedPropertyKey` system.
255
+ private struct DerivedEnvironmentPropertyKey < Key> : DerivedPropertyKey where Key: DerivedEnvironmentKey {
256
+ /// Calculates the derived value based on the current environment.
257
+ ///
258
+ /// - Parameter plist: The property list containing the current environment state.
259
+ /// - Returns: The calculated value for this derived key.
260
+ static func value( in plist: PropertyList ) -> Key . Value {
261
+ Key . value ( in: EnvironmentValues ( plist) )
185
262
}
186
263
}
0 commit comments