Skip to content

Fixes 3941 - Do not repeatedly raise new sets of keystrokes when modals pop (stack overflow) #3949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: v2_develop
Choose a base branch
from
5 changes: 5 additions & 0 deletions Terminal.Gui/Application/Application.Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ public static bool RunIteration (ref RunState state, bool firstIteration = false
return firstIteration;
}

internal static void RaiseIteration ()
{
Iteration?.Invoke (null, new IterationEventArgs ());
}

/// <summary>Stops the provided <see cref="Toplevel"/>, causing or the <paramref name="top"/> if provided.</summary>
/// <param name="top">The <see cref="Toplevel"/> to stop.</param>
/// <remarks>
Expand Down
9 changes: 9 additions & 0 deletions Terminal.Gui/Application/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public static partial class Application
/// <summary>Gets all cultures supported by the application without the invariant language.</summary>
public static List<CultureInfo>? SupportedCultures { get; private set; } = GetSupportedCultures ();

/// <summary>
/// Maximum number of iterations of the main loop (and hence draws)
/// to allow to occur per second. Defaults to 25 which is a 40ms sleep
/// after iteration (factoring in how long iteration took to run).
/// <remarks>Note that not ever iteration draws (see <see cref="View.NeedsDraw"/>).
/// Only affects v2 drivers.</remarks>
/// </summary>
public static ushort MaximumIterationsPerSecond = 25;

/// <summary>
/// Gets a string representation of the Application as rendered by <see cref="Driver"/>.
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@
public void Iteration ()
{
DateTime dt = Now ();
int timeAllowed = 1000 / Math.Max(1,(int)Application.MaximumIterationsPerSecond);

IterationImpl ();

TimeSpan took = Now () - dt;
TimeSpan sleepFor = TimeSpan.FromMilliseconds (50) - took;
TimeSpan sleepFor = TimeSpan.FromMilliseconds (timeAllowed) - took;

Logging.TotalIterationMetric.Record (took.Milliseconds);

Application.RaiseIteration ();

if (sleepFor.Milliseconds > 0)
{
Task.Delay (sleepFor).Wait ();
Expand Down Expand Up @@ -151,7 +154,7 @@

private void SetCursor ()
{
View? mostFocused = Application.Top.MostFocused;

Check warning on line 157 in Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs

View workflow job for this annotation

GitHub Actions / build_release

Dereference of a possibly null reference.

Check warning on line 157 in Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs

View workflow job for this annotation

GitHub Actions / Parallel Unit Tests (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 157 in Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 157 in Terminal.Gui/ConsoleDrivers/V2/MainLoop.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (windows-latest)

Dereference of a possibly null reference.

if (mostFocused == null)
{
Expand Down
9 changes: 8 additions & 1 deletion UICatalog/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,20 @@ private void OnApplicationOnIteration (object? s, IterationEventArgs a)
}
}

private HashSet<Scenario> _scenariosRun = new HashSet<Scenario> ();
private void OnApplicationNotifyNewRunState (object? sender, RunStateEventArgs e)
{
// We are just returning to the same scenario
if (!_scenariosRun.Add (this))
{
return;
}

SubscribeAllSubviews (Application.Top!);

_currentDemoKey = 0;
_demoKeys = GetDemoKeyStrokes ();

Application.AddTimeout (
new TimeSpan (0, 0, 0, 0, BENCHMARK_KEY_PACING),
() =>
Expand Down
1 change: 1 addition & 0 deletions UICatalog/UICatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ void ApplicationOnInitializedChanged (object? sender, EventArgs<bool> e)

private static void BenchmarkAllScenarios ()
{
Application.MaximumIterationsPerSecond = ushort.MaxValue;
List<BenchmarkResults> resultsList = new ();

var maxScenarios = 5;
Expand Down
Loading