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

Crash when attempting to open menu bar item with action but no menu items assigned #2228

Closed
Velorien opened this issue Nov 17, 2022 · 0 comments · Fixed by #2230
Closed

Crash when attempting to open menu bar item with action but no menu items assigned #2228

Velorien opened this issue Nov 17, 2022 · 0 comments · Fixed by #2230

Comments

@Velorien
Copy link

Velorien commented Nov 17, 2022

Describe the bug
When Alt key is pressed and MenuBarItem gets focus, attempting to move focus with left/right arrow keys causes a crash when the target MenuBarItem has an action but Menu property is set to null or an empty array.

This is caused by the framework attempting to open the menu even if it does not exist.

To Reproduce
Steps to reproduce the behavior:

  1. Create an application with a MenuBar and at least two MenuBarItems with actions but no MenuItems assigned.
  2. Press Alt key or focus the MenuBar by some other means.
  3. Press right arrow key
  4. Application crashes:
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Terminal.Gui.MenuBar.OnMenuOpened()
   at Terminal.Gui.MenuBar.set_openCurrentMenu(Menu value)
   at Terminal.Gui.MenuBar.OpenMenu(Int32 index, Int32 sIndex, MenuBarItem subMenu)
   at Terminal.Gui.MenuBar.MoveRight()
   at Terminal.Gui.MenuBar.<.ctor>b__21_1()
   at Terminal.Gui.View.InvokeKeybindings(KeyEvent keyEvent)
   at Terminal.Gui.MenuBar.ProcessKey(KeyEvent kb)
   at Terminal.Gui.View.ProcessHotKey(KeyEvent keyEvent)
   at Terminal.Gui.Application.ProcessKeyEvent(KeyEvent ke)
   at Terminal.Gui.WindowsDriver.ProcessInput(InputRecord inputEvent)
   at Terminal.Gui.WindowsDriver.<PrepareToRun>b__34_0(InputRecord e)
   at Terminal.Gui.WindowsMainLoop.Terminal.Gui.IMainLoopDriver.MainIteration()
   at Terminal.Gui.MainLoop.MainIteration()
   at Terminal.Gui.Application.RunMainLoopIteration(RunState& state, Boolean wait, Boolean& firstIteration)
   at Terminal.Gui.Application.RunLoop(RunState state, Boolean wait)
   at Terminal.Gui.Application.Run(Toplevel view, Func`2 errorHandler)
   at Terminal.Gui.Application.Run(Func`2 errorHandler)
   at Program.<Main>$(String[] args) in REDACTED 105

Minimal repro code

using Terminal.Gui;

Application.Init();

var menu = new MenuBar(
    menus: new[]
    {
        new MenuBarItem { Title = "Test 1", Action = () => { } },
        new MenuBarItem { Title = "Test 2", Action = () => { } },
    });

Application.Top.Add(menu);

Application.Run();
Application.Shutdown();

Expected behavior
Focus is moved without a crash.

BDisp added a commit to BDisp/Terminal.Gui that referenced this issue Nov 17, 2022
@tig tig closed this as completed in #2230 Dec 4, 2022
tig added a commit that referenced this issue Dec 4, 2022
Fixes #2228. Crash when attempting to open menu bar item with action but no menu items assigned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant