Skip to content

TextBlock TextHighlighter with no Background paints no highlight; WinUI falls back to the yellow TextControlHighlighterBackground #24747

Open

Description

@MartinZikmund

Current behavior 🐛

On Skia, a TextHighlighter added to a TextBlock with only Foreground set (no Background) paints no highlight background. You see the recolored glyphs and nothing behind them.

RichTextBlock handles the same case correctly (it falls back to yellow), so only TextBlock is affected.

When Foreground is left unset, TextBlock also falls back to a hard-coded black brush instead of the TextControlHighlighterForeground theme resource. That's the right color in Light/Dark, but wrong in High Contrast.

Expected behavior 🎯

Same as WinUI: when Background (or Foreground) is null, the highlighter falls back to the theme resources TextControlHighlighterBackground (#FFFFFF00 yellow in Light and Dark, SystemControlHyperlinkTextBrush in High Contrast) and TextControlHighlighterForeground.

This is based on the WinUI sources (not a native WinUI run):

How to reproduce it (as minimally and precisely as possible) 🔬

  1. Put the following on a page (or paste the runtime test below into Uno.UI.RuntimeTests):
var tb = new TextBlock
{
	FontSize = 24,
	Text = "AAAAAAAAAA BBBBBBBBBB",
};
var highlighter = new TextHighlighter { Foreground = new SolidColorBrush(Colors.Red) };
highlighter.Ranges.Add(new TextRange { StartIndex = 0, Length = 10 });
tb.TextHighlighters.Add(highlighter);
  1. Run it on any Skia head (verified on Skia Desktop, Windows).
  2. On Uno, the first word turns red with no background. On WinUI, it's red on a yellow background.

Runtime test (the TextBlock case fails on master; the RichTextBlock case passes):

[TestClass]
[RunsOnUIThread]
public class Given_TextBlock_Highlighter_Default
{
	[TestMethod]
	public async Task When_TextBlock_Highlighter_Has_No_Background()
	{
		var SUT = new TextBlock
		{
			Width = 400,
			FontSize = 24,
			Text = "AAAAAAAAAA BBBBBBBBBB",
			Foreground = new SolidColorBrush(Colors.Black),
		};
		var highlighter = new TextHighlighter { Foreground = new SolidColorBrush(Colors.Red) };
		highlighter.Ranges.Add(new TextRange { StartIndex = 0, Length = 10 });
		SUT.TextHighlighters.Add(highlighter);

		await AssertDefaultYellow(SUT);
	}

	[TestMethod]
	public async Task When_RichTextBlock_Highlighter_Has_No_Background()
	{
		var SUT = new RichTextBlock { Width = 400, FontSize = 24, Foreground = new SolidColorBrush(Colors.Black) };
		var paragraph = new Paragraph();
		paragraph.Inlines.Add(new Run { Text = "AAAAAAAAAA BBBBBBBBBB" });
		SUT.Blocks.Add(paragraph);
		var highlighter = new TextHighlighter { Foreground = new SolidColorBrush(Colors.Red) };
		highlighter.Ranges.Add(new TextRange { StartIndex = 0, Length = 10 });
		SUT.TextHighlighters.Add(highlighter);

		await AssertDefaultYellow(SUT);
	}

	private static async Task AssertDefaultYellow(FrameworkElement SUT)
	{
		try
		{
			await UITestHelper.Load(SUT);
			var screenshot = await UITestHelper.ScreenShot(SUT);
			var leftQuarter = new System.Drawing.Rectangle(0, 0, screenshot.Width / 4, screenshot.Height);
			var rightQuarter = new System.Drawing.Rectangle(screenshot.Width * 3 / 4, 0, screenshot.Width / 4, screenshot.Height);

			ImageAssert.HasColorInRectangle(screenshot, leftQuarter, Colors.Red, tolerance: 20);
			ImageAssert.HasColorInRectangle(screenshot, leftQuarter, Colors.Yellow, tolerance: 5);
			ImageAssert.DoesNotHaveColorInRectangle(screenshot, rightQuarter, Colors.Yellow, tolerance: 5);
		}
		finally
		{
			WindowHelper.WindowContent = null;
		}
	}
}

Result on master (ffcff329eb3), Skia Desktop:

  • When_RichTextBlock_Highlighter_Has_No_Background: passed
  • When_TextBlock_Highlighter_Has_No_Background: failed. The red glyphs are drawn, but Expected 'FFFFFF00' in rectangle '{X=0,Y=0,Width=100,Height=32}', but no pixel has this color.

Workaround 🛠️

Always set both Foreground and Background on every TextHighlighter.

Renderer 🎨

  • Skia
  • Native

Affected platforms 📱💻🖥️

All Skia targets (Desktop, WebAssembly, Android, iOS). TextBlock renders through the shared UnicodeText path.

Uno.Sdk version (and other relevant versions) 📦

master at ffcff329eb3120c88cebf214629ca8a810f7880c (7.0-dev), net11.0-desktop.

IDE version 🧑‍💻

N/A (reproduced with a runtime test)

Anything else we need to know? 💬

Root cause: UnicodeText.cs#L975-L976:

(highlighter.Background?.GetOrCreateCompositionBrush(Compositor.GetSharedCompositor()),
	highlighter.Foreground ?? _blackBrush));

A null Background stays null, so the background pass skips it at L1087. A null Foreground falls back to the hard-coded _blackBrush.

Also, Uno doesn't define TextControlHighlighterForeground / TextControlHighlighterBackground in its theme resources at all. RichTextBlock (ParsedText.cs#L1229-L1233) looks the brush up and falls back to a hard-coded Colors.Yellow. The ported TextHighlightRenderer.GetDefaultHighlighterBrushes asserts that both resources exist.

Suggested fix:

  1. Add both resources to Uno's theme dictionaries, matching WinUI generic.xaml (Light/Dark/HighContrast).
  2. In UnicodeText, when a highlighter's brush is null, fall back to those resources (as ParsedText does for the background) instead of null / black.

Related: #24684 (TextHighlighter brush type validation).

Activity

  1. added
    kind/bug
    triage/untriaged
    area/skia ✏️
    area/theming
    project/text 🔤
    platform/all
    difficulty/medium 🤔
    area/winui
    on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/skia ✏️area/skia/text ✏️area/themingarea/winuidifficulty/medium 🤔kind/bugplatform/allproject/text 🔤triage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions