Browse Source

Requested changes.

pull/1175/head
José Pedro 9 years ago
parent
commit
5cebc7792f
  1. 88
      src/Avalonia.Controls/ProgressBar.cs

88
src/Avalonia.Controls/ProgressBar.cs

@ -22,7 +22,7 @@ namespace Avalonia.Controls
AvaloniaProperty.Register<ProgressBar, Orientation>(nameof(Orientation), Orientation.Horizontal); AvaloniaProperty.Register<ProgressBar, Orientation>(nameof(Orientation), Orientation.Horizontal);
private Border _indicator; private Border _indicator;
private IDisposable _indeterminateBindSubscription; private IndeterminateAnimation _indeterminateAnimation;
static ProgressBar() static ProgressBar()
{ {
@ -30,26 +30,23 @@ namespace Avalonia.Controls
HorizontalAlignmentProperty.OverrideDefaultValue<ProgressBar>(HorizontalAlignment.Left); HorizontalAlignmentProperty.OverrideDefaultValue<ProgressBar>(HorizontalAlignment.Left);
VerticalAlignmentProperty.OverrideDefaultValue<ProgressBar>(VerticalAlignment.Top); VerticalAlignmentProperty.OverrideDefaultValue<ProgressBar>(VerticalAlignment.Top);
IsIndeterminateProperty.Changed.AddClassHandler<ProgressBar>(
(p, e) => { if (p._indicator != null) p.UpdateIsIndeterminate((bool)e.NewValue); });
OrientationProperty.Changed.AddClassHandler<ProgressBar>(
(p, e) => { if (p._indicator != null) p.UpdateOrientation((Orientation)e.NewValue); });
} }
public bool IsIndeterminate public bool IsIndeterminate
{ {
get => GetValue(IsIndeterminateProperty); get => GetValue(IsIndeterminateProperty);
set set => SetValue(IsIndeterminateProperty, value);
{
SetValue(IsIndeterminateProperty, value);
UpdateIsIndeterminate(value);
}
} }
public Orientation Orientation public Orientation Orientation
{ {
get => GetValue(OrientationProperty); get => GetValue(OrientationProperty);
set set => SetValue(OrientationProperty, value);
{
SetValue(OrientationProperty, value);
UpdateOrientation(value);
}
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -115,33 +112,66 @@ namespace Avalonia.Controls
private void UpdateIsIndeterminate(bool isIndeterminate) private void UpdateIsIndeterminate(bool isIndeterminate)
{ {
if (isIndeterminate) if (isIndeterminate)
_indeterminateAnimation = IndeterminateAnimation.StartAnimation(this);
else
_indeterminateAnimation?.Dispose();
}
private void ValueChanged(AvaloniaPropertyChangedEventArgs e)
{
UpdateIndicator(Bounds.Size);
}
private class IndeterminateAnimation : IDisposable
{
private WeakReference<ProgressBar> _progressBar;
private IDisposable _indeterminateBindSubscription;
private TimeSpan _startTime;
private IndeterminateAnimation(ProgressBar progressBar)
{
_progressBar = new WeakReference<ProgressBar>(progressBar);
_startTime = Animate.Stopwatch.Elapsed;
_indeterminateBindSubscription = Animate.Timer.TakeWhile(x => (x - _startTime).TotalSeconds <= 4.0)
.Select(GetAnimationRect)
.Finally(() => _startTime = Animate.Stopwatch.Elapsed)
.Repeat()
.Subscribe(AnimationTick);
}
public static IndeterminateAnimation StartAnimation(ProgressBar progressBar)
{ {
var start = Animate.Stopwatch.Elapsed; return new IndeterminateAnimation(progressBar);
}
if (Orientation == Orientation.Horizontal) private Rect GetAnimationRect(TimeSpan time)
{
if (_progressBar.TryGetTarget(out var progressBar))
{ {
_indeterminateBindSubscription = Animate.Timer.TakeWhile(x => (x - start).TotalSeconds <= 4.0) if (progressBar.Orientation == Orientation.Horizontal)
.Select(x => new Rect(-_indicator.Width - 5 + (x - start).TotalSeconds / 4.0 * (Bounds.Width + _indicator.Width + 10), 0, _indicator.Bounds.Width, _indicator.Bounds.Height)) return new Rect(-progressBar._indicator.Width - 5 + (time - _startTime).TotalSeconds / 4.0 * (progressBar.Bounds.Width + progressBar._indicator.Width + 10), 0, progressBar._indicator.Bounds.Width, progressBar._indicator.Bounds.Height);
.Finally(() => start = Animate.Stopwatch.Elapsed) else
.Repeat() return new Rect(0, progressBar.Bounds.Height + 5 - (time - _startTime).TotalSeconds / 4.0 * (progressBar.Bounds.Height + progressBar._indicator.Height + 10), progressBar._indicator.Bounds.Width, progressBar._indicator.Bounds.Height);
.Subscribe(x => _indicator.Arrange(x));
} }
else else
{ {
_indeterminateBindSubscription = Animate.Timer.TakeWhile(x => (x - start).TotalSeconds <= 4.0) _indeterminateBindSubscription.Dispose();
.Select(x => new Rect(0, Bounds.Height + 5 - (x - start).TotalSeconds / 4.0 * (Bounds.Height + _indicator.Height + 10), _indicator.Bounds.Width, _indicator.Bounds.Height)) return Rect.Empty;
.Finally(() => start = Animate.Stopwatch.Elapsed)
.Repeat()
.Subscribe(x => _indicator.Arrange(x));
} }
} }
else
_indeterminateBindSubscription?.Dispose();
}
private void ValueChanged(AvaloniaPropertyChangedEventArgs e) private void AnimationTick(Rect rect)
{ {
UpdateIndicator(Bounds.Size); if (_progressBar.TryGetTarget(out var progressBar))
progressBar._indicator.Arrange(rect);
else
_indeterminateBindSubscription.Dispose();
}
public void Dispose()
{
_indeterminateBindSubscription?.Dispose();
}
} }
} }
} }

Loading…
Cancel
Save