diff --git a/src/Avalonia.Controls/Converters/CornerRadiusFilterConverter.cs b/src/Avalonia.Controls/Converters/CornerRadiusFilterConverter.cs new file mode 100644 index 0000000000..7587a9dacb --- /dev/null +++ b/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 +{ + /// + /// 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. + /// + public class CornerRadiusFilterConverter : IValueConverter + { + /// + /// Gets or sets the type of the filter applied to the . + /// + public CornerRadiusFilterKinds Filter { get; set; } + + /// + /// Gets or sets the scale multiplier applied to the . + /// + 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(); + } + } +} diff --git a/src/Avalonia.Controls/Converters/CornerRadiusFilterKind.cs b/src/Avalonia.Controls/Converters/CornerRadiusFilterKind.cs new file mode 100644 index 0000000000..6a9d0596be --- /dev/null +++ b/src/Avalonia.Controls/Converters/CornerRadiusFilterKind.cs @@ -0,0 +1,32 @@ +using System; + +namespace Avalonia.Controls.Converters +{ + /// + /// Defines constants that specify the filter type for a instance. + /// + [Flags] + public enum CornerRadiusFilterKinds + { + /// + /// No filter applied. + /// + None, + /// + /// Filters TopLeft value. + /// + TopLeft = 1, + /// + /// Filters TopRight value. + /// + TopRight = 2, + /// + /// Filters BottomLeft value. + /// + BottomLeft = 4, + /// + /// Filters BottomRight value. + /// + BottomRight = 8 + } +}