From 33b1bfb8d28bcf17248a08a7c192106f65db2aa3 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Sat, 27 Jan 2024 10:24:23 +0100 Subject: [PATCH] Fix Inlines clear handling (#14247) * Make sure to clean up VisualChildren and created text runs when the TextLayout is invalidate * Remove localized comment --------- Co-authored-by: Tim <47110241+timunie@users.noreply.github.com> #Conflicts: # src/Avalonia.Controls/TextBlock.cs --- src/Avalonia.Controls/TextBlock.cs | 11 +++++++---- .../TextBlockTests.cs | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 817d6cf9e9..ca5339086e 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -671,9 +671,12 @@ namespace Avalonia.Controls { _textLayout?.Dispose(); _textLayout = null; + + VisualChildren.Clear(); - InvalidateVisual(); + _textRuns = null; + InvalidateVisual(); InvalidateMeasure(); } @@ -690,8 +693,6 @@ namespace Avalonia.Controls if (HasComplexContent) { - VisualChildren.Clear(); - var textRuns = new List(); foreach (var inline in inlines!) @@ -875,7 +876,9 @@ namespace Avalonia.Controls } } - private readonly struct InlinesTextSource : ITextSource +#pragma warning disable CA1815 + protected readonly struct InlinesTextSource : ITextSource +#pragma warning restore CA1815 { private readonly IReadOnlyList _textRuns; diff --git a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs index c0afe0c7ff..8191013dad 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs @@ -224,5 +224,21 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(underline, target.Inlines[0].TextDecorations); } } + + [Fact] + public void TextBlock_TextLines_Should_Be_Empty() + { + using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) + { + var textblock = new TextBlock(); + textblock.Inlines?.Add(new Run("123")); + textblock.Measure(new Size(200, 200)); + int count = textblock.TextLayout.TextLines[0].TextRuns.Count; + textblock.Inlines?.Clear(); + textblock.Measure(new Size(200, 200)); + int count1 = textblock.TextLayout.TextLines[0].TextRuns.Count; + Assert.NotEqual(count, count1); + } + } } }