From 2ecfbb978337ef7937821717c01421cdf60cd504 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> --- src/Avalonia.Controls/TextBlock.cs | 11 ++++++----- .../TextBlockTests.cs | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index cbda1dfbe6..01d67582d5 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -672,9 +672,12 @@ namespace Avalonia.Controls { _textLayout?.Dispose(); _textLayout = null; + + VisualChildren.Clear(); - InvalidateVisual(); + _textRuns = null; + InvalidateVisual(); InvalidateMeasure(); } @@ -691,8 +694,6 @@ namespace Avalonia.Controls if (HasComplexContent) { - VisualChildren.Clear(); - var textRuns = new List(); foreach (var inline in inlines!) @@ -876,9 +877,9 @@ namespace Avalonia.Controls } } -#pragma warning disable CA1815 // Equals und Gleichheitsoperator für Werttypen außer Kraft setzen +#pragma warning disable CA1815 protected readonly struct InlinesTextSource : ITextSource -#pragma warning restore CA1815 // Equals und Gleichheitsoperator für Werttypen außer Kraft setzen +#pragma warning restore CA1815 { private readonly IReadOnlyList _textRuns; private readonly IReadOnlyList>? _textModifier; 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); + } + } } }