Skip to content

Commit 0f02ed7

Browse files
authored
Merge pull request #22189 from ramezgerges/textblock_selection_showing
fix(textblock): selection should only show when focused
2 parents a7050dd + 710b8b6 commit 0f02ed7

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBlock.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,35 @@ public async Task When_IsTextSelectionEnabled_TouchScroll_Then_DoesNotAlterSelec
17491749
}
17501750
Assert.AreEqual(selectedText, sut.SelectedText);
17511751
}
1752+
1753+
#if HAS_UNO
1754+
[TestMethod]
1755+
public async Task When_Focus_Changes_Selection_Is_Not_Shown()
1756+
{
1757+
if (!ApiInformation.IsTypePresent("Microsoft.UI.Xaml.Media.Imaging.RenderTargetBitmap, Uno.UI"))
1758+
{
1759+
Assert.Inconclusive("RenderTargetBitmap is not supported on this platform");
1760+
}
1761+
1762+
var SUT = new TextBlock { Text = "Some text", IsTextSelectionEnabled = true };
1763+
var focusBtn = new Button();
1764+
await UITestHelper.Load(new StackPanel { Children = { SUT, focusBtn } });
1765+
1766+
SUT.Focus(FocusState.Programmatic);
1767+
SUT.SelectAll();
1768+
await UITestHelper.WaitForIdle();
1769+
await UITestHelper.WaitForRender();
1770+
var screenshot = await UITestHelper.ScreenShot(SUT);
1771+
ImageAssert.HasColorInRectangle(screenshot, new Rectangle(0, 0, screenshot.Width, screenshot.Height), ((SolidColorBrush)Uno.UI.Xaml.Media.DefaultBrushes.SelectionHighlightColor).Color);
1772+
1773+
focusBtn.Focus(FocusState.Programmatic);
1774+
await UITestHelper.WaitForIdle();
1775+
await UITestHelper.WaitForRender();
1776+
var screenshot2 = await UITestHelper.ScreenShot(SUT);
1777+
ImageAssert.DoesNotHaveColorInRectangle(screenshot2, new Rectangle(0, 0, screenshot2.Width, screenshot2.Height), ((SolidColorBrush)Uno.UI.Xaml.Media.DefaultBrushes.SelectionHighlightColor).Color);
1778+
}
1779+
#endif
1780+
17521781
#endregion
17531782
#endif
17541783

src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.skia.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ partial void OnIsTextSelectionEnabledChangedPartial()
119119
UpdateSelectionRendering();
120120
}
121121

122-
private void UpdateSelectionRendering() => RenderSelection = IsTextSelectionEnabled && (IsFocused || (_contextMenu?.IsOpen ?? false));
122+
private void UpdateSelectionRendering()
123+
{
124+
if (!IsTextBoxDisplay) // TextBox managed RenderSelection itself
125+
{
126+
RenderSelection = IsTextSelectionEnabled && (IsFocused || (_contextMenu?.IsOpen ?? false));
127+
}
128+
}
123129

124130
protected override Size ArrangeOverride(Size finalSize)
125131
{
@@ -173,10 +179,11 @@ internal void Draw(in Visual.PaintingSession session)
173179
{
174180
session.Canvas.Save();
175181
session.Canvas.Translate((float)Padding.Left, (float)Padding.Top);
182+
var selection = (Math.Min(Selection.start, Selection.end), Math.Max(Selection.start, Selection.end), SelectionHighlightColor.GetOrCreateCompositionBrush(Compositor.GetSharedCompositor()), DefaultBrushes.SelectedTextForegroundColor);
176183
ParsedText.Draw(
177184
session,
178-
(_caretPaint is { } c ? (c.index, c.brush, CaretThickness) : null),
179-
(Math.Min(Selection.start, Selection.end), Math.Max(Selection.start, Selection.end), SelectionHighlightColor.GetOrCreateCompositionBrush(Compositor.GetSharedCompositor()), DefaultBrushes.SelectedTextForegroundColor));
185+
_caretPaint is { } c ? (c.index, c.brush, CaretThickness) : null,
186+
_renderSelection ? selection : null);
180187
session.Canvas.Restore();
181188
DrawingFinished?.Invoke();
182189
}

0 commit comments

Comments
 (0)