From bb46fc5f3568adbc28cff0d0a5d837605de4edef Mon Sep 17 00:00:00 2001 From: SKProCH Date: Mon, 30 Mar 2026 02:49:07 +0300 Subject: [PATCH] Add Stretch/StretchDirection props, refactor sizing logic --- Material.Icons.Avalonia/MaterialIcon.axaml | 345 +++++++++++++++++- Material.Icons.Avalonia/MaterialIcon.axaml.cs | 39 +- .../MaterialIconText.axaml | 4 +- 3 files changed, 367 insertions(+), 21 deletions(-) diff --git a/Material.Icons.Avalonia/MaterialIcon.axaml b/Material.Icons.Avalonia/MaterialIcon.axaml index 159e59a..bad2e66 100644 --- a/Material.Icons.Avalonia/MaterialIcon.axaml +++ b/Material.Icons.Avalonia/MaterialIcon.axaml @@ -3,7 +3,7 @@ xmlns:icon="clr-namespace:Material.Icons.Avalonia" xmlns:system="clr-namespace:System;assembly=netstandard"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -224,24 +539,26 @@ BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}"> - + + Width="24" Height="24" /> - - + + + + + + + + - + \ No newline at end of file diff --git a/Material.Icons.Avalonia/MaterialIcon.axaml.cs b/Material.Icons.Avalonia/MaterialIcon.axaml.cs index 857efbe..c4194ac 100644 --- a/Material.Icons.Avalonia/MaterialIcon.axaml.cs +++ b/Material.Icons.Avalonia/MaterialIcon.axaml.cs @@ -1,10 +1,10 @@ -using Avalonia; +using System; +using Avalonia; using Avalonia.Controls.Primitives; using Avalonia.Interactivity; using Avalonia.Media; namespace Material.Icons.Avalonia { - public class MaterialIcon : TemplatedControl, IImage { #region Properties @@ -20,7 +20,7 @@ public MaterialIconKind? Kind { } public static readonly StyledProperty IconSizeProperty = - AvaloniaProperty.Register(nameof(IconSize), defaultValue: double.NaN); + AvaloniaProperty.Register(nameof(IconSize), defaultValue: double.NaN); /// /// Gets or sets the uniform size of the icon. @@ -30,6 +30,23 @@ public double IconSize { set => SetValue(IconSizeProperty, value); } + public static readonly StyledProperty StretchProperty = + AvaloniaProperty.Register(nameof(Stretch), defaultValue: Stretch.Uniform); + + public Stretch Stretch { + get => GetValue(StretchProperty); + set => SetValue(StretchProperty, value); + } + + public static readonly StyledProperty StretchDirectionProperty = + AvaloniaProperty.Register(nameof(StretchDirection), + defaultValue: StretchDirection.DownOnly); + + public StretchDirection StretchDirection { + get => GetValue(StretchDirectionProperty); + set => SetValue(StretchDirectionProperty, value); + } + public static readonly StyledProperty AnimationProperty = AvaloniaProperty.Register(nameof(Animation)); @@ -60,6 +77,7 @@ public MaterialIconAnimation Animation { static MaterialIcon() { MaterialIconsUtils.InitializeGeometryParser(); + AffectsMeasure(FontSizeProperty); } public MaterialIcon() { @@ -89,6 +107,18 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e) { } } + protected override Size MeasureOverride(Size availableSize) { + if (!double.IsNaN(IconSize)) { + availableSize = new Size(IconSize, IconSize); + } + var baseSize = double.IsNaN(IconSize) ? FontSize : IconSize; + var intrinsicSize = new Size(baseSize + Padding.Left + Padding.Right, + baseSize + Padding.Top + Padding.Bottom); + var size = Stretch.CalculateSize(availableSize, intrinsicSize, StretchDirection); + base.MeasureOverride(size); + return size; + } + #endregion #region Methods @@ -132,6 +162,5 @@ void IImage.Draw(DrawingContext context, Rect sourceRect, Rect destRect) { } #endregion - } -} +} \ No newline at end of file diff --git a/Material.Icons.Avalonia/MaterialIconText.axaml b/Material.Icons.Avalonia/MaterialIconText.axaml index 7a57b82..a743170 100644 --- a/Material.Icons.Avalonia/MaterialIconText.axaml +++ b/Material.Icons.Avalonia/MaterialIconText.axaml @@ -86,9 +86,9 @@ Text="Default icon / {x:Null}" /> + Text="Default icon / Class=collapse-null" />