Skip to content

Derive Content in Body so Missed .appear Events Cannot Blank the Text - #84

Open
PSchmiedmayer wants to merge 3 commits into
gonzalezreal:mainfrom
PSchmiedmayer:derive-content-in-body
Open

PSchmiedmayer wants to merge 3 commits into
gonzalezreal:mainfrom
PSchmiedmayer:derive-content-in-body

Conversation

@PSchmiedmayer

@PSchmiedmayer PSchmiedmayer commented Aug 20, 2026 •

Copy link
Copy Markdown

StructuredText, InlineText, and TextFragment seed their content through onChange(of:initial:) into @State.

SwiftUI doesn't guarantee appear-family events (onAppear, onChange(initial:), task) for every materialized view; a row created lazily inside a scroll container while content updates quickly in can miss them entirely. When that happens the view keeps rendering its empty initial state: body runs with the right markup, layout happens, but nothing is drawn and nothing reaches accessibility. We encountered when parsing streaming MD data updating frequently from LLMs.

The PR purposes to replace the event-driven seeding with a small memoizing cache held in @State: content is derived directly in body and parsed once per distinct markup value. Should not change any rendering behavior, if anything, first render now shows content one pass earlier, since it no longer waits for the initial onChange round trip. Attachment resolution still runs through task(id:) as before.

Two follow-ups from review, both about the caches reusing a result they should not:

  • A parser with the same markup but a different configuration (base URL, options, syntax extensions) got the previous parse. MarkupParser now has a cacheKey (defaulting to the parser's type) that AttributedStringMarkdownParser fills from its settings, and MarkupParseCache compares it alongside the markup.
  • A TextBuilder was kept across environment changes, so a colour scheme or dynamic type change only showed once something changed size. BuilderCache now compares the TextEnvironmentValues too.

Both come with tests.

Copilot AI lite review requested due to automatic review settings August 20, 2026 20:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@PSchmiedmayer PSchmiedmayer changed the title Derive content in body so missed appear events cannot blank the text Derive Content in Body so Missed .appear Events Cannot Blank the Text Aug 20, 2026
@PSchmiedmayer

Copy link
Copy Markdown
Author

@gonzalezreal happy for any feedback or input here; let me know wha you think 🚀

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new memoization introduces verified stale-rendering cases (parser changes not invalidating parse cache; environment changes not invalidating TextBuilder cache).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Lite

y-a-v-a added a commit to y-a-v-a/textual that referenced this pull request Oct 22, 2026
StructuredText, InlineText, WithInlineStyle and TextFragment seeded their
content from onChange(initial:), so every view first laid out empty. A List
or a UIHostingConfiguration cell measures a row on that pass, and the row
then changes height under it; a view that a lazy container materializes can
miss the event altogether and stay blank. Each of them now derives its
content in body through a Memo, as gonzalezreal#84 and gonzalezreal#90 propose.

Adapted to the fork's streaming throttle: the markup is parsed in body only
until the first change has been parsed, and later changes still go through
the throttle, parsing inside the throttled operation so that the window
keeps excluding the parse. The TextBuilder is keyed on the text environment
as well as the content (gonzalezreal#84), and starts from the last container size, since
the container may not resize again to report it when only the environment
changes.

In the renderer benchmarks this halves the TextBuilders a document builds
once its content lands, and initial render with selection enabled is faster.

Co-authored-by: Paul Schmiedmayer <PSchmiedmayer@users.noreply.github.com>
Co-authored-by: Shai Mishali <shai@riverside.fm>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y7W6e4M3bCGaGpJzVY8c39
y-a-v-a added a commit to y-a-v-a/textual that referenced this pull request Oct 22, 2026
Upstream main has had no new commits since 0.5.0 (2026-06-15), which the fork
already contains. The activity since then is in open pull requests; the fixes
among them are merged here, cherry-picked with their authorship and adapted
where they overlap with the fork:

- gonzalezreal#67, gonzalezreal#70, gonzalezreal#71, gonzalezreal#80: selection faults and crashes
- gonzalezreal#92: TextBuilder stack overflow on iOS
- gonzalezreal#83: caller attributes kept on code blocks
- gonzalezreal#86: stale content after markup changes, keeping
  loaded attachments while streamed content resolves
- gonzalezreal#91: attachment accessibility labels
- gonzalezreal#84, gonzalezreal#90: content on the first layout pass, so List
  rows keep their height and ImageRenderer draws text

Also fixes a fork bug the gonzalezreal#80 tests exposed (the selection model ignored a
change to an empty layout collection) and two timing-dependent tests.

Feature pull requests (gonzalezreal#69, gonzalezreal#78, gonzalezreal#79, gonzalezreal#81, gonzalezreal#82, gonzalezreal#88) are left out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y7W6e4M3bCGaGpJzVY8c39
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.

2 participants