From 05384c8903a026c5b3a7102c0f31b759f9963715 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/Views/Menu.cs | 9 +++++++-- UnitTests/ContextMenuTests.cs | 14 ++++++++++++++ UnitTests/MenuTests.cs | 26 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Views/Menu.cs b/Terminal.Gui/Views/Menu.cs index ed037a10f3..d693ca7f2a 100644 --- a/Terminal.Gui/Views/Menu.cs +++ b/Terminal.Gui/Views/Menu.cs @@ -632,7 +632,7 @@ public override bool ProcessKey (KeyEvent kb) } } } - return false; + return host.ProcessHotKey (kb); } void RunSelected () @@ -924,6 +924,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 . /// @@ -1686,7 +1691,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 aab102b6c2..ad01177cc3 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 549551fc68..0f10e48a91 100644 --- a/UnitTests/MenuTests.cs +++ b/UnitTests/MenuTests.cs @@ -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); + } } }