Browse Source

Nullable enable menu-related classes.

Steven Kirk 5 years ago
parent
commit
dd563805cd

+ 3 - 1
src/Avalonia.Controls/IMenuElement.cs

@@ -1,6 +1,8 @@
 using System.Collections.Generic;
 using Avalonia.Input;
 
+#nullable enable
+
 namespace Avalonia.Controls
 {
     /// <summary>
@@ -11,7 +13,7 @@ namespace Avalonia.Controls
         /// <summary>
         /// Gets or sets the currently selected submenu item.
         /// </summary>
-        IMenuItem SelectedItem { get; set; }
+        IMenuItem? SelectedItem { get; set; }
 
         /// <summary>
         /// Gets the submenu items.

+ 4 - 2
src/Avalonia.Controls/IMenuItem.cs

@@ -1,4 +1,6 @@
-namespace Avalonia.Controls
+#nullable enable
+
+namespace Avalonia.Controls
 {
     /// <summary>
     /// Represents a <see cref="MenuItem"/>.
@@ -29,7 +31,7 @@
         /// <summary>
         /// Gets the parent <see cref="IMenuElement"/>.
         /// </summary>
-        new IMenuElement Parent { get; }
+        new IMenuElement? Parent { get; }
 
         /// <summary>
         /// Raises a click event on the menu item.

+ 2 - 0
src/Avalonia.Controls/Menu.cs

@@ -4,6 +4,8 @@ using Avalonia.Input;
 using Avalonia.Interactivity;
 using Avalonia.Layout;
 
+#nullable enable
+
 namespace Avalonia.Controls
 {
     /// <summary>

+ 4 - 4
src/Avalonia.Controls/MenuBase.cs

@@ -8,6 +8,8 @@ using Avalonia.Input;
 using Avalonia.Interactivity;
 using Avalonia.LogicalTree;
 
+#nullable enable
+
 namespace Avalonia.Controls
 {
     /// <summary>
@@ -51,9 +53,7 @@ namespace Avalonia.Controls
         /// <param name="interactionHandler">The menu interaction handler.</param>
         public MenuBase(IMenuInteractionHandler interactionHandler)
         {
-            Contract.Requires<ArgumentNullException>(interactionHandler != null);
-
-            InteractionHandler = interactionHandler;
+            InteractionHandler = interactionHandler ?? throw new ArgumentNullException(nameof(interactionHandler));
         }
 
         /// <summary>
@@ -77,7 +77,7 @@ namespace Avalonia.Controls
         IMenuInteractionHandler IMenu.InteractionHandler => InteractionHandler;
 
         /// <inheritdoc/>
-        IMenuItem IMenuElement.SelectedItem
+        IMenuItem? IMenuElement.SelectedItem
         {
             get
             {

+ 11 - 9
src/Avalonia.Controls/MenuItem.cs

@@ -12,6 +12,8 @@ using Avalonia.Interactivity;
 using Avalonia.LogicalTree;
 using Avalonia.VisualTree;
 
+#nullable enable
+
 namespace Avalonia.Controls
 {
     /// <summary>
@@ -22,7 +24,7 @@ namespace Avalonia.Controls
         /// <summary>
         /// Defines the <see cref="Command"/> property.
         /// </summary>
-        public static readonly DirectProperty<MenuItem, ICommand> CommandProperty =
+        public static readonly DirectProperty<MenuItem, ICommand?> CommandProperty =
             Button.CommandProperty.AddOwner<MenuItem>(
                 menuItem => menuItem.Command,
                 (menuItem, command) => menuItem.Command = command,
@@ -94,10 +96,10 @@ namespace Avalonia.Controls
         private static readonly ITemplate<IPanel> DefaultPanel =
             new FuncTemplate<IPanel>(() => new StackPanel());
 
-        private ICommand _command;
+        private ICommand? _command;
         private bool _commandCanExecute = true;
-        private Popup _popup;
-        private IDisposable _gridHack;
+        private Popup? _popup;
+        private IDisposable? _gridHack;
 
         /// <summary>
         /// Initializes static members of the <see cref="MenuItem"/> class.
@@ -166,7 +168,7 @@ namespace Avalonia.Controls
         /// <summary>
         /// Gets or sets the command associated with the menu item.
         /// </summary>
-        public ICommand Command
+        public ICommand? Command
         {
             get { return _command; }
             set { SetAndRaise(CommandProperty, ref _command, value); }
@@ -246,7 +248,7 @@ namespace Avalonia.Controls
         bool IMenuItem.IsPointerOverSubMenu => _popup?.IsPointerOverPopup ?? false; 
 
         /// <inheritdoc/>
-        IMenuElement IMenuItem.Parent => Parent as IMenuElement;
+        IMenuElement? IMenuItem.Parent => Parent as IMenuElement;
 
         protected override bool IsEnabledCore => base.IsEnabledCore && _commandCanExecute;
 
@@ -254,7 +256,7 @@ namespace Avalonia.Controls
         bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap) => MoveSelection(direction, wrap);
 
         /// <inheritdoc/>
-        IMenuItem IMenuElement.SelectedItem
+        IMenuItem? IMenuElement.SelectedItem
         {
             get
             {
@@ -551,7 +553,7 @@ namespace Avalonia.Controls
         /// <param name="e">The property change event.</param>
         private void IsSelectedChanged(AvaloniaPropertyChangedEventArgs e)
         {
-            if ((bool)e.NewValue)
+            if ((bool)e.NewValue!)
             {
                 Focus();
             }
@@ -563,7 +565,7 @@ namespace Avalonia.Controls
         /// <param name="e">The property change event.</param>
         private void SubMenuOpenChanged(AvaloniaPropertyChangedEventArgs e)
         {
-            var value = (bool)e.NewValue;
+            var value = (bool)e.NewValue!;
 
             if (value)
             {

+ 4 - 3
src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs

@@ -3,7 +3,6 @@ using Avalonia.Input;
 using Avalonia.Input.Raw;
 using Avalonia.Interactivity;
 using Avalonia.LogicalTree;
-using Avalonia.Platform;
 using Avalonia.Rendering;
 using Avalonia.Threading;
 using Avalonia.VisualTree;
@@ -235,7 +234,9 @@ namespace Avalonia.Controls.Platform
                             // 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 (item.IsSubMenuOpen &&
+                                item.Parent is IMenu &&
+                                item.Parent.SelectedItem is object)
                             {
                                 item.Close();
                                 Open(item.Parent.SelectedItem, true);
@@ -385,7 +386,7 @@ namespace Avalonia.Controls.Platform
         {
             if (e.Source == Menu)
             {
-                Menu.MoveSelection(NavigationDirection.First, true);
+                Menu?.MoveSelection(NavigationDirection.First, true);
             }
         }