Browse Source

Merge branch 'v2_develop' into v2_3778-Command-Decoupling

Tig 8 months ago
parent
commit
334cb6839d

+ 21 - 5
Terminal.Gui/Application/Application.Initialization.cs

@@ -150,6 +150,7 @@ public static partial class Application // Initialization (Init/Shutdown)
         try
         try
         {
         {
             MainLoop = Driver!.Init ();
             MainLoop = Driver!.Init ();
+            SubscribeDriverEvents ();
         }
         }
         catch (InvalidOperationException ex)
         catch (InvalidOperationException ex)
         {
         {
@@ -163,11 +164,6 @@ public static partial class Application // Initialization (Init/Shutdown)
                                                 );
                                                 );
         }
         }
 
 
-        Driver.SizeChanged += Driver_SizeChanged;
-        Driver.KeyDown += Driver_KeyDown;
-        Driver.KeyUp += Driver_KeyUp;
-        Driver.MouseEvent += Driver_MouseEvent;
-
         SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());
         SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());
 
 
         SupportedCultures = GetSupportedCultures ();
         SupportedCultures = GetSupportedCultures ();
@@ -176,6 +172,26 @@ public static partial class Application // Initialization (Init/Shutdown)
         InitializedChanged?.Invoke (null, new (init));
         InitializedChanged?.Invoke (null, new (init));
     }
     }
 
 
+    internal static void SubscribeDriverEvents ()
+    {
+        ArgumentNullException.ThrowIfNull (Driver);
+
+        Driver.SizeChanged += Driver_SizeChanged;
+        Driver.KeyDown += Driver_KeyDown;
+        Driver.KeyUp += Driver_KeyUp;
+        Driver.MouseEvent += Driver_MouseEvent;
+    }
+
+    internal static void UnsubscribeDriverEvents ()
+    {
+        ArgumentNullException.ThrowIfNull (Driver);
+
+        Driver.SizeChanged -= Driver_SizeChanged;
+        Driver.KeyDown -= Driver_KeyDown;
+        Driver.KeyUp -= Driver_KeyUp;
+        Driver.MouseEvent -= Driver_MouseEvent;
+    }
+
     private static void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { OnSizeChanging (e); }
     private static void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { OnSizeChanging (e); }
     private static void Driver_KeyDown (object? sender, Key e) { RaiseKeyDownEvent (e); }
     private static void Driver_KeyDown (object? sender, Key e) { RaiseKeyDownEvent (e); }
     private static void Driver_KeyUp (object? sender, Key e) { RaiseKeyUpEvent (e); }
     private static void Driver_KeyUp (object? sender, Key e) { RaiseKeyUpEvent (e); }

+ 1 - 4
Terminal.Gui/Application/Application.cs

@@ -177,10 +177,7 @@ public static partial class Application
         // Driver stuff
         // Driver stuff
         if (Driver is { })
         if (Driver is { })
         {
         {
-            Driver.SizeChanged -= Driver_SizeChanged;
-            Driver.KeyDown -= Driver_KeyDown;
-            Driver.KeyUp -= Driver_KeyUp;
-            Driver.MouseEvent -= Driver_MouseEvent;
+            UnsubscribeDriverEvents ();
             Driver?.End ();
             Driver?.End ();
             Driver = null;
             Driver = null;
         }
         }

+ 4 - 2
UICatalog/UICatalog.cs

@@ -796,7 +796,8 @@ public class UICatalogApp
                                              {
                                              {
                                                  if (_statusBar.NeedsLayout)
                                                  if (_statusBar.NeedsLayout)
                                                  {
                                                  {
-                                                     //  throw new LayoutException ("DimFunc.Fn aborted because dependent View needs layout.");
+                                                       throw new LayoutException ("DimFunc.Fn aborted because dependent View needs layout.");
+                                                     //_statusBar.Layout ();
                                                  }
                                                  }
                                                  return _statusBar.Frame.Height;
                                                  return _statusBar.Frame.Height;
                                              })),
                                              })),
@@ -825,7 +826,8 @@ public class UICatalogApp
                                              {
                                              {
                                                  if (_statusBar.NeedsLayout)
                                                  if (_statusBar.NeedsLayout)
                                                  {
                                                  {
-                                                     // throw new LayoutException ("DimFunc.Fn aborted because dependent View needs layout.");
+                                                     throw new LayoutException ("DimFunc.Fn aborted because dependent View needs layout.");
+                                                     //_statusBar.Layout ();
                                                  }
                                                  }
                                                  return _statusBar.Frame.Height;
                                                  return _statusBar.Frame.Height;
                                              })),
                                              })),

+ 20 - 0
UnitTests/Application/ApplicationScreenTests.cs

@@ -65,4 +65,24 @@ public class ApplicationScreenTests (ITestOutputHelper output)
         Application.Top = null;
         Application.Top = null;
         Application.Shutdown ();
         Application.Shutdown ();
     }
     }
+
+    [Fact]
+    public void Screen_Changes_OnSizeChanged_Without_Call_Application_Init ()
+    {
+        // Arrange
+        Application.ResetState (true);
+        Assert.Null (Application.Driver);
+        Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
+        Application.SubscribeDriverEvents ();
+        Assert.Equal (new (0, 0, 25, 25), Application.Screen);
+
+        // Act
+        (((FakeDriver)Application.Driver)!).SetBufferSize (120, 30);
+
+        // Assert
+        Assert.Equal (new (0, 0, 120, 30), Application.Screen);
+
+        // Cleanup
+        Application.ResetState (true);
+    }
 }
 }

+ 6 - 0
UnitTests/Application/ApplicationTests.cs

@@ -641,6 +641,12 @@ public class ApplicationTests
         Application.Shutdown ();
         Application.Shutdown ();
     }
     }
 
 
+    [Fact]
+    public void InitState_Throws_If_Driver_Is_Null ()
+    {
+        Assert.Throws<ArgumentNullException> (static () => Application.SubscribeDriverEvents ());
+    }
+
     private void Init ()
     private void Init ()
     {
     {
         Application.Init (new FakeDriver ());
         Application.Init (new FakeDriver ());

+ 6 - 8
UnitTests/TestHelpers.cs

@@ -197,14 +197,9 @@ public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
         // Turn off diagnostic flags in case some test left them on
         // Turn off diagnostic flags in case some test left them on
         View.Diagnostics = ViewDiagnosticFlags.Off;
         View.Diagnostics = ViewDiagnosticFlags.Off;
 
 
-        if (Application.Driver is { })
-        {
-            ((FakeDriver)Application.Driver).Rows = 25;
-            ((FakeDriver)Application.Driver).Cols = 25;
-            ((FakeDriver)Application.Driver).End ();
-        }
-
-        Application.Driver = null;
+        Application.ResetState (true);
+        Assert.Null (Application.Driver);
+        Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
         base.After (methodUnderTest);
         base.After (methodUnderTest);
     }
     }
 
 
@@ -215,6 +210,9 @@ public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
         Application.ResetState (true);
         Application.ResetState (true);
         Assert.Null (Application.Driver);
         Assert.Null (Application.Driver);
         Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
         Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
+        Assert.Equal (new (0, 0, 25, 25), Application.Screen);
+        // Ensures subscribing events, at least for the SizeChanged event
+        Application.SubscribeDriverEvents ();
 
 
         base.Before (methodUnderTest);
         base.Before (methodUnderTest);
     }
     }