Browse Source

Port CornerRadiusFilterConverter from WinUI (#5861)

* Port CornerRadiusFilterConverter from WinUI

* Simplify CornerRadiusFilterConverter
Max Katz 4 years ago
parent
commit
09f5a6ebe6

+ 44 - 0
src/Avalonia.Controls/Converters/CornerRadiusFilterConverter.cs

@@ -0,0 +1,44 @@
+#nullable enable
+using System;
+using System.Globalization;
+
+using Avalonia.Data.Converters;
+
+namespace Avalonia.Controls.Converters
+{
+    /// <summary>
+    /// Converts an existing CornerRadius struct to a new CornerRadius struct,
+    /// with filters applied to extract only the specified fields, leaving the others set to 0.
+    /// </summary>
+    public class CornerRadiusFilterConverter : IValueConverter
+    {
+        /// <summary>
+        /// Gets or sets the type of the filter applied to the <see cref="CornerRadiusFilterConverter"/>.
+        /// </summary>
+        public CornerRadiusFilterKinds Filter { get; set; }
+
+        /// <summary>
+        /// Gets or sets the scale multiplier applied to the <see cref="CornerRadiusFilterConverter"/>.
+        /// </summary>
+        public double Scale { get; set; } = 1;
+
+        public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
+        {
+            if (!(value is CornerRadius radius))
+            {
+                return value;
+            }
+
+            return new CornerRadius(
+                Filter.HasAllFlags(CornerRadiusFilterKinds.TopLeft) ? radius.TopLeft * Scale : 0,
+                Filter.HasAllFlags(CornerRadiusFilterKinds.TopRight) ? radius.TopRight * Scale : 0,
+                Filter.HasAllFlags(CornerRadiusFilterKinds.BottomRight) ? radius.BottomRight * Scale : 0,
+                Filter.HasAllFlags(CornerRadiusFilterKinds.BottomLeft) ? radius.BottomLeft * Scale : 0);
+        }
+
+        public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 32 - 0
src/Avalonia.Controls/Converters/CornerRadiusFilterKind.cs

@@ -0,0 +1,32 @@
+using System;
+
+namespace Avalonia.Controls.Converters
+{
+    /// <summary>
+    /// Defines constants that specify the filter type for a <see cref="CornerRadiusFilterConverter"/> instance.
+    /// </summary>
+    [Flags]
+    public enum CornerRadiusFilterKinds
+    {
+        /// <summary>
+        /// No filter applied.
+        /// </summary>
+        None,
+        /// <summary>
+        /// Filters TopLeft value.
+        /// </summary>
+        TopLeft = 1,
+        /// <summary>
+        /// Filters TopRight value.
+        /// </summary>
+        TopRight = 2,
+        /// <summary>
+        /// Filters BottomLeft value.
+        /// </summary>
+        BottomLeft = 4,
+        /// <summary>
+        /// Filters BottomRight value.
+        /// </summary>
+        BottomRight = 8
+    }
+}