From 65851ad5271c577324f974c6123a00eb5c510952 Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 15 Aug 2023 13:07:06 +0100 Subject: [PATCH] Fixes #2810. Pressing Alt key on a Toplevel with only a MenuBar throws System.InvalidOperationException. --- Terminal.Gui/Core/Toplevel.cs | 6 ++++++ UnitTests/TopLevels/ToplevelTests.cs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/Terminal.Gui/Core/Toplevel.cs b/Terminal.Gui/Core/Toplevel.cs index f06345152..23fb0df1f 100644 --- a/Terminal.Gui/Core/Toplevel.cs +++ b/Terminal.Gui/Core/Toplevel.cs @@ -552,6 +552,9 @@ namespace Terminal.Gui { /// public override void Add (View view) { + if (!CanFocus) { + CanFocus = true; + } AddMenuStatusBar (view); base.Add (view); } @@ -569,6 +572,9 @@ namespace Terminal.Gui { /// public override void Remove (View view) { + if (InternalSubviews.Count < 1) { + CanFocus = false; + } if (this is Toplevel toplevel && toplevel.MenuBar != null) { RemoveMenuStatusBar (view); } diff --git a/UnitTests/TopLevels/ToplevelTests.cs b/UnitTests/TopLevels/ToplevelTests.cs index 120ed6b35..c76e0ad04 100644 --- a/UnitTests/TopLevels/ToplevelTests.cs +++ b/UnitTests/TopLevels/ToplevelTests.cs @@ -1031,5 +1031,22 @@ namespace Terminal.Gui.TopLevelTests { Application.Driver.GetCursorVisibility (out 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); + } } } \ No newline at end of file