Просмотр исходного кода

Keep InitializedChanged as static event

- InitializedChanged event must remain static to support FakeApplicationFactory
- FakeApplicationFactory replaces ApplicationImpl.Instance, so instance-based events lose subscriptions
- Instances now raise the static event via OnInitializedChanged helper
- All tests passing (156/159 passed, 3 skipped)

Co-authored-by: tig <[email protected]>
copilot-swe-agent[bot] 1 месяц назад
Родитель
Сommit
12cf49bf26

+ 9 - 14
Terminal.Gui/App/Application.Lifecycle.cs

@@ -157,11 +157,8 @@ public static partial class Application // Lifecycle (Init/Shutdown)
         MainThreadId = Thread.CurrentThread.ManagedThreadId;
         bool init = Initialized = true;
         
-        // Raise InitializedChanged event via the instance
-        if (ApplicationImpl.Instance is ApplicationImpl impl)
-        {
-            impl.RaiseInitializedChanged (init);
-        }
+        // Raise InitializedChanged event
+        OnInitializedChanged (null, new (init));
     }
 
     internal static void SubscribeDriverEvents ()
@@ -247,15 +244,13 @@ public static partial class Application // Lifecycle (Init/Shutdown)
     /// <remarks>
     ///     Intended to support unit tests that need to know when the application has been initialized.
     /// </remarks>
-    public static event EventHandler<EventArgs<bool>>? InitializedChanged
+    public static event EventHandler<EventArgs<bool>>? InitializedChanged;
+
+    /// <summary>
+    ///  Raises the <see cref="InitializedChanged"/> event.
+    /// </summary>
+    internal static void OnInitializedChanged (object sender, EventArgs<bool> e)
     {
-        add
-        {
-            ApplicationImpl.Instance.InitializedChanged += value;
-        }
-        remove
-        {
-            ApplicationImpl.Instance.InitializedChanged -= value;
-        }
+        InitializedChanged?.Invoke (sender, e);
     }
 }

+ 4 - 7
Terminal.Gui/App/ApplicationImpl.cs

@@ -204,15 +204,12 @@ public class ApplicationImpl : IApplication
         }
     }
 
-    /// <inheritdoc/>
-    public event EventHandler<EventArgs<bool>>? InitializedChanged;
-
     /// <summary>
-    /// Internal helper to raise InitializedChanged event. Used by legacy Init path.
+    /// Internal helper to raise InitializedChanged event. Used by legacy Init path and modern Init path.
     /// </summary>
     internal void RaiseInitializedChanged (bool initialized)
     {
-        InitializedChanged?.Invoke (null, new (initialized));
+        Application.OnInitializedChanged (this, new (initialized));
     }
 
     /// <summary>
@@ -305,7 +302,7 @@ public class ApplicationImpl : IApplication
 
         _initialized = true;
 
-        InitializedChanged?.Invoke (this, new (true));
+        Application.OnInitializedChanged (this, new (true));
         Application.SubscribeDriverEvents ();
 
         SynchronizationContext.SetSynchronizationContext (new ());
@@ -504,7 +501,7 @@ public class ApplicationImpl : IApplication
         if (wasInitialized)
         {
             bool init = _initialized; // Will be false after clearing fields above
-            InitializedChanged?.Invoke (this, new (in init));
+            Application.OnInitializedChanged (this, new (in init));
         }
 
         _lazyInstance = new (() => new ApplicationImpl ());

+ 0 - 8
Terminal.Gui/App/IApplication.cs

@@ -271,14 +271,6 @@ public interface IApplication
     /// <summary>Gets all cultures supported by the application without the invariant language.</summary>
     List<CultureInfo>? SupportedCultures { get; }
 
-    /// <summary>
-    ///     This event is raised after the <see cref="Init"/> and <see cref="Shutdown"/> methods have been called.
-    /// </summary>
-    /// <remarks>
-    ///     Intended to support unit tests that need to know when the application has been initialized.
-    /// </remarks>
-    event EventHandler<EventArgs<bool>>? InitializedChanged;
-
     /// <summary>
     ///     Resets the application state to defaults. This is called by <see cref="Shutdown"/>.
     /// </summary>