Skip to content

Fix the selection in lists, draw short texts into smaller tiles, and add a Writing Tools option - #29

Merged
servercimen merged 5 commits into
feature/lighter-viewsfrom
feature/view-fixes
Nov 23, 2026
Merged

servercimen merged 5 commits into
feature/lighter-viewsfrom
feature/view-fixes

Conversation

@servercimen

@servercimen servercimen commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Four changes for views that show short texts in lists and on pages. This PR is based on #28 (feature/lighter-views). Merge #28 first.

  • Clear the selection when the view leaves its window. A view with a selection that left its window kept its selection and stayed first responder. A selectable UITextView can raise "UIPreviewTarget requires that the container view is in a window" when its edit menu or drag preview outlives its window. Now the view clears its selection and resigns in willMove(toWindow:) when the new window is nil. A view leaves its window when a lazy stack recycles its row, or when a tab removes its pane. The crash was not reproduced with TranscriptTextView.
  • One selection at a time. When the person selected text in a second view, the first view kept its selection, its highlight and its handles. Now a view that becomes first responder ends the selection of the other views in the same window. 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. A text field or a UITextView that becomes first responder does not end the selection.
  • Smaller tiles for short texts. Each view drew its text into tiles 256 points high. A one-line title kept a whole tile. Now a view shorter than one tile gets one tile of 32, 64, 128 or 256 points. The tile covers the text, the shapes that reach below it, and glyphs that draw below the last line, such as marks stacked under a letter. To find how far the glyphs draw, a layout shorter than one tile measures the glyph bounds of its lines. Taller views keep full tiles and do not measure.
  • An option to hide Writing Tools. TranscriptText.showsWritingTools(_:) and TranscriptTextView.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.

Refs #10.

Cost on an iPhone 12 mini

The "cards" runs scroll through 200 cards, each with three short texts, at 3,000 points a second for 10 s. The "history" runs stream an answer of 6,000 characters at 300 characters a second, after 50 messages. Each value is the mean of 3 runs.

Measure Before After SwiftUI Text, before → after
Cards, memory peak 40 MB 21 MB 24 MB → 24 MB
Cards, main-thread CPU 3.38 s 4.01 s 2.95 s → 3.57 s
Cards, tile draws 531 532 –
History, main-thread CPU 1.84 s 2.06 s –
History, tile draws 68 70 –
Dropped frames 0 0 0

The runs before and after the change were in two sessions, and the phone was slower in the second one: SwiftUI Text also took 0.62 s more. The gap between TranscriptKit and Text in the cards runs is 0.43 s before and 0.44 s after.

The glyph measure was added later. In 3 more runs, the memory peak stays at 21 MB, the tile draws do not change, and no run dropped a frame. The main-thread CPU is 3.22 s to 3.93 s for the cards, against 3.37 s to 3.50 s for Text, and 2.08 s for the history. These values are within the spread between runs.

Tests

  • swift test: 178 tests pass, with 7 new tests for the tile heights and the glyph bounds. The package builds for iOS with no warnings. The unit tests do not cover the iOS view.
  • A check in a local demo app on the iPhone 12 mini:
    • A view that leaves its window has no selection and is not first responder.
    • When a second view takes first responder, the first view has no selection, no highlight and no handles.
    • A view 21 points high gets one 32-point tile. A view 69 points high gets one 128-point tile. A view 957 points high gets four 256-point tiles.
    • The Writing Tools option sets writingToolsBehavior to .none and back to .default.
  • Screenshots on the iPhone:
    • Texts with a pill tag at the start, in the middle and at the end of a line, and in a heading, draw in full in their small tiles.
    • An a with 5 dots under it and an a with 8 dots under it show all of their dots. Before the glyph measure, the tile cut both of them off after 4 dots.
  • By hand on the iPhone:
    • A second long-press ends the first selection.
    • Share and Translate keep the selection, and a sheet that closes on a nil selection stays open.
    • A tab switch with the edit menu open does not crash.
    • A row that scrolls far away ends its selection.
  • Not checked: Writing Tools on a device, because the iPhone 12 mini has none. Look Up in the edit menu.

🤖 Generated with Claude Code

servercimen and others added 5 commits November 23, 2026 09:28
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>
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>
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>
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>
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>
@servercimen
servercimen merged commit bbec0c8 into feature/lighter-views Nov 23, 2026
2 checks passed
servercimen added a commit that referenced this pull request Nov 23, 2026
#28)

* Set up text selection and scroll checks only when needed

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>

* Draw 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 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>

* Fix the selection in lists, draw short texts into smaller tiles, and 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>

---------

Co-authored-by: Server Cimen <505984+servercimen@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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