Browse Source

Fix some more bugs. Nullable is very treacherous.

BDisp 11 tháng trước cách đây
mục cha
commit
1e9f2f4778

+ 2 - 1
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);

+ 8 - 2
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;
+    }
+
     /// <summary>
     /// Add a <see cref="MenuBarItem"/> dynamically into the <see cref="MenuBar"/><c>.Menus</c>.
     /// </summary>

+ 29 - 14
Terminal.Gui/Views/Menu/MenuItem.cs

@@ -276,6 +276,31 @@ public class MenuItem
     /// <summary>Gets the text describing the keystroke combination defined by <see cref="ShortcutKey"/>.</summary>
     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

+ 1 - 1
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);