1.rename AspectRatioChangeAmbiguity to AspectRatioTolerance

2.CurrentAspectRatioModeProperty to DirectProperty.
3.Fix misspelling: Updata -> Update
4.IsUseAspectRatioRange to an internal CLR property.
This commit is contained in:
望尘空忧
2024-11-13 20:19:44 +08:00
parent 4bfaab3f3d
commit 06ba9af1d0
3 changed files with 21 additions and 47 deletions

View File

@@ -9,8 +9,8 @@
RowDefinitions="Auto,*">
<StackPanel
Grid.Row="0">
<u:NumericDoubleUpDown InnerLeftContent="AspectRatioChangeAmbiguity"
Value="{Binding #AspectRatioLayout.AspectRatioChangeAmbiguity}">
<u:NumericDoubleUpDown InnerLeftContent="AspectRatioTolerance"
Value="{Binding #AspectRatioLayout.AspectRatioTolerance}">
</u:NumericDoubleUpDown>
<TextBlock Text="{Binding #AspectRatioLayout.AspectRatioValue,StringFormat='AspectRatioValue: {0}'}"></TextBlock>
</StackPanel>

View File

@@ -13,13 +13,15 @@ public class AspectRatioLayout : TransitioningContentControl
AvaloniaProperty.Register<AspectRatioLayout, List<AspectRatioLayoutItem>>(
nameof(Items));
public static readonly StyledProperty<double> AspectRatioChangeAmbiguityProperty =
public static readonly StyledProperty<double> AspectRatioToleranceProperty =
AvaloniaProperty.Register<AspectRatioLayout, double>(
nameof(AspectRatioChangeAmbiguity), 0.2);
nameof(AspectRatioTolerance), 0.2);
public static readonly StyledProperty<AspectRatioMode> CurrentAspectRatioModeProperty =
AvaloniaProperty.Register<AspectRatioLayout, AspectRatioMode>(
nameof(CurrentAspectRatioMode));
private AspectRatioMode _currentAspectRatioMode;
public static readonly DirectProperty<AspectRatioLayout, AspectRatioMode> CurrentAspectRatioModeProperty =
AvaloniaProperty.RegisterDirect<AspectRatioLayout, AspectRatioMode>(
nameof(CurrentAspectRatioMode), o => o.CurrentAspectRatioMode);
private readonly Queue<bool> _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<double> 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);
}

View File

@@ -29,42 +29,14 @@ public class AspectRatioLayoutItem : ContentControl
set => SetValue(EndAspectRatioValueProperty, value);
}
private bool _isUseAspectRatioRange;
public static readonly DirectProperty<AspectRatioLayoutItem, bool> IsUseAspectRatioRangeProperty =
AvaloniaProperty.RegisterDirect<AspectRatioLayoutItem, bool>(
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;
}
}