From 06ba9af1d01b1c48692525e9d5e46d78a1dbfb30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=9B=E5=B0=98=E7=A9=BA=E5=BF=A7?= Date: Wed, 13 Nov 2024 20:19:44 +0800 Subject: [PATCH] 1.rename AspectRatioChangeAmbiguity to AspectRatioTolerance 2.CurrentAspectRatioModeProperty to DirectProperty. 3.Fix misspelling: Updata -> Update 4.IsUseAspectRatioRange to an internal CLR property. --- .../Pages/AspectRatioLayoutDemo.axaml | 4 +-- .../AspectRatioLayout/AspectRatioLayout.cs | 28 ++++++++------- .../AspectRatioLayoutItem.cs | 36 +++---------------- 3 files changed, 21 insertions(+), 47 deletions(-) diff --git a/demo/Ursa.Demo/Pages/AspectRatioLayoutDemo.axaml b/demo/Ursa.Demo/Pages/AspectRatioLayoutDemo.axaml index 77691d5..c56ec69 100644 --- a/demo/Ursa.Demo/Pages/AspectRatioLayoutDemo.axaml +++ b/demo/Ursa.Demo/Pages/AspectRatioLayoutDemo.axaml @@ -9,8 +9,8 @@ RowDefinitions="Auto,*"> - + diff --git a/src/Ursa/Controls/AspectRatioLayout/AspectRatioLayout.cs b/src/Ursa/Controls/AspectRatioLayout/AspectRatioLayout.cs index b73cb69..5608232 100644 --- a/src/Ursa/Controls/AspectRatioLayout/AspectRatioLayout.cs +++ b/src/Ursa/Controls/AspectRatioLayout/AspectRatioLayout.cs @@ -13,13 +13,15 @@ public class AspectRatioLayout : TransitioningContentControl AvaloniaProperty.Register>( nameof(Items)); - public static readonly StyledProperty AspectRatioChangeAmbiguityProperty = + public static readonly StyledProperty AspectRatioToleranceProperty = AvaloniaProperty.Register( - nameof(AspectRatioChangeAmbiguity), 0.2); + nameof(AspectRatioTolerance), 0.2); - public static readonly StyledProperty CurrentAspectRatioModeProperty = - AvaloniaProperty.Register( - nameof(CurrentAspectRatioMode)); + private AspectRatioMode _currentAspectRatioMode; + + public static readonly DirectProperty CurrentAspectRatioModeProperty = + AvaloniaProperty.RegisterDirect( + nameof(CurrentAspectRatioMode), o => o.CurrentAspectRatioMode); private readonly Queue _history = new(); @@ -42,7 +44,7 @@ public class AspectRatioLayout : TransitioningContentControl public AspectRatioMode CurrentAspectRatioMode { get => GetValue(CurrentAspectRatioModeProperty); - set => SetValue(CurrentAspectRatioModeProperty, value); + set => SetAndRaise(CurrentAspectRatioModeProperty, ref _currentAspectRatioMode, value); } public static readonly StyledProperty AspectRatioValueProperty = @@ -64,13 +66,13 @@ public class AspectRatioLayout : TransitioningContentControl set => SetValue(ItemsProperty, value); } - public double AspectRatioChangeAmbiguity + public double AspectRatioTolerance { - get => GetValue(AspectRatioChangeAmbiguityProperty); - set => SetValue(AspectRatioChangeAmbiguityProperty, value); + get => GetValue(AspectRatioToleranceProperty); + set => SetValue(AspectRatioToleranceProperty, value); } - private void UpdataHistory(bool value) + private void UpdateHistory(bool value) { _history.Enqueue(value); while (_history.Count > 3) @@ -91,7 +93,7 @@ public class AspectRatioLayout : TransitioningContentControl private AspectRatioMode GetScaleMode(Rect rect) { var scale = GetAspectRatio(rect); - var absA = Math.Abs(AspectRatioChangeAmbiguity); + var absA = Math.Abs(AspectRatioTolerance); var h = 1d + absA; var v = 1d - absA; if (scale >= h) return AspectRatioMode.HorizontalRectangle; @@ -104,14 +106,14 @@ public class AspectRatioLayout : TransitioningContentControl { base.OnPropertyChanged(change); if (change.Property == ItemsProperty || - change.Property == AspectRatioChangeAmbiguityProperty || + change.Property == AspectRatioToleranceProperty || change.Property == BoundsProperty) { if (change.Property == BoundsProperty) { var o = (Rect)change.OldValue!; var n = (Rect)change.NewValue!; - UpdataHistory(GetAspectRatio(o) <= GetAspectRatio(n)); + UpdateHistory(GetAspectRatio(o) <= GetAspectRatio(n)); if (!IsRightChanges()) return; CurrentAspectRatioMode = GetScaleMode(n); } diff --git a/src/Ursa/Controls/AspectRatioLayout/AspectRatioLayoutItem.cs b/src/Ursa/Controls/AspectRatioLayout/AspectRatioLayoutItem.cs index 0f80564..b941fa8 100644 --- a/src/Ursa/Controls/AspectRatioLayout/AspectRatioLayoutItem.cs +++ b/src/Ursa/Controls/AspectRatioLayout/AspectRatioLayoutItem.cs @@ -29,42 +29,14 @@ public class AspectRatioLayoutItem : ContentControl set => SetValue(EndAspectRatioValueProperty, value); } - - private bool _isUseAspectRatioRange; - - public static readonly DirectProperty IsUseAspectRatioRangeProperty = - AvaloniaProperty.RegisterDirect( - nameof(IsUseAspectRatioRange), o => o.IsUseAspectRatioRange); - - public bool IsUseAspectRatioRange - { - get => _isUseAspectRatioRange; - private set => SetAndRaise(IsUseAspectRatioRangeProperty, ref _isUseAspectRatioRange, value); - } + public bool IsUseAspectRatioRange => + !double.IsNaN(StartAspectRatioValue) + && !double.IsNaN(EndAspectRatioValue) + && !(StartAspectRatioValue > EndAspectRatioValue); public AspectRatioMode AcceptAspectRatioMode { get => GetValue(AcceptScaleModeProperty); set => SetValue(AcceptScaleModeProperty, value); } - - protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) - { - base.OnPropertyChanged(change); - if (change.Property == StartAspectRatioValueProperty || - change.Property == EndAspectRatioValueProperty) - { - UpdataIsUseAspectRatioRange(); - } - } - - private void UpdataIsUseAspectRatioRange() - { - if (double.IsNaN(StartAspectRatioValue) - || double.IsNaN(EndAspectRatioValue) - || StartAspectRatioValue > EndAspectRatioValue) - IsUseAspectRatioRange = false; - else - IsUseAspectRatioRange = true; - } } \ No newline at end of file