From 75d798d00a5ff2f75f7382f4be374a70ab957f35 Mon Sep 17 00:00:00 2001 From: zhouzj Date: Fri, 4 Nov 2022 17:04:34 +0800 Subject: [PATCH] Fix the size issue of indicator border in ProgressBar --- src/Avalonia.Controls/ProgressBar.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/ProgressBar.cs b/src/Avalonia.Controls/ProgressBar.cs index 1e406157d7..71a7a58da4 100644 --- a/src/Avalonia.Controls/ProgressBar.cs +++ b/src/Avalonia.Controls/ProgressBar.cs @@ -110,7 +110,7 @@ namespace Avalonia.Controls public static readonly StyledProperty ProgressTextFormatProperty = AvaloniaProperty.Register(nameof(ProgressTextFormat), "{1:0}%"); - + public static readonly StyledProperty OrientationProperty = AvaloniaProperty.Register(nameof(Orientation), Orientation.Horizontal); @@ -136,7 +136,7 @@ namespace Avalonia.Controls get { return _percentage; } private set { SetAndRaise(PercentageProperty, ref _percentage, value); } } - + public double IndeterminateStartingOffset { get => _indeterminateStartingOffset; @@ -156,6 +156,7 @@ namespace Avalonia.Controls 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)); } public ProgressBar() @@ -216,13 +217,13 @@ namespace Avalonia.Controls { // dispose any previous track size listener _trackSizeChangedListener?.Dispose(); - + _indicator = e.NameScope.Get("PART_Indicator"); // listen to size changes of the indicators track (parent) and update the indicator there. _trackSizeChangedListener = _indicator.Parent?.GetPropertyChangedObservable(BoundsProperty) .Subscribe(_ => UpdateIndicator()); - + UpdateIndicator(); } @@ -230,7 +231,7 @@ namespace Avalonia.Controls { // Gets the size of the parent indicator container var barSize = _indicator?.Parent?.Bounds.Size ?? Bounds.Size; - + if (_indicator != null) { if (IsIndeterminate) @@ -268,10 +269,18 @@ namespace Avalonia.Controls { double percent = Maximum == Minimum ? 1.0 : (Value - Minimum) / (Maximum - Minimum); + // When the Orientation changed, the indicator's Width or Height should set to double.NaN. if (Orientation == Orientation.Horizontal) + { _indicator.Width = barSize.Width * percent; + _indicator.Height = double.NaN; + } else + { + _indicator.Width = double.NaN; _indicator.Height = barSize.Height * percent; + } + Percentage = percent * 100; }