Quellcode durchsuchen

Enable nullability for InputElement, Interactive, Visual

Max Katz vor 4 Jahren
Ursprung
Commit
3984ba2ec5

+ 1 - 1
src/Avalonia.Controls/Control.cs

@@ -201,7 +201,7 @@ namespace Avalonia.Controls
         {
             base.OnLostFocus(e);
 
-            if (_focusAdorner != null)
+            if (_focusAdorner?.Parent != null)
             {
                 var adornerLayer = (IPanel)_focusAdorner.Parent;
                 adornerLayer.Children.Remove(_focusAdorner);

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

@@ -3,6 +3,8 @@ using Avalonia.Input;
 using Avalonia.Layout;
 using Avalonia.VisualTree;
 
+#nullable enable
+
 namespace Avalonia.Controls
 {
     /// <summary>
@@ -15,6 +17,6 @@ namespace Avalonia.Controls
         INamed,
         IStyledElement
     {
-        new IControl Parent { get; }
+        new IControl? Parent { get; }
     }
 }

+ 2 - 0
src/Avalonia.Input/IInputElement.cs

@@ -3,6 +3,8 @@ using System.Collections.Generic;
 using Avalonia.Interactivity;
 using Avalonia.VisualTree;
 
+#nullable enable
+
 namespace Avalonia.Input
 {
     /// <summary>

+ 2 - 0
src/Avalonia.Input/InputElement.cs

@@ -9,6 +9,8 @@ using Avalonia.Input.TextInput;
 using Avalonia.Interactivity;
 using Avalonia.VisualTree;
 
+#nullable enable
+
 namespace Avalonia.Input
 {
     /// <summary>

+ 2 - 2
src/Avalonia.Input/KeyboardDevice.cs

@@ -60,7 +60,7 @@ namespace Avalonia.Input
                     ie.IsKeyboardFocusWithin = false;
                 }
 
-                el = (IInputElement)el.VisualParent;
+                el = (IInputElement?)el.VisualParent;
             }
         }
         
@@ -212,7 +212,7 @@ namespace Avalonia.Input
                             Source = element,
                         };
 
-                        IVisual currentHandler = element;
+                        IVisual? currentHandler = element;
                         while (currentHandler != null && !ev.Handled && keyInput.Type == RawKeyEventType.KeyDown)
                         {
                             var bindings = (currentHandler as IInputElement)?.KeyBindings;

+ 4 - 4
src/Avalonia.Input/MouseDevice.cs

@@ -377,7 +377,7 @@ namespace Avalonia.Input
                 e.Source = element;
                 e.Handled = false;
                 element.RaiseEvent(e);
-                element = (IInputElement)element.VisualParent;
+                element = (IInputElement?)element.VisualParent;
             }
             
             root.PointerOverElement = null;
@@ -444,7 +444,7 @@ namespace Avalonia.Input
                     branch = el;
                     break;
                 }
-                el = (IInputElement)el.VisualParent;
+                el = (IInputElement?)el.VisualParent;
             }
 
             el = root.PointerOverElement;
@@ -460,7 +460,7 @@ namespace Avalonia.Input
                 e.Source = el;
                 e.Handled = false;
                 el.RaiseEvent(e);
-                el = (IInputElement)el.VisualParent;
+                el = (IInputElement?)el.VisualParent;
             }            
 
             el = root.PointerOverElement = element;
@@ -471,7 +471,7 @@ namespace Avalonia.Input
                 e.Source = el;
                 e.Handled = false;
                 el.RaiseEvent(e);
-                el = (IInputElement)el.VisualParent;
+                el = (IInputElement?)el.VisualParent;
             }
         }
 

+ 2 - 0
src/Avalonia.Interactivity/IInteractive.cs

@@ -1,5 +1,7 @@
 using System;
 
