#nullable disable namespace Terminal.Gui.App; /// Implements a logarithmic increasing timeout. public class LogarithmicTimeout : Timeout { /// /// Creates a new instance where stages are the logarithm multiplied by the /// (starts fast then slows). /// /// Multiple for the logarithm /// Method to invoke public LogarithmicTimeout (TimeSpan baseDelay, Func callback) { _baseDelay = baseDelay; Callback = callback; } private readonly TimeSpan _baseDelay; private int _stage; /// Increments the stage to increase the timeout. public void AdvanceStage () { _stage++; } /// Resets the stage back to zero. public void Reset () { _stage = 0; } /// Gets the current calculated Span based on the stage. public override TimeSpan Span { get { // Calculate logarithmic increase double multiplier = Math.Log (_stage + 1); // ln(stage + 1) return TimeSpan.FromMilliseconds (_baseDelay.TotalMilliseconds * multiplier); } } }