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 21, 2022
1 parent a69ceb2 commit 802820e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Terminal.Gui/Core/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public void Show ()
Y = position.Y,
Width = 0,
Height = 0,
UseSubMenusSingleFrame = UseSubMenusSingleFrame
UseSubMenusSingleFrame = UseSubMenusSingleFrame,
Key = Key
};

menuBar.isContextMenuLoading = true;
Expand Down
9 changes: 7 additions & 2 deletions Terminal.Gui/Views/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ public override bool ProcessKey (KeyEvent kb)
}
}
}
return false;
return host.ProcessHotKey (kb);
}

void RunSelected ()
Expand Down Expand Up @@ -905,6 +905,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 @@ -1678,7 +1683,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 @@ -1484,5 +1484,31 @@ File Edit Format
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 22, 1), pos);
}

[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 802820e

Please sign in to comment.