diff --git a/Terminal.Gui/App/Application.Run.cs b/Terminal.Gui/App/Application.Run.cs index d6dbb6728..0fbda40aa 100644 --- a/Terminal.Gui/App/Application.Run.cs +++ b/Terminal.Gui/App/Application.Run.cs @@ -398,7 +398,11 @@ public static partial class Application // Run (Begin -> Run -> Layout/Draw -> E /// Set to true to cause to be called after the first iteration. Set to false (the default) to /// cause the application to continue running until Application.RequestStop () is called. /// - public static bool EndAfterFirstIteration { get; set; } + public static bool EndAfterFirstIteration + { + get => ApplicationImpl.Instance.EndAfterFirstIteration; + set => ApplicationImpl.Instance.EndAfterFirstIteration = value; + } /// Building block API: Runs the main loop for the created . /// The state returned by the method. diff --git a/Terminal.Gui/App/Application.Screen.cs b/Terminal.Gui/App/Application.Screen.cs index 3804c19c7..920dec1fa 100644 --- a/Terminal.Gui/App/Application.Screen.cs +++ b/Terminal.Gui/App/Application.Screen.cs @@ -23,12 +23,31 @@ public static partial class Application // Screen related stuff; intended to hid /// Event handlers can set to to prevent /// from changing it's size to match the new terminal size. /// - public static event EventHandler? SizeChanging; + public static event EventHandler? SizeChanging + { + add + { + if (ApplicationImpl.Instance is ApplicationImpl impl) + { + impl.SizeChanging += value; + } + } + remove + { + if (ApplicationImpl.Instance is ApplicationImpl impl) + { + impl.SizeChanging -= value; + } + } + } // Internal helper method for ApplicationImpl.ResetState to clear this event internal static void ClearSizeChangingEvent () { - SizeChanging = null; + if (ApplicationImpl.Instance is ApplicationImpl impl) + { + impl.SizeChanging = null; + } } /// @@ -39,24 +58,7 @@ public static partial class Application // Screen related stuff; intended to hid /// if the size was changed. public static bool OnSizeChanging (SizeChangedEventArgs args) { - SizeChanging?.Invoke (null, args); - - if (args.Cancel || args.Size is null) - { - return false; - } - - Screen = new (Point.Empty, args.Size.Value); - - foreach (Toplevel t in TopLevels) - { - t.OnSizeChanging (new (args.Size)); - t.SetNeedsLayout (); - } - - LayoutAndDraw (true); - - return true; + return ApplicationImpl.Instance.OnSizeChanging (args); } /// diff --git a/Terminal.Gui/App/ApplicationImpl.cs b/Terminal.Gui/App/ApplicationImpl.cs index 5b6809ee5..d6cf56a53 100644 --- a/Terminal.Gui/App/ApplicationImpl.cs +++ b/Terminal.Gui/App/ApplicationImpl.cs @@ -128,6 +128,9 @@ public class ApplicationImpl : IApplication /// public bool ClearScreenNextIteration { get; set; } + /// + public bool EndAfterFirstIteration { get; set; } + /// public ApplicationPopover? Popover { get; set; } @@ -157,6 +160,11 @@ public class ApplicationImpl : IApplication } } + /// + /// This event is raised when the application's size changes. + /// + public event EventHandler? SizeChanging; + /// public void RequestStop () { RequestStop (null); } @@ -421,6 +429,29 @@ public class ApplicationImpl : IApplication _driver?.Refresh (); } + /// + public bool OnSizeChanging (SizeChangedEventArgs args) + { + SizeChanging?.Invoke (null, args); + + if (args.Cancel || args.Size is null) + { + return false; + } + + Screen = new (Point.Empty, args.Size.Value); + + foreach (Toplevel t in TopLevels) + { + t.OnSizeChanging (new (args.Size)); + t.SetNeedsLayout (); + } + + LayoutAndDraw (true); + + return true; + } + /// public void ResetState (bool ignoreDisposed = false) { @@ -463,9 +494,9 @@ public class ApplicationImpl : IApplication MainThreadId = -1; - // These static properties need to be reset - Application.EndAfterFirstIteration = false; - Application.ClearScreenNextIteration = false; + // Reset iteration flags + EndAfterFirstIteration = false; + ClearScreenNextIteration = false; // Driver stuff if (_driver is { }) @@ -488,12 +519,12 @@ public class ApplicationImpl : IApplication // Do not clear _lastMousePosition; Popovers require it to stay set with // last mouse pos. //_lastMousePosition = null; - Application.CachedViewsUnderMouse.Clear (); - Application.ResetMouseState (); + Mouse.CachedViewsUnderMouse.Clear (); + Mouse.ResetState (); // Keyboard events and bindings are now managed by the Keyboard instance - Application.ClearSizeChangingEvent (); + SizeChanging = null; Navigation = null; @@ -623,11 +654,11 @@ public class ApplicationImpl : IApplication return new MainLoopCoordinator (_timedEvents, inputBuffer, loop, cf); } - private void Driver_KeyDown (object? sender, Key e) { Application.RaiseKeyDownEvent (e); } - private void Driver_KeyUp (object? sender, Key e) { Application.RaiseKeyUpEvent (e); } - private void Driver_MouseEvent (object? sender, MouseEventArgs e) { Application.RaiseMouseEvent (e); } + private void Driver_KeyDown (object? sender, Key e) { Keyboard.RaiseKeyDownEvent (e); } + private void Driver_KeyUp (object? sender, Key e) { Keyboard.RaiseKeyUpEvent (e); } + private void Driver_MouseEvent (object? sender, MouseEventArgs e) { Mouse.RaiseMouseEvent (e); } - private void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { Application.OnSizeChanging (e); } + private void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { OnSizeChanging (e); } private static List GetAvailableCulturesFromEmbeddedResources () { diff --git a/Terminal.Gui/App/IApplication.cs b/Terminal.Gui/App/IApplication.cs index a2777327b..65332c23d 100644 --- a/Terminal.Gui/App/IApplication.cs +++ b/Terminal.Gui/App/IApplication.cs @@ -27,6 +27,14 @@ public interface IApplication /// Gets or sets the console driver being used. IConsoleDriver? Driver { get; set; } + /// + /// This is intended for unit tests and won't stop the if set to + /// If the caller calls with the returned token from , setting + /// this property to will cause only one iteration of the main loop to execute. The default is + /// , which will cause the application to continue running until Application.RequestStop () is called. + /// + bool EndAfterFirstIteration { get; set; } + /// /// Gets or sets whether will be forced to output only the 16 colors defined in /// . The default is , meaning 24-bit (TrueColor) colors will be output @@ -101,6 +109,14 @@ public interface IApplication /// public void LayoutAndDraw (bool forceRedraw = false); + /// + /// Called when the application's size changes. Sets the size of all s and fires the + /// SizeChanging event. + /// + /// The new size. + /// if the size was changed. + bool OnSizeChanging (SizeChangedEventArgs args); + /// /// Maximum number of iterations of the main loop (and hence draws) /// to allow to occur per second. Defaults to which is