From 82729a1bd92d35a2f7c4688c1a508024abbd9027 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Mon, 18 Oct 2021 14:57:58 +0200 Subject: [PATCH 01/53] fixes: some null annotation --- src/Avalonia.Base/Data/BindingValue.cs | 2 ++ .../Data/Converters/MethodToCommandConverter.cs | 2 +- src/Avalonia.Controls/Application.cs | 2 +- src/Avalonia.Controls/Primitives/Popup.cs | 8 ++++---- src/Avalonia.Controls/SplitView.cs | 10 +++++----- src/Avalonia.Input/ICommandSource.cs | 6 +++--- src/Avalonia.Styling/Styling/Setter.cs | 8 ++++++++ src/tools/MicroComGenerator/CSharpGen.Utils.cs | 2 +- 8 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index 93948e54ee..414baf88fc 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,9 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. _ => new BindingValue((T)value) +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. }; } diff --git a/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs b/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs index 7ff0a8ceca..add2280567 100644 --- a/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs +++ b/src/Avalonia.Base/Data/Converters/MethodToCommandConverter.cs @@ -170,7 +170,7 @@ namespace Avalonia.Data.Converters .Compile(); } - private static Expression? ConvertTarget(object? target, MethodInfo method) => + private static Expression ConvertTarget(object target, MethodInfo method) => target is null ? null : Expression.Convert(Expression.Constant(target), method.DeclaringType); internal class WeakPropertyChangedProxy diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 157bebe02b..bd53ec0100 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -175,7 +175,7 @@ namespace Avalonia /// - /// - /// - public IApplicationLifetime ApplicationLifetime { get; set; } + public IApplicationLifetime? ApplicationLifetime { get; set; } event Action> IGlobalStyles.GlobalStylesAdded { diff --git a/src/Avalonia.Controls/Primitives/Popup.cs b/src/Avalonia.Controls/Primitives/Popup.cs index 856bcd1079..50f130ab71 100644 --- a/src/Avalonia.Controls/Primitives/Popup.cs +++ b/src/Avalonia.Controls/Primitives/Popup.cs @@ -91,8 +91,8 @@ namespace Avalonia.Controls.Primitives public static readonly StyledProperty OverlayDismissEventPassThroughProperty = AvaloniaProperty.Register(nameof(OverlayDismissEventPassThrough)); - public static readonly DirectProperty OverlayInputPassThroughElementProperty = - AvaloniaProperty.RegisterDirect( + public static readonly DirectProperty OverlayInputPassThroughElementProperty = + AvaloniaProperty.RegisterDirect( nameof(OverlayInputPassThroughElement), o => o.OverlayInputPassThroughElement, (o, v) => o.OverlayInputPassThroughElement = v); @@ -136,7 +136,7 @@ namespace Avalonia.Controls.Primitives private bool _isOpen; private bool _ignoreIsOpenChanged; private PopupOpenState? _openState; - private IInputElement _overlayInputPassThroughElement; + private IInputElement? _overlayInputPassThroughElement; private Action? _popupHostChangedHandler; /// @@ -308,7 +308,7 @@ namespace Avalonia.Controls.Primitives /// Gets or sets an element that should receive pointer input events even when underneath /// the popup's overlay. /// - public IInputElement OverlayInputPassThroughElement + public IInputElement? OverlayInputPassThroughElement { get => _overlayInputPassThroughElement; set => SetAndRaise(OverlayInputPassThroughElementProperty, ref _overlayInputPassThroughElement, value); diff --git a/src/Avalonia.Controls/SplitView.cs b/src/Avalonia.Controls/SplitView.cs index 0e35c610b2..6a0d4e2023 100644 --- a/src/Avalonia.Controls/SplitView.cs +++ b/src/Avalonia.Controls/SplitView.cs @@ -129,14 +129,14 @@ namespace Avalonia.Controls /// /// Defines the property /// - public static readonly StyledProperty PaneProperty = - AvaloniaProperty.Register(nameof(Pane)); + public static readonly StyledProperty PaneProperty = + AvaloniaProperty.Register(nameof(Pane)); /// /// Defines the property. /// - public static readonly StyledProperty PaneTemplateProperty = - AvaloniaProperty.Register(nameof(PaneTemplate)); + public static readonly StyledProperty PaneTemplateProperty = + AvaloniaProperty.Register(nameof(PaneTemplate)); /// /// Defines the property @@ -267,7 +267,7 @@ namespace Avalonia.Controls /// /// Gets or sets the data template used to display the header content of the control. /// - public IDataTemplate? PaneTemplate + public IDataTemplate PaneTemplate { get => GetValue(PaneTemplateProperty); set => SetValue(PaneTemplateProperty, value); diff --git a/src/Avalonia.Input/ICommandSource.cs b/src/Avalonia.Input/ICommandSource.cs index eed71759d5..410b3a2e47 100644 --- a/src/Avalonia.Input/ICommandSource.cs +++ b/src/Avalonia.Input/ICommandSource.cs @@ -1,5 +1,5 @@ using System.Windows.Input; - +#nullable enable namespace Avalonia.Input { /// @@ -12,13 +12,13 @@ namespace Avalonia.Input /// Classes that implement this interface should enable or disable based on the command's CanExecute return value. /// The property may be implemented as read-write if desired. /// - ICommand Command { get; } + ICommand? Command { get; } /// /// The parameter that will be passed to the command when executing the command. /// The property may be implemented as read-write if desired. /// - object CommandParameter { get; } + object? CommandParameter { get; } /// diff --git a/src/Avalonia.Styling/Styling/Setter.cs b/src/Avalonia.Styling/Styling/Setter.cs index 1f3d6335a9..9e8969c164 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -101,7 +101,11 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. +#pragma warning disable CS8604 // Possible null reference argument. (T)data.value); +#pragma warning restore CS8604 // Possible null reference argument. +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. } } @@ -128,7 +132,11 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, +#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. +#pragma warning disable CS8604 // Possible null reference argument. (T)data.value); +#pragma warning restore CS8604 // Possible null reference argument. +#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. } } diff --git a/src/tools/MicroComGenerator/CSharpGen.Utils.cs b/src/tools/MicroComGenerator/CSharpGen.Utils.cs index da845b0ecd..28baaa65f8 100644 --- a/src/tools/MicroComGenerator/CSharpGen.Utils.cs +++ b/src/tools/MicroComGenerator/CSharpGen.Utils.cs @@ -40,7 +40,7 @@ namespace MicroComGenerator SyntaxToken Semicolon() => Token(SyntaxKind.SemicolonToken); static VariableDeclarationSyntax DeclareVar(string type, string name, - ExpressionSyntax? initializer = null) + ExpressionSyntax initializer = null) => VariableDeclaration(ParseTypeName(type), SingletonSeparatedList(VariableDeclarator(name) .WithInitializer(initializer == null ? null : EqualsValueClause(initializer)))); From 0309dda390b0583b07d1298c6c256ebd06b3f473 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 19 Oct 2021 10:02:54 +0200 Subject: [PATCH 02/53] fixes: Replace CS8600 warning suppression with null-forgiving operator --- src/Avalonia.Base/Data/BindingValue.cs | 4 +--- src/Avalonia.Styling/Styling/Setter.cs | 12 ++---------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index 414baf88fc..0e7bdfe0bf 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,9 +247,7 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), -#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. - _ => new BindingValue((T)value) -#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + _ => new BindingValue((T)value!) }; } diff --git a/src/Avalonia.Styling/Styling/Setter.cs b/src/Avalonia.Styling/Styling/Setter.cs index 9e8969c164..168a882499 100644 --- a/src/Avalonia.Styling/Styling/Setter.cs +++ b/src/Avalonia.Styling/Styling/Setter.cs @@ -101,11 +101,7 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, -#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. -#pragma warning disable CS8604 // Possible null reference argument. - (T)data.value); -#pragma warning restore CS8604 // Possible null reference argument. -#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + (T)data.value!); } } @@ -132,11 +128,7 @@ namespace Avalonia.Styling data.result = new PropertySetterInstance( data.target, property, -#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. -#pragma warning disable CS8604 // Possible null reference argument. - (T)data.value); -#pragma warning restore CS8604 // Possible null reference argument. -#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + (T)data.value!); } } From a828e5b4f5664a4aaf50280a7cf423d59e248da7 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 19 Oct 2021 10:12:36 +0200 Subject: [PATCH 03/53] fixes: MenuFlyout Possible null reference argument for parameter --- src/Avalonia.Controls/Flyouts/FlyoutBase.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Flyouts/FlyoutBase.cs b/src/Avalonia.Controls/Flyouts/FlyoutBase.cs index 4b903d056c..b874d149bf 100644 --- a/src/Avalonia.Controls/Flyouts/FlyoutBase.cs +++ b/src/Avalonia.Controls/Flyouts/FlyoutBase.cs @@ -562,8 +562,12 @@ namespace Avalonia.Controls.Primitives return eventArgs.Cancel; } - internal static void SetPresenterClasses(IControl presenter, Classes classes) + internal static void SetPresenterClasses(IControl? presenter, Classes classes) { + if(presenter is null) + { + return; + } //Remove any classes no longer in use, ignoring pseudo classes for (int i = presenter.Classes.Count - 1; i >= 0; i--) { From 895fd718d9f88a68b258cff12a545f642549f63c Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 19 Oct 2021 10:42:03 +0200 Subject: [PATCH 04/53] fixes: ConstantValueEntry Possible null reference argument for parameter --- src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs index dc4a1d88c1..ebcd064626 100644 --- a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs +++ b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs @@ -30,7 +30,7 @@ namespace Avalonia.PropertyStore IValueSink sink) { Property = property; - _value = value; + _value = value!; Priority = priority; _sink = sink; } From 22644028ca5c52843f87f45f42a2a44ed2eab7fe Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 19 Oct 2021 10:44:22 +0200 Subject: [PATCH 05/53] fixes: ValueStore Possible null reference argument for parameter --- src/Avalonia.Base/ValueStore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/ValueStore.cs b/src/Avalonia.Base/ValueStore.cs index 495f13e1a9..151e5d1949 100644 --- a/src/Avalonia.Base/ValueStore.cs +++ b/src/Avalonia.Base/ValueStore.cs @@ -192,7 +192,7 @@ namespace Avalonia _values.SetValue(property, sentinel); } - NotifyValueChanged(property, old, default, BindingPriority.Unset); + NotifyValueChanged(property, old!, default!, BindingPriority.Unset); } } } From 97a920db72a16a89aae24e2cb4d20b53a77d2883 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Sat, 23 Oct 2021 11:45:07 +0200 Subject: [PATCH 06/53] Review suggestions apply --- src/Avalonia.Base/Data/BindingValue.cs | 2 +- src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs | 2 +- src/Avalonia.Base/ValueStore.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index 0e7bdfe0bf..e8c84ffb21 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), - _ => new BindingValue((T)value!) + _ => new BindingValue((T?)value) }; } diff --git a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs index ebcd064626..dc4a1d88c1 100644 --- a/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs +++ b/src/Avalonia.Base/PropertyStore/ConstantValueEntry.cs @@ -30,7 +30,7 @@ namespace Avalonia.PropertyStore IValueSink sink) { Property = property; - _value = value!; + _value = value; Priority = priority; _sink = sink; } diff --git a/src/Avalonia.Base/ValueStore.cs b/src/Avalonia.Base/ValueStore.cs index 151e5d1949..495f13e1a9 100644 --- a/src/Avalonia.Base/ValueStore.cs +++ b/src/Avalonia.Base/ValueStore.cs @@ -192,7 +192,7 @@ namespace Avalonia _values.SetValue(property, sentinel); } - NotifyValueChanged(property, old!, default!, BindingPriority.Unset); + NotifyValueChanged(property, old, default, BindingPriority.Unset); } } } From 44225c66146cdfb63623052a19cc87f3639b29df Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Sat, 23 Oct 2021 12:31:46 +0200 Subject: [PATCH 07/53] fixes: Revert the fix, because the fix throw error CS8627 on Linux and macOS --- src/Avalonia.Base/Data/BindingValue.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index e8c84ffb21..93948e54ee 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), - _ => new BindingValue((T?)value) + _ => new BindingValue((T)value) }; } From 67e5962680c831fa6046ded59c66125cef3675e7 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Thu, 28 Oct 2021 10:13:44 +0200 Subject: [PATCH 08/53] fix(TrayIcon): CommandParameter alow null --- src/Avalonia.Controls/TrayIcon.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TrayIcon.cs b/src/Avalonia.Controls/TrayIcon.cs index 59edb6278a..9fbc2fc8b2 100644 --- a/src/Avalonia.Controls/TrayIcon.cs +++ b/src/Avalonia.Controls/TrayIcon.cs @@ -140,7 +140,7 @@ namespace Avalonia.Controls /// Gets or sets the parameter to pass to the property of a /// . /// - public object CommandParameter + public object? CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } From 94eeb7d97c33779f5f79985ab81675940993153e Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Thu, 28 Oct 2021 10:14:07 +0200 Subject: [PATCH 09/53] restore fix --- src/Avalonia.Base/Data/BindingValue.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Data/BindingValue.cs b/src/Avalonia.Base/Data/BindingValue.cs index 93948e54ee..e8c84ffb21 100644 --- a/src/Avalonia.Base/Data/BindingValue.cs +++ b/src/Avalonia.Base/Data/BindingValue.cs @@ -247,7 +247,7 @@ namespace Avalonia.Data UnsetValueType _ => Unset, DoNothingType _ => DoNothing, BindingNotification n => n.ToBindingValue().Cast(), - _ => new BindingValue((T)value) + _ => new BindingValue((T?)value) }; } From 59f4adcdd39ed878d9b8cf38936afd0d0cf2eb04 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Sat, 23 Oct 2021 12:14:38 +0200 Subject: [PATCH 10/53] fix: Warning CS0067 The event is never used --- .../Platform/InternalPlatformThreadingInterface.cs | 2 ++ src/Avalonia.Controls/TextBoxTextInputMethodClient.cs | 2 +- src/Avalonia.DesignerSupport/Remote/FileWatcherTransport.cs | 2 +- src/Avalonia.FreeDesktop/DBusMenuExporter.cs | 6 +++--- src/Avalonia.Native/AvaloniaNativeMenuExporter.cs | 2 +- src/Avalonia.X11/X11Window.Xim.cs | 4 ++-- tests/Avalonia.Controls.UnitTests/ItemsSourceViewTests.cs | 2 +- 7 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs index a5495fdfc9..6a95c2e047 100644 --- a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs +++ b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs @@ -85,7 +85,9 @@ namespace Avalonia.Controls.Platform public bool CurrentThreadIsLoopThread => TlsCurrentThreadIsLoopThread; public event Action Signaled; +#pragma warning disable CS0067 public event Action Tick; +#pragma warning restore CS0067 } } diff --git a/src/Avalonia.Controls/TextBoxTextInputMethodClient.cs b/src/Avalonia.Controls/TextBoxTextInputMethodClient.cs index e8122dd311..c5a729afae 100644 --- a/src/Avalonia.Controls/TextBoxTextInputMethodClient.cs +++ b/src/Avalonia.Controls/TextBoxTextInputMethodClient.cs @@ -18,7 +18,7 @@ namespace Avalonia.Controls public bool SupportsSurroundingText => false; public TextInputMethodSurroundingText SurroundingText => throw new NotSupportedException(); - public event EventHandler SurroundingTextChanged; + public event EventHandler SurroundingTextChanged { add { } remove { } } public string TextBeforeCursor => null; public string TextAfterCursor => null; diff --git a/src/Avalonia.DesignerSupport/Remote/FileWatcherTransport.cs b/src/Avalonia.DesignerSupport/Remote/FileWatcherTransport.cs index 0448a5c05d..45a6c97954 100644 --- a/src/Avalonia.DesignerSupport/Remote/FileWatcherTransport.cs +++ b/src/Avalonia.DesignerSupport/Remote/FileWatcherTransport.cs @@ -59,7 +59,7 @@ namespace Avalonia.DesignerSupport.Remote remove { _onMessage -= value; } } - public event Action OnException; + public event Action OnException { add { } remove { } } public void Start() { UpdaterThread(); diff --git a/src/Avalonia.FreeDesktop/DBusMenuExporter.cs b/src/Avalonia.FreeDesktop/DBusMenuExporter.cs index 9e426688d8..206c24ad5e 100644 --- a/src/Avalonia.FreeDesktop/DBusMenuExporter.cs +++ b/src/Avalonia.FreeDesktop/DBusMenuExporter.cs @@ -413,10 +413,10 @@ namespace Avalonia.FreeDesktop #region Events private event Action<((int, IDictionary)[] updatedProps, (int, string[])[] removedProps)> - ItemsPropertiesUpdated; + ItemsPropertiesUpdated { add { } remove { } } private event Action<(uint revision, int parent)> LayoutUpdated; - private event Action<(int id, uint timestamp)> ItemActivationRequested; - private event Action PropertiesChanged; + private event Action<(int id, uint timestamp)> ItemActivationRequested { add { } remove { } } + private event Action PropertiesChanged { add { } remove { } } async Task IDBusMenu.WatchItemsPropertiesUpdatedAsync(Action<((int, IDictionary)[] updatedProps, (int, string[])[] removedProps)> handler, Action onError) { diff --git a/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs b/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs index 4431e108ed..1582f794ae 100644 --- a/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs +++ b/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs @@ -44,7 +44,7 @@ namespace Avalonia.Native public bool IsNativeMenuExported => _exported; - public event EventHandler OnIsNativeMenuExportedChanged; + public event EventHandler OnIsNativeMenuExportedChanged { add { } remove { } } public void SetNativeMenu(NativeMenu menu) { diff --git a/src/Avalonia.X11/X11Window.Xim.cs b/src/Avalonia.X11/X11Window.Xim.cs index 444c82fd22..ecb23ff097 100644 --- a/src/Avalonia.X11/X11Window.Xim.cs +++ b/src/Avalonia.X11/X11Window.Xim.cs @@ -112,8 +112,8 @@ namespace Avalonia.X11 public ValueTask HandleEventAsync(RawKeyEventArgs args, int keyVal, int keyCode) => new ValueTask(false); - public event Action Commit; - public event Action ForwardKey; + public event Action Commit { add { } remove { } } + public event Action ForwardKey { add { } remove { } } } diff --git a/tests/Avalonia.Controls.UnitTests/ItemsSourceViewTests.cs b/tests/Avalonia.Controls.UnitTests/ItemsSourceViewTests.cs index 529b3b1aa8..22a9b28648 100644 --- a/tests/Avalonia.Controls.UnitTests/ItemsSourceViewTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ItemsSourceViewTests.cs @@ -47,7 +47,7 @@ namespace Avalonia.Controls.UnitTests private class InvalidCollection : INotifyCollectionChanged, IEnumerable { - public event NotifyCollectionChangedEventHandler CollectionChanged; + public event NotifyCollectionChangedEventHandler CollectionChanged { add { } remove { } } public IEnumerator GetEnumerator() { From 1a8cb9bba916fe3df8eb50b320b0a19b0a0fea90 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 28 Oct 2021 13:22:41 +0100 Subject: [PATCH 11/53] allow window to clear focus manager when it closes, preventing memory leak. --- src/Avalonia.Controls/WindowBase.cs | 6 ++++++ src/Avalonia.Input/ApiCompatBaseline.txt | 3 ++- src/Avalonia.Input/FocusManager.cs | 18 +++++++++++++++++- src/Avalonia.Input/IFocusManager.cs | 8 ++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/WindowBase.cs b/src/Avalonia.Controls/WindowBase.cs index ee008efb04..5861d0452d 100644 --- a/src/Avalonia.Controls/WindowBase.cs +++ b/src/Avalonia.Controls/WindowBase.cs @@ -193,6 +193,12 @@ namespace Avalonia.Controls try { IsVisible = false; + + if (this is IFocusScope scope) + { + FocusManager.Instance?.RemoveFocusScope(scope); + } + base.HandleClosed(); } finally diff --git a/src/Avalonia.Input/ApiCompatBaseline.txt b/src/Avalonia.Input/ApiCompatBaseline.txt index 98eb8598d8..221ef63476 100644 --- a/src/Avalonia.Input/ApiCompatBaseline.txt +++ b/src/Avalonia.Input/ApiCompatBaseline.txt @@ -3,6 +3,7 @@ MembersMustExist : Member 'public Avalonia.Platform.IPlatformHandle Avalonia.Inp MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.Gestures.DoubleTappedEvent' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.Gestures.RightTappedEvent' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.Gestures.TappedEvent' does not exist in the implementation but it does exist in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Input.IFocusManager.ClearFocusScope(Avalonia.Input.IFocusScope)' is present in the implementation but not in the contract. MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.InputElement.DoubleTappedEvent' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.InputElement.TappedEvent' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public void Avalonia.Input.InputElement.add_DoubleTapped(System.EventHandler)' does not exist in the implementation but it does exist in the contract. @@ -10,4 +11,4 @@ MembersMustExist : Member 'public void Avalonia.Input.InputElement.add_Tapped(Sy MembersMustExist : Member 'public void Avalonia.Input.InputElement.remove_DoubleTapped(System.EventHandler)' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public void Avalonia.Input.InputElement.remove_Tapped(System.EventHandler)' does not exist in the implementation but it does exist in the contract. TypesMustExist : Type 'Avalonia.Platform.IStandardCursorFactory' does not exist in the implementation but it does exist in the contract. -Total Issues: 11 +Total Issues: 12 diff --git a/src/Avalonia.Input/FocusManager.cs b/src/Avalonia.Input/FocusManager.cs index 1432092ba1..3e1ec84c03 100644 --- a/src/Avalonia.Input/FocusManager.cs +++ b/src/Avalonia.Input/FocusManager.cs @@ -145,7 +145,7 @@ namespace Avalonia.Input /// Notifies the focus manager of a change in focus scope. /// /// The new focus scope. - public void SetFocusScope(IFocusScope scope) + public void SetFocusScope(IFocusScope? scope) { scope = scope ?? throw new ArgumentNullException(nameof(scope)); @@ -162,6 +162,22 @@ namespace Avalonia.Input Focus(e); } + public void RemoveFocusScope(IFocusScope scope) + { + scope = scope ?? throw new ArgumentNullException(nameof(scope)); + + if (_focusScopes.TryGetValue(scope, out var e)) + { + SetFocusedElement(scope, null); + _focusScopes.Remove(scope); + } + + if (Scope == scope) + { + Scope = null; + } + } + public static bool GetIsFocusScope(IInputElement e) => e is IFocusScope; /// diff --git a/src/Avalonia.Input/IFocusManager.cs b/src/Avalonia.Input/IFocusManager.cs index e1b5087c3d..2510479a8e 100644 --- a/src/Avalonia.Input/IFocusManager.cs +++ b/src/Avalonia.Input/IFocusManager.cs @@ -35,5 +35,13 @@ namespace Avalonia.Input /// when it activates, e.g. when a Window is activated. /// void SetFocusScope(IFocusScope scope); + + /// + /// Notifies the focus manager that a focus scope has been removed. + /// + /// The focus scope to be removed. + /// This should not be called by client code. It is called by an + /// when it deactivates or closes, e.g. when a Window is closed. + void RemoveFocusScope(IFocusScope scope); } } From eb9f3343bf57346ecd5a599e95eb7ac7166f0aa4 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 28 Oct 2021 13:35:10 +0100 Subject: [PATCH 12/53] rename method in apicompat --- src/Avalonia.Input/ApiCompatBaseline.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Input/ApiCompatBaseline.txt b/src/Avalonia.Input/ApiCompatBaseline.txt index 221ef63476..270c5305e5 100644 --- a/src/Avalonia.Input/ApiCompatBaseline.txt +++ b/src/Avalonia.Input/ApiCompatBaseline.txt @@ -3,7 +3,7 @@ MembersMustExist : Member 'public Avalonia.Platform.IPlatformHandle Avalonia.Inp MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.Gestures.DoubleTappedEvent' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.Gestures.RightTappedEvent' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.Gestures.TappedEvent' does not exist in the implementation but it does exist in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Input.IFocusManager.ClearFocusScope(Avalonia.Input.IFocusScope)' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Input.IFocusManager.RemoveFocusScope(Avalonia.Input.IFocusScope)' is present in the implementation but not in the contract. MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.InputElement.DoubleTappedEvent' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public Avalonia.Interactivity.RoutedEvent Avalonia.Interactivity.RoutedEvent Avalonia.Input.InputElement.TappedEvent' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public void Avalonia.Input.InputElement.add_DoubleTapped(System.EventHandler)' does not exist in the implementation but it does exist in the contract. From 711ab49f0c3786e26d7dea4fedf714fbe4912e57 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 28 Oct 2021 13:38:08 +0100 Subject: [PATCH 13/53] discard var --- src/Avalonia.Input/FocusManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Input/FocusManager.cs b/src/Avalonia.Input/FocusManager.cs index 3e1ec84c03..f21edd7591 100644 --- a/src/Avalonia.Input/FocusManager.cs +++ b/src/Avalonia.Input/FocusManager.cs @@ -166,7 +166,7 @@ namespace Avalonia.Input { scope = scope ?? throw new ArgumentNullException(nameof(scope)); - if (_focusScopes.TryGetValue(scope, out var e)) + if (_focusScopes.TryGetValue(scope, out _)) { SetFocusedElement(scope, null); _focusScopes.Remove(scope); From 6f8f539a9fe9df86d753a4eba4158f1203ce8120 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 28 Oct 2021 14:20:27 +0100 Subject: [PATCH 14/53] prevent memory leak of mainwindow. --- .../ClassicDesktopStyleApplicationLifetime.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs b/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs index 2a42d99ac5..288939310c 100644 --- a/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs +++ b/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs @@ -122,12 +122,17 @@ namespace Avalonia.Controls.ApplicationLifetimes lifetimeEvents.ShutdownRequested += OnShutdownRequested; _cts = new CancellationTokenSource(); - MainWindow?.Show(); + ShowMainWindow(); Dispatcher.UIThread.MainLoop(_cts.Token); Environment.ExitCode = _exitCode; return _exitCode; } + private void ShowMainWindow() + { + MainWindow?.Show(); + } + public void Dispose() { if (_activeLifetime == this) From eb65760c51ece696539252ddd13bae137a0310f0 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 28 Oct 2021 15:07:56 +0100 Subject: [PATCH 15/53] add a comment explain JIT bug workaround. --- .../ClassicDesktopStyleApplicationLifetime.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs b/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs index 288939310c..a13813056b 100644 --- a/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs +++ b/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs @@ -122,7 +122,10 @@ namespace Avalonia.Controls.ApplicationLifetimes lifetimeEvents.ShutdownRequested += OnShutdownRequested; _cts = new CancellationTokenSource(); - ShowMainWindow(); + ShowMainWindow(); // Note due to a bug in the JIT we wrap this in a method, otherwise MainWindow + // gets stuffed into a local var and can not be GCed until after the program stops. + // this method never exits until program end. + Dispatcher.UIThread.MainLoop(_cts.Token); Environment.ExitCode = _exitCode; return _exitCode; From 3fadc669b58a2ed5f5a6f3b8377cd6ff55a963f1 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 28 Oct 2021 22:25:31 +0100 Subject: [PATCH 16/53] ensure static resource can implicitly convert to brush when used in setters. --- .../MarkupExtensions/StaticResourceExtension.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs index 4ad68fc63e..db33b88cc3 100644 --- a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs @@ -5,6 +5,7 @@ using Avalonia.Controls; using Avalonia.Markup.Data; using Avalonia.Markup.Xaml.Converters; using Avalonia.Markup.Xaml.XamlIl.Runtime; +using Avalonia.Styling; namespace Avalonia.Markup.Xaml.MarkupExtensions { @@ -33,6 +34,11 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions _ => null, }; + if (provideTarget.TargetObject is Setter setter) + { + targetType = setter.Property.PropertyType; + } + // Look upwards though the ambient context for IResourceHosts and IResourceProviders // which might be able to give us the resource. foreach (var e in stack.Parents) From 51ab3266dd27000496e372a8c32bffc61ba5cd51 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 28 Oct 2021 22:47:31 +0100 Subject: [PATCH 17/53] add a unit test. --- .../StaticResourceExtensionTests.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/StaticResourceExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/StaticResourceExtensionTests.cs index fb3fd6d7d4..340eac0d4f 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/StaticResourceExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/StaticResourceExtensionTests.cs @@ -512,6 +512,33 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions var brush = (ISolidColorBrush)border.Background; Assert.Equal(0xff506070, brush.Color.ToUint32()); } + + [Fact] + public void Automatically_Converts_Color_To_SolidColorBrush_From_Setter() + { + using (StyledWindow()) + { + var xaml = @" + + + #ff506070 + + + + +