Skip to content

Commit

Permalink
Feature: Added a Range selection mode by aspect ratio value.
Browse files Browse the repository at this point in the history
  • Loading branch information
WCKYWCKF committed Nov 13, 2024
1 parent 60ba488 commit 4bfaab3
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 7 deletions.
59 changes: 55 additions & 4 deletions demo/Ursa.Demo/Pages/AspectRatioLayoutDemo.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
<Grid
RowDefinitions="Auto,*">
<StackPanel
Grid.Row="0"
Orientation="Horizontal">
<u:NumericDoubleUpDown InnerLeftContent="AspectRatioChangeAmbiguity" Value="{Binding #AspectRatioLayout.AspectRatioChangeAmbiguity}"></u:NumericDoubleUpDown>
Grid.Row="0">
<u:NumericDoubleUpDown InnerLeftContent="AspectRatioChangeAmbiguity"
Value="{Binding #AspectRatioLayout.AspectRatioChangeAmbiguity}">
</u:NumericDoubleUpDown>
<TextBlock Text="{Binding #AspectRatioLayout.AspectRatioValue,StringFormat='AspectRatioValue: {0}'}"></TextBlock>
</StackPanel>

<u:AspectRatioLayout Name="AspectRatioLayout" Grid.Row="1">
<u:AspectRatioLayout Name="AspectRatioLayout" Grid.Row="1"
BorderThickness="1"
BorderBrush="Red"
Margin="2"
CornerRadius="10">
<u:AspectRatioLayoutItem AcceptAspectRatioMode="HorizontalRectangle">
<Button>HorizontalRectangle ControlLayout</Button>
</u:AspectRatioLayoutItem>
Expand All @@ -23,6 +29,51 @@
<u:AspectRatioLayoutItem AcceptAspectRatioMode="Square">
<Button>Square ControlLayout</Button>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="2" EndAspectRatioValue="2.2">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="2" EndAspectRatioValue="2.4">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="2" EndAspectRatioValue="2.6">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="2" EndAspectRatioValue="2.8">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="1.3" EndAspectRatioValue="1.5">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
</u:AspectRatioLayout>
</Grid>
</UserControl>
29 changes: 26 additions & 3 deletions src/Ursa/Controls/AspectRatioLayout/AspectRatioLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public AspectRatioMode CurrentAspectRatioMode
set => SetValue(CurrentAspectRatioModeProperty, value);
}

public static readonly StyledProperty<double> AspectRatioValueProperty =
AvaloniaProperty.Register<AspectRatioLayout, double>(
nameof(AspectRatioValue));

public double AspectRatioValue
{
get => GetValue(AspectRatioValueProperty);
set => SetValue(AspectRatioValueProperty, value);
}

protected override Type StyleKeyOverride => typeof(TransitioningContentControl);

[Content]
Expand Down Expand Up @@ -73,9 +83,14 @@ private bool IsRightChanges()
return _history.All(x => x) || _history.All(x => !x);
}

private double GetAspectRatio(Rect rect)
{
return Math.Round(Math.Truncate(Math.Abs(rect.Width)) / Math.Truncate(Math.Abs(rect.Height)), 3);
}

private AspectRatioMode GetScaleMode(Rect rect)
{
var scale = Math.Round(Math.Truncate(Math.Abs(rect.Width)) / Math.Truncate(Math.Abs(rect.Height)), 3);
var scale = GetAspectRatio(rect);
var absA = Math.Abs(AspectRatioChangeAmbiguity);
var h = 1d + absA;
var v = 1d - absA;
Expand All @@ -96,12 +111,20 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
{
var o = (Rect)change.OldValue!;
var n = (Rect)change.NewValue!;
UpdataHistory(GetScaleMode(o) == GetScaleMode(n));
UpdataHistory(GetAspectRatio(o) <= GetAspectRatio(n));
if (!IsRightChanges()) return;
CurrentAspectRatioMode = GetScaleMode(n);
}

var c = Items.FirstOrDefault(x => x.AcceptAspectRatioMode == GetScaleMode(Bounds));
AspectRatioValue = GetAspectRatio(Bounds);
var c =
Items
.Where(x => x.IsUseAspectRatioRange)
.FirstOrDefault(x =>
x.StartAspectRatioValue <= AspectRatioValue
&& AspectRatioValue <= x.EndAspectRatioValue);

c ??= Items.FirstOrDefault(x => x.AcceptAspectRatioMode == GetScaleMode(Bounds));
if (c == null)
{
if (Items.Count == 0) return;
Expand Down
53 changes: 53 additions & 0 deletions src/Ursa/Controls/AspectRatioLayout/AspectRatioLayoutItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,62 @@ public class AspectRatioLayoutItem : ContentControl
AvaloniaProperty.Register<AspectRatioLayoutItem, AspectRatioMode>(
nameof(AcceptAspectRatioMode));

public static readonly StyledProperty<double> StartAspectRatioValueProperty =
AvaloniaProperty.Register<AspectRatioLayoutItem, double>(
nameof(StartAspectRatioValue), defaultValue: double.NaN);

public double StartAspectRatioValue
{
get => GetValue(StartAspectRatioValueProperty);
set => SetValue(StartAspectRatioValueProperty, value);
}

public static readonly StyledProperty<double> EndAspectRatioValueProperty =
AvaloniaProperty.Register<AspectRatioLayoutItem, double>(
nameof(EndAspectRatioValue), defaultValue: double.NaN);

public double EndAspectRatioValue
{
get => GetValue(EndAspectRatioValueProperty);
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 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;
}
}

0 comments on commit 4bfaab3

Please sign in to comment.