Repository navigation
Description
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):
TextHighlightRenderer.cppGetDefaultHighlighterBrushes/IterateMergedHighlighterslook up both theme brushes and use them wheneverm_foreground/m_backgroundis null.TextBlockrenders highlighters through that sameHWRenderCollectionpath (TextBlock.cpp#L3944).- Theme resources: generic.xaml Dark, HighContrast, Light.
How to reproduce it (as minimally and precisely as possible) 🔬
- 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);- Run it on any Skia head (verified on Skia Desktop, Windows).
- 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: passedWhen_TextBlock_Highlighter_Has_No_Background: failed. The red glyphs are drawn, butExpected '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:
- Add both resources to Uno's theme dictionaries, matching WinUI generic.xaml (Light/Dark/HighContrast).
- In
UnicodeText, when a highlighter's brush is null, fall back to those resources (asParsedTextdoes for the background) instead of null / black.
Related: #24684 (TextHighlighter brush type validation).
Activity