Browse Source

Don't round size in TextBlock when UseLayoutRounding is false (#18456)

* Don't round size when UselayoutRounding is false for TextBlock. Fixes #18423

* Added back size rounding

* Removed rounding again for the text size and fixed padding rounding in RenderCore
pull/18991/head
Johan Appelgren 1 year ago
committed by GitHub
parent
commit
c0bd5078f5
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 31
      src/Avalonia.Controls/TextBlock.cs
  2. 74
      tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

31
src/Avalonia.Controls/TextBlock.cs

@ -604,8 +604,13 @@ namespace Avalonia.Controls
context.FillRectangle(background, new Rect(Bounds.Size));
}
var scale = LayoutHelper.GetLayoutScale(this);
var padding = LayoutHelper.RoundLayoutThickness(Padding, scale);
var padding = Padding;
if (UseLayoutRounding)
{
var scale = LayoutHelper.GetLayoutScale(this);
padding = LayoutHelper.RoundLayoutThickness(padding, scale);
}
var top = padding.Top;
var textHeight = TextLayout.Height;
@ -708,8 +713,14 @@ namespace Avalonia.Controls
protected override Size MeasureOverride(Size availableSize)
{
var scale = LayoutHelper.GetLayoutScale(this);
var padding = LayoutHelper.RoundLayoutThickness(Padding, scale);
var padding = Padding;
if (UseLayoutRounding)
{
var scale = LayoutHelper.GetLayoutScale(this);
padding = LayoutHelper.RoundLayoutThickness(Padding, scale);
}
var deflatedSize = availableSize.Deflate(padding);
if (_constraint != deflatedSize)
@ -741,15 +752,17 @@ namespace Avalonia.Controls
var textLayout = TextLayout;
// The textWidth used here is matching that TextPresenter uses to measure the text.
var size = LayoutHelper.RoundLayoutSizeUp(new Size(textLayout.WidthIncludingTrailingWhitespace, textLayout.Height).Inflate(padding), 1);
return size;
return new Size(textLayout.WidthIncludingTrailingWhitespace, textLayout.Height).Inflate(padding);
}
protected override Size ArrangeOverride(Size finalSize)
{
var scale = LayoutHelper.GetLayoutScale(this);
var padding = LayoutHelper.RoundLayoutThickness(Padding, scale);
var padding = Padding;
if (UseLayoutRounding)
{
var scale = LayoutHelper.GetLayoutScale(this);
padding = LayoutHelper.RoundLayoutThickness(Padding, scale);
}
var availableSize = finalSize.Deflate(padding);

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

@ -458,6 +458,80 @@ namespace Avalonia.Controls.UnitTests
Assert.True(target.DesiredSize.Height > 0);
}
[Fact]
public void TextBlock_With_UseLayoutRounding_True_Should_Round_DesiredSize()
{
using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
var target = new TextBlock { Text = "1980" };
target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Assert.Equal(target.DesiredSize, new Size(40, 10));
}
[Fact]
public void TextBlock_With_UseLayoutRounding_True_Should_Round_Padding_And_DesiredSize()
{
using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
var target = new TextBlock { Text = "1980", Padding = new(2.25) };
target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Assert.Equal(target.DesiredSize, new Size(44, 14));
}
[Fact]
public void TextBlock_With_UseLayoutRounding_False_Should_Not_Round_DesiredSize()
{
using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
var target = new TextBlock { Text = "1980", UseLayoutRounding = false };
target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Assert.Equal(target.DesiredSize, new Size(40, 9.6));
}
[Fact]
public void TextBlock_With_UseLayoutRounding_False_Should_Not_Round_Bounds()
{
using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
var target = new TextBlock { Text = "1980", UseLayoutRounding = false };
target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
target.Arrange(new Rect(default, target.DesiredSize));
Assert.Equal(target.Bounds, new Rect(0, 0, 40, 9.6));
}
[Fact]
public void TextBlock_With_UseLayoutRounding_False_Should_Not_Round_Padding_In_MeasureOverride()
{
using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
var target = new TextBlock { Text = "1980", UseLayoutRounding = false, Padding = new(2.25) };
target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Assert.Equal(target.DesiredSize, new Size(44.5, 14.1));
}
[Fact]
public void TextBlock_With_UseLayoutRounding_False_Should_Not_Round_Padding_In_ArrangeOverride()
{
using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
var target = new TextBlock { Text = "1980", UseLayoutRounding = false, Padding = new(2.25) };
target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
target.Arrange(new Rect(default, target.DesiredSize));
Assert.Equal(target.Bounds, new Rect(0, 0, 44.5, 14.1));
}
private class TestTextBlock : TextBlock
{
public Size Constraint => _constraint;

Loading…
Cancel
Save