Explorar o código

Fixes #2892 for v1 - Null reference pressing menu shortcuts when menus have separators (#2955)

BDisp hai 1 ano
pai
achega
d51322ad17
Modificáronse 2 ficheiros con 20 adicións e 0 borrados
  1. 5 0
      Terminal.Gui/Views/Menu.cs
  2. 15 0
      UnitTests/Menus/MenuTests.cs

+ 5 - 0
Terminal.Gui/Views/Menu.cs

@@ -1677,6 +1677,11 @@ namespace Terminal.Gui {
 			var c = ((uint)kb.Key & (uint)Key.CharMask);
 			for (int i = 0; i < children.Length; i++) {
 				var mi = children [i];
+
+				if (mi == null) {
+					continue;
+				}
+
 				int p = mi.Title.IndexOf (MenuBar.HotKeySpecifier);
 				if (p != -1 && p + 1 < mi.Title.RuneCount) {
 					if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {

+ 15 - 0
UnitTests/Menus/MenuTests.cs

@@ -1809,5 +1809,20 @@ Edit
 			Assert.True (menu.ProcessHotKey (new KeyEvent (menu.Key, new KeyModifiers ())));
 			Assert.False (menu.IsMenuOpen);
 		}
+
+		[Fact]
+		public void Separators_Does_Not_Throws_Pressing_Menu_Shortcut ()
+		{
+			var menu = new MenuBar (new MenuBarItem [] {
+				new MenuBarItem ("File", new MenuItem [] {
+					new MenuItem ("_New", "", null),
+					null,
+					new MenuItem ("_Quit", "", null)
+				})
+			});
+
+			var exception = Record.Exception (() => Assert.True (menu.ProcessHotKey (new KeyEvent (Key.AltMask | Key.Q, new KeyModifiers () { Alt = true }))));
+			Assert.Null (exception);
+		}
 	}
 }