From 20813facb3f446547b4b4b817bb799fbd7c0da69 Mon Sep 17 00:00:00 2001 From: adirh Date: Thu, 2 Jan 2025 23:38:37 +0200 Subject: [PATCH 1/3] Fixed TextBlock disappearing when inline used and measure is invalidated --- src/Avalonia.Controls/SelectableTextBlock.cs | 2 ++ src/Avalonia.Controls/TextBlock.cs | 27 ++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Controls/SelectableTextBlock.cs b/src/Avalonia.Controls/SelectableTextBlock.cs index b0fd78b321e..cedb358fb30 100644 --- a/src/Avalonia.Controls/SelectableTextBlock.cs +++ b/src/Avalonia.Controls/SelectableTextBlock.cs @@ -213,6 +213,8 @@ protected override TextLayout CreateTextLayout(string? text) }; } + UpdateTextRuns(); + ITextSource textSource; if (_textRuns != null) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index a8324b2a5a2..26c4e6a7717 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -665,6 +665,8 @@ protected virtual TextLayout CreateTextLayout(string? text) { LineSpacing = LineSpacing }; + + UpdateTextRuns(); ITextSource textSource; @@ -722,10 +724,22 @@ protected override Size MeasureOverride(Size availableSize) //Force arrange so text will be properly alligned. InvalidateArrange(); } - + + UpdateTextRuns(); + + //This implicitly recreated the TextLayout with a new constraint if we previously reset it. + var textLayout = TextLayout; + + var size = LayoutHelper.RoundLayoutSizeUp(new Size(textLayout.MinTextWidth, textLayout.Height).Inflate(padding), 1, 1); + + return size; + } + + protected void UpdateTextRuns() + { var inlines = Inlines; - if (HasComplexContent) + if (HasComplexContent && _textRuns == null) { var textRuns = new List(); @@ -736,13 +750,6 @@ protected override Size MeasureOverride(Size availableSize) _textRuns = textRuns; } - - //This implicitly recreated the TextLayout with a new constraint if we previously reset it. - var textLayout = TextLayout; - - var size = LayoutHelper.RoundLayoutSizeUp(new Size(textLayout.MinTextWidth, textLayout.Height).Inflate(padding), 1, 1); - - return size; } protected override Size ArrangeOverride(Size finalSize) @@ -882,7 +889,7 @@ private void OnInlinesChanged(InlineCollection? oldValue, InlineCollection? newV void IInlineHost.Invalidate() { - InvalidateMeasure(); + InvalidateTextLayout(); } IAvaloniaList IInlineHost.VisualChildren => VisualChildren; From 4e48dcaf60368fe42871d3c2ee69e8ba4a35e338 Mon Sep 17 00:00:00 2001 From: adirh Date: Thu, 2 Jan 2025 23:55:03 +0200 Subject: [PATCH 2/3] Reverted unnecessary changed --- src/Avalonia.Controls/TextBlock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 26c4e6a7717..449c1fa9abb 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -889,7 +889,7 @@ private void OnInlinesChanged(InlineCollection? oldValue, InlineCollection? newV void IInlineHost.Invalidate() { - InvalidateTextLayout(); + InvalidateMeasure(); } IAvaloniaList IInlineHost.VisualChildren => VisualChildren; From 7e3af81b18a4a9c98369c4260ce9709dfd922b93 Mon Sep 17 00:00:00 2001 From: adirh Date: Sat, 18 Jan 2025 10:56:11 +0200 Subject: [PATCH 3/3] Fixed update layout to be consistent as before this change --- src/Avalonia.Controls/TextBlock.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 449c1fa9abb..2d85e17f8ed 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -725,7 +725,7 @@ protected override Size MeasureOverride(Size availableSize) InvalidateArrange(); } - UpdateTextRuns(); + UpdateTextRuns(true); //This implicitly recreated the TextLayout with a new constraint if we previously reset it. var textLayout = TextLayout; @@ -735,11 +735,11 @@ protected override Size MeasureOverride(Size availableSize) return size; } - protected void UpdateTextRuns() + protected void UpdateTextRuns(bool force = false) { var inlines = Inlines; - if (HasComplexContent && _textRuns == null) + if (HasComplexContent && (force || _textRuns == null)) { var textRuns = new List();