From ebccd5415741df12176a817db6ec0eec5daf8523 Mon Sep 17 00:00:00 2001 From: Charlie Kindel Date: Wed, 20 May 2020 11:27:48 -0600 Subject: [PATCH] Add Toplevel.Ready event (#446) * add Ready event to Toplevel --- Terminal.Gui/Core.cs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/Core.cs b/Terminal.Gui/Core.cs index 4da96a6e3..08fa9a53a 100644 --- a/Terminal.Gui/Core.cs +++ b/Terminal.Gui/Core.cs @@ -1469,10 +1469,25 @@ namespace Terminal.Gui { /// public class Toplevel : View { /// - /// This flag is checked on each iteration of the mainloop and it continues - /// running until this flag is set to false. + /// Gets or sets whether the for this is running or not. Setting + /// this property to false will cause the MainLoop to exit. /// - public bool Running; + public bool Running { get; set; } + + /// + /// Fired once the Toplevel's has started it's first iteration. + /// Subscribe to this event to perform tasks when the has been laid out and focus has been set. + /// changes. A Ready event handler is a good place to finalize initialization after calling `(topLevel)`. + /// + public event EventHandler Ready; + + /// + /// Called from Application.RunLoop after the has entered it's first iteration of the loop. + /// + internal virtual void OnReady () + { + Ready?.Invoke (this, EventArgs.Empty); + } /// /// Initializes a new instance of the class with the specified absolute layout. @@ -2419,8 +2434,15 @@ namespace Terminal.Gui { if (state.Toplevel == null) throw new ObjectDisposedException ("state"); + bool firstIteration = true; for (state.Toplevel.Running = true; state.Toplevel.Running;) { if (MainLoop.EventsPending (wait)) { + // Notify Toplevel it's ready + if (firstIteration) { + state.Toplevel.OnReady (); + } + firstIteration = false; + MainLoop.MainIteration (); Iteration?.Invoke (null, EventArgs.Empty); } else if (wait == false)