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 #3986. ContextMenu crashing with right and left arrows #3994

Merged
merged 1 commit into from
Mar 17, 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
10 changes: 10 additions & 0 deletions Terminal.Gui/Views/Menu/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,11 @@
return;
}

if (_selected == -1)
{
return;
}

OpenMenu (_selected);

SelectEnabledItem (
Expand Down Expand Up @@ -984,6 +989,11 @@
return;
}

if (_selected == -1)
{
return;
}

OpenMenu (_selected);

if (!SelectEnabledItem (
Expand Down Expand Up @@ -1432,7 +1442,7 @@

if (SuperView is { })
{
locationOffset.X += SuperView.Border.Thickness.Left;

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

View workflow job for this annotation

GitHub Actions / build_and_test_debug (macos-latest)

Dereference of a possibly null reference.

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

View workflow job for this annotation

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

Dereference of a possibly null reference.
locationOffset.Y += SuperView.Border.Thickness.Top;
}

Expand Down
30 changes: 30 additions & 0 deletions Tests/UnitTests/Views/ContextMenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2105,4 +2105,34 @@ public void Mouse_Pressed_Released_Clicked (int button)

top.Dispose ();
}

[Fact]
[AutoInitShutdown]
public void Menu_Without_SubMenu_Is_Closed_When_Pressing_Key_Right_Or_Key_Left ()
{
var cm = new ContextMenu ();

var menuItems = new MenuBarItem (
[
new ("_New", string.Empty, null),
new ("_Save", string.Empty, null)
]
);
var top = new Toplevel ();
Application.Begin (top);

cm.Show (menuItems);
Assert.True (cm.MenuBar!.IsMenuOpen);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorRight));
Assert.False (cm.MenuBar!.IsMenuOpen);

cm.Show (menuItems);
Assert.True (cm.MenuBar!.IsMenuOpen);

Assert.True (Application.RaiseKeyDownEvent (Key.CursorLeft));
Assert.False (cm.MenuBar!.IsMenuOpen);

top.Dispose ();
}
}
Loading