Browse Source

Fix FakeDriver.Init() and add ScreenChanged event

Co-authored-by: tig <[email protected]>
copilot-swe-agent[bot] 1 month ago
parent
commit
6d398c79e1

+ 33 - 2
Terminal.Gui/Drivers/ConsoleDriver.cs

@@ -508,9 +508,20 @@ public abstract class ConsoleDriver : IConsoleDriver
         }
         }
     }
     }
 
 
+    /// <summary>
+    ///     Called when the terminal screen size changes. Fires the <see cref="ScreenChanged"/> event.
+    /// </summary>
+    /// <param name="args">The event arguments containing the new screen size.</param>
+    /// <remarks>
+    ///     This method should be called by driver implementations when the screen dimensions change.
+    ///     The event allows the application to respond to terminal resize operations and update layout accordingly.
+    /// </remarks>
+    public void OnScreenChanged (SizeChangedEventArgs args) { ScreenChanged?.Invoke (this, args); }
+
     /// <summary>Called when the terminal size changes. Fires the <see cref="SizeChanged"/> event.</summary>
     /// <summary>Called when the terminal size changes. Fires the <see cref="SizeChanged"/> event.</summary>
     /// <param name="args"></param>
     /// <param name="args"></param>
-    public void OnSizeChanged (SizeChangedEventArgs args) { SizeChanged?.Invoke (this, args); }
+    [Obsolete ("Use OnScreenChanged instead. This method will be removed in a future version.")]
+    public void OnSizeChanged (SizeChangedEventArgs args) { OnScreenChanged (args); }
 
 
     /// <summary>Updates the screen to reflect all the changes that have been done to the display buffer</summary>
     /// <summary>Updates the screen to reflect all the changes that have been done to the display buffer</summary>
     public void Refresh ()
     public void Refresh ()
@@ -545,8 +556,28 @@ public abstract class ConsoleDriver : IConsoleDriver
         throw new NotImplementedException ("SetScreenSize is only supported by FakeDriver for testing purposes.");
         throw new NotImplementedException ("SetScreenSize is only supported by FakeDriver for testing purposes.");
     }
     }
 
 
+    /// <summary>
+    ///     The event fired when the screen (terminal) dimensions change.
+    /// </summary>
+    /// <remarks>
+    ///     <para>
+    ///         This event is raised when the terminal window is resized by the user or when
+    ///         <see cref="SetScreenSize"/> is called (FakeDriver only).
+    ///     </para>
+    ///     <para>
+    ///         Consumers of this event should update their layout and redraw as needed to accommodate
+    ///         the new screen dimensions available via <see cref="Screen"/>, <see cref="Cols"/>, and <see cref="Rows"/>.
+    ///     </para>
+    /// </remarks>
+    public event EventHandler<SizeChangedEventArgs>? ScreenChanged;
+
     /// <summary>The event fired when the terminal is resized.</summary>
     /// <summary>The event fired when the terminal is resized.</summary>
-    public event EventHandler<SizeChangedEventArgs>? SizeChanged;
+    [Obsolete ("Use ScreenChanged instead. This event will be removed in a future version.")]
+    public event EventHandler<SizeChangedEventArgs>? SizeChanged
+    {
+        add => ScreenChanged += value;
+        remove => ScreenChanged -= value;
+    }
 
 
     #endregion Cursor Handling
     #endregion Cursor Handling
 
 

+ 3 - 2
Terminal.Gui/Drivers/FakeDriver/FakeDriver.cs

@@ -99,6 +99,7 @@ public class FakeDriver : ConsoleDriver
         Rows = FakeConsole.WindowHeight = FakeConsole.BufferHeight = FakeConsole.HEIGHT;
         Rows = FakeConsole.WindowHeight = FakeConsole.BufferHeight = FakeConsole.HEIGHT;
         FakeConsole.Clear ();
         FakeConsole.Clear ();
         ResizeScreen ();
         ResizeScreen ();
+        ClearContents (); // Ensure Contents and _dirtyLines are allocated before any rendering
         CurrentAttribute = new Attribute (Color.White, Color.Black);
         CurrentAttribute = new Attribute (Color.White, Color.Black);
     }
     }
 
 
@@ -406,7 +407,7 @@ public class FakeDriver : ConsoleDriver
     /// </summary>
     /// </summary>
     /// <remarks>
     /// <remarks>
     ///     This method updates <see cref="ConsoleDriver.Cols"/> and <see cref="ConsoleDriver.Rows"/>,
     ///     This method updates <see cref="ConsoleDriver.Cols"/> and <see cref="ConsoleDriver.Rows"/>,
-    ///     resizes the internal buffers, clears contents, and fires the <see cref="ConsoleDriver.SizeChanged"/> event.
+    ///     resizes the internal buffers, clears contents, and fires the <see cref="ConsoleDriver.ScreenChanged"/> event.
     /// </remarks>
     /// </remarks>
     /// <param name="width">The new width (columns).</param>
     /// <param name="width">The new width (columns).</param>
     /// <param name="height">The new height (rows).</param>
     /// <param name="height">The new height (rows).</param>
@@ -430,7 +431,7 @@ public class FakeDriver : ConsoleDriver
     {
     {
         ResizeScreen ();
         ResizeScreen ();
         ClearContents ();
         ClearContents ();
-        OnSizeChanged (new SizeChangedEventArgs (new (Cols, Rows)));
+        OnScreenChanged (new SizeChangedEventArgs (new (Cols, Rows)));
     }
     }
 
 
     public virtual void ResizeScreen ()
     public virtual void ResizeScreen ()

+ 1 - 1
Tests/UnitTests/Application/ApplicationScreenTests.cs

@@ -89,7 +89,7 @@ public class ApplicationScreenTests
     }
     }
 
 
     [Fact]
     [Fact]
-    public void Screen_Changes_OnSizeChanged_Without_Call_Application_Init ()
+    public void Screen_Changes_OnScreenChanged_Without_Call_Application_Init ()
     {
     {
         // Arrange
         // Arrange
         Application.ResetState (true);
         Application.ResetState (true);

+ 1 - 1
Tests/UnitTests/ConsoleDrivers/ConsoleDriverTests.cs

@@ -147,7 +147,7 @@ public class ConsoleDriverTests
         driver.Cols = 120;
         driver.Cols = 120;
         driver.Rows = 40;
         driver.Rows = 40;
 
 
-        ((ConsoleDriver)driver).OnSizeChanged (new SizeChangedEventArgs (new (driver.Cols, driver.Rows)));
+        ((ConsoleDriver)driver).OnScreenChanged (new SizeChangedEventArgs (new (driver.Cols, driver.Rows)));
         Assert.Equal (120, driver.Cols);
         Assert.Equal (120, driver.Cols);
         Assert.Equal (40, driver.Rows);
         Assert.Equal (40, driver.Rows);
         Assert.True (wasTerminalResized);
         Assert.True (wasTerminalResized);