From f8d33dc3e6a06757a2375c536a95429996c7f231 Mon Sep 17 00:00:00 2001 From: Bartosz Korczynski Date: Wed, 13 Nov 2024 23:39:49 +0000 Subject: [PATCH] fix: warnings and IsAotCompatible --- .github/workflows/build.yml | 2 +- .github/workflows/publish-package.yml | 2 +- Directory.Build.props | 2 +- .../Forms/Views/CodeEditorView.axaml.cs | 1 + .../Classic.Avalonia.Theme.ColorPicker.csproj | 6 ++++- .../Classic.Avalonia.Theme.DataGrid.csproj | 6 ++++- .../Classic.Avalonia.Theme.Dock.csproj | 6 ++++- src/Classic.Avalonia.Theme/BulletDecorator.cs | 23 +++++++++------- .../Classic.Avalonia.Theme.csproj | 6 ++++- .../ClassicBorderDecorator.cs | 15 ++++++----- src/Classic.Avalonia.Theme/ClassicWindow.cs | 2 +- .../Converters/BorderGapMaskConverter.cs | 12 ++++----- .../Converters/ProgressBarBrushConverter.cs | 20 +++++++------- .../AppBuilderExtensions.cs | 4 +-- .../Classic.CommonControls.Avalonia.csproj | 6 ++++- .../Dialogs/About/Metrics.cs | 26 +++++++++---------- .../Dialogs/Font/FontDialogViewModel.cs | 18 ++++++++----- .../Dialogs/InputBoxes/InputBox.cs | 4 +-- .../Utils/IconRenderer.cs | 2 +- 19 files changed, 98 insertions(+), 65 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4f0dcdd..efbd326 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v3.0.1 with: - dotnet-version: "8.0.x" + dotnet-version: "9.0.x" - name: Build run: | dotnet workload restore diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml index 9d8b662..71bde0b 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -15,7 +15,7 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@v3.0.1 with: - dotnet-version: '8.0.x' + dotnet-version: '9.0.x' - name: Install dependencies run: | dotnet workload restore diff --git a/Directory.Build.props b/Directory.Build.props index 641b944..308139d 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ 11.2.0 - 11.2.0.4 + 11.2.0.5 11.2.0 diff --git a/samples/AvaloniaVisualBasic/Forms/Views/CodeEditorView.axaml.cs b/samples/AvaloniaVisualBasic/Forms/Views/CodeEditorView.axaml.cs index 6ae9376..b41aeb0 100644 --- a/samples/AvaloniaVisualBasic/Forms/Views/CodeEditorView.axaml.cs +++ b/samples/AvaloniaVisualBasic/Forms/Views/CodeEditorView.axaml.cs @@ -1,3 +1,4 @@ +using System; using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; diff --git a/src/Classic.Avalonia.Theme.ColorPicker/Classic.Avalonia.Theme.ColorPicker.csproj b/src/Classic.Avalonia.Theme.ColorPicker/Classic.Avalonia.Theme.ColorPicker.csproj index 07b29ce..2bf8d1c 100644 --- a/src/Classic.Avalonia.Theme.ColorPicker/Classic.Avalonia.Theme.ColorPicker.csproj +++ b/src/Classic.Avalonia.Theme.ColorPicker/Classic.Avalonia.Theme.ColorPicker.csproj @@ -1,12 +1,16 @@  - netstandard2.0 + netstandard2.0;net8.0;net9.0 enable enable latest + + true + + false bandysc diff --git a/src/Classic.Avalonia.Theme.DataGrid/Classic.Avalonia.Theme.DataGrid.csproj b/src/Classic.Avalonia.Theme.DataGrid/Classic.Avalonia.Theme.DataGrid.csproj index aaa4ff0..cd4946e 100644 --- a/src/Classic.Avalonia.Theme.DataGrid/Classic.Avalonia.Theme.DataGrid.csproj +++ b/src/Classic.Avalonia.Theme.DataGrid/Classic.Avalonia.Theme.DataGrid.csproj @@ -1,12 +1,16 @@  - netstandard2.0 + netstandard2.0;net8.0;net9.0 enable enable latest + + true + + false bandysc diff --git a/src/Classic.Avalonia.Theme.Dock/Classic.Avalonia.Theme.Dock.csproj b/src/Classic.Avalonia.Theme.Dock/Classic.Avalonia.Theme.Dock.csproj index 81f555b..727c2c2 100644 --- a/src/Classic.Avalonia.Theme.Dock/Classic.Avalonia.Theme.Dock.csproj +++ b/src/Classic.Avalonia.Theme.Dock/Classic.Avalonia.Theme.Dock.csproj @@ -1,13 +1,17 @@  - netstandard2.0 + netstandard2.0;net8.0;net9.0 enable enable latest true + + true + + false bandysc diff --git a/src/Classic.Avalonia.Theme/BulletDecorator.cs b/src/Classic.Avalonia.Theme/BulletDecorator.cs index 4b8a3da..2d8fb75 100644 --- a/src/Classic.Avalonia.Theme/BulletDecorator.cs +++ b/src/Classic.Avalonia.Theme/BulletDecorator.cs @@ -99,7 +99,7 @@ public IBrush? Background /// It should be aligned to BulletDecorator.Child which is the second visual child. /// /// - public Control Bullet + public Control? Bullet { get => _bullet; set @@ -117,12 +117,15 @@ public Control Bullet _bullet = value; - LogicalChildren.Add(value); - // notify the visual layer about the new child. - VisualChildren.Add(value); + if (value != null) + { + LogicalChildren.Add(value); + // notify the visual layer about the new child. + VisualChildren.Add(value); + } // If we decorator content exists we need to move it at the end of the visual tree - Control child = Child; + Control? child = Child; if (child != null) { VisualChildren.Remove(child); @@ -169,8 +172,8 @@ protected override Size MeasureOverride(Size constraint) { Size bulletSize = new Size(); Size contentSize = new Size(); - Control bullet = Bullet; - Control content = Child; + Control? bullet = Bullet; + Control? content = Child; // If we have bullet we should measure it first if (bullet != null) @@ -200,8 +203,8 @@ protected override Size MeasureOverride(Size constraint) /// Size that BulletDecorator will assume to position children. protected override Size ArrangeOverride(Size arrangeSize) { - Control bullet = Bullet; - Control content = Child; + Control? bullet = Bullet; + Control? content = Child; double contentOffsetX = 0; double bulletOffsetY = 0; @@ -376,7 +379,7 @@ private double GetFirstLineHeight(Control element) //------------------------------------------------------------------- #region Private Members - Control _bullet = null; + Control? _bullet = null; #endregion Private Members } \ No newline at end of file diff --git a/src/Classic.Avalonia.Theme/Classic.Avalonia.Theme.csproj b/src/Classic.Avalonia.Theme/Classic.Avalonia.Theme.csproj index 707f4ca..2076bfd 100644 --- a/src/Classic.Avalonia.Theme/Classic.Avalonia.Theme.csproj +++ b/src/Classic.Avalonia.Theme/Classic.Avalonia.Theme.csproj @@ -1,7 +1,7 @@  Library - netstandard2.0 + netstandard2.0;net8.0;net9.0 enable true true @@ -9,6 +9,10 @@ true + + true + + false bandysc diff --git a/src/Classic.Avalonia.Theme/ClassicBorderDecorator.cs b/src/Classic.Avalonia.Theme/ClassicBorderDecorator.cs index 6b6d91c..5f1f163 100644 --- a/src/Classic.Avalonia.Theme/ClassicBorderDecorator.cs +++ b/src/Classic.Avalonia.Theme/ClassicBorderDecorator.cs @@ -555,8 +555,8 @@ public static IBrush ClassicBorderBrush /// /// AvaloniaProperty for property. /// - public static readonly StyledProperty BorderBrushProperty = - AvaloniaProperty.Register( + public static readonly StyledProperty BorderBrushProperty = + AvaloniaProperty.Register( nameof(BorderBrush), ClassicBorderBrush); @@ -1211,7 +1211,7 @@ private Geometry GetHighlight1(Rect bounds) private Geometry GetShadow1(Rect bounds) { // Assumed to always be called after GetHighlight1 - Debug.Assert(_tabCache != null, "_tabCache is null. GetShadow1 should only be called after GetHighlight1"); + _tabCache = _tabCache ?? throw new Exception("_tabCache is null. GetShadow1 should only be called after GetHighlight1"); if (_tabCache.Shadow1 == null) { @@ -1224,7 +1224,7 @@ private Geometry GetShadow1(Rect bounds) private Geometry GetHighlight2(Rect bounds) { // Assumed to always be called after GetHighlight1 - Debug.Assert(_tabCache != null, "_tabCache is null. GetHighlight2 should only be called after GetHighlight1"); + _tabCache = _tabCache ?? throw new Exception("_tabCache is null. GetHighlight2 should only be called after GetHighlight1"); if (_tabCache.Highlight2 == null) { @@ -1237,7 +1237,7 @@ private Geometry GetHighlight2(Rect bounds) private Geometry GetShadow2(Rect bounds) { // Assumed to always be called after GetHighlight1 - Debug.Assert(_tabCache != null, "_tabCache is null. GetHighlight2 should only be called after GetHighlight1"); + _tabCache = _tabCache ?? throw new Exception("_tabCache is null. GetHighlight2 should only be called after GetHighlight1"); if (_tabCache.Shadow2 == null) { @@ -1266,6 +1266,9 @@ private MatrixTransform GetTabTransform(ClassicBorderStyle style, double xOffset case ClassicBorderStyle.TabBottom: _tabCache.Transform = new MatrixTransform(new Matrix(-1.0, 0.0, 0.0, -1.0, xOffset, yOffset)); break; + default: + _tabCache.Transform = new MatrixTransform(Matrix.Identity); + break; } _tabCache.xOffset = xOffset; _tabCache.yOffset = yOffset; @@ -1419,7 +1422,7 @@ private void DrawRadioButtonBorder(DrawingContext dc, ref Rect bounds) private T GetResourceOrDefault(object key, T defaultValue) { - if (Application.Current.TryGetResource(key, out var res)) + if (Application.Current!.TryGetResource(key, out var res)) if (res is T resT) return resT; return defaultValue; diff --git a/src/Classic.Avalonia.Theme/ClassicWindow.cs b/src/Classic.Avalonia.Theme/ClassicWindow.cs index 27eb495..8104780 100644 --- a/src/Classic.Avalonia.Theme/ClassicWindow.cs +++ b/src/Classic.Avalonia.Theme/ClassicWindow.cs @@ -113,7 +113,7 @@ public ClassicWindow() } } - private void OnActivated(object sender, EventArgs e) + private void OnActivated(object? sender, EventArgs e) { // Fix for macOS: enable resizing despite SystemDecorations = None if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && CanResize) diff --git a/src/Classic.Avalonia.Theme/Converters/BorderGapMaskConverter.cs b/src/Classic.Avalonia.Theme/Converters/BorderGapMaskConverter.cs index b36839a..c3dbed2 100644 --- a/src/Classic.Avalonia.Theme/Converters/BorderGapMaskConverter.cs +++ b/src/Classic.Avalonia.Theme/Converters/BorderGapMaskConverter.cs @@ -25,9 +25,9 @@ internal class BorderGapMaskConverter : IMultiValueConverter values[0] == null || values[1] == null || values[2] == null || - !doubleType.IsAssignableFrom(values[0].GetType()) || - !doubleType.IsAssignableFrom(values[1].GetType()) || - !doubleType.IsAssignableFrom(values[2].GetType()) ) + !doubleType.IsInstanceOfType(values[0]) || + !doubleType.IsInstanceOfType(values[1]) || + !doubleType.IsInstanceOfType(values[2]) ) { return AvaloniaProperty.UnsetValue; } @@ -42,9 +42,9 @@ internal class BorderGapMaskConverter : IMultiValueConverter // Conversion // - double headerWidth = (double)values[0]; - double borderWidth = (double)values[1]; - double borderHeight = (double)values[2]; + double headerWidth = (double?)values[0] ?? 0; + double borderWidth = (double?)values[1] ?? 0; + double borderHeight = (double?)values[2] ?? 0; // Doesn't make sense to have a Grid // with 0 as width or height diff --git a/src/Classic.Avalonia.Theme/Converters/ProgressBarBrushConverter.cs b/src/Classic.Avalonia.Theme/Converters/ProgressBarBrushConverter.cs index 240216c..0a54d2f 100644 --- a/src/Classic.Avalonia.Theme/Converters/ProgressBarBrushConverter.cs +++ b/src/Classic.Avalonia.Theme/Converters/ProgressBarBrushConverter.cs @@ -26,11 +26,11 @@ internal class ProgressBarBrushConverter : IMultiValueConverter (values[2] == null) || (values[3] == null) || (values[4] == null) || - !typeof(IBrush).IsAssignableFrom(values[0].GetType()) || - !typeof(bool).IsAssignableFrom(values[1].GetType()) || - !doubleType.IsAssignableFrom(values[2].GetType()) || - !doubleType.IsAssignableFrom(values[3].GetType()) || - !doubleType.IsAssignableFrom(values[4].GetType())) + values[0] is not IBrush || + values[1] is not bool || + !doubleType.IsInstanceOfType(values[2]) || + !doubleType.IsInstanceOfType(values[3]) || + !doubleType.IsInstanceOfType(values[4])) { return null; } @@ -39,11 +39,11 @@ internal class ProgressBarBrushConverter : IMultiValueConverter // Conversion // - IBrush brush = (Brush)values[0]; - bool isIndeterminate = (bool)values[1]; - double width = (double)values[2]; - double height = (double)values[3]; - double trackWidth = (double)values[4]; + IBrush brush = (Brush)values[0]!; + bool isIndeterminate = (bool)values[1]!; + double width = (double)values[2]!; + double height = (double)values[3]!; + double trackWidth = (double)values[4]!; // if an invalid height, return a null brush if (width <= 0.0 || Double.IsInfinity(width) || Double.IsNaN(width) || diff --git a/src/Classic.CommonControls.Avalonia/AppBuilderExtensions.cs b/src/Classic.CommonControls.Avalonia/AppBuilderExtensions.cs index 8e8ba12..1ca2738 100644 --- a/src/Classic.CommonControls.Avalonia/AppBuilderExtensions.cs +++ b/src/Classic.CommonControls.Avalonia/AppBuilderExtensions.cs @@ -10,7 +10,7 @@ public static class AppBuilderExtensions { public static AppBuilder UseMessageBoxSounds(this AppBuilder builder) { - string? tempChimeFileName = null; + //string? tempChimeFileName = null; string? tempChordFileName = null; string? tempDingFileName = null; @@ -38,7 +38,7 @@ public static AppBuilder UseMessageBoxSounds(this AppBuilder builder) if (resourceStream != null) { var array = new byte[resourceStream.Length]; - resourceStream.Read(array, 0, array.Length); + int read = resourceStream.Read(array, 0, array.Length); fileName = Path.GetTempFileName(); File.WriteAllBytes(fileName, array); if (msgBox.Icon is MessageBoxIcon.Error or MessageBoxIcon.Warning) diff --git a/src/Classic.CommonControls.Avalonia/Classic.CommonControls.Avalonia.csproj b/src/Classic.CommonControls.Avalonia/Classic.CommonControls.Avalonia.csproj index 7d4c285..73b0f4e 100644 --- a/src/Classic.CommonControls.Avalonia/Classic.CommonControls.Avalonia.csproj +++ b/src/Classic.CommonControls.Avalonia/Classic.CommonControls.Avalonia.csproj @@ -1,13 +1,17 @@  - netstandard2.0 + netstandard2.0;net8.0;net9.0 enable enable nullable latest + + true + + diff --git a/src/Classic.CommonControls.Avalonia/Dialogs/About/Metrics.cs b/src/Classic.CommonControls.Avalonia/Dialogs/About/Metrics.cs index a93c71a..5e5616b 100644 --- a/src/Classic.CommonControls.Avalonia/Dialogs/About/Metrics.cs +++ b/src/Classic.CommonControls.Avalonia/Dialogs/About/Metrics.cs @@ -44,16 +44,16 @@ private MemoryMetrics GetWindowsMetrics() using(var process = Process.Start(info)) { - output = process.StandardOutput.ReadToEnd(); + output = process?.StandardOutput.ReadToEnd(); } - var lines = output.Trim().Split('\n'); - var freeMemoryParts = lines[0].Split(['='], StringSplitOptions.RemoveEmptyEntries); - var totalMemoryParts = lines[1].Split(['='], StringSplitOptions.RemoveEmptyEntries); + var lines = output?.Trim().Split('\n'); + var freeMemoryParts = lines?[0].Split(['='], StringSplitOptions.RemoveEmptyEntries); + var totalMemoryParts = lines?[1].Split(['='], StringSplitOptions.RemoveEmptyEntries); var metrics = new MemoryMetrics(); - metrics.Total = Math.Round(double.Parse(totalMemoryParts[1]) / 1024, 0); - metrics.Free = Math.Round(double.Parse(freeMemoryParts[1]) / 1024, 0); + metrics.Total = Math.Round(double.Parse(totalMemoryParts?[1] ?? "0") / 1024, 0); + metrics.Free = Math.Round(double.Parse(freeMemoryParts?[1] ?? "0") / 1024, 0); metrics.Used = metrics.Total - metrics.Free; return metrics; @@ -71,13 +71,13 @@ private MemoryMetrics GetMacOsMetrics() using(var process = Process.Start(info)) { - output = process.StandardOutput.ReadToEnd(); + output = process?.StandardOutput.ReadToEnd(); } - var memory = output.Split([' '], StringSplitOptions.RemoveEmptyEntries); + var memory = output?.Split([' '], StringSplitOptions.RemoveEmptyEntries); var metrics = new MemoryMetrics(); - metrics.Total = double.Parse(memory[1]); + metrics.Total = double.Parse(memory?[1] ?? "0"); return metrics; } @@ -94,14 +94,14 @@ private MemoryMetrics GetLinuxMetrics() using(var process = Process.Start(info)) { - output = process.StandardOutput.ReadToEnd(); + output = process?.StandardOutput.ReadToEnd(); } - var lines = output.Split('\n'); - var memory = lines[1].Split([' '], StringSplitOptions.RemoveEmptyEntries); + var lines = output?.Split('\n'); + var memory = lines?[1].Split([' '], StringSplitOptions.RemoveEmptyEntries); var metrics = new MemoryMetrics(); - metrics.Total = double.Parse(memory[1]); + metrics.Total = double.Parse(memory?[1] ?? "0"); return metrics; } diff --git a/src/Classic.CommonControls.Avalonia/Dialogs/Font/FontDialogViewModel.cs b/src/Classic.CommonControls.Avalonia/Dialogs/Font/FontDialogViewModel.cs index 16c636a..ca7f2ee 100644 --- a/src/Classic.CommonControls.Avalonia/Dialogs/Font/FontDialogViewModel.cs +++ b/src/Classic.CommonControls.Avalonia/Dialogs/Font/FontDialogViewModel.cs @@ -11,14 +11,14 @@ public class FontDialogViewModel : INotifyPropertyChanged public ObservableCollection FontStyles { get; } = new(); public List FontSizes { get; } = new() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 }; - private FontFamily selectedFont; - public FontFamily SelectedFont + private FontFamily? selectedFont; + public FontFamily? SelectedFont { get => selectedFont; set { SetField(ref selectedFont, value); - SetField(ref fontNameText, value.Name, nameof(FontNameText)); + SetField(ref fontNameText, value?.Name, nameof(FontNameText)); UpdateFontStyles(); } } @@ -37,8 +37,8 @@ public double SelectedFontSize set => SetField(ref selectedFontSize, value); } - private string fontNameText; - public string FontNameText + private string? fontNameText; + public string? FontNameText { get => fontNameText; set @@ -68,7 +68,7 @@ public FontDialogViewModel(FontDialogResult? initial) : this() if (initial == null) return; - selectedFont = Fonts.FirstOrDefault(x => x == initial.Family); + selectedFont = Fonts.FirstOrDefault(x => x == initial.Family) ?? Fonts.FirstOrDefault(); UpdateFontStyles(); selectedFontSize = initial.Size; selectedFontStyle = FontStyles.FirstOrDefault(x => x.FontWeight == initial.Weight && x.FontStyle == initial.Style); @@ -76,6 +76,9 @@ public FontDialogViewModel(FontDialogResult? initial) : this() public void Accept() { + if (SelectedFont == null) + return; + AcceptRequested?.Invoke( new FontDialogResult(SelectedFont, SelectedFontStyle?.FontStyle ?? FontStyle.Normal, @@ -108,6 +111,9 @@ public void Cancel() private void UpdateFontStyles() { FontStyles.Clear(); + if (selectedFont == null) + return; + foreach (var style in styles) { foreach (var weight in weights) diff --git a/src/Classic.CommonControls.Avalonia/Dialogs/InputBoxes/InputBox.cs b/src/Classic.CommonControls.Avalonia/Dialogs/InputBoxes/InputBox.cs index ef9b7bd..d6a7774 100644 --- a/src/Classic.CommonControls.Avalonia/Dialogs/InputBoxes/InputBox.cs +++ b/src/Classic.CommonControls.Avalonia/Dialogs/InputBoxes/InputBox.cs @@ -52,9 +52,9 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) textBox?.Focus(); } - public bool CanExecute(object parameter) => true; + public bool CanExecute(object? parameter) => true; - public void Execute(object parameter) => TextRequest?.Invoke(Text); + public void Execute(object? parameter) => TextRequest?.Invoke(Text); public event EventHandler? CanExecuteChanged; diff --git a/src/Classic.CommonControls.Avalonia/Utils/IconRenderer.cs b/src/Classic.CommonControls.Avalonia/Utils/IconRenderer.cs index bbd2132..bad892a 100644 --- a/src/Classic.CommonControls.Avalonia/Utils/IconRenderer.cs +++ b/src/Classic.CommonControls.Avalonia/Utils/IconRenderer.cs @@ -101,7 +101,7 @@ public void Dispose() { } public bool HitTest(Point p) => true; - public bool Equals(ICustomDrawOperation other) => false; + public bool Equals(ICustomDrawOperation? other) => false; /// /// This method handles the rendering of the chart using SkiaSharp.