Browse Source

Fixes #2810. Pressing Alt key on a Toplevel with only a MenuBar throws System.InvalidOperationException.

BDisp 2 years ago
parent
commit
65851ad527
2 changed files with 23 additions and 0 deletions
  1. 6 0
      Terminal.Gui/Core/Toplevel.cs
  2. 17 0
      UnitTests/TopLevels/ToplevelTests.cs

+ 6 - 0
Terminal.Gui/Core/Toplevel.cs

@@ -552,6 +552,9 @@ namespace Terminal.Gui {
 		///<inheritdoc/>
 		///<inheritdoc/>
 		public override void Add (View view)
 		public override void Add (View view)
 		{
 		{
+			if (!CanFocus) {
+				CanFocus = true;
+			}
 			AddMenuStatusBar (view);
 			AddMenuStatusBar (view);
 			base.Add (view);
 			base.Add (view);
 		}
 		}
@@ -569,6 +572,9 @@ namespace Terminal.Gui {
 		///<inheritdoc/>
 		///<inheritdoc/>
 		public override void Remove (View view)
 		public override void Remove (View view)
 		{
 		{
+			if (InternalSubviews.Count < 1) {
+				CanFocus = false;
+			}
 			if (this is Toplevel toplevel && toplevel.MenuBar != null) {
 			if (this is Toplevel toplevel && toplevel.MenuBar != null) {
 				RemoveMenuStatusBar (view);
 				RemoveMenuStatusBar (view);
 			}
 			}

+ 17 - 0
UnitTests/TopLevels/ToplevelTests.cs

@@ -1031,5 +1031,22 @@ namespace Terminal.Gui.TopLevelTests {
 			Application.Driver.GetCursorVisibility (out cursor);
 			Application.Driver.GetCursorVisibility (out cursor);
 			Assert.Equal (CursorVisibility.Invisible, cursor);
 			Assert.Equal (CursorVisibility.Invisible, cursor);
 		}
 		}
+
+		[Fact, AutoInitShutdown]
+		public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
+		{
+			var menu = new MenuBar (new MenuBarItem [] {
+				new MenuBarItem ("Child", new MenuItem [] {
+					new MenuItem ("_Create Child", "", null)
+				})
+			});
+			var topChild = new Toplevel ();
+			topChild.Add (menu);
+			Application.Top.Add (topChild);
+			Application.Begin (Application.Top);
+
+			var exception = Record.Exception (() => topChild.ProcessHotKey (new KeyEvent (Key.AltMask, new KeyModifiers { Alt = true })));
+			Assert.Null (exception);
+		}
 	}
 	}
 }
 }