Skip to content

Commit e76ec72

Browse files
BDisptig
andauthored
Fixes #3964. Null Reference in DoDrawBorderAndPadding (#3971)
* Fixes #3964. Null Reference in DoDrawBorderAndPadding * Fix unit test. * Ensures closing all opened menus that belong to another top when a new modal top is opened. * Reformat --------- Co-authored-by: Tig <[email protected]>
1 parent 4ad866e commit e76ec72

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

Terminal.Gui/Application/Application.Run.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ public static RunState Begin (Toplevel toplevel)
8989
//#endif
9090

9191
// Ensure the mouse is ungrabbed.
92-
MouseGrabView = null;
92+
if (MouseGrabView is { })
93+
{
94+
UngrabMouse ();
95+
MouseGrabView = null;
96+
}
9397

9498
var rs = new RunState (toplevel);
9599

Terminal.Gui/Views/Menu/Menu.cs

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public Menu ()
1717
}
1818

1919
Application.MouseEvent += Application_RootMouseEvent;
20+
Application.UnGrabbedMouse += Application_UnGrabbedMouse;
2021

2122
// Things this view knows how to do
2223
AddCommand (Command.Up, () => MoveUp ());
@@ -235,6 +236,7 @@ protected override void Dispose (bool disposing)
235236
}
236237

237238
Application.MouseEvent -= Application_RootMouseEvent;
239+
Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
238240
base.Dispose (disposing);
239241
}
240242

@@ -522,6 +524,14 @@ private void Application_RootMouseEvent (object? sender, MouseEventArgs a)
522524
}
523525
}
524526

527+
private void Application_UnGrabbedMouse (object? sender, ViewEventArgs a)
528+
{
529+
if (_host.IsMenuOpen)
530+
{
531+
_host.CloseAllMenus ();
532+
}
533+
}
534+
525535
private void CloseAllMenus ()
526536
{
527537
Application.UngrabMouse ();

Tests/UnitTests/Views/MenuBarTests.cs

+9-26
Original file line numberDiff line numberDiff line change
@@ -2562,47 +2562,30 @@ public void MouseEvent_Test ()
25622562
Assert.Equal ("_Edit", miCurrent.Parent.Title);
25632563
Assert.Equal ("_Paste", miCurrent.Title);
25642564

2565-
for (var i = 2; i >= -1; i--)
2565+
for (var i = 4; i >= -1; i--)
25662566
{
2567-
if (i == -1)
2568-
{
2569-
// Edit menu is open. Click on the menu at Y = -1, which is outside the menu.
2570-
Assert.False (
2571-
mCurrent.NewMouseEvent (
2572-
new () { Position = new (10, i), Flags = MouseFlags.ReportMousePosition, View = menu }
2573-
)
2574-
);
2575-
}
2576-
else
2577-
{
2578-
// Edit menu is open. Click on the menu at Y = i.
2579-
Assert.True (
2580-
mCurrent.NewMouseEvent (
2581-
new () { Position = new (10, i), Flags = MouseFlags.ReportMousePosition, View = mCurrent }
2582-
)
2583-
);
2584-
}
2567+
Application.RaiseMouseEvent (
2568+
new () { ScreenPosition = new (10, i), Flags = MouseFlags.ReportMousePosition }
2569+
);
25852570

25862571
Assert.True (menu.IsMenuOpen);
2572+
Assert.Equal (menu, Application.MouseGrabView);
2573+
Assert.Equal ("_Edit", miCurrent.Parent.Title);
25872574

2588-
if (i == 2)
2575+
if (i == 4)
25892576
{
2590-
Assert.Equal ("_Edit", miCurrent.Parent.Title);
25912577
Assert.Equal ("_Paste", miCurrent.Title);
25922578
}
2593-
else if (i == 1)
2579+
else if (i == 3)
25942580
{
2595-
Assert.Equal ("_Edit", miCurrent.Parent.Title);
25962581
Assert.Equal ("C_ut", miCurrent.Title);
25972582
}
2598-
else if (i == 0)
2583+
else if (i == 2)
25992584
{
2600-
Assert.Equal ("_Edit", miCurrent.Parent.Title);
26012585
Assert.Equal ("_Copy", miCurrent.Title);
26022586
}
26032587
else
26042588
{
2605-
Assert.Equal ("_Edit", miCurrent.Parent.Title);
26062589
Assert.Equal ("_Copy", miCurrent.Title);
26072590
}
26082591
}

0 commit comments

Comments
 (0)