Browse Source

Merge pull request #2262 from AvaloniaUI/fixes/2260-image-sizing

Fix image sizing
pull/2313/head
Steven Kirk 7 years ago
committed by GitHub
parent
commit
f7ddcd3709
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/Avalonia.Controls/Image.cs
  2. 56
      tests/Avalonia.Controls.UnitTests/ImageTests.cs

17
src/Avalonia.Controls/Image.cs

@ -99,5 +99,22 @@ namespace Avalonia.Controls
return new Size();
}
}
/// <inheritdoc/>
protected override Size ArrangeOverride(Size finalSize)
{
var source = Source;
if (source != null)
{
var sourceSize = new Size(source.PixelSize.Width, source.PixelSize.Height);
var result = Stretch.CalculateSize(finalSize, sourceSize);
return result;
}
else
{
return new Size();
}
}
}
}

56
tests/Avalonia.Controls.UnitTests/ImageTests.cs

@ -61,5 +61,61 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(new Size(50, 50), target.DesiredSize);
}
[Fact]
public void Arrange_Should_Return_Correct_Size_For_No_Stretch()
{
var bitmap = Mock.Of<IBitmap>(x => x.PixelSize == new PixelSize(50, 100));
var target = new Image();
target.Stretch = Stretch.None;
target.Source = bitmap;
target.Measure(new Size(50, 50));
target.Arrange(new Rect(0, 0, 100, 400));
Assert.Equal(new Size(50, 100), target.Bounds.Size);
}
[Fact]
public void Arrange_Should_Return_Correct_Size_For_Fill_Stretch()
{
var bitmap = Mock.Of<IBitmap>(x => x.PixelSize == new PixelSize(50, 100));
var target = new Image();
target.Stretch = Stretch.Fill;
target.Source = bitmap;
target.Measure(new Size(50, 50));
target.Arrange(new Rect(0, 0, 25, 100));
Assert.Equal(new Size(25, 100), target.Bounds.Size);
}
[Fact]
public void Arrange_Should_Return_Correct_Size_For_Uniform_Stretch()
{
var bitmap = Mock.Of<IBitmap>(x => x.PixelSize == new PixelSize(50, 100));
var target = new Image();
target.Stretch = Stretch.Uniform;
target.Source = bitmap;
target.Measure(new Size(50, 50));
target.Arrange(new Rect(0, 0, 25, 100));
Assert.Equal(new Size(25, 50), target.Bounds.Size);
}
[Fact]
public void Arrange_Should_Return_Correct_Size_For_UniformToFill_Stretch()
{
var bitmap = Mock.Of<IBitmap>(x => x.PixelSize == new PixelSize(50, 100));
var target = new Image();
target.Stretch = Stretch.UniformToFill;
target.Source = bitmap;
target.Measure(new Size(50, 50));
target.Arrange(new Rect(0, 0, 25, 100));
Assert.Equal(new Size(25, 100), target.Bounds.Size);
}
}
}

Loading…
Cancel
Save