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/14787/head
Benedikt Stebner 2 years ago
committed by Max Katz
parent
commit
760c6749d0
  1. 13
      src/Avalonia.Controls/TextBlock.cs
  2. 26
      tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

13
src/Avalonia.Controls/TextBlock.cs

@ -677,7 +677,6 @@ namespace Avalonia.Controls
{
_textLayout?.Dispose();
_textLayout = null;
_textRuns = null;
base.OnMeasureInvalidated();
@ -690,6 +689,10 @@ namespace Avalonia.Controls
_constraint = availableSize.Deflate(padding);
//Reset TextLayout otherwise constraint might be outdated.
_textLayout?.Dispose();
_textLayout = null;
var inlines = Inlines;
if (HasComplexContent)
@ -819,15 +822,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