From b65092fcaa183e9205ec373a4b13f8707778af1a Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 20 Jan 2024 16:34:51 +0000 Subject: [PATCH] Fixes #2095. Resizing when a context menu is open causes the Enter event to get spammed (#3193) * Fixes #2095. Resizing when a context menu is open causes the Enter event to get spammed * Update nuget packages. --- Terminal.Gui/Views/ContextMenu.cs | 9 --------- Terminal.Gui/Views/Menu.cs | 20 ++++++++++++++++++++ UnitTests/Menus/ContextMenuTests.cs | 6 ++++-- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/Views/ContextMenu.cs b/Terminal.Gui/Views/ContextMenu.cs index e0e5eca56..e8520f98e 100644 --- a/Terminal.Gui/Views/ContextMenu.cs +++ b/Terminal.Gui/Views/ContextMenu.cs @@ -79,7 +79,6 @@ namespace Terminal.Gui { } if (container != null) { container.Closing -= Container_Closing; - container.Resized -= Container_Resized; } } @@ -93,7 +92,6 @@ namespace Terminal.Gui { } container = Application.Current; container.Closing += Container_Closing; - container.Resized += Container_Resized; var frame = container.Frame; var position = Position; if (Host != null) { @@ -145,13 +143,6 @@ namespace Terminal.Gui { menuBar.OpenMenu (); } - private void Container_Resized (Size obj) - { - if (IsShow) { - Show (); - } - } - private void Container_Closing (ToplevelClosingEventArgs obj) { Hide (); diff --git a/Terminal.Gui/Views/Menu.cs b/Terminal.Gui/Views/Menu.cs index 28cb46db0..444f695a6 100644 --- a/Terminal.Gui/Views/Menu.cs +++ b/Terminal.Gui/Views/Menu.cs @@ -424,6 +424,10 @@ namespace Terminal.Gui { WantMousePositionReports = host.WantMousePositionReports; } + if (Application.Current != null) { + Application.Current.Resized += Current_Resized; + } + // Things this view knows how to do AddCommand (Command.LineUp, () => MoveUp ()); AddCommand (Command.LineDown, () => MoveDown ()); @@ -449,6 +453,13 @@ namespace Terminal.Gui { AddKeyBinding (Key.Enter, Command.Accept); } + private void Current_Resized (Size obj) + { + if (host.IsMenuOpen) { + host.CloseAllMenus (); + } + } + internal Attribute DetermineColorSchemeFor (MenuItem item, int index) { if (item != null) { @@ -846,6 +857,15 @@ namespace Terminal.Gui { return base.OnEnter (view); } + + protected override void Dispose (bool disposing) + { + if (Application.Current != null) { + Application.Current.Resized -= Current_Resized; + } + + base.Dispose (disposing); + } } /// diff --git a/UnitTests/Menus/ContextMenuTests.cs b/UnitTests/Menus/ContextMenuTests.cs index 3807e971a..dd493358f 100644 --- a/UnitTests/Menus/ContextMenuTests.cs +++ b/UnitTests/Menus/ContextMenuTests.cs @@ -385,6 +385,8 @@ namespace Terminal.Gui.MenuTests { [Fact, AutoInitShutdown] public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Width () { + ((FakeDriver)Application.Driver).SetBufferSize (5, 25); + var cm = new ContextMenu (0, 0, new MenuBarItem (new MenuItem [] { new MenuItem ("One", "", null), @@ -397,7 +399,6 @@ namespace Terminal.Gui.MenuTests { cm.Show (); Assert.Equal (new Point (0, 0), cm.Position); Application.Begin (Application.Top); - ((FakeDriver)Application.Driver).SetBufferSize (5, 25); var expected = @" ┌──── @@ -416,6 +417,8 @@ namespace Terminal.Gui.MenuTests { [Fact, AutoInitShutdown] public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_Height () { + ((FakeDriver)Application.Driver).SetBufferSize (80, 3); + var cm = new ContextMenu (0, 0, new MenuBarItem (new MenuItem [] { new MenuItem ("One", "", null), @@ -428,7 +431,6 @@ namespace Terminal.Gui.MenuTests { cm.Show (); Assert.Equal (new Point (0, 0), cm.Position); Application.Begin (Application.Top); - ((FakeDriver)Application.Driver).SetBufferSize (80, 3); var expected = @" ┌──────┐