Skip to content

perf(textblock): Repaint a foreground change without a new layout - #25409

Open
MartinZikmund wants to merge 4 commits into
masterfrom
dev/mazi/textblock-foreground-repaint
Open

MartinZikmund wants to merge 4 commits into
masterfrom
dev/mazi/textblock-foreground-repaint

Conversation

@MartinZikmund

Copy link
Copy Markdown
Member

GitHub Issue: closes #25408

PR Type:

✨ Feature (performance)

What changed? 🚀

A Foreground change on a TextBlock or one of its runs (including Spans and Hyperlinks) now repaints the text instead of laying it out again. This covers changing the property and changing a brush's color in place. It matches WinUI, where a brush change only dirties render.

  • UnicodeText.TryUpdateForegrounds points the laid-out runs at the current inline brushes. Draw already reads those brushes on every paint.
  • TextBlock.OnForegroundChanged and Run.OnForegroundChanged try that update first. They fall back to the old measure invalidation when:
    • a measure is pending,
    • a custom layout is used,
    • the inlines no longer match the runs, or
    • an ending paragraph marker paints with the default foreground.

RichTextBlock is unchanged: its own Foreground already only repaints, and Run changes inside a RichTextBlock keep the full path.

Numbers. Win32 Skia, Release, 3 rounds plus a null copy. Workload: flip the color of one SolidColorBrush shared by 2,000 TextBlocks.

Per color change master this PR
UI-thread time 20.6 ms 2.2 ms
Allocations 14.8 MB 112 KB
Frames rendered 2 1

The synchronous part grows from 0.44 to 1.56 ms, because the brush update runs inline instead of only invalidating.

Motivating case: #25389. Its ListViewItemPresenter chrome sets the multi-select check glyph's Foreground on every selection change.

Validation (runtime, Skia desktop).

  • 4 new Given_TextBlock tests: property change, brush color change, inherited change across Run, Span and a locally-brushed Run, and Text plus Foreground changed in the same tick. The 3 no-relayout tests fail on master.
  • Suites run: Given_TextBlock, Given_RichTextBlock*, Given_TextBlock_TextDecorations, Given_TextBox, Given_Hyperlink*, Given_Button, Given_ComboBox. 650 of 653 tests pass and 1 is skipped. The 2 failures are pre-existing: When_ComboBox_MinWidth and When_IsTextSelectionEnabled_CRLF fail identically on master.

PR Checklist ✅

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vi68uWCYVXxhcX9m6JEwSC

MartinZikmund and others added 4 commits October 13, 2026 21:22
A Foreground change on a TextBlock or one of its runs, including a brush
color change, invalidated measure, so the text was shaped again. In WinUI
a brush change only repaints. The laid-out runs now take the new brushes
and the block is repainted; anything that no longer matches the layout
falls back to the full invalidation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vi68uWCYVXxhcX9m6JEwSC
The runs are only updated when no measure is pending, as a new layout
starts from inlines that may no longer match them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vi68uWCYVXxhcX9m6JEwSC
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 21s —— View job


Review of #25409

Verdict: looks good to merge. I found no blocking issues, so I left no inline comments.

Findings by severity: 0 blocking, 0 major, 0 minor.

I reviewed the diff by inspection only. I did not build or run anything.

What I checked

  • Fallback conditions. TryUpdateForegrounds only takes the fast path when the layout can't be affected. It falls back to the full path when:
    • a measure is pending (IsMeasureDirty),
    • a custom layout is in use,
    • the inlines no longer line up with the runs (the count check at both ends of the loop), or
    • an ending paragraph marker would paint with a different default foreground.
  • Partial update. The loop can overwrite some _runBreaks entries and then return false. That is harmless, because the caller invalidates the measure and the text is laid out again.
  • Stale _defaultForeground. The field is readonly. After TextBlock.Foreground is replaced and there is no paragraph marker, it keeps pointing at the old brush. The fast path still works. If a marker exists, the reference comparison sends it down the full path. Painting stays correct either way.
  • Run.GetOwningTextBlock. It walks up through Span, which also covers Hyperlink. Runs inside a RichTextBlock get null and keep the full path, as the description says.
  • Tests. The 4 new runtime tests cover the main paths, and 3 of them fail on master. That fits the red/fix/green expectation.

