Fix state-during-view-update faults in text selection#67
Open
adamtheturtle wants to merge 1 commit into
Open
Conversation
With text selection enabled, SwiftUI logged non-fatal faults on layout passes: onChange(of: Layout) action tried to update multiple times per frame. onChange(of: AnyTextLayoutCollection) action tried to update multiple times per frame. Modifying state during view update, this will cause undefined behavior. `AppKitTextSelectionView` stored selection rectangles in `@State` and seeded them from two `onChange(initial: true)` handlers (`selectedRange` and `layout`), both writing the same state during the initial view update. Derive the rectangles as an `@Observable`-tracked computed property instead: no `@State`, no `onChange`, no state mutation during the update, while still recomputing whenever the selected range or layout changes. `TextSelectionModel.setLayoutCollection` re-wrote the observed `selectedRange` during reconciliation even when the reconciled value was unchanged; as the overlay's GeometryReader/preferences converge this runs several times per frame. Only assign when the value actually changes. Fixes gonzalezreal#22. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
4561138 to
d7e8e12
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Fixes #22.
With text selection enabled (
.textual.textSelection(.enabled)), SwiftUI logs non-fatal faults on layout passes. The issue reports the iOSAnyTextLayoutCollectionmessage; on macOS there's additionally anonChange(of: Layout)variant and — once the duplicate handler is removed — aModifying state during view updatefault. All stem from the selection layer writing observed/@Statevalues during a view update.Changes
AppKitTextSelectionViewstored selection rectangles in@Stateand seeded them from twoonChange(initial: true)handlers (one onselectedRange, one onlayout), both writing the same state during the initial view update — two writes in one frame (multiple times per frame), and a single surviving write still during the update (Modifying state during view update). This derives the rectangles as an@Observable-tracked computed property instead:No
@State, noonChange, no state mutation during the update. It still recomputes whenever the selected range orlayoutchanges (the read ofselectedRangeis tracked by Observation), so selection rendering is unchanged.TextSelectionModel.setLayoutCollectionre-wrote the observedselectedRangeduring reconciliation even when the reconciled value was unchanged. As the overlay'sGeometryReader/preferences converge this runs several times per frame, and the redundant write to the observed property is what trips theAnyTextLayoutCollectiondiagnostic. It now only assigns when the value actually changes.Testing
Textualtarget builds cleanly (xcodebuild -scheme Textual -destination 'platform=macOS'→ BUILD SUCCEEDED).StructuredText(markdown:).textual.textSelection(.enabled)): all three faults are gone from the console — both at launch and during interaction — with selection highlighting still rendering and reconciling correctly across layout/selection changes. The earlier handler-merge revision removed the two... multiple times per framefaults but surfacedModifying state during view update; the computed-property approach in this PR removes that last one as well.I didn't find an existing harness for exercising the SwiftUI selection layout in tests — happy to add coverage if you can point me at the preferred approach for these view-level interactions.