浏览代码

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.
BDisp 1 年之前
父节点
当前提交
b65092fcaa
共有 3 个文件被更改,包括 24 次插入11 次删除
  1. 0 9
      Terminal.Gui/Views/ContextMenu.cs
  2. 20 0
      Terminal.Gui/Views/Menu.cs
  3. 4 2
      UnitTests/Menus/ContextMenuTests.cs

+ 0 - 9
Terminal.Gui/Views/ContextMenu.cs

@@ -79,7 +79,6 @@ namespace Terminal.Gui {
 			}
 			}
 			if (container != null) {
 			if (container != null) {
 				container.Closing -= Container_Closing;
 				container.Closing -= Container_Closing;
-				container.Resized -= Container_Resized;
 			}
 			}
 		}
 		}
 
 
@@ -93,7 +92,6 @@ namespace Terminal.Gui {
 			}
 			}
 			container = Application.Current;
 			container = Application.Current;
 			container.Closing += Container_Closing;
 			container.Closing += Container_Closing;
-			container.Resized += Container_Resized;
 			var frame = container.Frame;
 			var frame = container.Frame;
 			var position = Position;
 			var position = Position;
 			if (Host != null) {
 			if (Host != null) {
@@ -145,13 +143,6 @@ namespace Terminal.Gui {
 			menuBar.OpenMenu ();
 			menuBar.OpenMenu ();
 		}
 		}
 
 
-		private void Container_Resized (Size obj)
-		{
-			if (IsShow) {
-				Show ();
-			}
-		}
-
 		private void Container_Closing (ToplevelClosingEventArgs obj)
 		private void Container_Closing (ToplevelClosingEventArgs obj)
 		{
 		{
 			Hide ();
 			Hide ();

+ 20 - 0
Terminal.Gui/Views/Menu.cs

@@ -424,6 +424,10 @@ namespace Terminal.Gui {
 				WantMousePositionReports = host.WantMousePositionReports;
 				WantMousePositionReports = host.WantMousePositionReports;
 			}
 			}
 
 
+			if (Application.Current != null) {
+				Application.Current.Resized += Current_Resized;
+			}
+
 			// Things this view knows how to do
 			// Things this view knows how to do
 			AddCommand (Command.LineUp, () => MoveUp ());
 			AddCommand (Command.LineUp, () => MoveUp ());
 			AddCommand (Command.LineDown, () => MoveDown ());
 			AddCommand (Command.LineDown, () => MoveDown ());
@@ -449,6 +453,13 @@ namespace Terminal.Gui {
 			AddKeyBinding (Key.Enter, Command.Accept);
 			AddKeyBinding (Key.Enter, Command.Accept);
 		}
 		}
 
 
+		private void Current_Resized (Size obj)
+		{
+			if (host.IsMenuOpen) {
+				host.CloseAllMenus ();
+			}
+		}
+
 		internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
 		internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
 		{
 		{
 			if (item != null) {
 			if (item != null) {
@@ -846,6 +857,15 @@ namespace Terminal.Gui {
 
 
 			return base.OnEnter (view);
 			return base.OnEnter (view);
 		}
 		}
+
+		protected override void Dispose (bool disposing)
+		{
+			if (Application.Current != null) {
+				Application.Current.Resized -= Current_Resized;
+			}
+
+			base.Dispose (disposing);
+		}
 	}
 	}
 
 
 	/// <summary>
 	/// <summary>

+ 4 - 2
UnitTests/Menus/ContextMenuTests.cs

@@ -385,6 +385,8 @@ namespace Terminal.Gui.MenuTests {
 		[Fact, AutoInitShutdown]
 		[Fact, AutoInitShutdown]
 		public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Width ()
 		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,
 			var cm = new ContextMenu (0, 0,
 				new MenuBarItem (new MenuItem [] {
 				new MenuBarItem (new MenuItem [] {
 					new MenuItem ("One", "", null),
 					new MenuItem ("One", "", null),
@@ -397,7 +399,6 @@ namespace Terminal.Gui.MenuTests {
 			cm.Show ();
 			cm.Show ();
 			Assert.Equal (new Point (0, 0), cm.Position);
 			Assert.Equal (new Point (0, 0), cm.Position);
 			Application.Begin (Application.Top);
 			Application.Begin (Application.Top);
-			((FakeDriver)Application.Driver).SetBufferSize (5, 25);
 
 
 			var expected = @"
 			var expected = @"
 ┌────
 ┌────
@@ -416,6 +417,8 @@ namespace Terminal.Gui.MenuTests {
 		[Fact, AutoInitShutdown]
 		[Fact, AutoInitShutdown]
 		public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_Height ()
 		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,
 			var cm = new ContextMenu (0, 0,
 				new MenuBarItem (new MenuItem [] {
 				new MenuBarItem (new MenuItem [] {
 					new MenuItem ("One", "", null),
 					new MenuItem ("One", "", null),
@@ -428,7 +431,6 @@ namespace Terminal.Gui.MenuTests {
 			cm.Show ();
 			cm.Show ();
 			Assert.Equal (new Point (0, 0), cm.Position);
 			Assert.Equal (new Point (0, 0), cm.Position);
 			Application.Begin (Application.Top);
 			Application.Begin (Application.Top);
-			((FakeDriver)Application.Driver).SetBufferSize (80, 3);
 
 
 			var expected = @"
 			var expected = @"
 ┌──────┐
 ┌──────┐