Преглед изворни кода

Rename to SubscribeDriverEvents and add UnsubscribeDriverEvents method.

BDisp пре 7 месеци
родитељ
комит
be07f5a276

+ 10 - 3
Terminal.Gui/Application/Application.Initialization.cs

@@ -150,6 +150,7 @@ public static partial class Application // Initialization (Init/Shutdown)
         try
         {
             MainLoop = Driver!.Init ();
+            SubscribeDriverEvents ();
         }
         catch (InvalidOperationException ex)
         {
@@ -163,8 +164,6 @@ public static partial class Application // Initialization (Init/Shutdown)
                                                 );
         }
 
-        InitState ();
-
         SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());
 
         SupportedCultures = GetSupportedCultures ();
@@ -173,7 +172,7 @@ public static partial class Application // Initialization (Init/Shutdown)
         InitializedChanged?.Invoke (null, new (init));
     }
 
-    internal static void InitState ()
+    internal static void SubscribeDriverEvents ()
     {
         ArgumentNullException.ThrowIfNull (Driver);
 
@@ -183,6 +182,14 @@ public static partial class Application // Initialization (Init/Shutdown)
         Driver.MouseEvent += Driver_MouseEvent;
     }
 
+    internal static void UnsubscribeDriverEvents ()
+    {
+        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_KeyDown (object? sender, Key e) { RaiseKeyDownEvent (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
         if (Driver is { })
         {
-            Driver.SizeChanged -= Driver_SizeChanged;
-            Driver.KeyDown -= Driver_KeyDown;
-            Driver.KeyUp -= Driver_KeyUp;
-            Driver.MouseEvent -= Driver_MouseEvent;
+            UnsubscribeDriverEvents ();
             Driver?.End ();
             Driver = null;
         }

+ 1 - 1
UnitTests/Application/ApplicationScreenTests.cs

@@ -73,7 +73,7 @@ public class ApplicationScreenTests (ITestOutputHelper output)
         Application.ResetState (true);
         Assert.Null (Application.Driver);
         Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
-        Application.InitState ();
+        Application.SubscribeDriverEvents ();
         Assert.Equal (new (0, 0, 25, 25), Application.Screen);
 
         // Act

+ 1 - 1
UnitTests/Application/ApplicationTests.cs

@@ -644,7 +644,7 @@ public class ApplicationTests
     [Fact]
     public void InitState_Throws_If_Driver_Is_Null ()
     {
-        Assert.Throws<ArgumentNullException> (static () => Application.InitState ());
+        Assert.Throws<ArgumentNullException> (static () => Application.SubscribeDriverEvents ());
     }
 
     private void Init ()

+ 1 - 1
UnitTests/TestHelpers.cs

@@ -212,7 +212,7 @@ public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
         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.InitState ();
+        Application.SubscribeDriverEvents ();
 
         base.Before (methodUnderTest);
     }