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.
This commit is contained in:
Tig
2025-12-04 16:42:51 -07:00
parent 4738e8535a
commit b061aacf18
4 changed files with 11 additions and 44 deletions

View File

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

View File

@@ -5,24 +5,10 @@ internal partial class ApplicationImpl
/// <inheritdoc/>
public event EventHandler<EventArgs<Rectangle>>? ScreenChanged;
private readonly object _lockScreen = new ();
private Rectangle? _screen;
/// <inheritdoc/>
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
{
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");
}
// 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;
}
/// <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>
/// 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>
private void RaiseScreenChangedEvent (Rectangle screen)
{
Screen = new (Point.Empty, screen.Size);
//Screen = new (Point.Empty, screen.Size);
ScreenChanged?.Invoke (this, new (screen));
@@ -185,7 +156,7 @@ internal partial class ApplicationImpl
// Only force a complete redraw if needed (needsLayout or forceRedraw).
// 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);

View File

@@ -463,8 +463,9 @@ public interface IApplication : IDisposable
string ForceDriver { get; set; }
/// <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>
/// <remarks>
/// <para>

View File

@@ -383,7 +383,7 @@ public class IApplicationScreenChangedTests (ITestOutputHelper output)
}
[Fact]
public void Screen_Property_Setting_Does_Not_Fire_ScreenChanged_Event ()
public void Screen_Property_Setting_Raises_ScreenChanged_Event ()
{
// Arrange
using IApplication app = Application.Create ();
@@ -397,11 +397,10 @@ public class IApplicationScreenChangedTests (ITestOutputHelper output)
try
{
// Act - Manually set Screen property (not via driver resize)
// Act - Manually set Screen property
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);
}
finally