+#nullable enable
+
 namespace Avalonia.Interactivity
 {
     /// <summary>

+ 2 - 0
src/Avalonia.Interactivity/Interactive.cs

@@ -3,6 +3,8 @@ using System.Collections.Generic;
 using Avalonia.Layout;
 using Avalonia.VisualTree;
 
+#nullable enable
+
 namespace Avalonia.Interactivity
 {
     /// <summary>

+ 1 - 1
src/Avalonia.Layout/Layoutable.cs

@@ -782,7 +782,7 @@ namespace Avalonia.Layout
         }
 
         /// <inheritdoc/>
-        protected sealed override void OnVisualParentChanged(IVisual oldParent, IVisual newParent)
+        protected sealed override void OnVisualParentChanged(IVisual? oldParent, IVisual? newParent)
         {
             LayoutHelper.InvalidateSelfAndChildrenMeasure(this);
 

+ 4 - 2
src/Avalonia.Styling/Controls/ISetInheritanceParent.cs

@@ -1,3 +1,5 @@
+#nullable enable
+
 namespace Avalonia.Controls
 {
     /// <summary>
@@ -14,6 +16,6 @@ namespace Avalonia.Controls
         /// Sets the control's inheritance parent.
         /// </summary>
         /// <param name="parent">The parent.</param>
-        void SetParent(IAvaloniaObject parent);
+        void SetParent(IAvaloniaObject? parent);
     }
-}
+}

+ 4 - 2
src/Avalonia.Styling/Controls/ISetLogicalParent.cs

@@ -1,5 +1,7 @@
 using Avalonia.LogicalTree;
 
+#nullable enable
+
 namespace Avalonia.Controls
 {
     /// <summary>
@@ -14,6 +16,6 @@ namespace Avalonia.Controls
         /// Sets the control's parent.
         /// </summary>
         /// <param name="parent">The parent.</param>
-        void SetParent(ILogical parent);
+        void SetParent(ILogical? parent);
     }
-}
+}

+ 4 - 2
src/Avalonia.Styling/IDataContextProvider.cs

@@ -1,4 +1,6 @@
-namespace Avalonia
+#nullable enable
+
+namespace Avalonia
 {
     /// <summary>
     /// Defines an element with a data context that can be used for binding.
@@ -8,6 +10,6 @@
         /// <summary>
         /// Gets or sets the element's data context.
         /// </summary>
-        object DataContext { get; set; }
+        object? DataContext { get; set; }
     }
 }

+ 4 - 2
src/Avalonia.Styling/IStyledElement.cs

@@ -4,6 +4,8 @@ using Avalonia.Controls;
 using Avalonia.LogicalTree;
 using Avalonia.Styling;
 
+#nullable enable
+
 namespace Avalonia
 {
     public interface IStyledElement :
@@ -17,7 +19,7 @@ namespace Avalonia
         /// <summary>
         /// Occurs when the control has finished initialization.
         /// </summary>
-        event EventHandler Initialized;
+        event EventHandler? Initialized;
 
         /// <summary>
         /// Gets a value that indicates whether the element has finished initialization.
@@ -32,6 +34,6 @@ namespace Avalonia
         /// <summary>
         /// Gets the control's logical parent.
         /// </summary>
-        IStyledElement Parent { get; }
+        IStyledElement? Parent { get; }
     }
 }

+ 1 - 1
src/Avalonia.Styling/StyledElement.cs

@@ -465,7 +465,7 @@ namespace Avalonia
         /// Sets the styled element's inheritance parent.
         /// </summary>
         /// <param name="parent">The parent.</param>
-        void ISetInheritanceParent.SetParent(IAvaloniaObject parent)
+        void ISetInheritanceParent.SetParent(IAvaloniaObject? parent)
         {
             InheritanceParent = parent;
         }

+ 3 - 1
src/Avalonia.Styling/Styling/ISetterValue.cs

