Skip to content

Scroll code blocks and wide display formulas sideways - #37

Merged
servercimen merged 9 commits into
feature/bold-text-contrastfrom
feature/code-scroll
Nov 24, 2026
Merged

servercimen merged 9 commits into
feature/bold-text-contrastfrom
feature/code-scroll

Conversation

@servercimen

Copy link
Copy Markdown
Owner

Part 7 of a stack of 13 pull requests. The base is #36 (feature/bold-text-contrast). #31 (part 1) tells how to merge the stack.

On this stacked branch, swift test passes (287 tests), and the iOS build and build-for-testing pass.

Summary

Model output often has code lines and display formulas that are wider than the screen. Before this change, code wrapped, and wide formulas scaled down to fit. To keep the lines whole, an app had to put the text view in a horizontal scroll view, which then drew every tile of the block. Now a block can scroll sideways inside TranscriptTextView. The block keeps the width of the column, and only its text moves. All new options default to the current behavior. Tables do not change.

  • Add TranscriptStyle.codeWraps (default true). When it is false, a line of code breaks only at a line break, and a wide block scrolls sideways.
  • Add TranscriptStyle.displayMathScrolls (default false). When it is true, a wide display formula keeps its size and scrolls sideways, and its LaTeX shows whole, not cut with "…". Inline formulas still scale down to fit.
  • Add TranscriptStyle.displayMathAlignment (default .center): the leading edge, the center or the trailing edge. A formula wider than the column starts at the leading edge.
  • Draw the lines of a block at its offset, clipped to the box of the block. Caret rects, closest positions, links and selection rects follow the offset. The public geometry methods of DocumentLayout use no offset.
  • Add a sideways pan in TranscriptTextView. It takes only touches in a block that can scroll, and only sideways drags in a direction that the block can move. Scroll views around the view wait for it to fail. After the pan, the block slows down as a scroll view does. A touch stops it and opens no link.
  • Keep the offset of a block while it streams in, and while the new document has a block with the same id and kind. A new document clears the offsets. No new text fades in while the block moves.
  • With VoiceOver, a three-finger swipe moves the block.
  • Log the offset when a pan ends and when the block stops (SideScroll category, debug level).
  • Add the -wideSample and -sideScroll launch arguments to the demo app, and document the options in the README.

Tests

  • swift test on macOS: 206 tests pass. 27 new tests in SideScrollTests.swift cover lines that do not wrap, streamed blocks, display math, offset limits, carets, hit tests, selection, links, drawing pixel by pixel, the tail, fades, kept offsets, the pan decision and the slow-down.
  • iOS: a local test app builds with this branch for an iPhone (Release), with no warnings in the package sources.

Checked on an iPhone 12 mini

iOS 27.0, in a check screen in a local test app. The check ran on main first. Main showed each problem, except in the first item, which looks for a change and finds none.

  • Default style: 0 different pixels against main in 3 screenshot pairs.
  • Options on: main wraps the long code line onto 4 lines. The branch keeps it on 1 line, in the same block box.
  • Formulas: main cuts the wide LaTeX with "…", centers the short one, and scales the drawn formula down (86 px tall). The branch shows all of the LaTeX, puts both formulas at the leading edge, and draws the formula at full size (131 px tall).
  • Drags in a UIKit and a SwiftUI scroll view: the branch moves only the block, and the host moves 0 pt, also for a diagonal drag. On main, nothing scrolls sideways, and the host follows a diagonal drag by 30 pt. A fling stops at the end (726.0 of 726.0 pt). A vertical drag on the block scrolls the host and keeps the offset.
  • Selection at an offset of 274 pt: the highlight and the handles move with the glyphs, and Copy gives the selected text.
  • The accessibility scroll: main returns false. The branch moves 274 pt for each step.
  • Streaming while a block is scrolled: in 3 films of about 590 frames, 0 frames are torn or flash.

Stand-ins: drags and flings are touches that the check app injects, through hit testing and the gesture recognizers. The VoiceOver swipe is a call to accessibilityScroll(_:). The films are snapshots of the view on each frame, not a screen recording.

Not checked

  • Drags, flings and the stop tap with a real finger.
  • A stop tap on a link: the code block in the check has no link.
  • A real three-finger swipe with VoiceOver on.

Known limits

  • While a line that does not wrap streams in, each update typesets that whole line again. On a Mac, a 30,000-character line that streams in 60-character pieces costs about 1 ms for each update on the layout queue, 6 ms at most.
  • A touch that stops a moving block opens no link, but a long press can still start a selection.
  • The block stops at its ends, with no bounce.

