Skip to content

Commit

Permalink
Merge pull request #27 from BDisp/tig_v2_3338-Toplevel-Must-Be-Disposed
Browse files Browse the repository at this point in the history
Set Visible to false on Closing event.
  • Loading branch information
tig authored Mar 25, 2024
2 parents 6ebee8d + 155e0a4 commit d3599c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Terminal.Gui/Application.MainLoopSyncContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private sealed class MainLoopSyncContext : SynchronizationContext

public override void Post (SendOrPostCallback d, object state)
{
MainLoop.AddIdle (
MainLoop?.AddIdle (
() =>
{
d (state);
Expand Down
16 changes: 15 additions & 1 deletion Terminal.Gui/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,15 @@ public static void Run (Toplevel view, Func<Exception, bool> errorHandler = null
// by using NotifyStopRunState event.
RunLoop (runState);

if (runState.Toplevel is null)
{
#if DEBUG_IDISPOSABLE
Debug.Assert (_topLevels.Count == 0);
#endif
runState.Dispose ();
return;
}

if (!EndAfterFirstIteration)
{
End (runState);
Expand Down Expand Up @@ -787,7 +796,7 @@ public static void RunLoop (RunState state)

var firstIteration = true;

for (state.Toplevel.Running = true; state.Toplevel.Running;)
for (state.Toplevel.Running = true; state.Toplevel?.Running == true;)
{
MainLoop.Running = true;

Expand Down Expand Up @@ -838,6 +847,11 @@ public static void RunIteration (ref RunState state, ref bool firstIteration)

firstIteration = false;

if (Current == null)
{
return;
}

if (state.Toplevel != Top && (Top.NeedsDisplay || Top.SubViewNeedsDisplay || Top.LayoutNeeded))
{
state.Toplevel.SetNeedsDisplay (state.Toplevel.Frame);
Expand Down
20 changes: 19 additions & 1 deletion UICatalog/Scenarios/BackgroundWorkerCollection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using Terminal.Gui;

Expand All @@ -19,6 +20,10 @@ public override void Init ()

//main.Dispose ();
Application.Top.Dispose ();

#if DEBUG_IDISPOSABLE
Debug.Assert (Application.OverlappedChildren.Count == 0);
#endif
}

public override void Run () { }
Expand Down Expand Up @@ -182,6 +187,7 @@ private void OverlappedMain_Deactivate (object sender, ToplevelEventArgs top)
{
_workerApp?.WriteLog ($"{top.Toplevel.Data} deactivate.");
}

private void Quit () { RequestStop (); }

private MenuBarItem View ()
Expand Down Expand Up @@ -356,6 +362,12 @@ public WorkerApp ()
Add (_listLog);

Closing += WorkerApp_Closing;
Closed += WorkerApp_Closed;
}

private void WorkerApp_Closed (object sender, ToplevelEventArgs e)
{
CancelWorker ();
}
private void WorkerApp_Closing (object sender, ToplevelClosingEventArgs e)
{
Expand Down Expand Up @@ -474,7 +486,13 @@ public void RunWorker ()
_stagingsUi.Add (stagingUI);
_stagingWorkers.Remove (staging);
#if DEBUG_IDISPOSABLE
if (Application.OverlappedTop is null)
{
stagingUI.Dispose ();
return;
}
#endif
Application.Run (stagingUI);
stagingUI.Dispose ();
}
Expand Down

0 comments on commit d3599c9

Please sign in to comment.