From 802820edfa0aee2631f41ce93dbaf45f1b688367 Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 21 Oct 2022 21:42:55 +0100 Subject: [PATCH] Fixes #2121. ContextMenu: When open, Shift-F10 should close menu. --- Terminal.Gui/Core/ContextMenu.cs | 3 ++- Terminal.Gui/Views/Menu.cs | 9 +++++++-- UnitTests/ContextMenuTests.cs | 14 ++++++++++++++ UnitTests/MenuTests.cs | 26 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/Core/ContextMenu.cs b/Terminal.Gui/Core/ContextMenu.cs index 726fc52c61..916f23b526 100644 --- a/Terminal.Gui/Core/ContextMenu.cs +++ b/Terminal.Gui/Core/ContextMenu.cs @@ -116,7 +116,8 @@ public void Show () Y = position.Y, Width = 0, Height = 0, - UseSubMenusSingleFrame = UseSubMenusSingleFrame + UseSubMenusSingleFrame = UseSubMenusSingleFrame, + Key = Key }; menuBar.isContextMenuLoading = true; diff --git a/Terminal.Gui/Views/Menu.cs b/Terminal.Gui/Views/Menu.cs index 804ddb5ed3..42ad3b8398 100644 --- a/Terminal.Gui/Views/Menu.cs +++ b/Terminal.Gui/Views/Menu.cs @@ -630,7 +630,7 @@ public override bool ProcessKey (KeyEvent kb) } } } - return false; + return host.ProcessHotKey (kb); } void RunSelected () @@ -905,6 +905,11 @@ public bool UseSubMenusSingleFrame { } } + /// + /// The used to activate the menu bar by keyboard. + /// + public Key Key { get; set; } = Key.F9; + /// /// Initializes a new instance of the . /// @@ -1678,7 +1683,7 @@ private void ProcessMenu (int i, MenuBarItem mi) /// public override bool ProcessHotKey (KeyEvent kb) { - if (kb.Key == Key.F9) { + if (kb.Key == Key) { if (!IsMenuOpen) OpenMenu (); else diff --git a/UnitTests/ContextMenuTests.cs b/UnitTests/ContextMenuTests.cs index a0c88168ae..1cae215379 100644 --- a/UnitTests/ContextMenuTests.cs +++ b/UnitTests/ContextMenuTests.cs @@ -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); + } } } diff --git a/UnitTests/MenuTests.cs b/UnitTests/MenuTests.cs index 77c336b8ab..a8687c4915 100644 --- a/UnitTests/MenuTests.cs +++ b/UnitTests/MenuTests.cs @@ -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); + } } }