🤖 Generated with Claude Code

servercimen and others added 8 commits November 23, 2026 17:09
TranscriptStyle gets three options. All defaults keep the current
behavior.

- codeWraps: when it is false, a line of code breaks only at a line
  break in its text.
- displayMathAlignment: puts a paragraph that holds only display math
  at the leading edge, the center or the trailing edge.
- displayMathScrolls: when it is true, a display formula that is wider
  than the column keeps its size instead of a scale-down.

BlockLayout records the right edge of its widest line and how far its
lines can move left. A streamed block keeps that value for the lines
that it does not typeset again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SideScroll keeps an offset for each block that scrolls sideways, by
block id. A block that grows keeps its id, so it keeps its offset. Each
use cuts the offset to what the block can scroll.

DocumentLayout draws the lines of such a block moved left by its offset
and clips them to the box of the block. The code background, list
markers and quote bars do not move. Caret rects, closest offsets, links
and selection rects use the same offset. A caret or a selection rect
outside the box moves to the edge of the box. The public methods use no
offset, so the text past the edge stays out of reach.

The tail and the text that fades in are drawn with the offset that the
view gives the layout engine. Each output records that offset, so the
view can find a tail that it drew before the latest scroll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the style lets blocks scroll, TranscriptTextView adds a pan
gesture recognizer. The pan starts only when all of these are true:

- The touch is in the box of a block whose lines are wider than the box.
- The drag goes more sideways than up or down.
- The block can still move in that direction.
- The touch is not on a selection handle.

The scroll views around the view wait for this pan to fail. Thus a
sideways drag in the block moves only the block, and a vertical drag
still scrolls the host. After the pan, the block slows down at the
normal deceleration rate of a scroll view. A touch on the block stops
it.

Each step draws the band of the block in the tiles again. If the tail
showed the block, the tiles take that part back. Caret rects, selection
rects, closest positions and link taps use the offsets. A view that is
not selectable still takes touches in a block that scrolls. A new
document clears the offsets. A document that grows keeps them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
At the debug level, the view logs the offset and the maximum offset of
the block when a pan ends and when the block stops after it. The log
category is SideScroll. It is shown in the Xcode console, and in Console
when debug messages are on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -wideSample, the intro and the streamed answer end with a code
block that has a long line and a wide display formula. With
-sideScroll, the demo sets codeWraps to false, displayMathScrolls to
true and displayMathAlignment to leading. Without -sideScroll, the same
text shows the default behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A line of code that does not wrap breaks only at a line break, however
  long it is.
- The tail renderer draws a block again when its offset changed. A pan
  hides an old tail and does not wait for the layout queue. Only the
  blocks in the tail count when the view compares offsets.
- No text fades in while a block that text streams into moves sideways.
  Fading text that reaches into a block that moves shows at rest.
- A block keeps its offset while the new document has a block with that
  id and the same kind. A new document clears all offsets.
- The sideways pan gets only the touches in blocks that scroll, so other
  drags do not wait for it. It checks the point where the touch went
  down. A touch that stops a moving block opens no link.
- Tag shapes and tag images in a block that scrolls are not cut. A tag
  animation in such a block does not play.
- Only the glyphs near the box are drawn for a line wider than the box.
- A pan that ends too slowly to go on logs "Stopped".
- With VoiceOver, a three-finger swipe scrolls the block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DocumentLayout.draw: the function takes highContrast before hiding, and scroll last.
DocumentLayout.draw, drawLine: the glyphs get both the visible range and highContrast.
DocumentLayout.draw: the image loop stays in the scrolled box, and its images get the quote text color.
DocumentLayout.drawGlyphs: the function takes both visible and highContrast, and keeps the doc of visible.
FadeRenderer.render: the function takes scroll, and a change of highContrast draws the pieces again.
TailRenderer: the draw call gives both highContrast and scroll.
TranscriptTextView: the draw call gives both highContrast and the side scroll.
TranscriptTextView.style: the setter calls styleChanged() and then updateSidePan().
README: the display math options come before the formula cache text.
README: the LaTeX of a scrolled formula shows when there is no renderer or the renderer returns nil.
SideScrollTests: the draw calls give highContrast: false.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen added this pull request to stack #44 November 24, 2026 20:57
@servercimen
servercimen removed this pull request from stack #44 November 24, 2026 21:01
@servercimen
servercimen added this pull request to stack #45 November 24, 2026 21:01
@servercimen
servercimen merged commit 4c9b1b8 into main Nov 24, 2026
1 of 3 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