From cf5527a9175672a7cd461714d9c4e56c3d746efa Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 17 Jul 2020 01:20:22 +0100 Subject: [PATCH] Fixes #803. Init and Shutdown only run once. --- Example/demo.cs | 4 +-- Terminal.Gui/Core/Application.cs | 51 ++++++++++-------------------- Terminal.Gui/Windows/MessageBox.cs | 2 +- UICatalog/Scenario.cs | 4 +-- UICatalog/Scenarios/HexEditor.cs | 2 +- UICatalog/UICatalog.cs | 9 ++++-- 6 files changed, 28 insertions(+), 44 deletions(-) diff --git a/Example/demo.cs b/Example/demo.cs index da61d517b..e8171f6c0 100644 --- a/Example/demo.cs +++ b/Example/demo.cs @@ -274,7 +274,7 @@ static class Demo { text.Text = System.IO.File.ReadAllText (fname); win.Add (text); - Application.Run (ntop, false); + Application.Run (ntop); } static bool Quit () @@ -680,7 +680,7 @@ static class Demo { top.Add (win); //top.Add (menu); top.Add (menu, statusBar); - Application.Run (top, false); + Application.Run (top); } private static void Win_KeyPress (View.KeyEventEventArgs e) diff --git a/Terminal.Gui/Core/Application.cs b/Terminal.Gui/Core/Application.cs index 0cbcb16a6..f7b096df7 100644 --- a/Terminal.Gui/Core/Application.cs +++ b/Terminal.Gui/Core/Application.cs @@ -161,9 +161,6 @@ namespace Terminal.Gui { internal static bool _initialized = false; - static IMainLoopDriver oldMainLoopDriver; - static ConsoleDriver oldDriver; - /// /// Initializes the Terminal.Gui application /// @@ -192,15 +189,8 @@ namespace Terminal.Gui { mainLoopDriver = windowsDriver; Driver = windowsDriver; } else { - if (oldMainLoopDriver == null && oldDriver == null) { - mainLoopDriver = new UnixMainLoop (); - Driver = new CursesDriver (); - oldMainLoopDriver = mainLoopDriver; - oldDriver = Driver; - } else { - mainLoopDriver = oldMainLoopDriver; - Driver = oldDriver; - } + mainLoopDriver = new UnixMainLoop (); + Driver = new CursesDriver (); } Driver.Init (TerminalResized); MainLoop = new MainLoop (mainLoopDriver); @@ -216,8 +206,6 @@ namespace Terminal.Gui { /// Captures the execution state for the provided view. /// public class RunState : IDisposable { - internal bool closeDriver = true; - /// /// Initializes a new class. /// @@ -238,7 +226,7 @@ namespace Terminal.Gui { /// was occupying. public void Dispose () { - Dispose (closeDriver); + Dispose (true); GC.SuppressFinalize (this); } @@ -249,8 +237,8 @@ namespace Terminal.Gui { /// If set to true disposing. protected virtual void Dispose (bool disposing) { - if (Toplevel != null) { - End (Toplevel, disposing); + if (Toplevel != null && disposing) { + End (Toplevel); Toplevel.Dispose (); Toplevel = null; } @@ -490,12 +478,11 @@ namespace Terminal.Gui { /// /// The runstate returned by the method. /// If true, closes the application. If false closes the toplevels only. - public static void End (RunState runState, bool closeDriver = true) + public static void End (RunState runState) { if (runState == null) throw new ArgumentNullException (nameof (runState)); - runState.closeDriver = closeDriver; runState.Dispose (); } @@ -503,7 +490,7 @@ namespace Terminal.Gui { /// Shutdown an application initialized with /// /// trueCloses the application.falseCloses toplevels only. - public static void Shutdown (bool closeDriver = true) + public static void Shutdown () { // Shutdown is the bookend for Init. As such it needs to clean up all resources // Init created. Apps that do any threading will need to code defensively for this. @@ -518,14 +505,9 @@ namespace Terminal.Gui { CurrentView = null; Top = null; - // Closes the application if it's true. - if (closeDriver) { - MainLoop = null; - Driver?.End (); - Driver = null; - } - - _initialized = false; + MainLoop = null; + Driver?.End (); + Driver = null; } static void Redraw (View view) @@ -558,14 +540,13 @@ namespace Terminal.Gui { Driver.Refresh (); } - internal static void End (View view, bool closeDriver = true) + internal static void End (View view) { if (toplevels.Peek () != view) throw new ArgumentException ("The view that you end with must be balanced"); toplevels.Pop (); - if (toplevels.Count == 0) - Shutdown (closeDriver); - else { + + if (toplevels.Count > 0) { Current = toplevels.Peek (); Refresh (); } @@ -598,7 +579,7 @@ namespace Terminal.Gui { MainLoop.MainIteration (); Iteration?.Invoke (); - } else if (wait == false) { + } else if (!wait) { return; } if (state.Toplevel != Top && (!Top.NeedDisplay.IsEmpty || Top.childNeedsDisplay)) { @@ -672,11 +653,11 @@ namespace Terminal.Gui { /// /// The tu run modally. /// Set to to cause the MainLoop to end when is called, clsing the toplevels only. - public static void Run (Toplevel view, bool closeDriver = true) + public static void Run (Toplevel view) { var runToken = Begin (view); RunLoop (runToken); - End (runToken, closeDriver); + End (runToken); } /// diff --git a/Terminal.Gui/Windows/MessageBox.cs b/Terminal.Gui/Windows/MessageBox.cs index fb5653665..ffdf790cb 100644 --- a/Terminal.Gui/Windows/MessageBox.cs +++ b/Terminal.Gui/Windows/MessageBox.cs @@ -149,7 +149,7 @@ namespace Terminal.Gui { } // Rin the modal; do not shutdown the mainloop driver when done - Application.Run (d, false); + Application.Run (d); return clicked; } } diff --git a/UICatalog/Scenario.cs b/UICatalog/Scenario.cs index 1073db24b..4bd963662 100644 --- a/UICatalog/Scenario.cs +++ b/UICatalog/Scenario.cs @@ -197,8 +197,8 @@ namespace UICatalog { /// public virtual void Run () { - // This method already performs a later automatic shutdown. - Application.Run (Top, false); + // Must explicit call Application.Shutdown method to shutdown. + Application.Run (Top); } /// diff --git a/UICatalog/Scenarios/HexEditor.cs b/UICatalog/Scenarios/HexEditor.cs index 03631edb6..c9015f72a 100644 --- a/UICatalog/Scenarios/HexEditor.cs +++ b/UICatalog/Scenarios/HexEditor.cs @@ -102,7 +102,7 @@ namespace UICatalog { private void Open () { var d = new OpenDialog ("Open", "Open a file") { AllowsMultipleSelection = false }; - Application.Run (d, false); + Application.Run (d); if (!d.Canceled) { _fileName = d.FilePaths [0]; diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 5770a0246..c240044c7 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -93,10 +93,13 @@ namespace UICatalog { #endif Application.UseSystemConsole = _useSystemConsole; - Application.Init (); scenario.Init (Application.Top, _baseColorScheme); scenario.Setup (); scenario.Run (); + _top.Ready += () => { + _top.SetFocus (_rightPane); + _top.Ready = null; + }; #if DEBUG_IDISPOSABLE // After the scenario runs, validate all Responder-based instances @@ -108,6 +111,8 @@ namespace UICatalog { #endif } + Application.Shutdown (); + #if DEBUG_IDISPOSABLE // This proves that when the user exited the UI Catalog app // it cleaned up properly. @@ -230,13 +235,11 @@ namespace UICatalog { _top.Add (_statusBar); _top.Ready += () => { if (_runningScenario != null) { - _top.SetFocus (_rightPane); _runningScenario = null; } }; Application.Run (_top); - Application.Shutdown (); return _runningScenario; }