Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3964. Null Reference in DoDrawBorderAndPadding #3971

Merged
merged 7 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Terminal.Gui/Application/Application.Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public static RunState Begin (Toplevel toplevel)
//#endif

// Ensure the mouse is ungrabbed.
MouseGrabView = null;
if (MouseGrabView is { })
{
UngrabMouse ();
MouseGrabView = null;
}

var rs = new RunState (toplevel);

Expand Down
10 changes: 10 additions & 0 deletions Terminal.Gui/Views/Menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/// </summary>
internal sealed class Menu : View
{
public Menu ()

Check warning on line 11 in Terminal.Gui/Views/Menu/Menu.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (macos-latest)

Non-nullable field '_host' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
if (Application.Top is { })
{
Expand All @@ -17,6 +17,7 @@
}

Application.MouseEvent += Application_RootMouseEvent;
Application.UnGrabbedMouse += Application_UnGrabbedMouse;

// Things this view knows how to do
AddCommand (Command.Up, () => MoveUp ());
Expand Down Expand Up @@ -235,6 +236,7 @@
}

Application.MouseEvent -= Application_RootMouseEvent;
Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
base.Dispose (disposing);
}

Expand Down Expand Up @@ -454,12 +456,12 @@
int maxW = (items.Max (z => z?.Width) ?? 0) + borderOffset;
int maxH = items.Length + borderOffset;

if (parent is { } && x + maxW > Driver.Cols)

Check warning on line 459 in Terminal.Gui/Views/Menu/Menu.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (macos-latest)

Dereference of a possibly null reference.
{
minX = Math.Max (parent.Frame.Right - parent.Frame.Width - maxW, 0);
}

if (y + maxH > Driver.Rows)

Check warning on line 464 in Terminal.Gui/Views/Menu/Menu.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (macos-latest)

Dereference of a possibly null reference.
{
minY = Math.Max (Driver.Rows - maxH, 0);
}
Expand Down Expand Up @@ -522,6 +524,14 @@
}
}

private void Application_UnGrabbedMouse (object? sender, ViewEventArgs a)
{
if (_host.IsMenuOpen)
{
_host.CloseAllMenus ();
}
}

private void CloseAllMenus ()
{
Application.UngrabMouse ();
Expand Down Expand Up @@ -836,7 +846,7 @@
continue;
}

if (ViewportToScreen (Viewport).Y + i >= Driver.Rows)

Check warning on line 849 in Terminal.Gui/Views/Menu/Menu.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (macos-latest)

Dereference of a possibly null reference.
{
break;
}
Expand Down
35 changes: 9 additions & 26 deletions Tests/UnitTests/Views/MenuBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2562,47 +2562,30 @@ public void MouseEvent_Test ()
Assert.Equal ("_Edit", miCurrent.Parent.Title);
Assert.Equal ("_Paste", miCurrent.Title);

for (var i = 2; i >= -1; i--)
for (var i = 4; i >= -1; i--)
{
if (i == -1)
{
// Edit menu is open. Click on the menu at Y = -1, which is outside the menu.
Assert.False (
mCurrent.NewMouseEvent (
new () { Position = new (10, i), Flags = MouseFlags.ReportMousePosition, View = menu }
)
);
}
else
{
// Edit menu is open. Click on the menu at Y = i.
Assert.True (
mCurrent.NewMouseEvent (
new () { Position = new (10, i), Flags = MouseFlags.ReportMousePosition, View = mCurrent }
)
);
}
Application.RaiseMouseEvent (
new () { ScreenPosition = new (10, i), Flags = MouseFlags.ReportMousePosition }
);

Assert.True (menu.IsMenuOpen);
Assert.Equal (menu, Application.MouseGrabView);
Assert.Equal ("_Edit", miCurrent.Parent.Title);

if (i == 2)
if (i == 4)
{
Assert.Equal ("_Edit", miCurrent.Parent.Title);
Assert.Equal ("_Paste", miCurrent.Title);
}
else if (i == 1)
else if (i == 3)
{
Assert.Equal ("_Edit", miCurrent.Parent.Title);
Assert.Equal ("C_ut", miCurrent.Title);
}
else if (i == 0)
else if (i == 2)
{
Assert.Equal ("_Edit", miCurrent.Parent.Title);
Assert.Equal ("_Copy", miCurrent.Title);
}
else
{
Assert.Equal ("_Edit", miCurrent.Parent.Title);
Assert.Equal ("_Copy", miCurrent.Title);
}
}
Expand Down
Loading