Skip to content

Commit

Permalink
Fixes gui-cs#2810. Pressing Alt key on a Toplevel with only a MenuBar…
Browse files Browse the repository at this point in the history
… throws System.InvalidOperationException.
  • Loading branch information
BDisp committed Aug 15, 2023
1 parent 42f5b16 commit 4419a48
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Terminal.Gui/Core/Toplevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public Toplevel () : base ()

void Initialize ()
{
CanFocus = true;
ColorScheme = Colors.TopLevel;

// Things this view knows how to do
Expand Down Expand Up @@ -569,6 +570,9 @@ internal void AddMenuStatusBar (View view)
///<inheritdoc/>
public override void Remove (View view)
{
if (InternalSubviews.Count < 1) {
CanFocus = false;
}
if (this is Toplevel toplevel && toplevel.MenuBar != null) {
RemoveMenuStatusBar (view);
}
Expand Down
1 change: 0 additions & 1 deletion Terminal.Gui/Core/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ public Window (ustring title = null, int padding = 0, Border border = null) : ba

void Initialize (ustring title, Rect frame, int padding = 0, Border border = null)
{
CanFocus = true;
ColorScheme = Colors.Base;
if (title == null) title = ustring.Empty;
Title = title;
Expand Down
17 changes: 17 additions & 0 deletions UnitTests/TopLevels/ToplevelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,5 +1031,22 @@ public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null (
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);
}
}
}

0 comments on commit 4419a48

Please sign in to comment.