From c7bf1ecb4bbfa8f8ffc92d23958049fe4e23dc03 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 14 Aug 2018 21:14:54 +0200 Subject: [PATCH] Fix null checking. --- .../Platform/DefaultMenuInteractionHandler.cs | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs index 3cd3094483..a44495b90c 100644 --- a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs +++ b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs @@ -135,6 +135,8 @@ namespace Avalonia.Controls.Platform protected internal virtual void KeyDown(IMenuItem item, KeyEventArgs e) { + Contract.Requires(item != null); + switch (e.Key) { case Key.Up: @@ -154,7 +156,7 @@ namespace Avalonia.Controls.Platform break; case Key.Left: - if (item?.Parent is IMenuItem parent && !parent.IsTopLevel && parent.IsSubMenuOpen) + if (item.Parent is IMenuItem parent && !parent.IsTopLevel && parent.IsSubMenuOpen) { parent.Close(); parent.Focus(); @@ -203,20 +205,17 @@ namespace Avalonia.Controls.Platform default: var direction = e.Key.ToNavigationDirection(); - if (direction.HasValue) + if (direction.HasValue && item.Parent?.MoveSelection(direction.Value, true) == true) { - if (item.Parent?.MoveSelection(direction.Value, true) == true) + // If the the parent is an IMenu which successfully moved its selection, + // and the current menu is open then close the current menu and open the + // new menu. + if (item.IsSubMenuOpen && item.Parent is IMenu) { - // If the the parent is an IMenu which successfully moved its selection, - // and the current menu is open then close the current menu and open the - // new menu. - if (item.IsSubMenuOpen && item.Parent is IMenu) - { - item.Close(); - Open(item.Parent.SelectedItem, true); - } - e.Handled = true; + item.Close(); + Open(item.Parent.SelectedItem, true); } + e.Handled = true; } break; @@ -387,7 +386,7 @@ namespace Avalonia.Controls.Platform while (current != null && !(current is IMenu)) { - current = (current as IMenuItem).Parent; + current = (current as IMenuItem)?.Parent; } current?.Close();