Skip to content

Add View.onPreferenceChange#926

Open
Dark-Existed wants to merge 1 commit into
OpenSwiftUIProject:mainfrom
Dark-Existed:feature/on_preference_change
Open

Add View.onPreferenceChange#926
Dark-Existed wants to merge 1 commit into
OpenSwiftUIProject:mainfrom
Dark-Existed:feature/on_preference_change

Conversation

@Dark-Existed

Copy link
Copy Markdown
Contributor

Implement #925

@Dark-Existed Dark-Existed requested a review from Kyle-Ye as a code owner July 2, 2026 13:46
@augmentcode

augmentcode Bot commented Jul 2, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: This PR implements View.onPreferenceChange (per #925) so views can react to preference value updates.

Changes:

  • Introduces onPreferenceChange(_:perform:) on View and a new internal _PreferenceActionModifier to observe a specific PreferenceKey.
  • Adds a stateful AttributeGraph binder (PreferenceBinder) that tracks the last observed value, resets across phase resets, and enqueues the user action when the preference changes.
  • Uses an UpdateCycleDetector to reduce the risk of feedback loops when an action triggers further updates.
  • Adjusts preference-writing scene/view modifiers to avoid mutating the original inputs while still removing the key from child inputs, ensuring writers are created when the ancestor requests the key.
  • Adds an example demonstrating .preference + .onPreferenceChange usage.

Technical Notes: Preference change delivery is implemented as a transactional graph attribute that schedules the callback via Update.enqueueAction, mirroring existing “action binder” patterns used elsewhere in the codebase.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestion posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

/// - Returns: A view that triggers `action` when the value for `key`
/// changes.
@inlinable
nonisolated public func onPreferenceChange<K>(

@augmentcode augmentcode Bot Jul 2, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onPreferenceChange is being added as a public API, but it doesn’t appear to carry the @available(OpenSwiftUI_v1_0, *) availability annotation pattern used by other preference APIs (e.g. preference, transformPreference). Consider aligning the availability annotations to keep the public surface/versioning consistent.

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

@Dark-Existed Dark-Existed force-pushed the feature/on_preference_change branch from 15de999 to 9bbefad Compare July 2, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant