diff --git a/Terminal.Gui/Views/Menu/Menu.cs b/Terminal.Gui/Views/Menu/Menu.cs
index f596c114b..f998c1a70 100644
--- a/Terminal.Gui/Views/Menu/Menu.cs
+++ b/Terminal.Gui/Views/Menu/Menu.cs
@@ -670,7 +670,8 @@ internal sealed class Menu : View
_currentChild = 0;
}
- if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild].IsFromSubMenu == true && _host._selectedSub > -1)
+ // ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
+ if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild]?.IsFromSubMenu == true && _host._selectedSub > -1)
{
_host.PreviousMenu (true);
_host.SelectEnabledItem (_barItems.Children, _currentChild, out _currentChild);
diff --git a/Terminal.Gui/Views/Menu/MenuBarItem.cs b/Terminal.Gui/Views/Menu/MenuBarItem.cs
index 727e0df54..232c0af6b 100644
--- a/Terminal.Gui/Views/Menu/MenuBarItem.cs
+++ b/Terminal.Gui/Views/Menu/MenuBarItem.cs
@@ -108,7 +108,7 @@ public class MenuBarItem : MenuItem
if (menuItem.ShortcutKey != Key.Empty)
{
menuItem._menuBar = menuBar;
- menuItem.UpdateShortcutKeyBinding (Key.Empty);
+ menuItem.AddShortcutKeyBinding (menuBar, Key.Empty);
}
SubMenu (menuItem)?.AddShortcutKeyBindings (menuBar);
@@ -126,7 +126,7 @@ public class MenuBarItem : MenuItem
);
}
- Title = title;
+ SetTitle (title);
if (parent is { })
{
@@ -176,6 +176,12 @@ public class MenuBarItem : MenuItem
}
}
+ private void SetTitle (string? title)
+ {
+ title ??= string.Empty;
+ Title = title;
+ }
+
///
/// Add a dynamically into the .Menus.
///
diff --git a/Terminal.Gui/Views/Menu/MenuItem.cs b/Terminal.Gui/Views/Menu/MenuItem.cs
index 0432823b9..b99f5f83c 100644
--- a/Terminal.Gui/Views/Menu/MenuItem.cs
+++ b/Terminal.Gui/Views/Menu/MenuItem.cs
@@ -276,6 +276,31 @@ public class MenuItem
/// Gets the text describing the keystroke combination defined by .
public string ShortcutTag => ShortcutKey != Key.Empty ? ShortcutKey!.ToString () : string.Empty;
+ internal void AddShortcutKeyBinding (MenuBar menuBar, Key key)
+ {
+ ArgumentNullException.ThrowIfNull (menuBar);
+
+ _menuBar = menuBar;
+
+ AddOrUpdateShortcutKeyBinding (key);
+ }
+
+ private void AddOrUpdateShortcutKeyBinding (Key key)
+ {
+ if (key != Key.Empty)
+ {
+ _menuBar.KeyBindings.Remove (key);
+ }
+
+ if (ShortcutKey != Key.Empty)
+ {
+ KeyBinding keyBinding = new ([Command.Select], KeyBindingScope.HotKey, this);
+ // Remove an existent ShortcutKey
+ _menuBar.KeyBindings.Remove (ShortcutKey!);
+ _menuBar.KeyBindings.Add (ShortcutKey!, keyBinding);
+ }
+ }
+
private void UpdateHotKeyBinding (Key oldKey)
{
if (_menuBar is null or { IsInitialized: false })
@@ -306,25 +331,15 @@ public class MenuItem
}
}
- internal void UpdateShortcutKeyBinding (Key oldKey)
+ private void UpdateShortcutKeyBinding (Key oldKey)
{
- if (_menuBar is null or { IsInitialized: false })
+ // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
+ if (_menuBar is null)
{
return;
}
- if (oldKey != Key.Empty)
- {
- _menuBar.KeyBindings.Remove (oldKey);
- }
-
- if (ShortcutKey != Key.Empty)
- {
- KeyBinding keyBinding = new ([Command.Select], KeyBindingScope.HotKey, this);
- // Remove an existent ShortcutKey
- _menuBar.KeyBindings.Remove (ShortcutKey!);
- _menuBar.KeyBindings.Add (ShortcutKey!, keyBinding);
- }
+ AddOrUpdateShortcutKeyBinding (oldKey);
}
#endregion Keyboard Handling
diff --git a/UnitTests/Views/MenuBarTests.cs b/UnitTests/Views/MenuBarTests.cs
index b9f69d65b..2697a3489 100644
--- a/UnitTests/Views/MenuBarTests.cs
+++ b/UnitTests/Views/MenuBarTests.cs
@@ -2938,7 +2938,7 @@ Edit
Assert.Contains (Key.A.WithCtrl, menuBar.KeyBindings.Bindings);
- menuBar.Menus [0].Children [0].ShortcutKey = Key.B.WithCtrl;
+ menuBar.Menus [0].Children! [0].ShortcutKey = Key.B.WithCtrl;
Assert.DoesNotContain (Key.A.WithCtrl, menuBar.KeyBindings.Bindings);
Assert.Contains (Key.B.WithCtrl, menuBar.KeyBindings.Bindings);