Skip to content

Commit 7e62588

Browse files
committed
Update EnvironmentValues implementation
1 parent b1432b7 commit 7e62588

File tree

2 files changed

+96
-18
lines changed

2 files changed

+96
-18
lines changed

Sources/OpenSwiftUI/Util/OpenSwiftUIGlue.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ final public class OpenSwiftUIGlue2: CoreGlue2 {
9797
}
9898

9999
override public final func configureDefaultEnvironment(_: inout EnvironmentValues) {
100+
// TODO
100101
}
101102

102103
override public final func makeRootView(base: AnyView, rootFocusScope: Namespace.ID) -> AnyView {

Sources/OpenSwiftUICore/Data/Environment/EnvironmentValues.swift

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// OpenSwiftUICore
44
//
55
// Audited for iOS 18.0
6-
// Status: TODO
6+
// Status: Complete
77
// ID: 83E729E7BD00420AB79EFD8DF557072A (SwiftUI)
88
// ID: 0CBA6217BE011883F496E97230B6CF8F (SwiftUICore)
99

@@ -87,28 +87,92 @@ public struct EnvironmentValues: CustomStringConvertible {
8787
public init() {
8888
_plist = PropertyList()
8989
tracker = nil
90-
// TODO: CoreGlue.shared
90+
CoreGlue2.shared.configureDefaultEnvironment(&self)
9191
}
9292

93+
/// Creates an environment values instance with the specified property list.
94+
///
95+
/// - Parameter plist: The property list to initialize the environment values with.
9396
package init(_ plist: PropertyList) {
9497
_plist = plist
9598
tracker = nil
9699
}
97100

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.
98106
package init(_ plist: PropertyList, tracker: PropertyList.Tracker) {
99-
// FIXME
107+
tracker.initializeValues(from: plist)
100108
self._plist = plist
101109
self.tracker = tracker
102110
}
103111

104-
// FIXME
112+
/// The underlying property list that stores environment values.
105113
package var plist: PropertyList {
106-
get { _plist }
114+
get {
115+
_plist
116+
}
107117
set {
118+
guard _plist.id != newValue.id else {
119+
return
120+
}
121+
if let tracker {
122+
tracker.invalidateAllValues(from: _plist, to: newValue)
123+
}
108124
_plist = newValue
109125
}
110126
}
111127

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+
112176
/// Accesses the environment value associated with a custom key.
113177
///
114178
/// Create custom environment values by defining a key
@@ -148,17 +212,17 @@ public struct EnvironmentValues: CustomStringConvertible {
148212
}
149213
}
150214
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)
159216
}
160217
}
161218

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.
162226
package subscript<K>(key: K.Type) -> K.Value where K: DerivedEnvironmentKey {
163227
if let tracker {
164228
tracker.derivedValue(_plist, for: DerivedEnvironmentPropertyKey<K>.self)
@@ -175,12 +239,25 @@ public struct EnvironmentValues: CustomStringConvertible {
175239
@available(*, unavailable)
176240
extension EnvironmentValues: Sendable {}
177241

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+
}
180250
}
181251

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))
185262
}
186263
}

0 commit comments

Comments
 (0)