Skip to content

Commit

Permalink
Added Image.StretchDirection.
Browse files Browse the repository at this point in the history
But not yet implemented.
  • Loading branch information
grokys committed Dec 19, 2019
1 parent a0f72ff commit 062ec0c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Avalonia.Controls/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class Image : Control
public static readonly StyledProperty<Stretch> StretchProperty =
AvaloniaProperty.Register<Image, Stretch>(nameof(Stretch), Stretch.Uniform);

/// <summary>
/// Defines the <see cref="StretchDirection"/> property.
/// </summary>
public static readonly StyledProperty<StretchDirection> StretchDirectionProperty =
AvaloniaProperty.Register<Image, StretchDirection>(nameof(StretchDirection));

static Image()
{
AffectsRender<Image>(SourceProperty, StretchProperty);
Expand All @@ -43,10 +49,19 @@ public IBitmap Source
/// </summary>
public Stretch Stretch
{
get { return (Stretch)GetValue(StretchProperty); }
get { return GetValue(StretchProperty); }
set { SetValue(StretchProperty, value); }
}

/// <summary>
/// Gets or sets a value controlling in what direction the image will be stretched.
/// </summary>
public StretchDirection StretchDirection
{
get { return GetValue(StretchDirectionProperty); }
set { SetValue(StretchDirectionProperty, value); }
}

/// <summary>
/// Renders the control.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/Avalonia.Visuals/Media/StretchDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Avalonia.Media
{
/// <summary>
/// Describes the type of scaling that can be used when scaling content.
/// </summary>
public enum StretchDirection
{
/// <summary>
/// Only scales the content upwards when the content is smaller than the available space.
/// If the content is larger, no scaling downwards is done.
/// </summary>
UpOnly,

/// <summary>
/// Only scales the content downwards when the content is larger than the available space.
/// If the content is smaller, no scaling upwards is done.
/// </summary>
DownOnly,

/// <summary>
/// Always stretches to fit the available space according to the stretch mode.
/// </summary>
Both,
}
}
13 changes: 13 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public void Measure_Should_Return_Correct_Size_For_UniformToFill_Stretch()
Assert.Equal(new Size(50, 50), target.DesiredSize);
}

[Fact]
public void Measure_Should_Return_Correct_Size_With_StretchDirection_DownOnly()
{
var bitmap = CreateBitmap(50, 100);
var target = new Image();
target.StretchDirection = StretchDirection.DownOnly;
target.Source = bitmap;

target.Measure(new Size(150, 150));

Assert.Equal(new Size(50, 100), target.DesiredSize);
}

[Fact]
public void Measure_Should_Return_Correct_Size_For_Infinite_Height()
{
Expand Down

0 comments on commit 062ec0c

Please sign in to comment.