Add wrap modifier
This commit is contained in:
parent
2cab78dedc
commit
f47727df52
@ -64,4 +64,28 @@ extension AnyView {
|
|||||||
wrap { storage, _, updateProperties in modify(storage, updateProperties) }
|
wrap { storage, _, updateProperties in modify(storage, updateProperties) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A wrapper for generic simple modifiers.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - properties: The properties will be stored. Do not change the layout throughout updates.
|
||||||
|
/// - update: If properties change, run this function.
|
||||||
|
/// - Returns: A view.
|
||||||
|
public func wrapModifier(properties: [any Hashable], update: @escaping (ViewStorage) -> Void) -> AnyView {
|
||||||
|
wrap { storage, _, updateProperties in
|
||||||
|
guard updateProperties else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var shouldUpdate = false
|
||||||
|
for (index, property) in properties.enumerated() {
|
||||||
|
let update = {
|
||||||
|
shouldUpdate = true
|
||||||
|
storage.fields[index.description] = property
|
||||||
|
}
|
||||||
|
if let equatable = storage.fields[index.description] as? any Hashable {
|
||||||
|
if property.hashValue != equatable.hashValue { update() }
|
||||||
|
} else { update() }
|
||||||
|
}
|
||||||
|
if shouldUpdate { update(storage) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user