Browse Source

Simplify the `Screen` property in `ApplicationImpl` by removing the `_screen` field and its locking mechanism. The getter now directly retrieves the screen size from the `Driver` or defaults to `2048x2048`. The setter now calls `Driver?.SetScreenSize` to update the screen size, eliminating the need for the `ResetScreen` method.

Update `RaiseScreenChangedEvent` to no longer explicitly set the `Screen` property. Remove unnecessary `.ToArray()` conversion in `View.Draw`.

Clarify `Screen` property documentation in `IApplication` to specify constraints on location and size. Update tests to reflect the new behavior where setting the `Screen` property raises the `ScreenChanged` event. Rename and adjust test cases accordingly.
Tig 1 week ago
parent
commit
b061aacf18

+ 1 - 5
Terminal.Gui/App/ApplicationImpl.Lifecycle.cs

@@ -87,7 +87,7 @@ internal partial class ApplicationImpl
         _keyboard.PrevTabGroupKey = existingPrevTabGroupKey;
         _keyboard.PrevTabGroupKey = existingPrevTabGroupKey;
 
 
         CreateDriver (_driverName);
         CreateDriver (_driverName);
-        Screen = Driver!.Screen;
+        //Screen = Driver!.Screen;
         Initialized = true;
         Initialized = true;
 
 
         RaiseInitializedChanged (this, new (true));
         RaiseInitializedChanged (this, new (true));
@@ -273,10 +273,6 @@ internal partial class ApplicationImpl
             Driver = null;
             Driver = null;
         }
         }
 
 
-        // Reset screen
-        ResetScreen ();
-        _screen = null;
-
         // === 5. Clear run state ===
         // === 5. Clear run state ===
         Iteration = null;
         Iteration = null;
         SessionBegun = null;
         SessionBegun = null;

+ 4 - 33
Terminal.Gui/App/ApplicationImpl.Screen.cs

@@ -5,24 +5,10 @@ internal partial class ApplicationImpl
     /// <inheritdoc/>
     /// <inheritdoc/>
     public event EventHandler<EventArgs<Rectangle>>? ScreenChanged;
     public event EventHandler<EventArgs<Rectangle>>? ScreenChanged;
 
 
-    private readonly object _lockScreen = new ();
-    private Rectangle? _screen;
-
     /// <inheritdoc/>
     /// <inheritdoc/>
     public Rectangle Screen
     public Rectangle Screen
     {
     {
-        get
-        {
-            lock (_lockScreen)
-            {
-                if (_screen == null)
-                {
-                    _screen = Driver?.Screen ?? new (new (0, 0), new (2048, 2048));
-                }
-
-                return _screen.Value;
-            }
-        }
+        get => Driver?.Screen ?? new (new (0, 0), new (2048, 2048));
         set
         set
         {
         {
             if (value is { } && (value.X != 0 || value.Y != 0))
             if (value is { } && (value.X != 0 || value.Y != 0))
@@ -30,12 +16,7 @@ internal partial class ApplicationImpl
                 throw new NotImplementedException ("Screen locations other than 0, 0 are not yet supported");
                 throw new NotImplementedException ("Screen locations other than 0, 0 are not yet supported");
             }
             }
 
 
-            // TODO: Enable this to actually change the Driver.
-
-            lock (_lockScreen)
-            {
-                _screen = value;
-            }
+            Driver?.SetScreenSize (value.Size.Width, value.Size.Height);
         }
         }
     }
     }
 
 
@@ -115,16 +96,6 @@ internal partial class ApplicationImpl
         return false;
         return false;
     }
     }
 
 
-    /// <summary>
-    ///     INTERNAL: Resets the Screen rectangle to null so it will be recalculated on next access.
-    /// </summary>
-    private void ResetScreen ()
-    {
-        lock (_lockScreen)
-        {
-            _screen = null;
-        }
-    }
 
 
     /// <summary>
     /// <summary>
     ///     INTERNAL: Called when the application's screen has changed.
     ///     INTERNAL: Called when the application's screen has changed.
@@ -133,7 +104,7 @@ internal partial class ApplicationImpl
     /// <param name="screen">The new screen size and position.</param>
     /// <param name="screen">The new screen size and position.</param>
     private void RaiseScreenChangedEvent (Rectangle screen)
     private void RaiseScreenChangedEvent (Rectangle screen)
     {
     {
-        Screen = new (Point.Empty, screen.Size);
+        //Screen = new (Point.Empty, screen.Size);
 
 
         ScreenChanged?.Invoke (this, new (screen));
         ScreenChanged?.Invoke (this, new (screen));
 
 
@@ -185,7 +156,7 @@ internal partial class ApplicationImpl
 
 
             // Only force a complete redraw if needed (needsLayout or forceRedraw).
             // Only force a complete redraw if needed (needsLayout or forceRedraw).
             // Otherwise, just redraw views that need it.
             // Otherwise, just redraw views that need it.
-            View.Draw (views: views.ToArray ().Cast<View> ()!, neededLayout || forceRedraw);
+            View.Draw (views: views.ToArray ().Cast<View> (), neededLayout || forceRedraw);
 
 
             Driver.Clip = new (Screen);
             Driver.Clip = new (Screen);
 
 

+ 3 - 2
Terminal.Gui/App/IApplication.cs

@@ -463,8 +463,9 @@ public interface IApplication : IDisposable
     string ForceDriver { get; set; }
     string ForceDriver { get; set; }
 
 
     /// <summary>
     /// <summary>
-    ///     Gets or sets the size of the screen. By default, this is the size of the screen as reported by the
-    ///     <see cref="IDriver"/>.
+    ///     Gets or location and size of the application in the terminal. By default, the location is (0, 0) and the size
+    ///     is the size of the terminal as reported by the <see cref="IDriver"/>.
+    ///     Setting the location to anything but (0, 0) is not supported and will throw <see cref="NotSupportedException"/>.
     /// </summary>
     /// </summary>
     /// <remarks>
     /// <remarks>
     ///     <para>
     ///     <para>

+ 3 - 4
Tests/UnitTestsParallelizable/Application/IApplicationScreenChangedTests.cs

@@ -383,7 +383,7 @@ public class IApplicationScreenChangedTests (ITestOutputHelper output)
     }
     }
 
 
     [Fact]
     [Fact]
-    public void Screen_Property_Setting_Does_Not_Fire_ScreenChanged_Event ()
+    public void Screen_Property_Setting_Raises_ScreenChanged_Event ()
     {
     {
         // Arrange
         // Arrange
         using IApplication app = Application.Create ();
         using IApplication app = Application.Create ();
@@ -397,11 +397,10 @@ public class IApplicationScreenChangedTests (ITestOutputHelper output)
 
 
         try
         try
         {
         {
-            // Act - Manually set Screen property (not via driver resize)
+            // Act - Manually set Screen property 
             app.Screen = new (0, 0, 100, 50);
             app.Screen = new (0, 0, 100, 50);
 
 
-            // Assert - Event should not fire for manual property setting
-            Assert.False (eventFired);
+            Assert.True (eventFired);
             Assert.Equal (new (0, 0, 100, 50), app.Screen);
             Assert.Equal (new (0, 0, 100, 50), app.Screen);
         }
         }
         finally
         finally