Skip to content
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
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);
}
}
}