Browse Source

Recreate TextLayout on measure to make sure constraint is updated (#14705)

* Recreate TextLayout on measure to make sure constraint is updated

* Add failing test
pull/14714/head
Benedikt Stebner 2 years ago
committed by GitHub
parent
commit
e35e94902e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 13
      src/Avalonia.Controls/TextBlock.cs
  2. 26
      tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

13
src/Avalonia.Controls/TextBlock.cs

@ -694,7 +694,6 @@ namespace Avalonia.Controls
{
_textLayout?.Dispose();
_textLayout = null;
_textRuns = null;
base.OnMeasureInvalidated();
@ -707,6 +706,10 @@ namespace Avalonia.Controls
_constraint = availableSize.Deflate(padding);
//Reset TextLayout otherwise constraint might be outdated.
_textLayout?.Dispose();
_textLayout = null;
var inlines = Inlines;
if (HasComplexContent)
@ -837,15 +840,19 @@ namespace Avalonia.Controls
{
oldValue.LogicalChildren = null;
oldValue.InlineHost = null;
oldValue.Invalidated -= (s, e) => InvalidateMeasure();
oldValue.Invalidated -= Invalidated;
}
if (newValue is not null)
{
newValue.LogicalChildren = LogicalChildren;
newValue.InlineHost = this;
newValue.Invalidated += (s, e) => InvalidateMeasure();
newValue.Invalidated += Invalidated;
}
return;
void Invalidated(object? sender, EventArgs e) => InvalidateMeasure();
}
void IInlineHost.Invalidate()

26
tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

@ -30,6 +30,27 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(null, textBlock.Text);
}
[Fact]
public void Calling_Measure_Should_Update_Constraint_And_TextLayout()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
{
var textBlock = new TestTextBlock { Text = "Hello World" };
textBlock.Measure(new Size(100, 100));
var textLayout = textBlock.TextLayout;
Assert.Equal(new Size(100,100), textBlock.Constraint);
textBlock.Measure(new Size(50, 100));
Assert.Equal(new Size(50, 100), textBlock.Constraint);
Assert.NotEqual(textLayout, textBlock.TextLayout);
}
}
[Fact]
public void Changing_InlinesCollection_Should_Invalidate_Measure()
{
@ -328,5 +349,10 @@ namespace Avalonia.Controls.UnitTests
Assert.NotEqual(count, count1);
}
}
private class TestTextBlock : TextBlock
{
public Size Constraint => _constraint;
}
}
}

Loading…
Cancel
Save