From 31497d60b11c50d3419bf1a79a18a1b86daec279 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Thu, 28 Nov 2024 16:34:00 +0100 Subject: [PATCH 1/3] Add failing test for TextBlock --- .../TextBlockTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs index 3d7c6e6c1bd..71fb12a8b30 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs @@ -413,6 +413,24 @@ public void TextBlock_TextLines_Should_Be_Empty() } } + [Fact] + public void TextBlock_With_Infinite_Size_Should_Be_Remeasured_After_TextLayout_Created() + { + using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface); + + var target = new TextBlock { Text = "" }; + var layout = target.TextLayout; + + Assert.Equal(0.0, layout.MaxWidth); + Assert.Equal(0.0, layout.MaxHeight); + + target.Text = "foo"; + target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); + + Assert.True(target.DesiredSize.Width > 0); + Assert.True(target.DesiredSize.Height > 0); + } + private class TestTextBlock : TextBlock { public Size Constraint => _constraint; From 5c3492afd9dc65233b3a0fc8e503270be3550df3 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Thu, 28 Nov 2024 16:35:36 +0100 Subject: [PATCH 2/3] Fix TextBlock re-measure in infinite container --- src/Avalonia.Controls/SelectableTextBlock.cs | 6 ++++-- src/Avalonia.Controls/TextBlock.cs | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/SelectableTextBlock.cs b/src/Avalonia.Controls/SelectableTextBlock.cs index 8f1248027c5..b0fd78b321e 100644 --- a/src/Avalonia.Controls/SelectableTextBlock.cs +++ b/src/Avalonia.Controls/SelectableTextBlock.cs @@ -224,12 +224,14 @@ protected override TextLayout CreateTextLayout(string? text) textSource = new FormattedTextSource(text ?? "", defaultProperties, textStyleOverrides); } + var maxSize = GetMaxSizeFromConstraint(); + return new TextLayout( textSource, paragraphProperties, TextTrimming, - _constraint.Width, - _constraint.Height, + maxSize.Width, + maxSize.Height, MaxLines); } diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 0afafe44b87..f6a0cdbdfdf 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -162,7 +162,7 @@ public class TextBlock : Control, IInlineHost nameof(Inlines), t => t.Inlines, (t, v) => t.Inlines = v); private TextLayout? _textLayout; - protected Size _constraint = Size.Infinity; + protected Size _constraint = new(double.NaN, double.NaN); protected IReadOnlyList? _textRuns; private InlineCollection? _inlines; @@ -366,6 +366,13 @@ public InlineCollection? Inlines internal bool HasComplexContent => Inlines != null && Inlines.Count > 0; + private protected Size GetMaxSizeFromConstraint() + { + var maxWidth = double.IsNaN(_constraint.Width) ? 0.0 : _constraint.Width; + var maxHeight = double.IsNaN(_constraint.Height) ? 0.0 : _constraint.Height; + return new Size(maxWidth, maxHeight); + } + /// /// The BaselineOffset property provides an adjustment to baseline offset /// @@ -670,12 +677,14 @@ protected virtual TextLayout CreateTextLayout(string? text) textSource = new SimpleTextSource(text ?? "", defaultProperties); } + var maxSize = GetMaxSizeFromConstraint(); + return new TextLayout( textSource, paragraphProperties, TextTrimming, - _constraint.Width, - _constraint.Height, + maxSize.Width, + maxSize.Height, MaxLines); } From 429a53ec4c06475516ee346e636940a1c1b655fc Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Thu, 28 Nov 2024 17:19:16 +0100 Subject: [PATCH 3/3] Fix outdated test --- tests/Avalonia.Controls.UnitTests/TextBlockTests.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs index 71fb12a8b30..4a7281fae3a 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.Controls.Documents; using Avalonia.Controls.Templates; using Avalonia.Data; @@ -33,7 +34,9 @@ public void Calling_Measure_Should_Update_TextLayout() { var textBlock = new TestTextBlock { Text = "Hello World" }; - Assert.Equal(Size.Infinity, textBlock.Constraint); + var constraint = textBlock.Constraint; + Assert.True(double.IsNaN(constraint.Width)); + Assert.True(double.IsNaN(constraint.Height)); textBlock.Measure(new Size(100, 100));