Browse Source
Measure/Arrange TextPresenter with WidthIncludingTrailingWhitespace (#21067)
pull/21083/head
Benedikt Stebner
6 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with
54 additions and
2 deletions
-
src/Avalonia.Controls/Presenters/TextPresenter.cs
-
tests/Avalonia.Controls.UnitTests/Presenters/TextPresenter_Tests.cs
-
tests/Avalonia.Controls.UnitTests/TextBlockTests.cs
|
|
|
@ -647,7 +647,7 @@ namespace Avalonia.Controls.Presenters |
|
|
|
InvalidateArrange(); |
|
|
|
|
|
|
|
// The textWidth used here is matching that TextBlock uses to measure the text.
|
|
|
|
var textWidth = TextLayout.OverhangLeading + TextLayout.WidthIncludingTrailingWhitespace + TextLayout.OverhangTrailing; |
|
|
|
var textWidth = TextLayout.WidthIncludingTrailingWhitespace; |
|
|
|
return new Size(textWidth, TextLayout.Height); |
|
|
|
} |
|
|
|
|
|
|
|
@ -655,7 +655,7 @@ namespace Avalonia.Controls.Presenters |
|
|
|
{ |
|
|
|
var finalWidth = finalSize.Width; |
|
|
|
|
|
|
|
var textWidth = TextLayout.OverhangLeading + TextLayout.WidthIncludingTrailingWhitespace + TextLayout.OverhangTrailing; |
|
|
|
var textWidth = TextLayout.WidthIncludingTrailingWhitespace; |
|
|
|
textWidth = Math.Ceiling(textWidth); |
|
|
|
|
|
|
|
if (finalSize.Width < textWidth) |
|
|
|
|
|
|
|
@ -75,5 +75,30 @@ namespace Avalonia.Controls.UnitTests.Presenters |
|
|
|
Assert.Equal(fontStretch, presenter.TextLayout.TextLines[0].TextRuns[0].Properties!.Typeface.Stretch); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Measure_And_Arrange_Should_Use_WidthIncludingTrailingWhitespace_For_Bounds() |
|
|
|
{ |
|
|
|
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) |
|
|
|
{ |
|
|
|
var presenter = new TextPresenter |
|
|
|
{ |
|
|
|
Text = "fy", |
|
|
|
FontStyle = FontStyle.Italic, |
|
|
|
FontSize = 48, |
|
|
|
UseLayoutRounding = false |
|
|
|
}; |
|
|
|
|
|
|
|
presenter.Measure(Size.Infinity); |
|
|
|
|
|
|
|
var expectedSize = new Size(presenter.TextLayout.WidthIncludingTrailingWhitespace, presenter.TextLayout.Height); |
|
|
|
|
|
|
|
Assert.Equal(expectedSize, presenter.DesiredSize); |
|
|
|
|
|
|
|
presenter.Arrange(new Rect(default, presenter.DesiredSize)); |
|
|
|
|
|
|
|
Assert.Equal(new Rect(default, expectedSize), presenter.Bounds); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -561,6 +561,33 @@ namespace Avalonia.Controls.UnitTests |
|
|
|
Assert.Equal(new Rect(0, 0, 32.45454545454545, 19.022727272727273), target.Bounds); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Measure_And_Arrange_Should_Use_WidthIncludingTrailingWhitespace_For_Bounds() |
|
|
|
{ |
|
|
|
using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface); |
|
|
|
|
|
|
|
var target = new TextBlock |
|
|
|
{ |
|
|
|
Text = "fy", |
|
|
|
FontStyle = FontStyle.Italic, |
|
|
|
FontSize = 48, |
|
|
|
UseLayoutRounding = false, |
|
|
|
Padding = new Thickness(3, 2, 5, 4) |
|
|
|
}; |
|
|
|
|
|
|
|
target.Measure(Size.Infinity); |
|
|
|
|
|
|
|
var expectedSize = |
|
|
|
new Size(target.TextLayout.WidthIncludingTrailingWhitespace, target.TextLayout.Height) |
|
|
|
.Inflate(target.Padding); |
|
|
|
|
|
|
|
Assert.Equal(expectedSize, target.DesiredSize); |
|
|
|
|
|
|
|
target.Arrange(new Rect(default, target.DesiredSize)); |
|
|
|
|
|
|
|
Assert.Equal(new Rect(default, expectedSize), target.Bounds); |
|
|
|
} |
|
|
|
|
|
|
|
private class TestTextBlock : TextBlock |
|
|
|
{ |
|
|
|
public Size Constraint => _constraint; |
|
|
|
|