From f47727df52b1c2b36dec5df1c2bd10dc63e4c7d6 Mon Sep 17 00:00:00 2001 From: david-swift Date: Tue, 3 Feb 2026 00:06:52 +0100 Subject: [PATCH] Add wrap modifier --- Sources/View/SafeWrapper.swift | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sources/View/SafeWrapper.swift b/Sources/View/SafeWrapper.swift index 5f4ec05..a6e53b9 100644 --- a/Sources/View/SafeWrapper.swift +++ b/Sources/View/SafeWrapper.swift @@ -64,4 +64,28 @@ extension AnyView { 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) } + } + } + }