From df22fb29bc5125aee5e9ceb498145eed8ae1036b Mon Sep 17 00:00:00 2001 From: Jumar Macato <16554748+jmacato@users.noreply.github.com> Date: Mon, 29 May 2023 19:39:33 +0800 Subject: [PATCH] move IndeterminateStarting/EndingOffset to TemplateSettings. --- src/Avalonia.Controls/ProgressBar.cs | 141 ++++++++---------- .../Controls/ProgressBar.xaml | 8 +- 2 files changed, 70 insertions(+), 79 deletions(-) diff --git a/src/Avalonia.Controls/ProgressBar.cs b/src/Avalonia.Controls/ProgressBar.cs index 7dcdaa2f8d..47c51fb839 100644 --- a/src/Avalonia.Controls/ProgressBar.cs +++ b/src/Avalonia.Controls/ProgressBar.cs @@ -32,29 +32,33 @@ namespace Avalonia.Controls private double _container2AnimationStartPosition; private double _container2AnimationEndPosition; - public static readonly DirectProperty ContainerAnimationStartPositionProperty = - AvaloniaProperty.RegisterDirect( - nameof(ContainerAnimationStartPosition), - p => p.ContainerAnimationStartPosition, - (p, o) => p.ContainerAnimationStartPosition = o, 0d); - - public static readonly DirectProperty ContainerAnimationEndPositionProperty = - AvaloniaProperty.RegisterDirect( - nameof(ContainerAnimationEndPosition), - p => p.ContainerAnimationEndPosition, - (p, o) => p.ContainerAnimationEndPosition = o, 0d); - - public static readonly DirectProperty Container2AnimationStartPositionProperty = - AvaloniaProperty.RegisterDirect( - nameof(Container2AnimationStartPosition), - p => p.Container2AnimationStartPosition, - (p, o) => p.Container2AnimationStartPosition = o, 0d); - - public static readonly DirectProperty Container2AnimationEndPositionProperty = - AvaloniaProperty.RegisterDirect( - nameof(Container2AnimationEndPosition), - p => p.Container2AnimationEndPosition, - (p, o) => p.Container2AnimationEndPosition = o); + public static readonly DirectProperty + ContainerAnimationStartPositionProperty = + AvaloniaProperty.RegisterDirect( + nameof(ContainerAnimationStartPosition), + p => p.ContainerAnimationStartPosition, + (p, o) => p.ContainerAnimationStartPosition = o); + + public static readonly DirectProperty + ContainerAnimationEndPositionProperty = + AvaloniaProperty.RegisterDirect( + nameof(ContainerAnimationEndPosition), + p => p.ContainerAnimationEndPosition, + (p, o) => p.ContainerAnimationEndPosition = o); + + public static readonly DirectProperty + Container2AnimationStartPositionProperty = + AvaloniaProperty.RegisterDirect( + nameof(Container2AnimationStartPosition), + p => p.Container2AnimationStartPosition, + (p, o) => p.Container2AnimationStartPosition = o); + + public static readonly DirectProperty + Container2AnimationEndPositionProperty = + AvaloniaProperty.RegisterDirect( + nameof(Container2AnimationEndPosition), + p => p.Container2AnimationEndPosition, + (p, o) => p.Container2AnimationEndPosition = o); public static readonly DirectProperty Container2WidthProperty = AvaloniaProperty.RegisterDirect( @@ -68,10 +72,17 @@ namespace Avalonia.Controls p => p.ContainerWidth, (p, o) => p.ContainerWidth = o); + public static readonly StyledProperty IndeterminateStartingOffsetProperty = + AvaloniaProperty.Register(nameof(IndeterminateStartingOffset)); + + public static readonly StyledProperty IndeterminateEndingOffsetProperty = + AvaloniaProperty.Register(nameof(IndeterminateEndingOffset)); + public double ContainerAnimationStartPosition { get => _containerAnimationStartPosition; - set => SetAndRaise(ContainerAnimationStartPositionProperty, ref _containerAnimationStartPosition, value); + set => SetAndRaise(ContainerAnimationStartPositionProperty, ref _containerAnimationStartPosition, + value); } public double ContainerAnimationEndPosition @@ -83,7 +94,8 @@ namespace Avalonia.Controls public double Container2AnimationStartPosition { get => _container2AnimationStartPosition; - set => SetAndRaise(Container2AnimationStartPositionProperty, ref _container2AnimationStartPosition, value); + set => SetAndRaise(Container2AnimationStartPositionProperty, ref _container2AnimationStartPosition, + value); } public double Container2Width @@ -103,6 +115,18 @@ namespace Avalonia.Controls get => _container2AnimationEndPosition; set => SetAndRaise(Container2AnimationEndPositionProperty, ref _container2AnimationEndPosition, value); } + + public double IndeterminateStartingOffset + { + get => GetValue(IndeterminateStartingOffsetProperty); + set => SetValue(IndeterminateStartingOffsetProperty, value); + } + + public double IndeterminateEndingOffset + { + get => GetValue(IndeterminateEndingOffsetProperty); + set => SetValue(IndeterminateEndingOffsetProperty, value); + } } private double _percentage; @@ -131,7 +155,7 @@ namespace Avalonia.Controls /// Defines the property. /// public static readonly StyledProperty OrientationProperty = - AvaloniaProperty.Register(nameof(Orientation), Orientation.Horizontal); + AvaloniaProperty.Register(nameof(Orientation)); /// /// Defines the property. @@ -141,18 +165,6 @@ namespace Avalonia.Controls nameof(Percentage), o => o.Percentage); - /// - /// Defines the property. - /// - public static readonly StyledProperty IndeterminateStartingOffsetProperty = - AvaloniaProperty.Register(nameof(IndeterminateStartingOffset)); - - /// - /// Defines the property. - /// - public static readonly StyledProperty IndeterminateEndingOffsetProperty = - AvaloniaProperty.Register(nameof(IndeterminateEndingOffset)); - /// /// Gets the overall percentage complete of the progress /// @@ -166,26 +178,14 @@ namespace Avalonia.Controls private set { SetAndRaise(PercentageProperty, ref _percentage, value); } } - public double IndeterminateStartingOffset - { - get => GetValue(IndeterminateStartingOffsetProperty); - set => SetValue(IndeterminateStartingOffsetProperty, value); - } - - public double IndeterminateEndingOffset - { - get => GetValue(IndeterminateEndingOffsetProperty); - set => SetValue(IndeterminateEndingOffsetProperty, value); - } - static ProgressBar() { ValueProperty.OverrideMetadata(new(defaultBindingMode: BindingMode.OneWay)); - ValueProperty.Changed.AddClassHandler((x, e) => x.UpdateIndicatorWhenPropChanged(e)); - MinimumProperty.Changed.AddClassHandler((x, e) => x.UpdateIndicatorWhenPropChanged(e)); - MaximumProperty.Changed.AddClassHandler((x, e) => x.UpdateIndicatorWhenPropChanged(e)); - IsIndeterminateProperty.Changed.AddClassHandler((x, e) => x.UpdateIndicatorWhenPropChanged(e)); - OrientationProperty.Changed.AddClassHandler((x, e) => x.UpdateIndicatorWhenPropChanged(e)); + ValueProperty.Changed.AddClassHandler((x, _) => x.UpdateIndicatorWhenPropChanged()); + MinimumProperty.Changed.AddClassHandler((x, _) => x.UpdateIndicatorWhenPropChanged()); + MaximumProperty.Changed.AddClassHandler((x, _) => x.UpdateIndicatorWhenPropChanged()); + IsIndeterminateProperty.Changed.AddClassHandler((x, _) => x.UpdateIndicatorWhenPropChanged()); + OrientationProperty.Changed.AddClassHandler((x, _) => x.UpdateIndicatorWhenPropChanged()); } /// @@ -305,22 +305,14 @@ namespace Avalonia.Controls TemplateSettings.Container2AnimationStartPosition = barIndicatorWidth2 * -1.5; // Position at -150% TemplateSettings.Container2AnimationEndPosition = barIndicatorWidth2 * 1.66; // Position at 166% - // Remove these properties when we switch to fluent as default and removed the old one. - SetCurrentValue(IndeterminateStartingOffsetProperty,-dim); - SetCurrentValue(IndeterminateEndingOffsetProperty,dim); - - var padding = Padding; - var rectangle = new RectangleGeometry( - new Rect( - padding.Left, - padding.Top, - barSize.Width - (padding.Right + padding.Left), - barSize.Height - (padding.Bottom + padding.Top) - )); + TemplateSettings.IndeterminateStartingOffset = -dim; + TemplateSettings.IndeterminateEndingOffset = dim; } else { - double percent = Maximum == Minimum ? 1.0 : (Value - Minimum) / (Maximum - Minimum); + var percent = Math.Abs(Maximum - Minimum) < double.Epsilon ? + 1.0 : + (Value - Minimum) / (Maximum - Minimum); // When the Orientation changed, the indicator's Width or Height should set to double.NaN. // Indicator size calculation should consider the ProgressBar's Padding property setting @@ -332,7 +324,8 @@ namespace Avalonia.Controls else { _indicator.Width = double.NaN; - _indicator.Height = (barSize.Height - _indicator.Margin.Top - _indicator.Margin.Bottom) * percent; + _indicator.Height = (barSize.Height - _indicator.Margin.Top - _indicator.Margin.Bottom) * + percent; } @@ -341,7 +334,7 @@ namespace Avalonia.Controls } } - private void UpdateIndicatorWhenPropChanged(AvaloniaPropertyChangedEventArgs e) + private void UpdateIndicatorWhenPropChanged() { UpdateIndicator(); } @@ -355,11 +348,9 @@ namespace Avalonia.Controls PseudoClasses.Set(":indeterminate", isIndeterminate.Value); } - if (o.HasValue) - { - PseudoClasses.Set(":vertical", o == Orientation.Vertical); - PseudoClasses.Set(":horizontal", o == Orientation.Horizontal); - } + if (!o.HasValue) return; + PseudoClasses.Set(":vertical", o == Orientation.Vertical); + PseudoClasses.Set(":horizontal", o == Orientation.Horizontal); } } } diff --git a/src/Avalonia.Themes.Simple/Controls/ProgressBar.xaml b/src/Avalonia.Themes.Simple/Controls/ProgressBar.xaml index 4278c7ee28..e7ad33d04e 100644 --- a/src/Avalonia.Themes.Simple/Controls/ProgressBar.xaml +++ b/src/Avalonia.Themes.Simple/Controls/ProgressBar.xaml @@ -87,10 +87,10 @@ IterationCount="Infinite" Duration="0:0:3"> - + - + @@ -102,10 +102,10 @@ IterationCount="Infinite" Duration="0:0:3"> - + - +