using System; namespace Terminal.Gui { /// /// A 1x1 based on which displays a spinning /// line character. /// /// /// By default animation only occurs when you call . /// Use to make the automate calls to . /// public class SpinnerView : Label { private Rune [] _runes = new Rune [] { '|', '/', '\u2500', '\\' }; private int _currentIdx = 0; private DateTime _lastRender = DateTime.MinValue; private object _timeout; /// /// Gets or sets the number of milliseconds to wait between characters /// in the spin. Defaults to 250. /// /// This is the maximum speed the spinner will rotate at. You still need to /// call or to /// advance/start animation. public int SpinDelayInMilliseconds { get; set; } = 250; /// /// Creates a new instance of the class. /// public SpinnerView () { Width = 1; Height = 1; } /// public override void Redraw (Rect bounds) { if (DateTime.Now - _lastRender > TimeSpan.FromMilliseconds (SpinDelayInMilliseconds)) { _currentIdx = (_currentIdx + 1) % _runes.Length; Text = "" + _runes [_currentIdx]; _lastRender = DateTime.Now; } base.Redraw (bounds); } /// /// Automates spinning /// public void AutoSpin () { if (_timeout != null) { return; } _timeout = Application.MainLoop.AddTimeout ( TimeSpan.FromMilliseconds (SpinDelayInMilliseconds), (m) => { Application.MainLoop.Invoke (this.SetNeedsDisplay); return true; }); } /// protected override void Dispose (bool disposing) { if (_timeout != null) { Application.MainLoop.RemoveTimeout (_timeout); _timeout = null; } base.Dispose (disposing); } } }