From 328199b2d22aafcb194f961508ac8b59d394b820 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Dec 2021 14:02:28 +0100 Subject: [PATCH] Added nullable annotations to Avalonia.Styling. --- src/Avalonia.Styling/Avalonia.Styling.csproj | 1 + .../Controls/ChildNameScope.cs | 12 ++--- src/Avalonia.Styling/Controls/INameScope.cs | 4 +- src/Avalonia.Styling/Controls/NameScope.cs | 30 ++++++----- .../Controls/NameScopeExtensions.cs | 28 +++++------ .../Controls/NameScopeLocator.cs | 8 +-- .../Controls/PseudoClassesExtensions.cs | 2 +- .../Controls/ResourceDictionary.cs | 2 +- .../Controls/ResourceNodeExtensions.cs | 6 +-- src/Avalonia.Styling/INamed.cs | 2 +- .../LogicalTree/ControlLocator.cs | 16 +++--- src/Avalonia.Styling/LogicalTree/ILogical.cs | 6 +-- .../LogicalTree/LogicalExtensions.cs | 26 +++++----- .../LogicalTreeAttachmentEventArgs.cs | 7 +-- src/Avalonia.Styling/StyledElement.cs | 18 +++---- .../Styling/Activators/NthChildActivator.cs | 2 +- .../Styling/Activators/StyleClassActivator.cs | 2 +- src/Avalonia.Styling/Styling/ChildSelector.cs | 6 +-- src/Avalonia.Styling/Styling/OrSelector.cs | 2 +- .../Styling/PropertyEqualsSelector.cs | 2 +- src/Avalonia.Styling/Styling/Selectors.cs | 50 +++++++++++-------- src/Avalonia.Styling/Styling/Styles.cs | 14 +++--- .../Styling/TemplateSelector.cs | 6 +-- src/Avalonia.Visuals/Visual.cs | 2 +- .../Markup/Parsers/SelectorParser.cs | 6 +-- 25 files changed, 132 insertions(+), 128 deletions(-) diff --git a/src/Avalonia.Styling/Avalonia.Styling.csproj b/src/Avalonia.Styling/Avalonia.Styling.csproj index 20b3183b002..35487498469 100644 --- a/src/Avalonia.Styling/Avalonia.Styling.csproj +++ b/src/Avalonia.Styling/Avalonia.Styling.csproj @@ -9,4 +9,5 @@ + diff --git a/src/Avalonia.Styling/Controls/ChildNameScope.cs b/src/Avalonia.Styling/Controls/ChildNameScope.cs index e6707e71dbe..58114a57fdb 100644 --- a/src/Avalonia.Styling/Controls/ChildNameScope.cs +++ b/src/Avalonia.Styling/Controls/ChildNameScope.cs @@ -15,20 +15,20 @@ public ChildNameScope(INameScope parentScope) public void Register(string name, object element) => _inner.Register(name, element); - public SynchronousCompletionAsyncResult FindAsync(string name) + public SynchronousCompletionAsyncResult FindAsync(string name) { var found = Find(name); if (found != null) - return new SynchronousCompletionAsyncResult(found); + return new SynchronousCompletionAsyncResult(found); // Not found and both current and parent scope are in completed state if(IsCompleted) - return new SynchronousCompletionAsyncResult(null); + return new SynchronousCompletionAsyncResult(null); return DoFindAsync(name); } - public SynchronousCompletionAsyncResult DoFindAsync(string name) + public SynchronousCompletionAsyncResult DoFindAsync(string name) { - var src = new SynchronousCompletionAsyncResultSource(); + var src = new SynchronousCompletionAsyncResultSource(); void ParentSearch() { @@ -56,7 +56,7 @@ void ParentSearch() return src.AsyncResult; } - public object Find(string name) + public object? Find(string name) { var found = _inner.Find(name); if (found != null) diff --git a/src/Avalonia.Styling/Controls/INameScope.cs b/src/Avalonia.Styling/Controls/INameScope.cs index 2d5295fe453..1ca7db2f37b 100644 --- a/src/Avalonia.Styling/Controls/INameScope.cs +++ b/src/Avalonia.Styling/Controls/INameScope.cs @@ -22,14 +22,14 @@ public interface INameScope /// /// The name. /// The element, or null if the name was not found. - SynchronousCompletionAsyncResult FindAsync(string name); + SynchronousCompletionAsyncResult FindAsync(string name); /// /// Finds a named element in the name scope, returns immediately, doesn't traverse the name scope stack /// /// The name. /// The element, or null if the name was not found. - object Find(string name); + object? Find(string name); /// /// Marks the name scope as completed, no further registrations will be allowed diff --git a/src/Avalonia.Styling/Controls/NameScope.cs b/src/Avalonia.Styling/Controls/NameScope.cs index 62a04eac8b7..77f98f85c40 100644 --- a/src/Avalonia.Styling/Controls/NameScope.cs +++ b/src/Avalonia.Styling/Controls/NameScope.cs @@ -22,8 +22,8 @@ public class NameScope : INameScope private readonly Dictionary _inner = new Dictionary(); - private readonly Dictionary> _pendingSearches = - new Dictionary>(); + private readonly Dictionary> _pendingSearches = + new Dictionary>(); /// /// Gets the value of the attached on a styled element. @@ -32,7 +32,7 @@ public class NameScope : INameScope /// The value of the NameScope attached property. public static INameScope GetNameScope(StyledElement styled) { - Contract.Requires(styled != null); + _ = styled ?? throw new ArgumentNullException(nameof(styled)); return styled.GetValue(NameScopeProperty); } @@ -44,7 +44,7 @@ public static INameScope GetNameScope(StyledElement styled) /// The value to set. public static void SetNameScope(StyledElement styled, INameScope value) { - Contract.Requires(styled != null); + _ = styled ?? throw new ArgumentNullException(nameof(styled)); styled.SetValue(NameScopeProperty, value); } @@ -54,12 +54,11 @@ public void Register(string name, object element) { if (IsCompleted) throw new InvalidOperationException("NameScope is completed, no further registrations are allowed"); - Contract.Requires(name != null); - Contract.Requires(element != null); - object existing; + _ = name ?? throw new ArgumentNullException(nameof(name)); + _ = element ?? throw new ArgumentNullException(nameof(element)); - if (_inner.TryGetValue(name, out existing)) + if (_inner.TryGetValue(name, out var existing)) { if (existing != element) { @@ -77,27 +76,26 @@ public void Register(string name, object element) } } - public SynchronousCompletionAsyncResult FindAsync(string name) + public SynchronousCompletionAsyncResult FindAsync(string name) { var found = Find(name); if (found != null) - return new SynchronousCompletionAsyncResult(found); + return new SynchronousCompletionAsyncResult(found); if (IsCompleted) - return new SynchronousCompletionAsyncResult((object)null); + return new SynchronousCompletionAsyncResult(null); if (!_pendingSearches.TryGetValue(name, out var tcs)) // We are intentionally running continuations synchronously here - _pendingSearches[name] = tcs = new SynchronousCompletionAsyncResultSource(); + _pendingSearches[name] = tcs = new SynchronousCompletionAsyncResultSource(); return tcs.AsyncResult; } /// - public object Find(string name) + public object? Find(string name) { - Contract.Requires(name != null); + _ = name ?? throw new ArgumentNullException(nameof(name)); - object result; - _inner.TryGetValue(name, out result); + _inner.TryGetValue(name, out var result); return result; } diff --git a/src/Avalonia.Styling/Controls/NameScopeExtensions.cs b/src/Avalonia.Styling/Controls/NameScopeExtensions.cs index 75630711b85..3895b6ceb9c 100644 --- a/src/Avalonia.Styling/Controls/NameScopeExtensions.cs +++ b/src/Avalonia.Styling/Controls/NameScopeExtensions.cs @@ -17,11 +17,11 @@ public static class NameScopeExtensions /// The name scope. /// The name. /// The named element or null if not found. - public static T Find(this INameScope nameScope, string name) + public static T? Find(this INameScope nameScope, string name) where T : class { - Contract.Requires(nameScope != null); - Contract.Requires(name != null); + _ = nameScope ?? throw new ArgumentNullException(nameof(nameScope)); + _ = name ?? throw new ArgumentNullException(nameof(name)); var result = nameScope.Find(name); @@ -31,7 +31,7 @@ public static T Find(this INameScope nameScope, string name) $"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'."); } - return (T)result; + return (T?)result; } /// @@ -41,11 +41,11 @@ public static T Find(this INameScope nameScope, string name) /// The control to take the name scope from. /// The name. /// The named element or null if not found. - public static T Find(this ILogical anchor, string name) + public static T? Find(this ILogical anchor, string name) where T : class { - Contract.Requires(anchor != null); - Contract.Requires(name != null); + _ = anchor ?? throw new ArgumentNullException(nameof(anchor)); + _ = name ?? throw new ArgumentNullException(nameof(name)); var styledAnchor = anchor as StyledElement; if (styledAnchor == null) return null; @@ -64,8 +64,8 @@ public static T Find(this ILogical anchor, string name) public static T Get(this INameScope nameScope, string name) where T : class { - Contract.Requires(nameScope != null); - Contract.Requires(name != null); + _ = nameScope ?? throw new ArgumentNullException(nameof(nameScope)); + _ = name ?? throw new ArgumentNullException(nameof(name)); var result = nameScope.Find(name); @@ -94,9 +94,9 @@ public static T Get(this INameScope nameScope, string name) public static T Get(this ILogical anchor, string name) where T : class { - Contract.Requires(anchor != null); - Contract.Requires(name != null); - + _ = anchor ?? throw new ArgumentNullException(nameof(anchor)); + _ = name ?? throw new ArgumentNullException(nameof(name)); + var nameScope = (anchor as INameScope) ?? NameScope.GetNameScope((StyledElement)anchor); if (nameScope == null) throw new InvalidOperationException( @@ -105,9 +105,9 @@ public static T Get(this ILogical anchor, string name) return nameScope.Get(name); } - public static INameScope FindNameScope(this ILogical control) + public static INameScope? FindNameScope(this ILogical control) { - Contract.Requires(control != null); + _ = control ?? throw new ArgumentNullException(nameof(control)); var scope = control.GetSelfAndLogicalAncestors() .OfType() diff --git a/src/Avalonia.Styling/Controls/NameScopeLocator.cs b/src/Avalonia.Styling/Controls/NameScopeLocator.cs index 51f4c5c4ebd..f0ce7f8a5b7 100644 --- a/src/Avalonia.Styling/Controls/NameScopeLocator.cs +++ b/src/Avalonia.Styling/Controls/NameScopeLocator.cs @@ -11,9 +11,9 @@ public class NameScopeLocator /// /// The scope relative from which the object should be resolved. /// The name of the object to find. - public static IObservable Track(INameScope scope, string name) + public static IObservable Track(INameScope scope, string name) { - return new NeverEndingSynchronousCompletionAsyncResultObservable(scope.FindAsync(name)); + return new NeverEndingSynchronousCompletionAsyncResultObservable(scope.FindAsync(name)); } // This class is implemented in such weird way because for some reason @@ -22,7 +22,7 @@ public static IObservable Track(INameScope scope, string name) private class NeverEndingSynchronousCompletionAsyncResultObservable : IObservable { - private T _value; + private T? _value; private SynchronousCompletionAsyncResult? _asyncResult; public NeverEndingSynchronousCompletionAsyncResultObservable(SynchronousCompletionAsyncResult task) @@ -47,7 +47,7 @@ public IDisposable Subscribe(IObserver observer) observer.OnNext(_asyncResult.Value.GetResult()); }); else - observer.OnNext(_value); + observer.OnNext(_value!); return Disposable.Empty; } diff --git a/src/Avalonia.Styling/Controls/PseudoClassesExtensions.cs b/src/Avalonia.Styling/Controls/PseudoClassesExtensions.cs index 7b28d1a9118..4cddc8c8fb4 100644 --- a/src/Avalonia.Styling/Controls/PseudoClassesExtensions.cs +++ b/src/Avalonia.Styling/Controls/PseudoClassesExtensions.cs @@ -12,7 +12,7 @@ public static class PseudolassesExtensions /// True to add the pseudoclass or false to remove. public static void Set(this IPseudoClasses classes, string name, bool value) { - Contract.Requires(classes != null); + _ = classes ?? throw new ArgumentNullException(nameof(classes)); if (value) { diff --git a/src/Avalonia.Styling/Controls/ResourceDictionary.cs b/src/Avalonia.Styling/Controls/ResourceDictionary.cs index e797f8cf8b7..3af14daa833 100644 --- a/src/Avalonia.Styling/Controls/ResourceDictionary.cs +++ b/src/Avalonia.Styling/Controls/ResourceDictionary.cs @@ -177,7 +177,7 @@ void IResourceProvider.RemoveOwner(IResourceHost owner) } } - private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); } diff --git a/src/Avalonia.Styling/Controls/ResourceNodeExtensions.cs b/src/Avalonia.Styling/Controls/ResourceNodeExtensions.cs index be1069a4da4..513b3f24246 100644 --- a/src/Avalonia.Styling/Controls/ResourceNodeExtensions.cs +++ b/src/Avalonia.Styling/Controls/ResourceNodeExtensions.cs @@ -109,7 +109,7 @@ protected override void Subscribed(IObserver observer, bool first) observer.OnNext(Convert(_target.FindResource(_key))); } - private void ResourcesChanged(object sender, ResourcesChangedEventArgs e) + private void ResourcesChanged(object? sender, ResourcesChangedEventArgs e) { PublishNext(Convert(_target.FindResource(_key))); } @@ -159,7 +159,7 @@ private void PublishNext() } } - private void OwnerChanged(object sender, EventArgs e) + private void OwnerChanged(object? sender, EventArgs e) { if (_owner is object) { @@ -176,7 +176,7 @@ private void OwnerChanged(object sender, EventArgs e) PublishNext(); } - private void ResourcesChanged(object sender, ResourcesChangedEventArgs e) + private void ResourcesChanged(object? sender, ResourcesChangedEventArgs e) { PublishNext(); } diff --git a/src/Avalonia.Styling/INamed.cs b/src/Avalonia.Styling/INamed.cs index 1f8d269b073..df83f3b6ef6 100644 --- a/src/Avalonia.Styling/INamed.cs +++ b/src/Avalonia.Styling/INamed.cs @@ -8,6 +8,6 @@ public interface INamed /// /// Gets the element name. /// - string Name { get; } + string? Name { get; } } } diff --git a/src/Avalonia.Styling/LogicalTree/ControlLocator.cs b/src/Avalonia.Styling/LogicalTree/ControlLocator.cs index 6d0302ace43..f60ed5aab85 100644 --- a/src/Avalonia.Styling/LogicalTree/ControlLocator.cs +++ b/src/Avalonia.Styling/LogicalTree/ControlLocator.cs @@ -9,19 +9,19 @@ namespace Avalonia.LogicalTree /// public static class ControlLocator { - public static IObservable Track(ILogical relativeTo, int ancestorLevel, Type ancestorType = null) + public static IObservable Track(ILogical relativeTo, int ancestorLevel, Type? ancestorType = null) { return new ControlTracker(relativeTo, ancestorLevel, ancestorType); } - private class ControlTracker : LightweightObservableBase + private class ControlTracker : LightweightObservableBase { private readonly ILogical _relativeTo; private readonly int _ancestorLevel; - private readonly Type _ancestorType; - private ILogical _value; + private readonly Type? _ancestorType; + private ILogical? _value; - public ControlTracker(ILogical relativeTo, int ancestorLevel, Type ancestorType) + public ControlTracker(ILogical relativeTo, int ancestorLevel, Type? ancestorType) { _relativeTo = relativeTo; _ancestorLevel = ancestorLevel; @@ -43,18 +43,18 @@ protected override void Deinitialize() _value = null; } - protected override void Subscribed(IObserver observer, bool first) + protected override void Subscribed(IObserver observer, bool first) { observer.OnNext(_value); } - private void Attached(object sender, LogicalTreeAttachmentEventArgs e) + private void Attached(object? sender, LogicalTreeAttachmentEventArgs e) { Update(); PublishNext(_value); } - private void Detached(object sender, LogicalTreeAttachmentEventArgs e) + private void Detached(object? sender, LogicalTreeAttachmentEventArgs e) { _value = null; PublishNext(null); diff --git a/src/Avalonia.Styling/LogicalTree/ILogical.cs b/src/Avalonia.Styling/LogicalTree/ILogical.cs index 9c4223618f9..caff3d81500 100644 --- a/src/Avalonia.Styling/LogicalTree/ILogical.cs +++ b/src/Avalonia.Styling/LogicalTree/ILogical.cs @@ -12,12 +12,12 @@ public interface ILogical /// /// Raised when the control is attached to a rooted logical tree. /// - event EventHandler AttachedToLogicalTree; + event EventHandler? AttachedToLogicalTree; /// /// Raised when the control is detached from a rooted logical tree. /// - event EventHandler DetachedFromLogicalTree; + event EventHandler? DetachedFromLogicalTree; /// /// Gets a value indicating whether the element is attached to a rooted logical tree. @@ -27,7 +27,7 @@ public interface ILogical /// /// Gets the logical parent. /// - ILogical LogicalParent { get; } + ILogical? LogicalParent { get; } /// /// Gets the logical children. diff --git a/src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs b/src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs index 458ab0fce2b..74720c0a77a 100644 --- a/src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs +++ b/src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs @@ -15,14 +15,14 @@ public static class LogicalExtensions /// The logical's ancestors. public static IEnumerable GetLogicalAncestors(this ILogical logical) { - Contract.Requires(logical != null); + _ = logical ?? throw new ArgumentNullException(nameof(logical)); - logical = logical.LogicalParent; + ILogical? l = logical.LogicalParent; - while (logical != null) + while (l != null) { - yield return logical; - logical = logical.LogicalParent; + yield return l; + l = l.LogicalParent; } } @@ -48,14 +48,14 @@ public static IEnumerable GetSelfAndLogicalAncestors(this ILogical log /// The logical. /// If given logical should be included in search. /// First ancestor of given type. - public static T FindLogicalAncestorOfType(this ILogical logical, bool includeSelf = false) where T : class + public static T? FindLogicalAncestorOfType(this ILogical logical, bool includeSelf = false) where T : class { if (logical is null) { return null; } - ILogical parent = includeSelf ? logical : logical.LogicalParent; + var parent = includeSelf ? logical : logical.LogicalParent; while (parent != null) { @@ -120,7 +120,7 @@ public static IEnumerable GetSelfAndLogicalDescendants(this ILogical l /// The logical. /// If given logical should be included in search. /// First descendant of given type. - public static T FindLogicalDescendantOfType(this ILogical logical, bool includeSelf = false) where T : class + public static T? FindLogicalDescendantOfType(this ILogical logical, bool includeSelf = false) where T : class { if (logical is null) { @@ -140,7 +140,7 @@ public static T FindLogicalDescendantOfType(this ILogical logical, bool inclu /// /// The logical. /// The parent, or null if the logical is unparented. - public static ILogical GetLogicalParent(this ILogical logical) + public static ILogical? GetLogicalParent(this ILogical logical) { return logical.LogicalParent; } @@ -153,7 +153,7 @@ public static ILogical GetLogicalParent(this ILogical logical) /// /// The parent, or null if the logical is unparented or its parent is not of type . /// - public static T GetLogicalParent(this ILogical logical) where T : class + public static T? GetLogicalParent(this ILogical logical) where T : class { return logical.LogicalParent as T; } @@ -165,7 +165,7 @@ public static T GetLogicalParent(this ILogical logical) where T : class /// The logical siblings. public static IEnumerable GetLogicalSiblings(this ILogical logical) { - ILogical parent = logical.LogicalParent; + var parent = logical.LogicalParent; if (parent != null) { @@ -187,7 +187,7 @@ public static IEnumerable GetLogicalSiblings(this ILogical logical) /// public static bool IsLogicalAncestorOf(this ILogical logical, ILogical target) { - ILogical current = target?.LogicalParent; + var current = target?.LogicalParent; while (current != null) { @@ -202,7 +202,7 @@ public static bool IsLogicalAncestorOf(this ILogical logical, ILogical target) return false; } - private static T FindDescendantOfTypeCore(ILogical logical) where T : class + private static T? FindDescendantOfTypeCore(ILogical logical) where T : class { var logicalChildren = logical.LogicalChildren; var logicalChildrenCount = logicalChildren.Count; diff --git a/src/Avalonia.Styling/LogicalTree/LogicalTreeAttachmentEventArgs.cs b/src/Avalonia.Styling/LogicalTree/LogicalTreeAttachmentEventArgs.cs index 891724599cc..826fc5b2a47 100644 --- a/src/Avalonia.Styling/LogicalTree/LogicalTreeAttachmentEventArgs.cs +++ b/src/Avalonia.Styling/LogicalTree/LogicalTreeAttachmentEventArgs.cs @@ -19,11 +19,8 @@ public LogicalTreeAttachmentEventArgs( ILogical source, ILogical parent) { - Contract.Requires(root != null); - Contract.Requires(source != null); - - Root = root; - Source = source; + Root = root ?? throw new ArgumentNullException(nameof(root)); + Source = source ?? throw new ArgumentNullException(nameof(source)); Parent = parent; } diff --git a/src/Avalonia.Styling/StyledElement.cs b/src/Avalonia.Styling/StyledElement.cs index 2292f5c5182..5f498623e15 100644 --- a/src/Avalonia.Styling/StyledElement.cs +++ b/src/Avalonia.Styling/StyledElement.cs @@ -427,7 +427,7 @@ void ISetLogicalParent.SetParent(ILogical? parent) if (_logicalRoot != null) { - var e = new LogicalTreeAttachmentEventArgs(_logicalRoot, this, old); + var e = new LogicalTreeAttachmentEventArgs(_logicalRoot, this, old!); OnDetachedFromLogicalTreeCore(e); } @@ -435,7 +435,7 @@ void ISetLogicalParent.SetParent(ILogical? parent) if (newRoot is object) { - var e = new LogicalTreeAttachmentEventArgs(newRoot, this, parent); + var e = new LogicalTreeAttachmentEventArgs(newRoot, this, parent!); OnAttachedToLogicalTreeCore(e); } else if (parent is null) @@ -495,21 +495,21 @@ void IStyleHost.StylesRemoved(IReadOnlyList styles) DetachStylesFromThisAndDescendents(allStyles); } - protected virtual void LogicalChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + protected virtual void LogicalChildrenCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: - SetLogicalParent(e.NewItems); + SetLogicalParent(e.NewItems!); break; case NotifyCollectionChangedAction.Remove: - ClearLogicalParent(e.OldItems); + ClearLogicalParent(e.OldItems!); break; case NotifyCollectionChangedAction.Replace: - ClearLogicalParent(e.OldItems); - SetLogicalParent(e.NewItems); + ClearLogicalParent(e.OldItems!); + SetLogicalParent(e.NewItems!); break; case NotifyCollectionChangedAction.Reset: @@ -729,7 +729,7 @@ private void SetLogicalParent(IList children) for (var i = 0; i < count; i++) { - var logical = (ILogical) children[i]; + var logical = (ILogical) children[i]!; if (logical.LogicalParent is null) { @@ -744,7 +744,7 @@ private void ClearLogicalParent(IList children) for (var i = 0; i < count; i++) { - var logical = (ILogical) children[i]; + var logical = (ILogical) children[i]!; if (logical.LogicalParent == this) { diff --git a/src/Avalonia.Styling/Styling/Activators/NthChildActivator.cs b/src/Avalonia.Styling/Styling/Activators/NthChildActivator.cs index 803809a8ce5..6f54cd5904b 100644 --- a/src/Avalonia.Styling/Styling/Activators/NthChildActivator.cs +++ b/src/Avalonia.Styling/Styling/Activators/NthChildActivator.cs @@ -37,7 +37,7 @@ protected override void Deinitialize() _provider.ChildIndexChanged -= ChildIndexChanged; } - private void ChildIndexChanged(object sender, ChildIndexChangedEventArgs e) + private void ChildIndexChanged(object? sender, ChildIndexChangedEventArgs e) { // Run matching again if: // 1. Selector is reversed, so other item insertion/deletion might affect total count without changing subscribed item index. diff --git a/src/Avalonia.Styling/Styling/Activators/StyleClassActivator.cs b/src/Avalonia.Styling/Styling/Activators/StyleClassActivator.cs index 7906a29cb56..98d3f16a0aa 100644 --- a/src/Avalonia.Styling/Styling/Activators/StyleClassActivator.cs +++ b/src/Avalonia.Styling/Styling/Activators/StyleClassActivator.cs @@ -66,7 +66,7 @@ protected override void Deinitialize() _classes.CollectionChanged -= ClassesChangedHandler; } - private void ClassesChanged(object sender, NotifyCollectionChangedEventArgs e) + private void ClassesChanged(object? sender, NotifyCollectionChangedEventArgs e) { if (e.Action != NotifyCollectionChangedAction.Move) { diff --git a/src/Avalonia.Styling/Styling/ChildSelector.cs b/src/Avalonia.Styling/Styling/ChildSelector.cs index 85e7aaabde0..5c92182b807 100644 --- a/src/Avalonia.Styling/Styling/ChildSelector.cs +++ b/src/Avalonia.Styling/Styling/ChildSelector.cs @@ -6,7 +6,7 @@ namespace Avalonia.Styling internal class ChildSelector : Selector { private readonly Selector _parent; - private string _selectorString; + private string? _selectorString; public ChildSelector(Selector parent) { @@ -25,7 +25,7 @@ public ChildSelector(Selector parent) public override bool IsCombinator => true; /// - public override Type TargetType => null; + public override Type? TargetType => null; public override string ToString() { @@ -64,6 +64,6 @@ protected override SelectorMatch Evaluate(IStyleable control, bool subscribe) } } - protected override Selector MovePrevious() => null; + protected override Selector? MovePrevious() => null; } } diff --git a/src/Avalonia.Styling/Styling/OrSelector.cs b/src/Avalonia.Styling/Styling/OrSelector.cs index 8251915504d..3d6db9b01e1 100644 --- a/src/Avalonia.Styling/Styling/OrSelector.cs +++ b/src/Avalonia.Styling/Styling/OrSelector.cs @@ -120,7 +120,7 @@ protected override SelectorMatch Evaluate(IStyleable control, bool subscribe) } else { - while (!result.IsAssignableFrom(selector.TargetType)) + while (result is not null && !result.IsAssignableFrom(selector.TargetType)) { result = result.BaseType; } diff --git a/src/Avalonia.Styling/Styling/PropertyEqualsSelector.cs b/src/Avalonia.Styling/Styling/PropertyEqualsSelector.cs index ebdfff222ad..1cd1a650efe 100644 --- a/src/Avalonia.Styling/Styling/PropertyEqualsSelector.cs +++ b/src/Avalonia.Styling/Styling/PropertyEqualsSelector.cs @@ -109,7 +109,7 @@ internal static bool Compare(Type propertyType, object? propertyValue, object? v var converter = TypeDescriptor.GetConverter(propertyType); if (converter?.CanConvertFrom(valueType) == true) { - return Equals(propertyValue, converter.ConvertFrom(null, CultureInfo.InvariantCulture, value)); + return Equals(propertyValue, converter.ConvertFrom(null, CultureInfo.InvariantCulture, value!)); } return false; diff --git a/src/Avalonia.Styling/Styling/Selectors.cs b/src/Avalonia.Styling/Styling/Selectors.cs index 64d0a0e96b3..7c66469cf13 100644 --- a/src/Avalonia.Styling/Styling/Selectors.cs +++ b/src/Avalonia.Styling/Styling/Selectors.cs @@ -25,10 +25,14 @@ public static Selector Child(this Selector previous) /// The previous selector. /// The name of the style class. /// The selector. - public static Selector Class(this Selector previous, string name) + public static Selector Class(this Selector? previous, string name) { - Contract.Requires(name != null); - Contract.Requires(!string.IsNullOrWhiteSpace(name)); + _ = name ?? throw new ArgumentNullException(nameof(name)); + + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException("Name may not be empty", nameof(name)); + } var tac = previous as TypeNameAndClassSelector; @@ -48,7 +52,7 @@ public static Selector Class(this Selector previous, string name) /// /// The previous selector. /// The selector. - public static Selector Descendant(this Selector previous) + public static Selector Descendant(this Selector? previous) { return new DescendantSelector(previous); } @@ -59,9 +63,9 @@ public static Selector Descendant(this Selector previous) /// The previous selector. /// The type. /// The selector. - public static Selector Is(this Selector previous, Type type) + public static Selector Is(this Selector? previous, Type type) { - Contract.Requires(type != null); + _ = type ?? throw new ArgumentNullException(nameof(type)); return TypeNameAndClassSelector.Is(previous, type); } @@ -72,7 +76,7 @@ public static Selector Is(this Selector previous, Type type) /// The type. /// The previous selector. /// The selector. - public static Selector Is(this Selector previous) where T : IStyleable + public static Selector Is(this Selector? previous) where T : IStyleable { return previous.Is(typeof(T)); } @@ -83,10 +87,14 @@ public static Selector Is(this Selector previous) where T : IStyleable /// The previous selector. /// The name. /// The selector. - public static Selector Name(this Selector previous, string name) + public static Selector Name(this Selector? previous, string name) { - Contract.Requires(name != null); - Contract.Requires(!string.IsNullOrWhiteSpace(name)); + _ = name ?? throw new ArgumentNullException(nameof(name)); + + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException("Name may not be empty", nameof(name)); + } var tac = previous as TypeNameAndClassSelector; @@ -107,7 +115,7 @@ public static Selector Name(this Selector previous, string name) /// The previous selector. /// The selector to be not-ed. /// The selector. - public static Selector Not(this Selector previous, Func argument) + public static Selector Not(this Selector? previous, Func argument) { return new NotSelector(previous, argument(null)); } @@ -118,7 +126,7 @@ public static Selector Not(this Selector previous, Func argu /// The previous selector. /// The selector to be not-ed. /// The selector. - public static Selector Not(this Selector previous, Selector argument) + public static Selector Not(this Selector? previous, Selector argument) { return new NotSelector(previous, argument); } @@ -126,7 +134,7 @@ public static Selector Not(this Selector previous, Selector argument) /// /// /// The selector. - public static Selector NthChild(this Selector previous, int step, int offset) + public static Selector NthChild(this Selector? previous, int step, int offset) { return new NthChildSelector(previous, step, offset); } @@ -134,7 +142,7 @@ public static Selector NthChild(this Selector previous, int step, int offset) /// /// /// The selector. - public static Selector NthLastChild(this Selector previous, int step, int offset) + public static Selector NthLastChild(this Selector? previous, int step, int offset) { return new NthLastChildSelector(previous, step, offset); } @@ -145,9 +153,9 @@ public static Selector NthLastChild(this Selector previous, int step, int offset /// The previous selector. /// The type. /// The selector. - public static Selector OfType(this Selector previous, Type type) + public static Selector OfType(this Selector? previous, Type type) { - Contract.Requires(type != null); + _ = type ?? throw new ArgumentNullException(nameof(type)); return TypeNameAndClassSelector.OfType(previous, type); } @@ -158,7 +166,7 @@ public static Selector OfType(this Selector previous, Type type) /// The type. /// The previous selector. /// The selector. - public static Selector OfType(this Selector previous) where T : IStyleable + public static Selector OfType(this Selector? previous) where T : IStyleable { return previous.OfType(typeof(T)); } @@ -191,9 +199,9 @@ public static Selector Or(IReadOnlyList selectors) /// The property. /// The property value. /// The selector. - public static Selector PropertyEquals(this Selector previous, AvaloniaProperty property, object value) + public static Selector PropertyEquals(this Selector? previous, AvaloniaProperty property, object? value) { - Contract.Requires(property != null); + _ = property ?? throw new ArgumentNullException(nameof(property)); return new PropertyEqualsSelector(previous, property, value); } @@ -205,9 +213,9 @@ public static Selector PropertyEquals(this Selector previous, AvaloniaPropert /// The property. /// The property value. /// The selector. - public static Selector PropertyEquals(this Selector previous, AvaloniaProperty property, object value) + public static Selector PropertyEquals(this Selector? previous, AvaloniaProperty property, object? value) { - Contract.Requires(property != null); + _ = property ?? throw new ArgumentNullException(nameof(property)); return new PropertyEqualsSelector(previous, property, value); } diff --git a/src/Avalonia.Styling/Styling/Styles.cs b/src/Avalonia.Styling/Styling/Styles.cs index c752bdfeb89..81502f1570e 100644 --- a/src/Avalonia.Styling/Styling/Styles.cs +++ b/src/Avalonia.Styling/Styling/Styles.cs @@ -262,7 +262,7 @@ void IResourceProvider.RemoveOwner(IResourceHost owner) } } - private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { static IReadOnlyList ToReadOnlyList(IList list) { @@ -282,7 +282,7 @@ void Add(IList items) { for (var i = 0; i < items.Count; ++i) { - var style = (IStyle)items[i]; + var style = (IStyle)items[i]!; if (Owner is object && style is IResourceProvider resourceProvider) { @@ -299,7 +299,7 @@ void Remove(IList items) { for (var i = 0; i < items.Count; ++i) { - var style = (IStyle)items[i]; + var style = (IStyle)items[i]!; if (Owner is object && style is IResourceProvider resourceProvider) { @@ -315,14 +315,14 @@ void Remove(IList items) switch (e.Action) { case NotifyCollectionChangedAction.Add: - Add(e.NewItems); + Add(e.NewItems!); break; case NotifyCollectionChangedAction.Remove: - Remove(e.OldItems); + Remove(e.OldItems!); break; case NotifyCollectionChangedAction.Replace: - Remove(e.OldItems); - Add(e.NewItems); + Remove(e.OldItems!); + Add(e.NewItems!); break; case NotifyCollectionChangedAction.Reset: throw new InvalidOperationException("Reset should not be called on Styles."); diff --git a/src/Avalonia.Styling/Styling/TemplateSelector.cs b/src/Avalonia.Styling/Styling/TemplateSelector.cs index 5ea8defedac..e8051efa6de 100644 --- a/src/Avalonia.Styling/Styling/TemplateSelector.cs +++ b/src/Avalonia.Styling/Styling/TemplateSelector.cs @@ -5,7 +5,7 @@ namespace Avalonia.Styling internal class TemplateSelector : Selector { private readonly Selector _parent; - private string _selectorString; + private string? _selectorString; public TemplateSelector(Selector parent) { @@ -24,7 +24,7 @@ public TemplateSelector(Selector parent) public override bool IsCombinator => true; /// - public override Type TargetType => null; + public override Type? TargetType => null; public override string ToString() { @@ -48,6 +48,6 @@ protected override SelectorMatch Evaluate(IStyleable control, bool subscribe) return _parent.Match(templatedParent, subscribe); } - protected override Selector MovePrevious() => null; + protected override Selector? MovePrevious() => null; } } diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index 78c6d9c0576..324b253a0fc 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -377,7 +377,7 @@ static void InvalidateAndSubscribe(AvaloniaPropertyChangedEventArgs e) } } - protected override void LogicalChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + protected override void LogicalChildrenCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { base.LogicalChildrenCollectionChanged(sender, e); VisualRoot?.Renderer?.RecalculateChildren(this); diff --git a/src/Markup/Avalonia.Markup/Markup/Parsers/SelectorParser.cs b/src/Markup/Avalonia.Markup/Markup/Parsers/SelectorParser.cs index 1b7ee9025e6..5b6522064a4 100644 --- a/src/Markup/Avalonia.Markup/Markup/Parsers/SelectorParser.cs +++ b/src/Markup/Avalonia.Markup/Markup/Parsers/SelectorParser.cs @@ -138,16 +138,16 @@ public SelectorParser(Func typeResolver) break; case SelectorGrammar.ChildSyntax child: - result = result.Child(); + result = result!.Child(); break; case SelectorGrammar.DescendantSyntax descendant: result = result.Descendant(); break; case SelectorGrammar.TemplateSyntax template: - result = result.Template(); + result = result!.Template(); break; case SelectorGrammar.NotSyntax not: - result = result.Not(x => Create(not.Argument)); + result = result.Not(x => Create(not.Argument)!); break; case SelectorGrammar.NthChildSyntax nth: result = result.NthChild(nth.Step, nth.Offset);