|
@@ -8,19 +8,12 @@ namespace Terminal.Gui;
|
|
|
/// <summary>A static, singleton class representing the application. This class is the entry point for the application.</summary>
|
|
|
/// <example>
|
|
|
/// <code>
|
|
|
-/// // A simple Terminal.Gui app that creates a window with a frame and title with
|
|
|
-/// // 5 rows/columns of padding.
|
|
|
-/// Application.Init();
|
|
|
-/// var win = new Window ($"Example App ({Application.QuitKey} to quit)") {
|
|
|
-/// X = 5,
|
|
|
-/// Y = 5,
|
|
|
-/// Width = Dim.Fill (5),
|
|
|
-/// Height = Dim.Fill (5)
|
|
|
-/// };
|
|
|
-/// Application.Top.Add(win);
|
|
|
-/// Application.Run();
|
|
|
-/// Application.Shutdown();
|
|
|
-/// </code>
|
|
|
+/// Application.Init();
|
|
|
+/// var win = new Window ($"Example App ({Application.QuitKey} to quit)");
|
|
|
+/// Application.Run(win);
|
|
|
+/// win.Dispose();
|
|
|
+/// Application.Shutdown();
|
|
|
+/// </code>
|
|
|
/// </example>
|
|
|
/// <remarks>TODO: Flush this out.</remarks>
|
|
|
public static partial class Application
|
|
@@ -75,6 +68,10 @@ public static partial class Application
|
|
|
.ToList ();
|
|
|
}
|
|
|
|
|
|
+ // When `End ()` is called, it is possible `RunState.Toplevel` is a different object than `Top`.
|
|
|
+ // This variable is set in `End` in this case so that `Begin` correctly sets `Top`.
|
|
|
+ private static Toplevel _cachedRunStateToplevel;
|
|
|
+
|
|
|
// IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test.
|
|
|
// Encapsulate all setting of initial state for Application; Having
|
|
|
// this in a function like this ensures we don't make mistakes in
|
|
@@ -88,13 +85,32 @@ public static partial class Application
|
|
|
foreach (Toplevel t in _topLevels)
|
|
|
{
|
|
|
t.Running = false;
|
|
|
- t.Dispose ();
|
|
|
+#if DEBUG_IDISPOSABLE
|
|
|
+
|
|
|
+ // Don't dispose the toplevels. It's up to caller dispose them
|
|
|
+ Debug.Assert (t.WasDisposed);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
_topLevels.Clear ();
|
|
|
Current = null;
|
|
|
- Top?.Dispose ();
|
|
|
+#if DEBUG_IDISPOSABLE
|
|
|
+
|
|
|
+ // Don't dispose the Top. It's up to caller dispose it
|
|
|
+ if (Top is { })
|
|
|
+ {
|
|
|
+ Debug.Assert (Top.WasDisposed);
|
|
|
+
|
|
|
+ // If End wasn't called _cachedRunStateToplevel may be null
|
|
|
+ if (_cachedRunStateToplevel is { })
|
|
|
+ {
|
|
|
+ Debug.Assert (_cachedRunStateToplevel.WasDisposed);
|
|
|
+ Debug.Assert (_cachedRunStateToplevel == Top);
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
Top = null;
|
|
|
+ _cachedRunStateToplevel = null;
|
|
|
|
|
|
// MainLoop stuff
|
|
|
MainLoop?.Dispose ();
|
|
@@ -161,12 +177,14 @@ public static partial class Application
|
|
|
/// </para>
|
|
|
/// <para>
|
|
|
/// <see cref="Shutdown"/> must be called when the application is closing (typically after
|
|
|
- /// <see cref="Run(Func{Exception, bool})"/> has returned) to ensure resources are cleaned up and terminal settings
|
|
|
+ /// <see cref="Run(Func{Exception, bool}, ConsoleDriver)"/> has returned) to ensure resources are cleaned up and
|
|
|
+ /// terminal settings
|
|
|
/// restored.
|
|
|
/// </para>
|
|
|
/// <para>
|
|
|
/// The <see cref="Run{T}(Func{Exception, bool}, ConsoleDriver)"/> function combines
|
|
|
- /// <see cref="Init(ConsoleDriver, string)"/> and <see cref="Run(Toplevel, Func{Exception, bool})"/> into a single
|
|
|
+ /// <see cref="Init(ConsoleDriver, string)"/> and <see cref="Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>
|
|
|
+ /// into a single
|
|
|
/// call. An application cam use <see cref="Run{T}(Func{Exception, bool}, ConsoleDriver)"/> without explicitly calling
|
|
|
/// <see cref="Init(ConsoleDriver, string)"/>.
|
|
|
/// </para>
|
|
@@ -179,7 +197,7 @@ public static partial class Application
|
|
|
/// <see cref="ConsoleDriver"/> to use. If neither <paramref name="driver"/> or <paramref name="driverName"/> are
|
|
|
/// specified the default driver for the platform will be used.
|
|
|
/// </param>
|
|
|
- public static void Init (ConsoleDriver driver = null, string driverName = null) { InternalInit (() => new Toplevel (), driver, driverName); }
|
|
|
+ public static void Init (ConsoleDriver driver = null, string driverName = null) { InternalInit (driver, driverName); }
|
|
|
|
|
|
internal static bool _initialized;
|
|
|
internal static int _mainThreadId = -1;
|
|
@@ -194,7 +212,6 @@ public static partial class Application
|
|
|
//
|
|
|
// calledViaRunT: If false (default) all state will be reset. If true the state will not be reset.
|
|
|
internal static void InternalInit (
|
|
|
- Func<Toplevel> topLevelFactory,
|
|
|
ConsoleDriver driver = null,
|
|
|
string driverName = null,
|
|
|
bool calledViaRunT = false
|
|
@@ -227,7 +244,8 @@ public static partial class Application
|
|
|
// multiple times. We need to do this because some settings are only
|
|
|
// valid after a Driver is loaded. In this cases we need just
|
|
|
// `Settings` so we can determine which driver to use.
|
|
|
- Load (true);
|
|
|
+ // Don't reset, so we can inherit the theme from the previous run.
|
|
|
+ Load ();
|
|
|
Apply ();
|
|
|
|
|
|
// Ignore Configuration for ForceDriver if driverName is specified
|
|
@@ -292,12 +310,6 @@ public static partial class Application
|
|
|
|
|
|
SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());
|
|
|
|
|
|
- Top = topLevelFactory ();
|
|
|
- Current = Top;
|
|
|
-
|
|
|
- // Ensure Top's layout is up to date.
|
|
|
- Current.SetRelativeLayout (Driver.Bounds);
|
|
|
-
|
|
|
SupportedCultures = GetSupportedCultures ();
|
|
|
_mainThreadId = Thread.CurrentThread.ManagedThreadId;
|
|
|
_initialized = true;
|
|
@@ -332,11 +344,13 @@ public static partial class Application
|
|
|
/// <summary>Shutdown an application initialized with <see cref="Init"/>.</summary>
|
|
|
/// <remarks>
|
|
|
/// Shutdown must be called for every call to <see cref="Init"/> or
|
|
|
- /// <see cref="Application.Run(Toplevel, Func{Exception, bool})"/> to ensure all resources are cleaned up (Disposed)
|
|
|
+ /// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/> to ensure all resources are cleaned
|
|
|
+ /// up (Disposed)
|
|
|
/// and terminal settings are restored.
|
|
|
/// </remarks>
|
|
|
public static void Shutdown ()
|
|
|
{
|
|
|
+ // TODO: Throw an exception if Init hasn't been called.
|
|
|
ResetState ();
|
|
|
PrintJsonErrors ();
|
|
|
}
|
|
@@ -369,59 +383,76 @@ public static partial class Application
|
|
|
/// The <see cref="RunState"/> handle that needs to be passed to the <see cref="End(RunState)"/> method upon
|
|
|
/// completion.
|
|
|
/// </returns>
|
|
|
- /// <param name="Toplevel">The <see cref="Toplevel"/> to prepare execution for.</param>
|
|
|
+ /// <param name="toplevel">The <see cref="Toplevel"/> to prepare execution for.</param>
|
|
|
/// <remarks>
|
|
|
/// This method prepares the provided <see cref="Toplevel"/> for running with the focus, it adds this to the list
|
|
|
/// of <see cref="Toplevel"/>s, lays out the Subviews, focuses the first element, and draws the <see cref="Toplevel"/>
|
|
|
/// in the screen. This is usually followed by executing the <see cref="RunLoop"/> method, and then the
|
|
|
/// <see cref="End(RunState)"/> method upon termination which will undo these changes.
|
|
|
/// </remarks>
|
|
|
- public static RunState Begin (Toplevel Toplevel)
|
|
|
+ public static RunState Begin (Toplevel toplevel)
|
|
|
{
|
|
|
- if (Toplevel is null)
|
|
|
- {
|
|
|
- throw new ArgumentNullException (nameof (Toplevel));
|
|
|
- }
|
|
|
+ ArgumentNullException.ThrowIfNull (toplevel);
|
|
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
|
- Debug.Assert (!Toplevel.WasDisposed);
|
|
|
+ Debug.Assert (!toplevel.WasDisposed);
|
|
|
+
|
|
|
+ if (_cachedRunStateToplevel is { } && _cachedRunStateToplevel != toplevel)
|
|
|
+ {
|
|
|
+ Debug.Assert (_cachedRunStateToplevel.WasDisposed);
|
|
|
+ }
|
|
|
#endif
|
|
|
|
|
|
- if (Toplevel.IsOverlappedContainer && OverlappedTop != Toplevel && OverlappedTop is { })
|
|
|
+ if (toplevel.IsOverlappedContainer && OverlappedTop != toplevel && OverlappedTop is { })
|
|
|
{
|
|
|
throw new InvalidOperationException ("Only one Overlapped Container is allowed.");
|
|
|
}
|
|
|
|
|
|
- // Ensure the mouse is ungrabed.
|
|
|
+ // Ensure the mouse is ungrabbed.
|
|
|
MouseGrabView = null;
|
|
|
|
|
|
- var rs = new RunState (Toplevel);
|
|
|
+ var rs = new RunState (toplevel);
|
|
|
|
|
|
// View implements ISupportInitializeNotification which is derived from ISupportInitialize
|
|
|
- if (!Toplevel.IsInitialized)
|
|
|
+ if (!toplevel.IsInitialized)
|
|
|
{
|
|
|
- Toplevel.BeginInit ();
|
|
|
- Toplevel.EndInit ();
|
|
|
+ toplevel.BeginInit ();
|
|
|
+ toplevel.EndInit ();
|
|
|
}
|
|
|
|
|
|
+#if DEBUG_IDISPOSABLE
|
|
|
+ if (Top is { } && toplevel != Top && !_topLevels.Contains (Top))
|
|
|
+ {
|
|
|
+ // This assertion confirm if the Top was already disposed
|
|
|
+ Debug.Assert (Top.WasDisposed);
|
|
|
+ Debug.Assert (Top == _cachedRunStateToplevel);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
lock (_topLevels)
|
|
|
{
|
|
|
- // If Top was already initialized with Init, and Begin has never been called
|
|
|
- // Top was not added to the Toplevels Stack. It will thus never get disposed.
|
|
|
- // Clean it up here:
|
|
|
- if (Top is { } && Toplevel != Top && !_topLevels.Contains (Top))
|
|
|
+ if (Top is { } && toplevel != Top && !_topLevels.Contains (Top))
|
|
|
{
|
|
|
- Top.Dispose ();
|
|
|
- Top = null;
|
|
|
+ // If Top was already disposed and isn't on the Toplevels Stack,
|
|
|
+ // clean it up here if is the same as _cachedRunStateToplevel
|
|
|
+ if (Top == _cachedRunStateToplevel)
|
|
|
+ {
|
|
|
+ Top = null;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // Probably this will never hit
|
|
|
+ throw new ObjectDisposedException (Top.GetType ().FullName);
|
|
|
+ }
|
|
|
}
|
|
|
- else if (Top is { } && Toplevel != Top && _topLevels.Contains (Top))
|
|
|
+ else if (OverlappedTop is { } && toplevel != Top && _topLevels.Contains (Top))
|
|
|
{
|
|
|
- Top.OnLeave (Toplevel);
|
|
|
+ Top.OnLeave (toplevel);
|
|
|
}
|
|
|
|
|
|
// BUGBUG: We should not depend on `Id` internally.
|
|
|
// BUGBUG: It is super unclear what this code does anyway.
|
|
|
- if (string.IsNullOrEmpty (Toplevel.Id))
|
|
|
+ if (string.IsNullOrEmpty (toplevel.Id))
|
|
|
{
|
|
|
var count = 1;
|
|
|
var id = (_topLevels.Count + count).ToString ();
|
|
@@ -432,42 +463,46 @@ public static partial class Application
|
|
|
id = (_topLevels.Count + count).ToString ();
|
|
|
}
|
|
|
|
|
|
- Toplevel.Id = (_topLevels.Count + count).ToString ();
|
|
|
+ toplevel.Id = (_topLevels.Count + count).ToString ();
|
|
|
|
|
|
- _topLevels.Push (Toplevel);
|
|
|
+ _topLevels.Push (toplevel);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- Toplevel dup = _topLevels.FirstOrDefault (x => x.Id == Toplevel.Id);
|
|
|
+ Toplevel dup = _topLevels.FirstOrDefault (x => x.Id == toplevel.Id);
|
|
|
|
|
|
if (dup is null)
|
|
|
{
|
|
|
- _topLevels.Push (Toplevel);
|
|
|
+ _topLevels.Push (toplevel);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (_topLevels.FindDuplicates (new ToplevelEqualityComparer ()).Count > 0)
|
|
|
{
|
|
|
- throw new ArgumentException ("There are duplicates Toplevels Id's");
|
|
|
+ throw new ArgumentException ("There are duplicates Toplevel IDs");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (Top is null || Toplevel.IsOverlappedContainer)
|
|
|
+ if (Top is null || toplevel.IsOverlappedContainer)
|
|
|
{
|
|
|
- Top = Toplevel;
|
|
|
+ Top = toplevel;
|
|
|
}
|
|
|
|
|
|
var refreshDriver = true;
|
|
|
|
|
|
- if (OverlappedTop == null
|
|
|
- || Toplevel.IsOverlappedContainer
|
|
|
- || (Current?.Modal == false && Toplevel.Modal)
|
|
|
- || (Current?.Modal == false && !Toplevel.Modal)
|
|
|
- || (Current?.Modal == true && Toplevel.Modal))
|
|
|
+ if (OverlappedTop is null
|
|
|
+ || toplevel.IsOverlappedContainer
|
|
|
+ || (Current?.Modal == false && toplevel.Modal)
|
|
|
+ || (Current?.Modal == false && !toplevel.Modal)
|
|
|
+ || (Current?.Modal == true && toplevel.Modal))
|
|
|
{
|
|
|
- if (Toplevel.Visible)
|
|
|
+ if (toplevel.Visible)
|
|
|
{
|
|
|
- Current = Toplevel;
|
|
|
+ Current?.OnDeactivate (toplevel);
|
|
|
+ Toplevel previousCurrent = Current;
|
|
|
+ Current = toplevel;
|
|
|
+ Current.OnActivate (previousCurrent);
|
|
|
+
|
|
|
SetCurrentOverlappedAsTop ();
|
|
|
}
|
|
|
else
|
|
@@ -476,13 +511,13 @@ public static partial class Application
|
|
|
}
|
|
|
}
|
|
|
else if ((OverlappedTop != null
|
|
|
- && Toplevel != OverlappedTop
|
|
|
+ && toplevel != OverlappedTop
|
|
|
&& Current?.Modal == true
|
|
|
&& !_topLevels.Peek ().Modal)
|
|
|
- || (OverlappedTop is { } && Toplevel != OverlappedTop && Current?.Running == false))
|
|
|
+ || (OverlappedTop is { } && toplevel != OverlappedTop && Current?.Running == false))
|
|
|
{
|
|
|
refreshDriver = false;
|
|
|
- MoveCurrent (Toplevel);
|
|
|
+ MoveCurrent (toplevel);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -490,113 +525,88 @@ public static partial class Application
|
|
|
MoveCurrent (Current);
|
|
|
}
|
|
|
|
|
|
- //if (Toplevel.LayoutStyle == LayoutStyle.Computed) {
|
|
|
- Toplevel.SetRelativeLayout (Driver.Bounds);
|
|
|
+ toplevel.SetRelativeLayout (Driver.Bounds);
|
|
|
|
|
|
- //}
|
|
|
-
|
|
|
- // BUGBUG: This call is likley not needed.
|
|
|
- Toplevel.LayoutSubviews ();
|
|
|
- Toplevel.PositionToplevels ();
|
|
|
- Toplevel.FocusFirst ();
|
|
|
+ // BUGBUG: This call is likely not needed.
|
|
|
+ toplevel.LayoutSubviews ();
|
|
|
+ toplevel.PositionToplevels ();
|
|
|
+ toplevel.FocusFirst ();
|
|
|
|
|
|
if (refreshDriver)
|
|
|
{
|
|
|
- OverlappedTop?.OnChildLoaded (Toplevel);
|
|
|
- Toplevel.OnLoaded ();
|
|
|
- Toplevel.SetNeedsDisplay ();
|
|
|
- Toplevel.Draw ();
|
|
|
- Toplevel.PositionCursor ();
|
|
|
+ OverlappedTop?.OnChildLoaded (toplevel);
|
|
|
+ toplevel.OnLoaded ();
|
|
|
+ toplevel.SetNeedsDisplay ();
|
|
|
+ toplevel.Draw ();
|
|
|
+ toplevel.PositionCursor ();
|
|
|
Driver.Refresh ();
|
|
|
}
|
|
|
|
|
|
- NotifyNewRunState?.Invoke (Toplevel, new RunStateEventArgs (rs));
|
|
|
+ NotifyNewRunState?.Invoke (toplevel, new (rs));
|
|
|
|
|
|
return rs;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Runs the application by calling <see cref="Run(Toplevel, Func{Exception, bool})"/> with the value of
|
|
|
- /// <see cref="Top"/>.
|
|
|
+ /// Runs the application by creating a <see cref="Toplevel"/> object and calling
|
|
|
+ /// <see cref="Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>.
|
|
|
/// </summary>
|
|
|
- /// <remarks>See <see cref="Run(Toplevel, Func{Exception, bool})"/> for more details.</remarks>
|
|
|
- public static void Run (Func<Exception, bool> errorHandler = null) { Run (Top, errorHandler); }
|
|
|
+ /// <remarks>
|
|
|
+ /// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
|
|
|
+ /// <para>
|
|
|
+ /// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has returned) to
|
|
|
+ /// ensure resources are cleaned up and terminal settings restored.
|
|
|
+ /// </para>
|
|
|
+ /// <para>
|
|
|
+ /// The caller is responsible for disposing the object returned by this method.
|
|
|
+ /// </para>
|
|
|
+ /// </remarks>
|
|
|
+ /// <returns>The created <see cref="Toplevel"/> object. The caller is responsible for disposing this object.</returns>
|
|
|
+ public static Toplevel Run (Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null) { return Run<Toplevel> (errorHandler, driver); }
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Runs the application by calling <see cref="Run(Toplevel, Func{Exception, bool})"/> with a new instance of the
|
|
|
- /// specified <see cref="Toplevel"/>-derived class.
|
|
|
+ /// Runs the application by creating a <see cref="Toplevel"/>-derived object of type <c>T</c> and calling
|
|
|
+ /// <see cref="Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>.
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>
|
|
|
/// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
|
|
|
/// <para>
|
|
|
/// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has returned) to
|
|
|
/// ensure resources are cleaned up and terminal settings restored.
|
|
|
/// </para>
|
|
|
- /// </summary>
|
|
|
- /// <remarks>See <see cref="Run(Toplevel, Func{Exception, bool})"/> for more details.</remarks>
|
|
|
+ /// <para>
|
|
|
+ /// The caller is responsible for disposing the object returned by this method.
|
|
|
+ /// </para>
|
|
|
+ /// </remarks>
|
|
|
/// <param name="errorHandler"></param>
|
|
|
/// <param name="driver">
|
|
|
/// The <see cref="ConsoleDriver"/> to use. If not specified the default driver for the platform will
|
|
|
/// be used ( <see cref="WindowsDriver"/>, <see cref="CursesDriver"/>, or <see cref="NetDriver"/>). Must be
|
|
|
/// <see langword="null"/> if <see cref="Init"/> has already been called.
|
|
|
/// </param>
|
|
|
- public static void Run<T> (Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null)
|
|
|
+ /// <returns>The created T object. The caller is responsible for disposing this object.</returns>
|
|
|
+ public static T Run<T> (Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null)
|
|
|
where T : Toplevel, new()
|
|
|
{
|
|
|
- if (_initialized)
|
|
|
- {
|
|
|
- // Init created Application.Top. If it hasn't been disposed...
|
|
|
- if (Top is { })
|
|
|
- {
|
|
|
- Top.Dispose ();
|
|
|
- Top = null;
|
|
|
- }
|
|
|
+ var top = new T ();
|
|
|
|
|
|
- if (Driver is { })
|
|
|
- {
|
|
|
- // Init() has been called and we have a driver, so just run the app.
|
|
|
- // This Toplevel will get disposed in `Shutdown`
|
|
|
- var top = new T ();
|
|
|
- Type type = top.GetType ().BaseType;
|
|
|
+ Run (top, errorHandler, driver);
|
|
|
|
|
|
- while (type != typeof (Toplevel) && type != typeof (object))
|
|
|
- {
|
|
|
- type = type.BaseType;
|
|
|
- }
|
|
|
-
|
|
|
- if (type != typeof (Toplevel))
|
|
|
- {
|
|
|
- throw new ArgumentException ($"{top.GetType ().Name} must be derived from TopLevel");
|
|
|
- }
|
|
|
-
|
|
|
- Run (top, errorHandler);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- // This code path should be impossible because Init(null, null) will select the platform default driver
|
|
|
- throw new InvalidOperationException (
|
|
|
- "Init() completed without a driver being set (this should be impossible); Run<T>() cannot be called."
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- // Init() has NOT been called.
|
|
|
- InternalInit (() => new T (), driver, null, true);
|
|
|
- Run (Top, errorHandler);
|
|
|
- }
|
|
|
+ return top;
|
|
|
}
|
|
|
|
|
|
- /// <summary>Runs the main loop on the given <see cref="Toplevel"/> container.</summary>
|
|
|
+ /// <summary>Runs the Application using the provided <see cref="Toplevel"/> view.</summary>
|
|
|
/// <remarks>
|
|
|
/// <para>
|
|
|
/// This method is used to start processing events for the main application, but it is also used to run other
|
|
|
/// modal <see cref="View"/>s such as <see cref="Dialog"/> boxes.
|
|
|
/// </para>
|
|
|
/// <para>
|
|
|
- /// To make a <see cref="Run(Toplevel, Func{Exception, bool})"/> stop execution, call
|
|
|
+ /// To make a <see cref="Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/> stop execution, call
|
|
|
/// <see cref="Application.RequestStop"/>.
|
|
|
/// </para>
|
|
|
/// <para>
|
|
|
- /// Calling <see cref="Run(Toplevel, Func{Exception, bool})"/> is equivalent to calling
|
|
|
+ /// Calling <see cref="Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/> is equivalent to calling
|
|
|
/// <see cref="Begin(Toplevel)"/>, followed by <see cref="RunLoop(RunState)"/>, and then calling
|
|
|
/// <see cref="End(RunState)"/>.
|
|
|
/// </para>
|
|
@@ -607,6 +617,7 @@ public static partial class Application
|
|
|
/// <see cref="RunLoop(RunState)"/> method will only process any pending events, timers, idle handlers and then
|
|
|
/// return control immediately.
|
|
|
/// </para>
|
|
|
+ /// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
|
|
|
/// <para>
|
|
|
/// RELEASE builds only: When <paramref name="errorHandler"/> is <see langword="null"/> any exceptions will be
|
|
|
/// rethrown. Otherwise, if <paramref name="errorHandler"/> will be called. If <paramref name="errorHandler"/>
|
|
@@ -619,8 +630,34 @@ public static partial class Application
|
|
|
/// RELEASE builds only: Handler for any unhandled exceptions (resumes when returns true,
|
|
|
/// rethrows when null).
|
|
|
/// </param>
|
|
|
- public static void Run (Toplevel view, Func<Exception, bool> errorHandler = null)
|
|
|
+ /// <param name="driver">
|
|
|
+ /// The <see cref="ConsoleDriver"/> to use. If not specified the default driver for the platform will
|
|
|
+ /// be used ( <see cref="WindowsDriver"/>, <see cref="CursesDriver"/>, or <see cref="NetDriver"/>). Must be
|
|
|
+ /// <see langword="null"/> if <see cref="Init"/> was called.
|
|
|
+ /// </param>
|
|
|
+ public static void Run (Toplevel view, Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null)
|
|
|
{
|
|
|
+ ArgumentNullException.ThrowIfNull (view);
|
|
|
+
|
|
|
+ if (_initialized)
|
|
|
+ {
|
|
|
+ if (Driver is null)
|
|
|
+ {
|
|
|
+ // Disposing before throwing
|
|
|
+ view.Dispose ();
|
|
|
+
|
|
|
+ // This code path should be impossible because Init(null, null) will select the platform default driver
|
|
|
+ throw new InvalidOperationException (
|
|
|
+ "Init() completed without a driver being set (this should be impossible); Run<T>() cannot be called."
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // Init() has NOT been called.
|
|
|
+ InternalInit (driver, null, true);
|
|
|
+ }
|
|
|
+
|
|
|
var resume = true;
|
|
|
|
|
|
while (resume)
|
|
@@ -636,6 +673,16 @@ public static partial class Application
|
|
|
// 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);
|
|
@@ -735,19 +782,12 @@ public static partial class Application
|
|
|
/// <param name="state">The state returned by the <see cref="Begin(Toplevel)"/> method.</param>
|
|
|
public static void RunLoop (RunState state)
|
|
|
{
|
|
|
- if (state is null)
|
|
|
- {
|
|
|
- throw new ArgumentNullException (nameof (state));
|
|
|
- }
|
|
|
-
|
|
|
- if (state.Toplevel is null)
|
|
|
- {
|
|
|
- throw new ObjectDisposedException ("state");
|
|
|
- }
|
|
|
+ ArgumentNullException.ThrowIfNull (state);
|
|
|
+ ObjectDisposedException.ThrowIf (state.Toplevel is null, "state");
|
|
|
|
|
|
var firstIteration = true;
|
|
|
|
|
|
- for (state.Toplevel.Running = true; state.Toplevel.Running;)
|
|
|
+ for (state.Toplevel.Running = true; state.Toplevel?.Running == true;)
|
|
|
{
|
|
|
MainLoop.Running = true;
|
|
|
|
|
@@ -783,7 +823,7 @@ public static partial class Application
|
|
|
}
|
|
|
|
|
|
MainLoop.RunIteration ();
|
|
|
- Iteration?.Invoke (null, new IterationEventArgs ());
|
|
|
+ Iteration?.Invoke (null, new ());
|
|
|
EnsureModalOrVisibleAlwaysOnTop (state.Toplevel);
|
|
|
|
|
|
if (state.Toplevel != Current)
|
|
@@ -798,6 +838,11 @@ public static partial class Application
|
|
|
|
|
|
firstIteration = false;
|
|
|
|
|
|
+ if (Current == null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (state.Toplevel != Top && (Top.NeedsDisplay || Top.SubViewNeedsDisplay || Top.LayoutNeeded))
|
|
|
{
|
|
|
state.Toplevel.SetNeedsDisplay (state.Toplevel.Frame);
|
|
@@ -842,10 +887,10 @@ public static partial class Application
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// <summary>Stops running the most recent <see cref="Toplevel"/> or the <paramref name="top"/> if provided.</summary>
|
|
|
+ /// <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>
|
|
|
- /// <para>This will cause <see cref="Application.Run(Func{Exception, bool})"/> to return.</para>
|
|
|
+ /// <para>This will cause <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/> to return.</para>
|
|
|
/// <para>
|
|
|
/// Calling <see cref="Application.RequestStop"/> is equivalent to setting the <see cref="Toplevel.Running"/>
|
|
|
/// property on the currently running <see cref="Toplevel"/> to false.
|
|
@@ -880,7 +925,7 @@ public static partial class Application
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- ev = new ToplevelClosingEventArgs (top);
|
|
|
+ ev = new (top);
|
|
|
top.OnClosing (ev);
|
|
|
|
|
|
if (ev.Cancel)
|
|
@@ -968,7 +1013,7 @@ public static partial class Application
|
|
|
{
|
|
|
if (EndAfterFirstIteration)
|
|
|
{
|
|
|
- NotifyStopRunState?.Invoke (top, new ToplevelEventArgs (top));
|
|
|
+ NotifyStopRunState?.Invoke (top, new (top));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -979,10 +1024,7 @@ public static partial class Application
|
|
|
/// <param name="runState">The <see cref="RunState"/> returned by the <see cref="Begin(Toplevel)"/> method.</param>
|
|
|
public static void End (RunState runState)
|
|
|
{
|
|
|
- if (runState is null)
|
|
|
- {
|
|
|
- throw new ArgumentNullException (nameof (runState));
|
|
|
- }
|
|
|
+ ArgumentNullException.ThrowIfNull (runState);
|
|
|
|
|
|
if (OverlappedTop is { })
|
|
|
{
|
|
@@ -1024,6 +1066,11 @@ public static partial class Application
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ if (_topLevels.Count > 1 && _topLevels.Peek () == OverlappedTop && OverlappedChildren.Any (t => t.Visible) is { })
|
|
|
+ {
|
|
|
+ OverlappedMoveNext ();
|
|
|
+ }
|
|
|
+
|
|
|
Current = _topLevels.Peek ();
|
|
|
|
|
|
if (_topLevels.Count == 1 && Current == OverlappedTop)
|
|
@@ -1040,8 +1087,18 @@ public static partial class Application
|
|
|
Refresh ();
|
|
|
}
|
|
|
|
|
|
- // Do NOT dispose .Toplevel here. It was not created by
|
|
|
- // Run, but Init or someone else.
|
|
|
+ // Don't dispose runState.Toplevel. It's up to caller dispose it
|
|
|
+ // If it's not the same as the current in the RunIteration,
|
|
|
+ // it will be fixed later in the next RunIteration.
|
|
|
+ if (OverlappedTop is { } && !_topLevels.Contains (OverlappedTop))
|
|
|
+ {
|
|
|
+ _cachedRunStateToplevel = OverlappedTop;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ _cachedRunStateToplevel = runState.Toplevel;
|
|
|
+ }
|
|
|
+
|
|
|
runState.Toplevel = null;
|
|
|
runState.Dispose ();
|
|
|
}
|
|
@@ -1061,10 +1118,13 @@ public static partial class Application
|
|
|
public static Toplevel Top { get; private set; }
|
|
|
|
|
|
/// <summary>
|
|
|
- /// The current <see cref="Toplevel"/> object. This is updated when
|
|
|
- /// <see cref="Application.Run(Func{Exception, bool})"/> enters and leaves to point to the current
|
|
|
+ /// The current <see cref="Toplevel"/> object. This is updated in <see cref="Application.Begin"/> enters and leaves to
|
|
|
+ /// point to the current
|
|
|
/// <see cref="Toplevel"/> .
|
|
|
/// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// Only relevant in scenarios where <see cref="Toplevel.IsOverlappedContainer"/> is <see langword="true"/>.
|
|
|
+ /// </remarks>
|
|
|
/// <value>The current.</value>
|
|
|
public static Toplevel Current { get; private set; }
|
|
|
|
|
@@ -1182,7 +1242,7 @@ public static partial class Application
|
|
|
&& top != OverlappedTop
|
|
|
&& top != Current
|
|
|
&& Current?.Running == false
|
|
|
- && !top.Running)
|
|
|
+ && top?.Running == false)
|
|
|
{
|
|
|
lock (_topLevels)
|
|
|
{
|
|
@@ -1250,7 +1310,7 @@ public static partial class Application
|
|
|
t.SetRelativeLayout (Rectangle.Empty with { Size = args.Size });
|
|
|
t.LayoutSubviews ();
|
|
|
t.PositionToplevels ();
|
|
|
- t.OnSizeChanging (new SizeChangedEventArgs (args.Size));
|
|
|
+ t.OnSizeChanging (new (args.Size));
|
|
|
}
|
|
|
|
|
|
Refresh ();
|
|
@@ -1316,7 +1376,7 @@ public static partial class Application
|
|
|
|
|
|
if (!OnUnGrabbingMouse (MouseGrabView))
|
|
|
{
|
|
|
- var view = MouseGrabView;
|
|
|
+ View view = MouseGrabView;
|
|
|
MouseGrabView = null;
|
|
|
OnUnGrabbedMouse (view);
|
|
|
}
|
|
@@ -1355,7 +1415,7 @@ public static partial class Application
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- GrabbedMouse?.Invoke (view, new ViewEventArgs (view));
|
|
|
+ GrabbedMouse?.Invoke (view, new (view));
|
|
|
}
|
|
|
|
|
|
private static void OnUnGrabbedMouse (View view)
|
|
@@ -1365,7 +1425,7 @@ public static partial class Application
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- UnGrabbedMouse?.Invoke (view, new ViewEventArgs (view));
|
|
|
+ UnGrabbedMouse?.Invoke (view, new (view));
|
|
|
}
|
|
|
|
|
|
#nullable enable
|
|
@@ -1410,7 +1470,7 @@ public static partial class Application
|
|
|
a.MouseEvent.View = view;
|
|
|
}
|
|
|
|
|
|
- MouseEvent?.Invoke (null, new MouseEventEventArgs (a.MouseEvent));
|
|
|
+ MouseEvent?.Invoke (null, new (a.MouseEvent));
|
|
|
|
|
|
if (a.MouseEvent.Handled)
|
|
|
{
|
|
@@ -1477,9 +1537,9 @@ public static partial class Application
|
|
|
|
|
|
if (view is Adornment adornment)
|
|
|
{
|
|
|
- var frameLoc = adornment.ScreenToFrame (a.MouseEvent.X, a.MouseEvent.Y);
|
|
|
+ Point frameLoc = adornment.ScreenToFrame (a.MouseEvent.X, a.MouseEvent.Y);
|
|
|
|
|
|
- me = new MouseEvent
|
|
|
+ me = new ()
|
|
|
{
|
|
|
X = frameLoc.X,
|
|
|
Y = frameLoc.Y,
|
|
@@ -1492,7 +1552,7 @@ public static partial class Application
|
|
|
{
|
|
|
Point boundsPoint = view.ScreenToBounds (a.MouseEvent.X, a.MouseEvent.Y);
|
|
|
|
|
|
- me = new MouseEvent
|
|
|
+ me = new ()
|
|
|
{
|
|
|
X = boundsPoint.X,
|
|
|
Y = boundsPoint.Y,
|
|
@@ -1556,7 +1616,7 @@ public static partial class Application
|
|
|
{
|
|
|
Key oldKey = _alternateForwardKey;
|
|
|
_alternateForwardKey = value;
|
|
|
- OnAlternateForwardKeyChanged (new KeyChangedEventArgs (oldKey, value));
|
|
|
+ OnAlternateForwardKeyChanged (new (oldKey, value));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -1583,7 +1643,7 @@ public static partial class Application
|
|
|
{
|
|
|
Key oldKey = _alternateBackwardKey;
|
|
|
_alternateBackwardKey = value;
|
|
|
- OnAlternateBackwardKeyChanged (new KeyChangedEventArgs (oldKey, value));
|
|
|
+ OnAlternateBackwardKeyChanged (new (oldKey, value));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -1610,7 +1670,7 @@ public static partial class Application
|
|
|
{
|
|
|
Key oldKey = _quitKey;
|
|
|
_quitKey = value;
|
|
|
- OnQuitKeyChanged (new KeyChangedEventArgs (oldKey, value));
|
|
|
+ OnQuitKeyChanged (new (oldKey, value));
|
|
|
}
|
|
|
}
|
|
|
}
|