From 90430ff32f056e04a012dc1837a6ffdd7af72d3a Mon Sep 17 00:00:00 2001 From: holger Date: Thu, 15 Aug 2024 21:39:10 +0200 Subject: [PATCH] Do something --- src/App.axaml.cs | 21 ++++----- src/ApplicationTheme.cs | 2 +- src/AvaloniaCoreRTDemo.csproj | 19 ++++---- src/Controls/MainControl.axaml.cs | 3 +- .../ViewModels/MainControlViewModel.cs | 14 +++--- src/GlobalUsings.cs | 3 ++ src/Interfaces/IMainWindow.cs | 2 +- src/Interfaces/IMainWindowState.cs | 6 +-- src/Interfaces/IThemeSwitch.cs | 2 +- src/Utilities.cs | 18 ++++--- src/Windows/AboutWindow.axaml | 2 +- src/Windows/AboutWindow.axaml.cs | 7 ++- src/Windows/MainWindow.axaml.cs | 6 +-- src/Windows/ViewModels/AboutViewModel.cs | 47 +++++++++---------- .../ViewModels/ApplicationModelBase.cs | 21 ++++----- src/Windows/ViewModels/MainViewModel.cs | 11 +++-- 16 files changed, 88 insertions(+), 96 deletions(-) create mode 100644 src/GlobalUsings.cs diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 46745ab..d65345a 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -5,7 +5,6 @@ using Avalonia.Markup.Xaml; using Avalonia.Styling; using Avalonia.Themes.Fluent; using Avalonia.Themes.Simple; - using AvaloniaCoreRTDemo.Interfaces; using AvaloniaCoreRTDemo.Windows; @@ -57,7 +56,7 @@ public sealed class App : Application, IThemeSwitch if (theme == _currentTheme) return; - bool themeChanged = theme switch + var themeChanged = theme switch { ApplicationTheme.SimpleLight => _currentTheme is ApplicationTheme.FluentDark or ApplicationTheme.FluentLight, @@ -95,17 +94,15 @@ public sealed class App : Application, IThemeSwitch break; } - if (themeChanged && ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) - { - MainWindow oldWindow = (desktop.MainWindow as MainWindow)!; - MainWindow newWindow = new MainWindow(oldWindow); + if (!themeChanged || ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return; + var oldWindow = (desktop.MainWindow as MainWindow)!; + var newWindow = new MainWindow(oldWindow); - desktop.MainWindow = newWindow; - DataContext = newWindow.DataContext; + desktop.MainWindow = newWindow; + DataContext = newWindow.DataContext; - oldWindow.Hide(); - newWindow.Show(); - oldWindow?.Close(); - } + oldWindow.Hide(); + newWindow.Show(); + oldWindow.Close(); } } diff --git a/src/ApplicationTheme.cs b/src/ApplicationTheme.cs index 831b27b..7d9a1b5 100644 --- a/src/ApplicationTheme.cs +++ b/src/ApplicationTheme.cs @@ -6,4 +6,4 @@ public enum ApplicationTheme : byte SimpleDark = 1, FluentLight = 2, FluentDark = 3, -} \ No newline at end of file +} diff --git a/src/AvaloniaCoreRTDemo.csproj b/src/AvaloniaCoreRTDemo.csproj index 13e79c6..a2303eb 100644 --- a/src/AvaloniaCoreRTDemo.csproj +++ b/src/AvaloniaCoreRTDemo.csproj @@ -4,11 +4,11 @@ WinExe Exe - net7.0 + net9.0 Assets/app.ico true enable - false + true true true enable @@ -48,20 +48,19 @@ - - - - - + + + + + - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/src/Controls/MainControl.axaml.cs b/src/Controls/MainControl.axaml.cs index bb10f80..3077a5f 100644 --- a/src/Controls/MainControl.axaml.cs +++ b/src/Controls/MainControl.axaml.cs @@ -1,6 +1,5 @@ using Avalonia.Controls; using Avalonia.Markup.Xaml; - using AvaloniaCoreRTDemo.Controls.ViewModels; namespace AvaloniaCoreRTDemo.Controls; @@ -22,4 +21,4 @@ public sealed partial class MainControl : UserControl { DataContext = new MainControlViewModel(model); } -} \ No newline at end of file +} diff --git a/src/Controls/ViewModels/MainControlViewModel.cs b/src/Controls/ViewModels/MainControlViewModel.cs index 3d6c915..94f17cd 100644 --- a/src/Controls/ViewModels/MainControlViewModel.cs +++ b/src/Controls/ViewModels/MainControlViewModel.cs @@ -1,18 +1,17 @@ using Avalonia.Media.Imaging; -using MvvmGen; namespace AvaloniaCoreRTDemo.Controls.ViewModels; [ViewModel] internal sealed partial class MainControlViewModel : IMainWindowState { - private IBitmap _dotNetImage; - private IBitmap _avaloniaImage; + private Bitmap _dotNetImage; + private Bitmap _avaloniaImage; private bool _unloadable = false; - public IBitmap DotNetImage => _dotNetImage; - public IBitmap AvaloniaImage => _avaloniaImage; + public Bitmap DotNetImage => _dotNetImage; + public Bitmap AvaloniaImage => _avaloniaImage; public string? Text { get; set; } partial void OnInitialize() @@ -21,7 +20,8 @@ internal sealed partial class MainControlViewModel : IMainWindowState _avaloniaImage = Utilities.GetImageFromFile("avalonia.png"); } - public MainControlViewModel(IMainWindowState state) : this() + public MainControlViewModel(IMainWindowState state) + : this() { _avaloniaImage = state.AvaloniaImage; _dotNetImage = state.DotNetImage; @@ -42,4 +42,4 @@ internal sealed partial class MainControlViewModel : IMainWindowState { _unloadable = true; } -} \ No newline at end of file +} diff --git a/src/GlobalUsings.cs b/src/GlobalUsings.cs new file mode 100644 index 0000000..65cee5c --- /dev/null +++ b/src/GlobalUsings.cs @@ -0,0 +1,3 @@ +// Global using directives + +global using MvvmGen; \ No newline at end of file diff --git a/src/Interfaces/IMainWindow.cs b/src/Interfaces/IMainWindow.cs index ec1d934..487ff7d 100644 --- a/src/Interfaces/IMainWindow.cs +++ b/src/Interfaces/IMainWindow.cs @@ -11,4 +11,4 @@ public interface IMainWindow Size ClientSize { get; } Size? FrameSize { get; } WindowState State { get; } -} \ No newline at end of file +} diff --git a/src/Interfaces/IMainWindowState.cs b/src/Interfaces/IMainWindowState.cs index a077964..4e6cdbc 100644 --- a/src/Interfaces/IMainWindowState.cs +++ b/src/Interfaces/IMainWindowState.cs @@ -4,9 +4,9 @@ namespace AvaloniaCoreRTDemo; public interface IMainWindowState { - IBitmap DotNetImage { get; } - IBitmap AvaloniaImage { get; } + Bitmap DotNetImage { get; } + Bitmap AvaloniaImage { get; } string? Text { get; } void SetUnloadable(); -} \ No newline at end of file +} diff --git a/src/Interfaces/IThemeSwitch.cs b/src/Interfaces/IThemeSwitch.cs index 144de3c..41d66ea 100644 --- a/src/Interfaces/IThemeSwitch.cs +++ b/src/Interfaces/IThemeSwitch.cs @@ -4,4 +4,4 @@ public interface IThemeSwitch { ApplicationTheme Current { get; } void ChangeTheme(ApplicationTheme theme); -} \ No newline at end of file +} diff --git a/src/Utilities.cs b/src/Utilities.cs index a00da52..13e6153 100644 --- a/src/Utilities.cs +++ b/src/Utilities.cs @@ -1,5 +1,4 @@ using System.Runtime.InteropServices; - using Avalonia; using Avalonia.Controls; using Avalonia.Media.Imaging; @@ -14,8 +13,9 @@ internal static class Utilities public static Bitmap GetImageFromResources(string fileName) { - var assetLoader = AvaloniaLocator.Current.GetRequiredService(); - using var assetStream = assetLoader.Open(new Uri($"avares://AvaloniaCoreRTDemo/Images/{fileName}")); + using var assetStream = AssetLoader.Open( + new Uri($"avares://AvaloniaCoreRTDemo/Images/{fileName}") + ); return new Bitmap(assetStream); } @@ -23,11 +23,8 @@ internal static class Utilities { if (!IsOSX || !window.FrameSize.HasValue) return window.Position; - else - { - int yOffset = (int)(window.FrameSize.Value.Height - window.ClientSize.Height); - return new(window.Position.X, window.Position.Y + yOffset); - } + var yOffset = (int)(window.FrameSize.Value.Height - window.ClientSize.Height); + return new(window.Position.X, window.Position.Y + yOffset); } public static Bitmap GetImageFromFile(string path) @@ -42,5 +39,6 @@ internal static class Utilities } } - private static string GetImageFullPath(string fileName) => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); -} \ No newline at end of file + private static string GetImageFullPath(string fileName) => + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); +} diff --git a/src/Windows/AboutWindow.axaml b/src/Windows/AboutWindow.axaml index fd54d3d..f8489f0 100644 --- a/src/Windows/AboutWindow.axaml +++ b/src/Windows/AboutWindow.axaml @@ -37,7 +37,7 @@ CanUserSortColumns="False" HeadersVisibility="None" IsReadOnly="True" - Items="{Binding SystemDetails}"> + ItemsSource="{Binding SystemDetails}"> this.GetControl("MainControl"); - public MainWindow() : this(default) { } + public MainWindow() + : this(default) { } public MainWindow(IMainWindow? window) { @@ -45,4 +45,4 @@ public sealed partial class MainWindow : Window, IMainWindow ClientSize = window.ClientSize; } } -} \ No newline at end of file +} diff --git a/src/Windows/ViewModels/AboutViewModel.cs b/src/Windows/ViewModels/AboutViewModel.cs index 425074f..3a20826 100644 --- a/src/Windows/ViewModels/AboutViewModel.cs +++ b/src/Windows/ViewModels/AboutViewModel.cs @@ -1,7 +1,5 @@ using System.Runtime.InteropServices; - using Avalonia.Media.Imaging; -using MvvmGen; namespace AvaloniaCoreRTDemo.Windows.ViewModels; @@ -10,28 +8,27 @@ internal record SystemDetail(string Key, string Value); [ViewModel] internal sealed partial class AboutViewModel { - private readonly IBitmap _computerImage; + private readonly Bitmap _computerImage; private readonly bool _darkTheme; - public IBitmap ComputerImage => _computerImage; + public Bitmap ComputerImage => _computerImage; public IReadOnlyList SystemDetails { get; } = - new[] - { - new SystemDetail("Number of Cores", Environment.ProcessorCount.ToString()), - new SystemDetail("OS", RuntimeInformation.OSDescription), - new SystemDetail("OS Arch", RuntimeInformation.OSArchitecture.ToString()), - new SystemDetail("OS Version", Environment.OSVersion.ToString()), - new SystemDetail("Computer", Environment.MachineName), - new SystemDetail("User", Environment.UserName), - new SystemDetail("System Path", Environment.SystemDirectory), - new SystemDetail("Current Path", Environment.CurrentDirectory), - new SystemDetail("Process Arch", RuntimeInformation.ProcessArchitecture.ToString()), - new SystemDetail("Runtime Name", RuntimeInformation.FrameworkDescription), - new SystemDetail("Runtime Path", RuntimeEnvironment.GetRuntimeDirectory()), - new SystemDetail("Runtime Version", RuntimeEnvironment.GetSystemVersion()), - new SystemDetail("Framework Version", Environment.Version.ToString()), - }; + [ + new SystemDetail("Number of Cores", Environment.ProcessorCount.ToString()), + new SystemDetail("OS", RuntimeInformation.OSDescription), + new SystemDetail("OS Arch", RuntimeInformation.OSArchitecture.ToString()), + new SystemDetail("OS Version", Environment.OSVersion.ToString()), + new SystemDetail("Computer", Environment.MachineName), + new SystemDetail("User", Environment.UserName), + new SystemDetail("System Path", Environment.SystemDirectory), + new SystemDetail("Current Path", Environment.CurrentDirectory), + new SystemDetail("Process Arch", RuntimeInformation.ProcessArchitecture.ToString()), + new SystemDetail("Runtime Name", RuntimeInformation.FrameworkDescription), + new SystemDetail("Runtime Path", RuntimeEnvironment.GetRuntimeDirectory()), + new SystemDetail("Runtime Version", RuntimeEnvironment.GetSystemVersion()), + new SystemDetail("Framework Version", Environment.Version.ToString()) + ]; private string ComputerImageName { @@ -39,14 +36,14 @@ internal sealed partial class AboutViewModel { if (Utilities.IsWindows) return !_darkTheme ? "windows.png" : "windows_d.png"; - else if (Utilities.IsOSX) + if (Utilities.IsOSX) return !_darkTheme ? "macos.png" : "macos_d.png"; - else - return !_darkTheme ? "linux.png" : "linux_d.png"; + return !_darkTheme ? "linux.png" : "linux_d.png"; } } - public AboutViewModel(bool darkTheme) : this() + public AboutViewModel(bool darkTheme) + : this() { _darkTheme = darkTheme; _computerImage = Utilities.GetImageFromResources(ComputerImageName); @@ -56,4 +53,4 @@ internal sealed partial class AboutViewModel { _computerImage.Dispose(); } -} \ No newline at end of file +} diff --git a/src/Windows/ViewModels/ApplicationModelBase.cs b/src/Windows/ViewModels/ApplicationModelBase.cs index 960663e..e600c7d 100644 --- a/src/Windows/ViewModels/ApplicationModelBase.cs +++ b/src/Windows/ViewModels/ApplicationModelBase.cs @@ -1,7 +1,5 @@ using Avalonia.Controls; - using AvaloniaCoreRTDemo.Interfaces; -using MvvmGen; namespace AvaloniaCoreRTDemo.Windows.ViewModels; @@ -14,21 +12,22 @@ internal abstract partial class ApplicationModelBase private bool _aboutEnabled = true; [Property] - private bool _defaultLightEnabled = false; + private bool _defaultLightEnabled; [Property] - private bool _defaultDarkEnabled = false; + private bool _defaultDarkEnabled; [Property] - private bool _fluentLightEnabled = false; + private bool _fluentLightEnabled; [Property] - private bool _fluentDarkEnabled = false; + private bool _fluentDarkEnabled; - public ApplicationModelBase(IThemeSwitch themeSwitch) : this() + public ApplicationModelBase(IThemeSwitch themeSwitch) + : this() { _themeSwitch = themeSwitch; - IntializeTheme(themeSwitch.Current); + InitializeTheme(themeSwitch.Current); } [Command] @@ -62,14 +61,14 @@ internal abstract partial class ApplicationModelBase protected void SetTheme(ApplicationTheme theme) { - IntializeTheme(theme); + InitializeTheme(theme); _themeSwitch.ChangeTheme(theme); } [Command] private void FileExit() => Environment.Exit(0); - private void IntializeTheme(ApplicationTheme theme) + private void InitializeTheme(ApplicationTheme theme) { DefaultLightEnabled = theme != ApplicationTheme.SimpleLight; DefaultDarkEnabled = theme != ApplicationTheme.SimpleDark; @@ -84,4 +83,4 @@ internal abstract partial class ApplicationModelBase ApplicationTheme.FluentDark => true, _ => false, }; -} \ No newline at end of file +} diff --git a/src/Windows/ViewModels/MainViewModel.cs b/src/Windows/ViewModels/MainViewModel.cs index 9a8f0d0..1bddd57 100644 --- a/src/Windows/ViewModels/MainViewModel.cs +++ b/src/Windows/ViewModels/MainViewModel.cs @@ -1,14 +1,15 @@ using Avalonia.Controls; - using AvaloniaCoreRTDemo.Interfaces; namespace AvaloniaCoreRTDemo.Windows.ViewModels; -internal sealed class MainViewModel : ApplicationModelBase where TWindow : Window, IMainWindow +internal sealed class MainViewModel : ApplicationModelBase + where TWindow : Window, IMainWindow { - private TWindow _window; + private readonly TWindow _window; - public MainViewModel(TWindow window) : base(window.ThemeSwitch) + public MainViewModel(TWindow window) + : base(window.ThemeSwitch) { _window = window; } @@ -22,4 +23,4 @@ internal sealed class MainViewModel : ApplicationModelBase where TWindo protected override void FluentLight() => SetTheme(ApplicationTheme.FluentLight); protected override void FluentDark() => SetTheme(ApplicationTheme.FluentDark); -} \ No newline at end of file +}