Browse Source

Added Image.StretchDirection.

But not yet implemented.
Steven Kirk 5 years ago
parent
commit
062ec0ccb6

+ 16 - 1
src/Avalonia.Controls/Image.cs

@@ -23,6 +23,12 @@ namespace Avalonia.Controls
         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);
@@ -43,10 +49,19 @@ namespace Avalonia.Controls
         /// </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>

+ 25 - 0
src/Avalonia.Visuals/Media/StretchDirection.cs

@@ -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 - 0
tests/Avalonia.Controls.UnitTests/ImageTests.cs

@@ -62,6 +62,19 @@ namespace Avalonia.Controls.UnitTests
             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()
         {