Skip to content

Commit

Permalink
Fixes gui-cs#2121. ContextMenu: When open, Shift-F10 should close menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp committed Oct 30, 2022
1 parent 40c1faf commit 05384c8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Terminal.Gui/Views/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public override bool ProcessKey (KeyEvent kb)
}
}
}
return false;
return host.ProcessHotKey (kb);
}

void RunSelected ()
Expand Down Expand Up @@ -924,6 +924,11 @@ public bool UseSubMenusSingleFrame {
}
}

/// <summary>
/// The <see cref="Gui.Key"/> used to activate the menu bar by keyboard.
/// </summary>
public Key Key { get; set; } = Key.F9;

/// <summary>
/// Initializes a new instance of the <see cref="MenuBar"/>.
/// </summary>
Expand Down Expand Up @@ -1686,7 +1691,7 @@ private void ProcessMenu (int i, MenuBarItem mi)
///<inheritdoc/>
public override bool ProcessHotKey (KeyEvent kb)
{
if (kb.Key == Key.F9) {
if (kb.Key == Key) {
if (!IsMenuOpen)
OpenMenu ();
else
Expand Down
14 changes: 14 additions & 0 deletions UnitTests/ContextMenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -888,5 +888,19 @@ public void Menus_And_SubMenus_Always_Try_To_Be_On_Screen ()
│ SubMenu7 │────┘
", output);
}

[Fact, AutoInitShutdown]
public void Key_Open_And_Close_The_ContextMenu ()
{
var tf = new TextField ();
var top = Application.Top;
top.Add (tf);
Application.Begin (top);

Assert.True (tf.ProcessKey (new KeyEvent (Key.F10 | Key.ShiftMask, new KeyModifiers ())));
Assert.True (tf.ContextMenu.MenuBar.IsMenuOpen);
Assert.True (top.Subviews [1].ProcessKey (new KeyEvent (Key.F10 | Key.ShiftMask, new KeyModifiers ())));
Assert.Null (tf.ContextMenu.MenuBar);
}
}
}
26 changes: 26 additions & 0 deletions UnitTests/MenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,5 +1533,31 @@ public void Parent_MenuItem_Stay_Focused_If_Child_MenuItem_Is_Empty_By_Keyboard
Application.Top.Redraw (Application.Top.Bounds);
GraphViewTests.AssertDriverContentsAre (expectedMenu.ClosedMenuText, output);
}

[Fact, AutoInitShutdown]
public void Key_Open_And_Close_The_MenuBar ()
{
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("File", new MenuItem [] {
new MenuItem ("New", "", null)
})
});
Application.Top.Add (menu);
Application.Begin (Application.Top);

Assert.True (menu.ProcessHotKey (new KeyEvent (Key.F9, new KeyModifiers ())));
Assert.True (menu.IsMenuOpen);
Assert.True (menu.ProcessHotKey (new KeyEvent (Key.F9, new KeyModifiers ())));
Assert.False (menu.IsMenuOpen);

menu.Key = Key.F10 | Key.ShiftMask;
Assert.False (menu.ProcessHotKey (new KeyEvent (Key.F9, new KeyModifiers ())));
Assert.False (menu.IsMenuOpen);

Assert.True (menu.ProcessHotKey (new KeyEvent (Key.F10 | Key.ShiftMask, new KeyModifiers ())));
Assert.True (menu.IsMenuOpen);
Assert.True (menu.ProcessHotKey (new KeyEvent (Key.F10 | Key.ShiftMask, new KeyModifiers ())));
Assert.False (menu.IsMenuOpen);
}
}
}

0 comments on commit 05384c8

Please sign in to comment.