@@ -1,4 +1,6 @@
-namespace Avalonia.Styling
+#nullable enable
+
+namespace Avalonia.Styling
 {
     /// <summary>
     /// Customizes the behavior of a class when added as a value to an <see cref="ISetter"/>.

+ 26 - 24
src/Avalonia.Visuals/Visual.cs

@@ -11,6 +11,8 @@ using Avalonia.Rendering;
 using Avalonia.Utilities;
 using Avalonia.VisualTree;
 
+#nullable enable
+
 namespace Avalonia
 {
     /// <summary>
@@ -45,8 +47,8 @@ namespace Avalonia
         /// <summary>
         /// Defines the <see cref="Clip"/> property.
         /// </summary>
-        public static readonly StyledProperty<Geometry> ClipProperty =
-            AvaloniaProperty.Register<Visual, Geometry>(nameof(Clip));
+        public static readonly StyledProperty<Geometry?> ClipProperty =
+            AvaloniaProperty.Register<Visual, Geometry?>(nameof(Clip));
 
         /// <summary>
         /// Defines the <see cref="IsVisibleProperty"/> property.
@@ -63,14 +65,14 @@ namespace Avalonia
         /// <summary>
         /// Defines the <see cref="OpacityMask"/> property.
         /// </summary>
-        public static readonly StyledProperty<IBrush> OpacityMaskProperty =
-            AvaloniaProperty.Register<Visual, IBrush>(nameof(OpacityMask));
+        public static readonly StyledProperty<IBrush?> OpacityMaskProperty =
+            AvaloniaProperty.Register<Visual, IBrush?>(nameof(OpacityMask));
 
         /// <summary>
         /// Defines the <see cref="RenderTransform"/> property.
         /// </summary>
-        public static readonly StyledProperty<ITransform> RenderTransformProperty =
-            AvaloniaProperty.Register<Visual, ITransform>(nameof(RenderTransform));
+        public static readonly StyledProperty<ITransform?> RenderTransformProperty =
+            AvaloniaProperty.Register<Visual, ITransform?>(nameof(RenderTransform));
 
         /// <summary>
         /// Defines the <see cref="RenderTransformOrigin"/> property.
@@ -81,8 +83,8 @@ namespace Avalonia
         /// <summary>
         /// Defines the <see cref="IVisual.VisualParent"/> property.
         /// </summary>
-        public static readonly DirectProperty<Visual, IVisual> VisualParentProperty =
-            AvaloniaProperty.RegisterDirect<Visual, IVisual>("VisualParent", o => o._visualParent);
+        public static readonly DirectProperty<Visual, IVisual?> VisualParentProperty =
+            AvaloniaProperty.RegisterDirect<Visual, IVisual?>(nameof(IVisual.VisualParent), o => o._visualParent);
 
         /// <summary>
         /// Defines the <see cref="ZIndex"/> property.
@@ -92,8 +94,8 @@ namespace Avalonia
 
         private Rect _bounds;
         private TransformedBounds? _transformedBounds;
-        private IRenderRoot _visualRoot;
-        private IVisual _visualParent;
+        private IRenderRoot? _visualRoot;
+        private IVisual? _visualParent;
 
         /// <summary>
         /// Initializes static members of the <see cref="Visual"/> class.
@@ -128,12 +130,12 @@ namespace Avalonia
         /// <summary>
         /// Raised when the control is attached to a rooted visual tree.
         /// </summary>
-        public event EventHandler<VisualTreeAttachmentEventArgs> AttachedToVisualTree;
+        public event EventHandler<VisualTreeAttachmentEventArgs>? AttachedToVisualTree;
 
         /// <summary>
         /// Raised when the control is detached from a rooted visual tree.
         /// </summary>
-        public event EventHandler<VisualTreeAttachmentEventArgs> DetachedFromVisualTree;
+        public event EventHandler<VisualTreeAttachmentEventArgs>? DetachedFromVisualTree;
 
         /// <summary>
         /// Gets the bounds of the control relative to its parent.
@@ -161,7 +163,7 @@ namespace Avalonia
         /// <summary>
         /// Gets or sets the geometry clip for this visual.
         /// </summary>
-        public Geometry Clip
+        public Geometry? Clip
         {
             get { return GetValue(ClipProperty); }
             set { SetValue(ClipProperty, value); }
@@ -174,7 +176,7 @@ namespace Avalonia
         {
             get
             {
-                IVisual node = this;
+                IVisual? node = this;
 
                 while (node != null)
                 {
@@ -211,7 +213,7 @@ namespace Avalonia
         /// <summary>
         /// Gets or sets the opacity mask of the control.
         /// </summary>
-        public IBrush OpacityMask
+        public IBrush? OpacityMask
         {
             get { return GetValue(OpacityMaskProperty); }
             set { SetValue(OpacityMaskProperty, value); }
@@ -220,7 +222,7 @@ namespace Avalonia
         /// <summary>
         /// Gets or sets the render transform of the control.
         /// </summary>
-        public ITransform RenderTransform
+        public ITransform? RenderTransform
         {
             get { return GetValue(RenderTransformProperty); }
             set { SetValue(RenderTransformProperty, value); }
@@ -261,7 +263,7 @@ namespace Avalonia
         /// <summary>
         /// Gets the root of the visual tree, if the control is attached to a visual tree.
         /// </summary>
-        protected IRenderRoot VisualRoot => _visualRoot ?? (this as IRenderRoot);
+        protected IRenderRoot? VisualRoot => _visualRoot ?? (this as IRenderRoot);
 
         /// <summary>
         /// Gets a value indicating whether this control is attached to a visual root.
@@ -276,12 +278,12 @@ namespace Avalonia
         /// <summary>
         /// Gets the control's parent visual.
         /// </summary>
-        IVisual IVisual.VisualParent => _visualParent;
+        IVisual? IVisual.VisualParent => _visualParent;
 
         /// <summary>
         /// Gets the root of the visual tree, if the control is attached to a visual tree.
         /// </summary>
-        IRenderRoot IVisual.VisualRoot => VisualRoot;
+        IRenderRoot? IVisual.VisualRoot => VisualRoot;
         
         TransformedBounds? IVisual.TransformedBounds
         {
@@ -476,12 +478,12 @@ namespace Avalonia
         /// </summary>
         /// <param name="oldParent">The old visual parent.</param>
         /// <param name="newParent">The new visual parent.</param>
-        protected virtual void OnVisualParentChanged(IVisual oldParent, IVisual newParent)
+        protected virtual void OnVisualParentChanged(IVisual? oldParent, IVisual? newParent)
         {
             RaisePropertyChanged(
                 VisualParentProperty,
-                new Optional<IVisual>(oldParent),
-                new BindingValue<IVisual>(newParent),
+                new Optional<IVisual?>(oldParent),
+                new BindingValue<IVisual?>(newParent),
                 BindingPriority.LocalValue);
         }
 
@@ -582,7 +584,7 @@ namespace Avalonia
         /// Sets the visual parent of the Visual.
         /// </summary>
         /// <param name="value">The visual parent.</param>
-        private void SetVisualParent(Visual value)
+        private void SetVisualParent(Visual? value)
         {
             if (_visualParent == value)
             {
@@ -634,7 +636,7 @@ namespace Avalonia
             }
         }
         
-        private static void SetVisualParent(IList children, Visual parent)
+        private static void SetVisualParent(IList children, Visual? parent)
         {
             var count = children.Count;
 

+ 9 - 7
src/Avalonia.Visuals/VisualTree/IVisual.cs

@@ -3,6 +3,8 @@ using Avalonia.Collections;
 using Avalonia.Media;
 using Avalonia.Rendering;
 
+#nullable enable
+
 namespace Avalonia.VisualTree
 {
     /// <summary>
@@ -21,12 +23,12 @@ namespace Avalonia.VisualTree
         /// <summary>
         /// Raised when the control is attached to a rooted visual tree.
         /// </summary>
-        event EventHandler<VisualTreeAttachmentEventArgs> AttachedToVisualTree;
+        event EventHandler<VisualTreeAttachmentEventArgs>? AttachedToVisualTree;
 
         /// <summary>
         /// Raised when the control is detached from a rooted visual tree.
         /// </summary>
-        event EventHandler<VisualTreeAttachmentEventArgs> DetachedFromVisualTree;
+        event EventHandler<VisualTreeAttachmentEventArgs>? DetachedFromVisualTree;
 
         /// <summary>
         /// Gets the bounds of the control relative to its parent.
@@ -46,7 +48,7 @@ namespace Avalonia.VisualTree
         /// <summary>
         /// Gets or sets the geometry clip for this visual.
         /// </summary>
-        Geometry Clip { get; set; }
+        Geometry? Clip { get; set; }
 
         /// <summary>
         /// Gets a value indicating whether this control is attached to a visual root.
@@ -71,12 +73,12 @@ namespace Avalonia.VisualTree
         /// <summary>
         /// Gets or sets the opacity mask for the control.
         /// </summary>
-        IBrush OpacityMask { get; set; }
+        IBrush? OpacityMask { get; set; }
 
         /// <summary>
         /// Gets or sets the render transform of the control.
         /// </summary>
-        ITransform RenderTransform { get; set; }
+        ITransform? RenderTransform { get; set; }
 
         /// <summary>
         /// Gets or sets the render transform origin of the control.
@@ -91,12 +93,12 @@ namespace Avalonia.VisualTree
         /// <summary>
         /// Gets the control's parent visual.
         /// </summary>
-        IVisual VisualParent { get; }
+        IVisual? VisualParent { get; }
 
         /// <summary>
         /// Gets the root of the visual tree, if the control is attached to a visual tree.
         /// </summary>
-        IRenderRoot VisualRoot { get; }
+        IRenderRoot? VisualRoot { get; }
 
         /// <summary>
         /// Gets or sets the Z index of the node.