-
Notifications
You must be signed in to change notification settings - Fork 70
Add View.onPreferenceChange support #937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
Example/OpenSwiftUIUITests/Modifier/ViewModifier/PreferenceActionModifierUITests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // | ||
| // PreferenceActionModifierUITests.swift | ||
| // OpenSwiftUIUITests | ||
|
|
||
| import Foundation | ||
| import Testing | ||
| @testable import TestingHost | ||
|
|
||
| private final class PreferenceActionRecorder: @unchecked Sendable { | ||
| private let lock = NSLock() | ||
| private var value: String? | ||
|
|
||
| func record(value: String) { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| self.value = value | ||
| } | ||
|
|
||
| func snapshot() -> String? { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| return value | ||
| } | ||
| } | ||
|
|
||
| private final class TransactionalPreferenceActionRecorder: @unchecked Sendable { | ||
| private let lock = NSLock() | ||
| private var value: String? | ||
| private var disablesAnimations = false | ||
|
|
||
| func record(value: String, disablesAnimations: Bool) { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| self.value = value | ||
| self.disablesAnimations = disablesAnimations | ||
| } | ||
|
|
||
| func snapshot() -> (value: String?, disablesAnimations: Bool) { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
| return (value, disablesAnimations) | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| struct PreferenceActionModifierUITests { | ||
| @Test | ||
| func onPreferenceChange() { | ||
| let recorder = PreferenceActionRecorder() | ||
| let controller = AnimationDebugController( | ||
| PreferenceActionModifierExample(action: { value in | ||
| recorder.record(value: value) | ||
| }) | ||
| ) | ||
| controller.advance(interval: .zero) | ||
| #expect(recorder.snapshot() == "changed") | ||
| withExtendedLifetime(controller) {} | ||
| } | ||
|
|
||
| @Test | ||
| @available(iOS 18.2, macOS 15.2, *) | ||
| func transactionalOnPreferenceChange() { | ||
| let recorder = TransactionalPreferenceActionRecorder() | ||
| let controller = AnimationDebugController( | ||
| TransactionalPreferenceActionExample(action: { value, transaction in | ||
| recorder.record( | ||
| value: value, | ||
| disablesAnimations: transaction.disablesAnimations | ||
| ) | ||
| }) | ||
| ) | ||
| controller.advance(interval: .zero) | ||
| let snapshot = recorder.snapshot() | ||
| #expect(snapshot.value == "changed") | ||
| #expect(snapshot.disablesAnimations) | ||
| withExtendedLifetime(controller) {} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
Example/Shared/ViewModifier/PreferenceActionModifierExample.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // | ||
| // PreferenceActionModifierExample.swift | ||
| // Shared | ||
|
|
||
| #if OPENSWIFTUI | ||
| import OpenSwiftUI | ||
| #else | ||
| import SwiftUI | ||
| #endif | ||
|
|
||
| struct PreferenceActionModifierExample: View { | ||
| private struct Key: PreferenceKey { | ||
| static let defaultValue = "" | ||
|
|
||
| static func reduce(value: inout String, nextValue: () -> String) { | ||
| value = nextValue() | ||
| } | ||
| } | ||
|
|
||
| var action: (String) -> Void | ||
|
|
||
| var body: some View { | ||
| Color.red | ||
| .preference(key: Key.self, value: "changed") | ||
| .onPreferenceChange(Key.self, perform: action) | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
Example/Shared/ViewModifier/TransactionalPreferenceActionModifierExample.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // | ||
| // TransactionalPreferenceActionModifierExample.swift | ||
| // Shared | ||
|
|
||
| #if OPENSWIFTUI | ||
| @_spi(Private) import OpenSwiftUI | ||
| #else | ||
| import SwiftUI_SPI | ||
| #endif | ||
|
|
||
| #if OPENSWIFTUI | ||
| @available(OpenSwiftUI_v8_0, *) | ||
| #else | ||
| @available(iOS 18.2, macOS 15.2, *) | ||
| #endif | ||
| struct TransactionalPreferenceActionExample: View { | ||
| private struct Key: PreferenceKey { | ||
| static let defaultValue = "" | ||
|
|
||
| static func reduce(value: inout String, nextValue: () -> String) { | ||
| value = nextValue() | ||
| } | ||
| } | ||
|
|
||
| var action: @Sendable (String, Transaction) -> Void | ||
|
|
||
| var body: some View { | ||
| Color.red | ||
| .preference(key: Key.self, value: "changed") | ||
| .onPreferenceChange(Key.self) { value, transaction in | ||
| action(value, transaction) | ||
| } | ||
| .transaction { transaction in | ||
| transaction.disablesAnimations = true | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Sources/OpenSwiftUI/Modifier/ViewModifier/PreferenceActionModifier.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // | ||
| // PreferenceActionModifier.swift | ||
| // OpenSwiftUI | ||
| // | ||
| // Audited for 8.0.66 | ||
| // Status: Complete | ||
|
|
||
| package import OpenSwiftUICore | ||
|
|
||
| @_spi(Private) | ||
| @available(OpenSwiftUI_v8_0, *) | ||
| extension View { | ||
| /// Adds an action to perform when the specified preference key's value | ||
| /// changes, including the transaction that produced the change. | ||
| nonisolated public func onPreferenceChange<K>( | ||
| _ key: K.Type = K.self, | ||
| action: @escaping @Sendable (K.Value, Transaction) -> Void | ||
| ) -> some View where K: PreferenceKey, K.Value: Equatable { | ||
| return modifier(TransactionalPreferenceActionModifier<K>(action: action)) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@available(iOS 18.2, macOS 15.2, *)makes this symbol effectively available on tvOS/watchOS/visionOS at all versions; consider adding explicit availability for the other Apple platforms (or marking them unavailable) to avoid cross-platform build mismatches. (Also applies to the similar availability usage in the transactional example/test in this PR.)Severity: medium
Other Locations
Example/Shared/ViewModifier/TransactionalPreferenceActionModifierExample.swift:14Example/OpenSwiftUIUITests/Modifier/ViewModifier/PreferenceActionModifierUITests.swift:50🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.