Cross-cutting notes

  • Docs. The docs checkbox is unchecked. That seems fine for a perf-only change with no API surface change.
  • Screenshot compare. The Screenshots Compare Test Run item is unchecked. Please check it once CI finishes, since the change touches text painting.
  • Code style. The code follows the repo conventions (braces, tabs, #nullable, short comments) and uses no event Action.

/// <returns>False when the inlines no longer match the runs, or an ending paragraph marker paints with the default foreground.</returns>
internal bool TryUpdateForegrounds(Inline[] inlines, Brush? defaultForeground)
{
if (!ReferenceEquals(defaultForeground, _defaultForeground) && _endingParagraphLayout is { MarkerText.Length: > 0 })
@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 239311 found UI Test snapshots differences: skia-linux-screenshots: 118, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 118 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • Buttons.png
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • ClipboardTests.png-dark
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • Focus_FocusVisual_Properties.png-dark
    • Focus_FocusVisual_Properties.png
    • DisplayInformation.png-dark
    • ElementLevelTheme.png-dark
    • ElementLevelTheme.png
    • DropDownButtonPage.png-dark
    • Examples.png
    • ImageBrushInList.png-dark
    • ImageBrushInList.png
  • skia-windows-screenshots: 2458 changed over 2488

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png-dark
    • AppBarButtonWithIconTest.png-dark
    • ApplicationViewMode.png
    • AppWindowClosing.png
    • AppWindowTitleBar Properties.png
    • Attributed_text_Supserscript.png
    • AutoBorderStretchwithrightmargin.png-dark
    • AutomationProperties_AutomationId.png-dark
    • AutoSizedPathCentered.png-dark
    • AutoSizedPathCentered.png
    • AutoSuggestBox_Keyboard.png-dark
    • AutoSuggestBox_Keyboard.png
    • BasicEnability.png-dark
    • BasicEnability.png
    • BindableDrawerLayout_ChangePane.png-dark
    • BindableDrawerLayout_ChangePane.png
    • BitmapImage_vs_SvgImageSource.png-dark
    • BitmapImage_vs_SvgImageSource.png
    • BorderVersusPanelClipping.png-dark
    • BorderVersusPanelClipping.png

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 239311 has failed on Uno.UI - CI.

@MartinZikmund MartinZikmund added the 7.1 PRs only. For issues, use the Release field. label Oct 14, 2026
MartinZikmund added a commit that referenced this pull request Nov 2, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 2, 2026
The implicit GridViewItem style is BasedOn DefaultGridViewItemStyle again,
as in WinUI, so default GridView items render through the ListViewItemPresenter
chrome and default to AllowDrop=True. The Fluent GridViewItemExpanded is removed.

Merge after #25403, #25404, #25406, #25409, #25411 and #25413.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 9, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This matches WinUI. In dxaml/xcp/core/text/TextBlock/TextBlock.cpp, a TextBlock_Foreground change only calls InvalidateRender, and an inline or inherited Foreground change does the same. The fallbacks look safe: a pending measure, a custom layout, a run-count mismatch and a marker painted with the default foreground all take the full path. One non-blocking perf note inline.

base.OnForegroundChanged();
InvalidateInlinesForFormatChange();

if (GetOwningTextBlock() is not { } textBlock || !textBlock.TryUpdateForegrounds())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Non-blocking. When the TextBlock's Foreground changes, every Run that inherits it lands here, and each TryUpdateForegrounds walks the whole leaf tree after the TextBlock's own callback already did one walk, so it's O(n^2) in the number of runs. Fine for typical text, but it adds up with large inline sets. Marking foregrounds stale and applying them once (from the TextBlock callback or before the next draw) would avoid it.

@mergify

mergify Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 14, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

7.1 PRs only. For issues, use the Release field.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextBlock Foreground change re-measures and re-shapes the text

4 participants