Skip to content

Commit

Permalink
Prevents open menu bar if it's invisible and close all opened menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp authored and tig committed Aug 11, 2023
1 parent b9c9079 commit 42fcc35
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Terminal.Gui/Views/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,17 @@ public bool UseSubMenusSingleFrame {
/// </summary>
public Key Key { get; set; } = Key.F9;

///<inheritdoc/>
public override bool Visible {
get => base.Visible;
set {
base.Visible = value;
if (!value) {
CloseAllMenus ();
}
}
}

/// <summary>
/// Initializes a new instance of the <see cref="MenuBar"/>.
/// </summary>
Expand Down Expand Up @@ -1745,10 +1756,11 @@ private void ProcessMenu (int i, MenuBarItem mi)
public override bool ProcessHotKey (KeyEvent kb)
{
if (kb.Key == Key) {
if (!IsMenuOpen)
if (Visible && !IsMenuOpen) {
OpenMenu ();
else
} else {
CloseAllMenus ();
}
return true;
}

Expand Down
22 changes: 22 additions & 0 deletions UnitTests/Menus/MenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,5 +1787,27 @@ void Close ()
Assert.False (CanExecuteNew ());
Assert.True (CanExecuteClose ());
}

[Fact, AutoInitShutdown]
public void Visible_False_Key_Does_Not_Open_And_Close_All_Opened_Menus ()
{
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("File", new MenuItem [] {
new MenuItem ("New", "", null)
})
});
Application.Top.Add (menu);
Application.Begin (Application.Top);

Assert.True (menu.Visible);
Assert.True (menu.ProcessHotKey (new KeyEvent (menu.Key, new KeyModifiers ())));
Assert.True (menu.IsMenuOpen);

menu.Visible = false;
Assert.False (menu.IsMenuOpen);

Assert.True (menu.ProcessHotKey (new KeyEvent (menu.Key, new KeyModifiers ())));
Assert.False (menu.IsMenuOpen);
}
}
}

0 comments on commit 42fcc35

Please sign in to comment.