Skip to content

Set up text selection on demand, and draw rows that a lazy stack moves - #28

Merged
servercimen merged 3 commits into
mainfrom
feature/lighter-views
Nov 23, 2026
Merged

servercimen merged 3 commits into
mainfrom
feature/lighter-views

Conversation

@servercimen

Copy link
Copy Markdown
Owner

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 a UITextInteraction, and each view looked up its scroll view twice on every frame of a scroll.

  • Text selection on demand. 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 first long-press still selects. A view that is never touched never sets it up.
  • Cheaper scroll checks. A view looks up its scroll view once while it stays where it is. A view whose tiles are already released does not look at them again on every frame.
  • Fix: rows that a lazy stack moves. 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 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 on main.

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.

Comparison Runs Before After SwiftUI Text in the same runs
Text interaction set up when the view is made → on demand 5 each 3.75 s 3.31 s –
The same 3 each 3.66 s 3.23 s 2.68 s
Content size watcher off → on 3 each 3.27 s 3.32 s 2.63 s

No 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.

Text still 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

  • The package builds for macOS and iOS. The unit tests do not cover the iOS view.
  • On the iPhone 12 mini, the repros of a local demo app pass. With selection on, a touch adds the text interaction. With selection off, it is never added. Select all when shown still works.
  • A long-press on the iPhone selects text on the first try, also right after a scroll.
  • The blank band after a push showed in 2 of 2 tries on main. With this change, the screen is drawn in full in 3 of 3 tries.

🤖 Generated with Claude Code

servercimen and others added 2 commits November 20, 2026 15:33
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>
@servercimen
servercimen merged commit 7e7e365 into main Nov 23, 2026
0 of 2 checks passed
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