-
Notifications
You must be signed in to change notification settings - Fork 58
Add ViewGraphGeometryObservers API #695
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
+349
−10
Merged
Changes from all commits
Commits
Show all changes
3 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
254 changes: 254 additions & 0 deletions
254
Sources/OpenSwiftUICore/View/Graph/ViewGraphGeometryObservers.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,254 @@ | ||
| // | ||
| // ViewGraphGeometryObservers.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Complete | ||
| // ID: 4717DAAA68693648A460F26E88C7D804 (SwiftUICore) | ||
|
|
||
| // MARK: - ViewGraphGeometryObservers | ||
|
|
||
| /// A container that manages geometry observers for a view graph. | ||
| /// | ||
| /// `ViewGraphGeometryObservers` tracks size changes for different layout proposals | ||
| /// and notifies registered callbacks when sizes change. It uses a measurer conforming | ||
| /// to ``ViewGraphGeometryMeasurer`` to compute sizes. | ||
| /// | ||
| /// The observer maintains a state machine for each proposal that tracks: | ||
| /// - The current stable size (`.value`) | ||
| /// - A pending size transition (`.pending`) | ||
| /// - Uninitialized state (`.none` or `.invalid`) | ||
| package struct ViewGraphGeometryObservers<Measurer> where Measurer: ViewGraphGeometryMeasurer { | ||
| /// The proposal type used for layout measurements. | ||
| package typealias Proposal = Measurer.Proposal | ||
|
|
||
| /// The size type returned by measurements. | ||
| package typealias Size = Measurer.Size | ||
|
|
||
| /// A callback invoked when a size change is detected. | ||
| /// | ||
| /// - Parameters: | ||
| /// - oldSize: The previous size value. | ||
| /// - newSize: The new size value. | ||
| package typealias Callback = (Size, Size) -> Void | ||
|
|
||
| private var store: [Proposal: Observer] | ||
|
|
||
| /// Creates an empty geometry observers container. | ||
| init() { | ||
Kyle-Ye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| store = [:] | ||
| } | ||
|
|
||
| /// Checks if any observer needs an update based on the current view graph state. | ||
| /// | ||
| /// This method measures sizes for all registered proposals and transitions | ||
| /// their storage states accordingly. | ||
| /// | ||
| /// - Parameter graph: The view graph to measure against. | ||
| /// - Returns: `true` if any observer detected a size change, `false` otherwise. | ||
| package mutating func needsUpdate(graph: ViewGraph) -> Bool { | ||
| guard !graph.data.isHiddenForReuse else { | ||
| return false | ||
| } | ||
| var result = false | ||
| let keys = store.keys | ||
| for proposal in keys { | ||
| let size = Measurer.measure(given: proposal, in: graph) | ||
| let changed = store[proposal]!.storage.transition(to: size) | ||
| result = result || changed | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| /// Collects and returns all pending size notifications. | ||
| /// | ||
| /// For each observer with a pending size change, this method transitions | ||
| /// the storage to the new value and collects the size to notify. | ||
| /// | ||
| /// - Returns: A dictionary mapping proposals to their new sizes that need notification. | ||
| package mutating func notifySizes() -> [Proposal: Size] { | ||
| var result: [Proposal: Size] = [:] | ||
| let keys = store.keys | ||
| for proposal in keys { | ||
| if let size = store[proposal]!.sizeToNotifyIfNeeded() { | ||
| result[proposal] = size | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| /// Adds an observer for a specific layout proposal. | ||
| /// | ||
| /// - Parameters: | ||
| /// - proposal: The layout proposal to observe. | ||
| /// - exclusive: If `true`, removes all existing observers before adding. | ||
| /// Defaults to `true`. | ||
| /// - callback: The callback to invoke when the size changes. | ||
| package mutating func addObserver( | ||
| for proposal: Proposal, | ||
| exclusive: Bool = true, | ||
| callback: @escaping Callback | ||
| ) { | ||
| if exclusive { | ||
| removeAll() | ||
| } | ||
| store[proposal] = Observer(callback: callback) | ||
| } | ||
|
|
||
| /// Resets the observer for a specific proposal to its initial state. | ||
| /// | ||
| /// - Parameter proposal: The proposal whose observer should be reset. | ||
| /// - Returns: `true` if an observer existed and was reset, `false` otherwise. | ||
| @discardableResult | ||
| package mutating func resetObserver(for proposal: Proposal) -> Bool { | ||
| store[proposal]?.reset() ?? false | ||
| } | ||
|
|
||
| /// Stops observing a specific proposal. | ||
| /// | ||
| /// - Parameter proposal: The proposal to stop observing. | ||
| package mutating func stopObserving(proposal: Proposal) { | ||
| store[proposal] = nil | ||
| } | ||
|
|
||
| /// Removes all observers. | ||
| package mutating func removeAll() { | ||
| store.removeAll() | ||
| } | ||
|
|
||
| // MARK: - Observer | ||
|
|
||
| /// An individual geometry observer that tracks size changes for a proposal. | ||
| private struct Observer { | ||
| /// The current storage state tracking size transitions. | ||
| var storage: Storage | ||
|
|
||
| /// The callback to invoke when a size change is detected. | ||
| let callback: Callback | ||
|
|
||
| /// Creates an observer with the specified callback. | ||
| /// | ||
| /// The observer starts in the `.invalid` state. | ||
| /// | ||
| /// - Parameter callback: The callback to invoke on size changes. | ||
| init(callback: @escaping Callback) { | ||
| self.storage = .invalid | ||
| self.callback = callback | ||
| } | ||
|
|
||
| /// Returns the size to notify if there is a pending transition. | ||
| /// | ||
| /// If the storage is in the `.pending` state with a size change, | ||
| /// transitions to `.value` and returns the new size. | ||
| /// | ||
| /// - Returns: The new size to notify, or `nil` if no notification is needed. | ||
| mutating func sizeToNotifyIfNeeded() -> Size? { | ||
| guard case let .pending(size, pending) = storage else { | ||
| return nil | ||
| } | ||
| storage = .value(pending) | ||
| guard pending != size else { | ||
| return nil | ||
| } | ||
| return pending | ||
| } | ||
|
|
||
| /// Resets the observer to its initial `.invalid` state. | ||
| /// | ||
| /// - Returns: Always returns `true`. | ||
| mutating func reset() -> Bool { | ||
| storage = .invalid | ||
| return true | ||
| } | ||
|
|
||
| // MARK: - Storage | ||
|
|
||
| /// The state machine for tracking size transitions. | ||
| /// | ||
| /// The storage tracks the lifecycle of size measurements: | ||
| /// - `value`: A stable, committed size. | ||
| /// - `pending`: A size transition is in progress. | ||
| /// - `none`: Uninitialized state. | ||
| /// - `invalid`: Explicitly invalidated, needs fresh measurement. | ||
| enum Storage { | ||
| /// A stable size value. | ||
| case value(Size) | ||
| /// A pending transition from the first size to the second. | ||
| case pending(Size, pending: Size) | ||
| /// Uninitialized state. | ||
| case none | ||
| /// Invalidated state requiring fresh measurement. | ||
| case invalid | ||
|
|
||
| /// Transitions the storage to reflect a new measured size. | ||
| /// | ||
| /// The state machine logic: | ||
| /// - `.value(x)` where `x == size`: No change, returns `false`. | ||
| /// - `.value(x)` where `x != size`: Transitions to `.pending(x, pending: size)`, returns `true`. | ||
| /// - `.pending(v, _)` where `v == size`: Settles to `.value(size)`, returns `false`. | ||
| /// - `.pending(v, _)` where `v != size`: Updates pending to new size, returns `true`. | ||
| /// - `.none`: Transitions to `.pending(invalidValue, pending: size)`, returns `true`. | ||
| /// - `.invalid`: Transitions to `.value(size)`, returns `false`. | ||
| /// | ||
| /// - Parameter size: The new measured size. | ||
| /// - Returns: `true` if a change was detected that requires notification. | ||
| mutating func transition(to size: Size) -> Bool { | ||
| switch self { | ||
| case let .value(currentSize): | ||
| guard currentSize != size else { | ||
| return false | ||
| } | ||
| self = .pending(currentSize, pending: size) | ||
| return true | ||
| case let .pending(value, _): | ||
| guard size != value else { | ||
| self = .value(size) | ||
| return false | ||
| } | ||
| self = .pending(value, pending: size) | ||
| return true | ||
| case .none: | ||
| self = .pending(Measurer.invalidValue, pending: size) | ||
| return true | ||
| case .invalid: | ||
| self = .value(size) | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - ViewGraphGeometryMeasurer | ||
|
|
||
| /// A protocol that defines how to measure geometry in a view graph. | ||
| /// | ||
| /// Types conforming to `ViewGraphGeometryMeasurer` provide the measurement | ||
| /// logic used by ``ViewGraphGeometryObservers`` to track size changes. | ||
| package protocol ViewGraphGeometryMeasurer { | ||
| /// The type used to propose layout dimensions. | ||
| associatedtype Proposal: Hashable | ||
|
|
||
| /// The type representing the measured size. | ||
| associatedtype Size: Equatable | ||
|
|
||
| /// Measures the size for a given proposal in a view graph. | ||
| /// | ||
| /// - Parameters: | ||
| /// - proposal: The layout proposal to measure. | ||
| /// - graph: The view graph context for measurement. | ||
| /// - Returns: The measured size. | ||
| static func measure(given proposal: Proposal, in graph: ViewGraph) -> Size | ||
|
|
||
| /// Measures the size using a layout computer and insets. | ||
| /// | ||
| /// - Parameters: | ||
| /// - proposal: The layout proposal to measure. | ||
| /// - layoutComputer: The layout computer to use for measurement. | ||
| /// - insets: The edge insets to apply. | ||
| /// - Returns: The measured size. | ||
| static func measure(proposal: Proposal, layoutComputer: LayoutComputer, insets: EdgeInsets) -> Size | ||
|
|
||
| /// A sentinel value representing an invalid or uninitialized size. | ||
| static var invalidValue: Size { get } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
Tests/OpenSwiftUICoreTests/View/Graph/ViewGraphGeometryObserversTests.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,60 @@ | ||
| // | ||
| // ViewGraphGeometryObserversTests.swift | ||
| // OpenSwiftUICoreTests | ||
|
|
||
| @testable import OpenSwiftUICore | ||
| import Foundation | ||
| import Testing | ||
|
|
||
| private struct TestMeasurer: ViewGraphGeometryMeasurer { | ||
| typealias Proposal = CGSize | ||
| typealias Size = CGFloat | ||
|
|
||
| static func measure(given proposal: CGSize, in graph: ViewGraph) -> CGFloat { | ||
| max(proposal.width, proposal.height) | ||
| } | ||
|
|
||
| static func measure(proposal: CGSize, layoutComputer: LayoutComputer, insets: EdgeInsets) -> CGFloat { | ||
| max(proposal.width, proposal.height) | ||
| } | ||
|
|
||
| static var invalidValue: CGFloat = .nan | ||
| } | ||
|
|
||
| struct ViewGraphGeometryObserversTests { | ||
| fileprivate typealias Observers = ViewGraphGeometryObservers<TestMeasurer> | ||
|
|
||
| #if canImport(Darwin) | ||
| @MainActor | ||
| @Test | ||
| func observeCallback() async throws { | ||
| // TODO: when the callback got called. | ||
| await confirmation(expectedCount: 0) { confirm in | ||
| var observers = Observers() | ||
| observers.addObserver(for: CGSize(width: 10, height: 20)) { _, _ in | ||
| confirm() | ||
| } | ||
| let emptyViewGraph = ViewGraph(rootViewType: EmptyView.self) | ||
| _ = observers.needsUpdate(graph: emptyViewGraph) | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| @Test | ||
| func addObserverExclusiveRemovesExisting() { | ||
| var observers = Observers() | ||
| observers.addObserver(for: CGSize(width: 10, height: 20)) { _, _ in } | ||
| observers.addObserver(for: CGSize(width: 30, height: 40), exclusive: true) { _, _ in } | ||
| #expect(observers.resetObserver(for: CGSize(width: 10, height: 20)) == false) | ||
| #expect(observers.resetObserver(for: CGSize(width: 30, height: 40)) == true) | ||
| } | ||
|
|
||
| @Test | ||
| func addObserverNonExclusiveKeepsExisting() { | ||
| var observers = Observers() | ||
| observers.addObserver(for: CGSize(width: 10, height: 20)) { _, _ in } | ||
| observers.addObserver(for: CGSize(width: 30, height: 40), exclusive: false) { _, _ in } | ||
| #expect(observers.resetObserver(for: CGSize(width: 10, height: 20)) == true) | ||
| #expect(observers.resetObserver(for: CGSize(width: 30, height: 40)) == true) | ||
| } | ||
| } |
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.