From ba8354dd61aa00a824f69bcdbe6acd148ae30821 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Sun, 10 Dec 2023 23:25:46 +0200 Subject: [PATCH] Refactor with C# 12 features --- LightBulb.WindowsApi/DeviceContext.cs | 5 +-- LightBulb.WindowsApi/NativeResource.cs | 6 ++-- LightBulb.WindowsApi/RegistrySwitch.cs | 33 ++++++------------- LightBulb/Models/ExternalApplication.cs | 7 ++-- LightBulb/Services/SettingsService.cs | 5 +-- LightBulb/Services/UpdateService.cs | 13 ++------ .../Settings/AdvancedSettingsTabViewModel.cs | 6 ++-- ...pplicationWhitelistSettingsTabViewModel.cs | 18 +++------- .../Settings/GeneralSettingsTabViewModel.cs | 6 ++-- .../Settings/HotKeySettingsTabViewModel.cs | 6 ++-- .../ViewModels/Framework/DialogManager.cs | 13 ++------ 11 files changed, 33 insertions(+), 85 deletions(-) diff --git a/LightBulb.WindowsApi/DeviceContext.cs b/LightBulb.WindowsApi/DeviceContext.cs index 8c13b76..d7014cd 100644 --- a/LightBulb.WindowsApi/DeviceContext.cs +++ b/LightBulb.WindowsApi/DeviceContext.cs @@ -5,13 +5,10 @@ namespace LightBulb.WindowsApi; -public partial class DeviceContext : NativeResource +public partial class DeviceContext(nint handle) : NativeResource(handle) { private int _gammaChannelOffset; - public DeviceContext(nint handle) - : base(handle) { } - private void SetGammaRamp(GammaRamp ramp) { if (!NativeMethods.SetDeviceGammaRamp(Handle, ref ramp)) diff --git a/LightBulb.WindowsApi/NativeResource.cs b/LightBulb.WindowsApi/NativeResource.cs index 9ecaaf7..b50b6c7 100644 --- a/LightBulb.WindowsApi/NativeResource.cs +++ b/LightBulb.WindowsApi/NativeResource.cs @@ -3,11 +3,9 @@ namespace LightBulb.WindowsApi; -public abstract class NativeResource : IDisposable +public abstract class NativeResource(nint handle) : IDisposable { - public nint Handle { get; } - - protected NativeResource(nint handle) => Handle = handle; + public nint Handle { get; } = handle; [ExcludeFromCodeCoverage] ~NativeResource() => Dispose(false); diff --git a/LightBulb.WindowsApi/RegistrySwitch.cs b/LightBulb.WindowsApi/RegistrySwitch.cs index cf3fa09..e56d8ab 100644 --- a/LightBulb.WindowsApi/RegistrySwitch.cs +++ b/LightBulb.WindowsApi/RegistrySwitch.cs @@ -7,14 +7,9 @@ namespace LightBulb.WindowsApi; -public class RegistrySwitch +public class RegistrySwitch(RegistryHive hive, string keyName, string entryName, T enabledValue) where T : notnull { - private readonly RegistryHive _hive; - private readonly string _keyName; - private readonly string _entryName; - private readonly T _enabledValue; - private readonly object _lock = new(); public bool IsSet @@ -24,11 +19,11 @@ public bool IsSet lock (_lock) { // This should always be accessible without elevation - var value = _hive.OpenKey().OpenSubKey(_keyName, false)?.GetValue(_entryName); + var value = hive.OpenKey().OpenSubKey(keyName, false)?.GetValue(entryName); if (value is null) return false; - return EqualityComparer.Default.Equals(_enabledValue, (T)value); + return EqualityComparer.Default.Equals(enabledValue, (T)value); } } set @@ -41,34 +36,26 @@ public bool IsSet try { - var key = _hive.OpenKey().CreateSubKey(_keyName, true); + var key = hive.OpenKey().CreateSubKey(keyName, true); if (value) - key.SetValue(_entryName, _enabledValue); + key.SetValue(entryName, enabledValue); else - key.DeleteValue(_entryName); + key.DeleteValue(entryName); } catch (Exception ex) when (ex is SecurityException or UnauthorizedAccessException) { // Run reg.exe with elevation if (value) Reg.SetValue( - _hive.GetShortMoniker() + '\\' + _keyName, - _entryName, - _enabledValue + hive.GetShortMoniker() + '\\' + keyName, + entryName, + enabledValue ); else - Reg.DeleteValue(_hive.GetShortMoniker() + '\\' + _keyName, _entryName); + Reg.DeleteValue(hive.GetShortMoniker() + '\\' + keyName, entryName); } } } } - - public RegistrySwitch(RegistryHive hive, string keyName, string entryName, T enabledValue) - { - _hive = hive; - _keyName = keyName; - _entryName = entryName; - _enabledValue = enabledValue; - } } diff --git a/LightBulb/Models/ExternalApplication.cs b/LightBulb/Models/ExternalApplication.cs index 24d29e8..cba81ff 100644 --- a/LightBulb/Models/ExternalApplication.cs +++ b/LightBulb/Models/ExternalApplication.cs @@ -3,15 +3,12 @@ namespace LightBulb.Models; -public partial class ExternalApplication +public partial class ExternalApplication(string executableFilePath) { - public string ExecutableFilePath { get; } + public string ExecutableFilePath { get; } = executableFilePath; public string Name => Path.GetFileNameWithoutExtension(ExecutableFilePath); - public ExternalApplication(string executableFilePath) => - ExecutableFilePath = executableFilePath; - public override string ToString() => Name; } diff --git a/LightBulb/Services/SettingsService.cs b/LightBulb/Services/SettingsService.cs index 0f636ea..c902989 100644 --- a/LightBulb/Services/SettingsService.cs +++ b/LightBulb/Services/SettingsService.cs @@ -14,7 +14,7 @@ namespace LightBulb.Services; [AddINotifyPropertyChangedInterface] -public partial class SettingsService : SettingsBase, INotifyPropertyChanged +public partial class SettingsService() : SettingsBase(GetFilePath()), INotifyPropertyChanged { private readonly RegistrySwitch _extendedGammaRangeSwitch = new( @@ -112,9 +112,6 @@ public partial class SettingsService : SettingsBase, INotifyPropertyChanged public event EventHandler? SettingsSaved; - public SettingsService() - : base(GetFilePath()) { } - public override void Reset() { base.Reset(); diff --git a/LightBulb/Services/UpdateService.cs b/LightBulb/Services/UpdateService.cs index d479424..d375a58 100644 --- a/LightBulb/Services/UpdateService.cs +++ b/LightBulb/Services/UpdateService.cs @@ -7,25 +7,18 @@ namespace LightBulb.Services; -public class UpdateService : IDisposable +public class UpdateService(SettingsService settingsService) : IDisposable { - private readonly SettingsService _settingsService; - private readonly IUpdateManager _updateManager = new UpdateManager( new GithubPackageResolver("Tyrrrz", "LightBulb", "LightBulb.zip"), new ZipPackageExtractor() ); - public UpdateService(SettingsService settingsService) - { - _settingsService = settingsService; - } - private Version? TryGetLastPreparedUpdate() => _updateManager.GetPreparedUpdates().Max(); public async Task CheckPrepareUpdateAsync() { - if (!_settingsService.IsAutoUpdateEnabled) + if (!settingsService.IsAutoUpdateEnabled) return; try @@ -45,7 +38,7 @@ public async Task CheckPrepareUpdateAsync() public void FinalizePendingUpdates() { - if (!_settingsService.IsAutoUpdateEnabled) + if (!settingsService.IsAutoUpdateEnabled) return; try diff --git a/LightBulb/ViewModels/Components/Settings/AdvancedSettingsTabViewModel.cs b/LightBulb/ViewModels/Components/Settings/AdvancedSettingsTabViewModel.cs index 2a253ec..4f8b2d0 100644 --- a/LightBulb/ViewModels/Components/Settings/AdvancedSettingsTabViewModel.cs +++ b/LightBulb/ViewModels/Components/Settings/AdvancedSettingsTabViewModel.cs @@ -2,7 +2,8 @@ namespace LightBulb.ViewModels.Components.Settings; -public class AdvancedSettingsTabViewModel : SettingsTabViewModelBase +public class AdvancedSettingsTabViewModel(SettingsService settingsService) + : SettingsTabViewModelBase(settingsService, 2, "Advanced") { public bool IsAutoStartEnabled { @@ -39,7 +40,4 @@ public bool IsGammaPollingEnabled get => SettingsService.IsGammaPollingEnabled; set => SettingsService.IsGammaPollingEnabled = value; } - - public AdvancedSettingsTabViewModel(SettingsService settingsService) - : base(settingsService, 2, "Advanced") { } } diff --git a/LightBulb/ViewModels/Components/Settings/ApplicationWhitelistSettingsTabViewModel.cs b/LightBulb/ViewModels/Components/Settings/ApplicationWhitelistSettingsTabViewModel.cs index 1e7b088..d811dc8 100644 --- a/LightBulb/ViewModels/Components/Settings/ApplicationWhitelistSettingsTabViewModel.cs +++ b/LightBulb/ViewModels/Components/Settings/ApplicationWhitelistSettingsTabViewModel.cs @@ -6,10 +6,11 @@ namespace LightBulb.ViewModels.Components.Settings; -public class ApplicationWhitelistSettingsTabViewModel : SettingsTabViewModelBase +public class ApplicationWhitelistSettingsTabViewModel( + SettingsService settingsService, + ExternalApplicationService externalApplicationService +) : SettingsTabViewModelBase(settingsService, 3, "Application whitelist") { - private readonly ExternalApplicationService _externalApplicationService; - public bool IsApplicationWhitelistEnabled { get => SettingsService.IsApplicationWhitelistEnabled; @@ -24,15 +25,6 @@ public IReadOnlyList? WhitelistedApplications set => SettingsService.WhitelistedApplications = value; } - public ApplicationWhitelistSettingsTabViewModel( - SettingsService settingsService, - ExternalApplicationService externalApplicationService - ) - : base(settingsService, 3, "Application whitelist") - { - _externalApplicationService = externalApplicationService; - } - public void OnViewLoaded() => PullAvailableApplications(); public void PullAvailableApplications() @@ -45,7 +37,7 @@ public void PullAvailableApplications() applications.Add(application); // Add all running applications - foreach (var application in _externalApplicationService.GetAllRunningApplications()) + foreach (var application in externalApplicationService.GetAllRunningApplications()) applications.Add(application); AvailableApplications = applications.ToArray(); diff --git a/LightBulb/ViewModels/Components/Settings/GeneralSettingsTabViewModel.cs b/LightBulb/ViewModels/Components/Settings/GeneralSettingsTabViewModel.cs index f1d6d7d..3df5e2c 100644 --- a/LightBulb/ViewModels/Components/Settings/GeneralSettingsTabViewModel.cs +++ b/LightBulb/ViewModels/Components/Settings/GeneralSettingsTabViewModel.cs @@ -4,7 +4,8 @@ namespace LightBulb.ViewModels.Components.Settings; -public class GeneralSettingsTabViewModel : SettingsTabViewModelBase +public class GeneralSettingsTabViewModel(SettingsService settingsService) + : SettingsTabViewModelBase(settingsService, 0, "General") { public double NightTemperature { @@ -96,7 +97,4 @@ public double ConfigurationTransitionOffset get => SettingsService.ConfigurationTransitionOffset; set => SettingsService.ConfigurationTransitionOffset = Math.Clamp(value, 0, 1); } - - public GeneralSettingsTabViewModel(SettingsService settingsService) - : base(settingsService, 0, "General") { } } diff --git a/LightBulb/ViewModels/Components/Settings/HotKeySettingsTabViewModel.cs b/LightBulb/ViewModels/Components/Settings/HotKeySettingsTabViewModel.cs index 33e82f3..fbb8334 100644 --- a/LightBulb/ViewModels/Components/Settings/HotKeySettingsTabViewModel.cs +++ b/LightBulb/ViewModels/Components/Settings/HotKeySettingsTabViewModel.cs @@ -3,7 +3,8 @@ namespace LightBulb.ViewModels.Components.Settings; -public class HotKeySettingsTabViewModel : SettingsTabViewModelBase +public class HotKeySettingsTabViewModel(SettingsService settingsService) + : SettingsTabViewModelBase(settingsService, 4, "Hotkeys") { public HotKey ToggleHotKey { @@ -40,7 +41,4 @@ public HotKey ResetConfigurationOffsetHotKey get => SettingsService.ResetConfigurationOffsetHotKey; set => SettingsService.ResetConfigurationOffsetHotKey = value; } - - public HotKeySettingsTabViewModel(SettingsService settingsService) - : base(settingsService, 4, "Hotkeys") { } } diff --git a/LightBulb/ViewModels/Framework/DialogManager.cs b/LightBulb/ViewModels/Framework/DialogManager.cs index b1b1789..15a05be 100644 --- a/LightBulb/ViewModels/Framework/DialogManager.cs +++ b/LightBulb/ViewModels/Framework/DialogManager.cs @@ -8,31 +8,24 @@ namespace LightBulb.ViewModels.Framework; -public class DialogManager : IDisposable +public class DialogManager(IViewManager viewManager) : IDisposable { - private readonly IViewManager _viewManager; - // Cache and reuse dialog screen views, as creating them is incredibly slow private readonly Dictionary _dialogScreenViewCache = new(); private readonly SemaphoreSlim _dialogLock = new(1, 1); - public DialogManager(IViewManager viewManager) - { - _viewManager = viewManager; - } - public UIElement GetViewForDialogScreen(DialogScreen dialogScreen) { var dialogScreenType = dialogScreen.GetType(); if (_dialogScreenViewCache.TryGetValue(dialogScreenType, out var cachedView)) { - _viewManager.BindViewToModel(cachedView, dialogScreen); + viewManager.BindViewToModel(cachedView, dialogScreen); return cachedView; } else { - var view = _viewManager.CreateAndBindViewForModelIfNecessary(dialogScreen); + var view = viewManager.CreateAndBindViewForModelIfNecessary(dialogScreen); // This warms up the view and triggers all bindings. // We need to do this, as the view may have nested model-bound ContentControls