mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* Initial plan * Refactor Application.Mouse - Create IMouse interface and Mouse implementation Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add enhanced documentation for Application.Mouse property Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add parallelizable unit tests for IMouse interface Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor Application.Mouse for decoupling and parallelism Co-authored-by: tig <585482+tig@users.noreply.github.com> * Move HandleMouseGrab method to IMouseGrabHandler interface Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add parallelizable tests for IMouse and IMouseGrabHandler interfaces Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add MouseEventRoutingTests - 27 parallelizable tests for View mouse event handling Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fix terminology: Replace parent/child with superView/subView in MouseEventRoutingTests Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fix coding standards: Use explicit types and target-typed new() in test files Co-authored-by: tig <585482+tig@users.noreply.github.com> * Update coding standards documentation with explicit var and target-typed new() guidance Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor Application classes and improve maintainability Refactored `Sixel` property to be immutable, enhancing thread safety. Cleaned up `ApplicationImpl` by removing redundant fields, restructuring methods (`CreateDriver`, `CreateSubcomponents`), and improving exception handling. Updated `Run<T>` and `Shutdown` methods for consistency. Standardized logging/debugging messages and fixed formatting issues. Reorganized `IApplication` interface, added detailed XML documentation, and grouped related methods logically. Performed general code cleanup, including fixing typos, improving readability, and removing legacy/unnecessary code to reduce technical debt. * Code cleanup * Remove unreferenced LayoutAndDraw method from ApplicationImpl * Code cleanup and TODOs - Updated namespaces to reflect the new structure. - Added `Driver`, `Force16Colors`, and `ForceDriver` properties. - Introduced `Sixel` collection for sixel image management. - Added lifecycle methods: `GetDriverTypes`, `Shutdown`, and events. - Refactored `Init` to support legacy and modern drivers. - Improved driver event handling and screen abstraction. - Updated `Run` method to align with the application lifecycle. - Simplified `IConsoleDriver` documentation. - Removed redundant methods and improved code readability. * Refactor LayoutAndDraw logic for better encapsulation Refactored `Application.Run` to delegate `LayoutAndDraw` to `ApplicationImpl.Instance.LayoutAndDraw`, improving separation of concerns. Renamed `forceDraw` to `forceRedraw` for clarity and moved `LayoutAndDraw` implementation to `ApplicationImpl`. Added a new `LayoutAndDraw` method in `ApplicationImpl` to handle layout and drawing, including managing `TopLevels`, handling active popovers, and refreshing the screen. Updated the `IApplication` interface to reflect the new method and improved its documentation. Implemented `RequestStop` in `ApplicationImpl` and fixed formatting inconsistencies in `Run<T>`. Added TODOs for future refactoring to encapsulate `Top` and `TopLevels` into an `IViewHierarchy` and move certain properties to `IApplication`. * Refactor ApplicationImpl to enhance mouse and keyboard support Added a new `Mouse` property to the `ApplicationImpl` class, replacing its previous declaration, to improve mouse functionality. Updated `MouseGrabHandler` to initialize with a default instance of `MouseGrabHandler`. Added comments to ensure the preservation of existing keyboard settings (`QuitKey`, `ArrangeKey`, `NextTabKey`) for backward compatibility. These changes enhance clarity, functionality, and maintainability of the class. * Merge IMouseGrabHandler into IMouse - consolidate mouse handling into single interface Co-authored-by: tig <585482+tig@users.noreply.github.com> * Rename Mouse to MouseImpl and Keyboard to KeyboardImpl for consistency Co-authored-by: tig <585482+tig@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tig <585482+tig@users.noreply.github.com> Co-authored-by: Tig <tig@users.noreply.github.com>
414 lines
14 KiB
C#
414 lines
14 KiB
C#
using UnitTests;
|
|
using Xunit.Abstractions;
|
|
|
|
// Alias Console to MockConsole so we don't accidentally use Console
|
|
|
|
namespace UnitTests.ApplicationTests;
|
|
|
|
[Trait ("Category", "Input")]
|
|
public class ApplicationMouseTests
|
|
{
|
|
private readonly ITestOutputHelper _output;
|
|
|
|
public ApplicationMouseTests (ITestOutputHelper output)
|
|
{
|
|
_output = output;
|
|
#if DEBUG_IDISPOSABLE
|
|
View.Instances.Clear ();
|
|
RunState.Instances.Clear ();
|
|
#endif
|
|
}
|
|
|
|
#region mouse coordinate tests
|
|
|
|
// test Application.MouseEvent - ensure coordinates are screen relative
|
|
[Theory]
|
|
|
|
// inside tests
|
|
[InlineData (0, 0, 0, 0, true)]
|
|
[InlineData (1, 0, 1, 0, true)]
|
|
[InlineData (0, 1, 0, 1, true)]
|
|
[InlineData (9, 0, 9, 0, true)]
|
|
[InlineData (0, 9, 0, 9, true)]
|
|
|
|
// outside tests
|
|
[InlineData (-1, -1, -1, -1, true)]
|
|
[InlineData (0, -1, 0, -1, true)]
|
|
[InlineData (-1, 0, -1, 0, true)]
|
|
public void MouseEventCoordinatesAreScreenRelative (
|
|
int clickX,
|
|
int clickY,
|
|
int expectedX,
|
|
int expectedY,
|
|
bool expectedClicked
|
|
)
|
|
{
|
|
var mouseEvent = new MouseEventArgs { ScreenPosition = new (clickX, clickY), Flags = MouseFlags.Button1Pressed };
|
|
var clicked = false;
|
|
|
|
void OnApplicationOnMouseEvent (object s, MouseEventArgs e)
|
|
{
|
|
Assert.Equal (expectedX, e.ScreenPosition.X);
|
|
Assert.Equal (expectedY, e.ScreenPosition.Y);
|
|
clicked = true;
|
|
}
|
|
|
|
Application.MouseEvent += OnApplicationOnMouseEvent;
|
|
|
|
Application.RaiseMouseEvent (mouseEvent);
|
|
Assert.Equal (expectedClicked, clicked);
|
|
Application.MouseEvent -= OnApplicationOnMouseEvent;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tests that the mouse coordinates passed to the focused view are correct when the mouse is clicked. No adornments;
|
|
/// Frame == Viewport
|
|
/// </summary>
|
|
[Theory]
|
|
[AutoInitShutdown]
|
|
|
|
// click inside view tests
|
|
[InlineData (0, 0, 0, 0, 0, true)]
|
|
[InlineData (0, 1, 0, 1, 0, true)]
|
|
[InlineData (0, 0, 1, 0, 1, true)]
|
|
[InlineData (0, 9, 0, 9, 0, true)]
|
|
[InlineData (0, 0, 9, 0, 9, true)]
|
|
|
|
// view is offset from origin ; click is inside view
|
|
[InlineData (1, 1, 1, 0, 0, true)]
|
|
[InlineData (1, 2, 1, 1, 0, true)]
|
|
[InlineData (1, 1, 2, 0, 1, true)]
|
|
[InlineData (1, 9, 1, 8, 0, true)]
|
|
[InlineData (1, 1, 9, 0, 8, true)]
|
|
|
|
// click outside view tests
|
|
[InlineData (0, -1, -1, 0, 0, false)]
|
|
[InlineData (0, 0, -1, 0, 0, false)]
|
|
[InlineData (0, -1, 0, 0, 0, false)]
|
|
[InlineData (0, 0, 10, 0, 0, false)]
|
|
[InlineData (0, 10, 0, 0, 0, false)]
|
|
[InlineData (0, 10, 10, 0, 0, false)]
|
|
|
|
// view is offset from origin ; click is outside view
|
|
[InlineData (1, 0, 0, 0, 0, false)]
|
|
[InlineData (1, 1, 0, 0, 0, false)]
|
|
[InlineData (1, 0, 1, 0, 0, false)]
|
|
[InlineData (1, 9, 0, 0, 0, false)]
|
|
[InlineData (1, 0, 9, 0, 0, false)]
|
|
public void MouseCoordinatesTest_NoAdornments (
|
|
int offset,
|
|
int clickX,
|
|
int clickY,
|
|
int expectedX,
|
|
int expectedY,
|
|
bool expectedClicked
|
|
)
|
|
{
|
|
Size size = new (10, 10);
|
|
Point pos = new (offset, offset);
|
|
|
|
var clicked = false;
|
|
|
|
var view = new View
|
|
{
|
|
X = pos.X,
|
|
Y = pos.Y,
|
|
Width = size.Width,
|
|
Height = size.Height
|
|
};
|
|
|
|
var mouseEvent = new MouseEventArgs { ScreenPosition = new (clickX, clickY), Flags = MouseFlags.Button1Clicked };
|
|
|
|
view.MouseClick += (s, e) =>
|
|
{
|
|
Assert.Equal (expectedX, e.Position.X);
|
|
Assert.Equal (expectedY, e.Position.Y);
|
|
clicked = true;
|
|
};
|
|
|
|
var top = new Toplevel ();
|
|
top.Add (view);
|
|
Application.Begin (top);
|
|
|
|
Application.RaiseMouseEvent (mouseEvent);
|
|
Assert.Equal (expectedClicked, clicked);
|
|
top.Dispose ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tests that the mouse coordinates passed to the focused view are correct when the mouse is clicked. With
|
|
/// Frames; Frame != Viewport
|
|
/// </summary>
|
|
//[AutoInitShutdown]
|
|
[Theory]
|
|
|
|
// click on border
|
|
[InlineData (0, 0, 0, 0, 0, false)]
|
|
[InlineData (0, 1, 0, 0, 0, false)]
|
|
[InlineData (0, 0, 1, 0, 0, false)]
|
|
[InlineData (0, 9, 0, 0, 0, false)]
|
|
[InlineData (0, 0, 9, 0, 0, false)]
|
|
|
|
// outside border
|
|
[InlineData (0, 10, 0, 0, 0, false)]
|
|
[InlineData (0, 0, 10, 0, 0, false)]
|
|
|
|
// view is offset from origin ; click is on border
|
|
[InlineData (1, 1, 1, 0, 0, false)]
|
|
[InlineData (1, 2, 1, 0, 0, false)]
|
|
[InlineData (1, 1, 2, 0, 0, false)]
|
|
[InlineData (1, 10, 1, 0, 0, false)]
|
|
[InlineData (1, 1, 10, 0, 0, false)]
|
|
|
|
// outside border
|
|
[InlineData (1, -1, 0, 0, 0, false)]
|
|
[InlineData (1, 0, -1, 0, 0, false)]
|
|
[InlineData (1, 10, 10, 0, 0, false)]
|
|
[InlineData (1, 11, 11, 0, 0, false)]
|
|
|
|
// view is at origin, click is inside border
|
|
[InlineData (0, 1, 1, 0, 0, true)]
|
|
[InlineData (0, 2, 1, 1, 0, true)]
|
|
[InlineData (0, 1, 2, 0, 1, true)]
|
|
[InlineData (0, 8, 1, 7, 0, true)]
|
|
[InlineData (0, 1, 8, 0, 7, true)]
|
|
[InlineData (0, 8, 8, 7, 7, true)]
|
|
|
|
// view is offset from origin ; click inside border
|
|
// our view is 10x10, but has a border, so it's bounds is 8x8
|
|
[InlineData (1, 2, 2, 0, 0, true)]
|
|
[InlineData (1, 3, 2, 1, 0, true)]
|
|
[InlineData (1, 2, 3, 0, 1, true)]
|
|
[InlineData (1, 9, 2, 7, 0, true)]
|
|
[InlineData (1, 2, 9, 0, 7, true)]
|
|
[InlineData (1, 9, 9, 7, 7, true)]
|
|
[InlineData (1, 10, 10, 7, 7, false)]
|
|
|
|
//01234567890123456789
|
|
// |12345678|
|
|
// |xxxxxxxx
|
|
public void MouseCoordinatesTest_Border (
|
|
int offset,
|
|
int clickX,
|
|
int clickY,
|
|
int expectedX,
|
|
int expectedY,
|
|
bool expectedClicked
|
|
)
|
|
{
|
|
Size size = new (10, 10);
|
|
Point pos = new (offset, offset);
|
|
|
|
var clicked = false;
|
|
|
|
Application.Top = new Toplevel ()
|
|
{
|
|
Id = "top",
|
|
};
|
|
Application.Top.X = 0;
|
|
Application.Top.Y = 0;
|
|
Application.Top.Width = size.Width * 2;
|
|
Application.Top.Height = size.Height * 2;
|
|
Application.Top.BorderStyle = LineStyle.None;
|
|
|
|
var view = new View { Id = "view", X = pos.X, Y = pos.Y, Width = size.Width, Height = size.Height };
|
|
|
|
// Give the view a border. With PR #2920, mouse clicks are only passed if they are inside the view's Viewport.
|
|
view.BorderStyle = LineStyle.Single;
|
|
view.CanFocus = true;
|
|
|
|
Application.Top.Add (view);
|
|
|
|
var mouseEvent = new MouseEventArgs { Position = new (clickX, clickY), ScreenPosition = new (clickX, clickY), Flags = MouseFlags.Button1Clicked };
|
|
|
|
view.MouseClick += (s, e) =>
|
|
{
|
|
Assert.Equal (expectedX, e.Position.X);
|
|
Assert.Equal (expectedY, e.Position.Y);
|
|
clicked = true;
|
|
};
|
|
|
|
Application.RaiseMouseEvent (mouseEvent);
|
|
Assert.Equal (expectedClicked, clicked);
|
|
Application.Top.Dispose ();
|
|
Application.ResetState (ignoreDisposed: true);
|
|
|
|
}
|
|
|
|
#endregion mouse coordinate tests
|
|
|
|
#region mouse grab tests
|
|
|
|
[Fact (Skip = "Rebuild to use ScrollBar")]
|
|
[AutoInitShutdown]
|
|
public void MouseGrabView_WithNullMouseEventView ()
|
|
{
|
|
//var tf = new TextField { Width = 10 };
|
|
//var sv = new ScrollView { Width = Dim.Fill (), Height = Dim.Fill () };
|
|
//sv.SetContentSize (new (100, 100));
|
|
|
|
//sv.Add (tf);
|
|
//var top = new Toplevel ();
|
|
//top.Add (sv);
|
|
|
|
//int iterations = -1;
|
|
|
|
//Application.Iteration += (s, a) =>
|
|
// {
|
|
// iterations++;
|
|
|
|
// if (iterations == 0)
|
|
// {
|
|
// Assert.True (tf.HasFocus);
|
|
// Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
// Application.RaiseMouseEvent (new () { ScreenPosition = new (5, 5), Flags = MouseFlags.ReportMousePosition });
|
|
|
|
// Assert.Equal (sv, Application.Mouse.MouseGrabView);
|
|
|
|
// MessageBox.Query ("Title", "Test", "Ok");
|
|
|
|
// Assert.Null (Application.Mouse.MouseGrabView);
|
|
// }
|
|
// else if (iterations == 1)
|
|
// {
|
|
// // Application.Mouse.MouseGrabView is null because
|
|
// // another toplevel (Dialog) was opened
|
|
// Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
// Application.RaiseMouseEvent (new () { ScreenPosition = new (5, 5), Flags = MouseFlags.ReportMousePosition });
|
|
|
|
// Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
// Application.RaiseMouseEvent (new () { ScreenPosition = new (40, 12), Flags = MouseFlags.ReportMousePosition });
|
|
|
|
// Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
// Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed });
|
|
|
|
// Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
// Application.RequestStop ();
|
|
// }
|
|
// else if (iterations == 2)
|
|
// {
|
|
// Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
// Application.RequestStop ();
|
|
// }
|
|
// };
|
|
|
|
//Application.Run (top);
|
|
//top.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void MouseGrabView_GrabbedMouse_UnGrabbedMouse ()
|
|
{
|
|
View grabView = null;
|
|
var count = 0;
|
|
|
|
var view1 = new View { Id = "view1" };
|
|
var view2 = new View { Id = "view2" };
|
|
var view3 = new View { Id = "view3" };
|
|
|
|
Application.Mouse.GrabbedMouse += Application_GrabbedMouse;
|
|
Application.Mouse.UnGrabbedMouse += Application_UnGrabbedMouse;
|
|
|
|
Application.Mouse.GrabMouse (view1);
|
|
Assert.Equal (0, count);
|
|
Assert.Equal (grabView, view1);
|
|
Assert.Equal (view1, Application.Mouse.MouseGrabView);
|
|
|
|
Application.Mouse.UngrabMouse ();
|
|
Assert.Equal (1, count);
|
|
Assert.Equal (grabView, view1);
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
Application.Mouse.GrabbedMouse += Application_GrabbedMouse;
|
|
Application.Mouse.UnGrabbedMouse += Application_UnGrabbedMouse;
|
|
|
|
Application.Mouse.GrabMouse (view2);
|
|
Assert.Equal (1, count);
|
|
Assert.Equal (grabView, view2);
|
|
Assert.Equal (view2, Application.Mouse.MouseGrabView);
|
|
|
|
Application.Mouse.UngrabMouse ();
|
|
Assert.Equal (2, count);
|
|
Assert.Equal (grabView, view2);
|
|
Assert.Equal (view3, Application.Mouse.MouseGrabView);
|
|
Application.Mouse.UngrabMouse ();
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
void Application_GrabbedMouse (object sender, ViewEventArgs e)
|
|
{
|
|
if (count == 0)
|
|
{
|
|
Assert.Equal (view1, e.View);
|
|
grabView = view1;
|
|
}
|
|
else
|
|
{
|
|
Assert.Equal (view2, e.View);
|
|
grabView = view2;
|
|
}
|
|
|
|
Application.Mouse.GrabbedMouse -= Application_GrabbedMouse;
|
|
}
|
|
|
|
void Application_UnGrabbedMouse (object sender, ViewEventArgs e)
|
|
{
|
|
if (count == 0)
|
|
{
|
|
Assert.Equal (view1, e.View);
|
|
Assert.Equal (grabView, e.View);
|
|
}
|
|
else
|
|
{
|
|
Assert.Equal (view2, e.View);
|
|
Assert.Equal (grabView, e.View);
|
|
}
|
|
|
|
count++;
|
|
|
|
if (count > 1)
|
|
{
|
|
// It's possible to grab another view after the previous was ungrabbed
|
|
Application.Mouse.GrabMouse (view3);
|
|
}
|
|
|
|
Application.Mouse.UnGrabbedMouse -= Application_UnGrabbedMouse;
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void View_Is_Responsible_For_Calling_UnGrabMouse_Before_Being_Disposed ()
|
|
{
|
|
var count = 0;
|
|
var view = new View { Width = 1, Height = 1 };
|
|
view.MouseEvent += (s, e) => count++;
|
|
var top = new Toplevel ();
|
|
top.Add (view);
|
|
Application.Begin (top);
|
|
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
Application.Mouse.GrabMouse (view);
|
|
Assert.Equal (view, Application.Mouse.MouseGrabView);
|
|
top.Remove (view);
|
|
Application.Mouse.UngrabMouse ();
|
|
view.Dispose ();
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.True (view.WasDisposed);
|
|
#endif
|
|
|
|
Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed });
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
Assert.Equal (0, count);
|
|
top.Dispose ();
|
|
}
|
|
|
|
#endregion
|
|
}
|