Repository navigation
Set up text selection on demand, and draw rows that a lazy stack moves - #28
Merged
Merged
Conversation
A list of cards can show hundreds of short texts. A profile of 200 cards scrolling on an iPhone 12 mini showed that most of a text view's extra main-thread time was not its text. It was setting up `UITextInteraction` and its gesture recognizers, freeing it again, and, for every frame of the scroll, looking up the enclosing scroll view. - A view adds its `UITextInteraction` the first time a touch reaches it, or when the app selects text. Hit testing comes before a touch begins, and the touch's gesture recognizers are gathered after it, so the new ones see that touch. A view that is never touched never sets the interaction up. - A view looks up its scroll view once while it stays where it is, not twice for every frame of a scroll. - A view whose tiles are already released does not look at them again on every frame. Refs #10. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After a push, a tall `TranscriptText` in a `LazyVStack` could show a blank band at its top until the list was scrolled or the text touched. The lazy stack measured the rows above with their real heights and moved the row. The row's own frame and the scroll offset stayed the same, so nothing told the view, and it kept the tiles for where it was before. A watcher now observes each scroll view's content size, which changes when the stack moves rows, once for all the text views in it. When it changes, a view that has some but not all of its tiles updates them on the next turn of the run loop, once the rows are placed. A view whose tiles cover all of it is drawn right wherever it moves. A view without tiles updates when it is scrolled or laid out. The bug was already on `main`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…add a Writing Tools option (#29) * Clear the selection when the view leaves its window A text view in a lazy stack, or in a tab that is not selected, can leave its window while some of its text is selected. A selectable `UITextView` can then raise "UIPreviewTarget requires that the container view is in a window" when its edit menu or drag preview refers to it. Apps avoid this by clearing the selection of such a view when it leaves its window. `TranscriptTextView` now does the same. When it leaves its window, it clears its selection and resigns first responder. The crash was not reproduced with `TranscriptTextView`. This change guards against the same failure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Draw a short text into a smaller tile Each view drew its text into tiles 256 points high, whatever its height. A one-line title kept a whole tile, which at 3x on a phone is about 3 MB, ten times what its text needs. A list of cards with three short texts each used more memory than SwiftUI `Text` for this reason. A view shorter than one tile now has one tile that covers only the view and the shapes that reach below its last line. Its height is 32, 64, 128 or 256 points, so text that grows resizes it at most three times. In a taller view, every tile stays 256 points high, so a long answer that streams in does not resize each new tile as it grows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add an option to hide Writing Tools in the edit menu On devices that have Writing Tools, the edit menu can offer them for the selected text. A `UITextView` that shows text which is not the user's often turns them off. `TranscriptTextView.showsWritingTools` and `TranscriptText.showsWritingTools(_:)` turn them off. They are on by default, as in a `UITextView`. On iOS 18 and later, the view reports the choice through `writingToolsBehavior`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * End the selection when another view takes first responder When the person selected text in a second `TranscriptTextView`, the first view kept its selection, its highlight and its handles. So two views showed a selection at the same time. Now a view that becomes first responder posts a notification, and the other views in the same window end their selection. The view does not end its selection in `resignFirstResponder()`: Share and Translate in the edit menu make the view resign. A sheet that closes on a nil selection then closed together with the system sheet. Checked on an iPhone: a second selection ends the first one, and Share and Translate keep the selection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Size a short text's tile to cover its glyphs A view shorter than one tile got a tile that covered its height and the shapes of its tags. Some glyphs draw further down: marks stacked under a letter, and some letters at large sizes. The tile cut them off. On main, the 256-point tile showed them. Now a layout shorter than one tile measures the lowest point of its glyphs, and the tile covers it. A taller layout does not measure. On an iPhone 12 mini, an `a` with eight dots under it shows all of them. In a list of 200 cards, the memory peak stays at 21 MB, and the main-thread CPU stays within the spread between runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Server Cimen <505984+servercimen@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
A list of cards can show hundreds of short texts (#10). On an iPhone 12 mini, a Time Profiler trace of 200 cards that scroll showed that TranscriptKit lays out and draws them in less main-thread time than SwiftUI
Text. The extra time was view overhead. Each view set up and later freed aUITextInteraction, and each view looked up its scroll view twice on every frame of a scroll.UITextInteractionthe first time a touch reaches it, or when the app selects text. Hit testing comes before a touch begins, and the touch's gesture recognizers are gathered after it, so the first long-press still selects. A view that is never touched never sets it up.TranscriptTextin aLazyVStackcould show a blank band at its top until the list was scrolled or the text touched. The lazy stack measured the rows above and moved the row, but the row's frame and the scroll offset stayed the same, so nothing told the view. Now one watcher for each scroll view observes the content size. When it changes, a view that has some but not all of its tiles updates them on the next turn of the run loop. The bug was already onmain.Refs #10.
Cost on an iPhone 12 mini
Main-thread CPU while 200 cards, each with three short texts, scroll at 3,000 points a second for 10 s. Each pair was measured in alternating runs with one build.
Textin the same runsNo run dropped a frame. Streaming did not change: with one view for each message, an answer of 6,000 characters after 50 messages takes 2.04 s of main-thread CPU, and scrolling through the 50 messages takes 2.26 s.
Textstill takes about 0.6 s less. Most of that is the cost of a UIKit view in SwiftUI: the hosting, and the view's subviews and layers.Tests
main. With this change, the screen is drawn in full in 3 of 3 tries.🤖 Generated with Claude Code