Skip to content

Commit

Permalink
Refactor with C# 12 features
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Dec 10, 2023
1 parent 50a0479 commit ba8354d
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 85 deletions.
5 changes: 1 addition & 4 deletions LightBulb.WindowsApi/DeviceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 2 additions & 4 deletions LightBulb.WindowsApi/NativeResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 10 additions & 23 deletions LightBulb.WindowsApi/RegistrySwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@

namespace LightBulb.WindowsApi;

public class RegistrySwitch<T>
public class RegistrySwitch<T>(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
Expand All @@ -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<T>.Default.Equals(_enabledValue, (T)value);
return EqualityComparer<T>.Default.Equals(enabledValue, (T)value);
}
}
set
Expand All @@ -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;
}
}
7 changes: 2 additions & 5 deletions LightBulb/Models/ExternalApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 1 addition & 4 deletions LightBulb/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace LightBulb.Services;

[AddINotifyPropertyChangedInterface]
public partial class SettingsService : SettingsBase, INotifyPropertyChanged
public partial class SettingsService() : SettingsBase(GetFilePath()), INotifyPropertyChanged
{
private readonly RegistrySwitch<int> _extendedGammaRangeSwitch =
new(
Expand Down Expand Up @@ -112,9 +112,6 @@ public partial class SettingsService : SettingsBase, INotifyPropertyChanged

public event EventHandler? SettingsSaved;

public SettingsService()
: base(GetFilePath()) { }

public override void Reset()
{
base.Reset();
Expand Down
13 changes: 3 additions & 10 deletions LightBulb/Services/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +38,7 @@ public async Task CheckPrepareUpdateAsync()

public void FinalizePendingUpdates()
{
if (!_settingsService.IsAutoUpdateEnabled)
if (!settingsService.IsAutoUpdateEnabled)
return;

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -39,7 +40,4 @@ public bool IsGammaPollingEnabled
get => SettingsService.IsGammaPollingEnabled;
set => SettingsService.IsGammaPollingEnabled = value;
}

public AdvancedSettingsTabViewModel(SettingsService settingsService)
: base(settingsService, 2, "Advanced") { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,15 +25,6 @@ public IReadOnlyList<ExternalApplication>? 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()
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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") { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -40,7 +41,4 @@ public HotKey ResetConfigurationOffsetHotKey
get => SettingsService.ResetConfigurationOffsetHotKey;
set => SettingsService.ResetConfigurationOffsetHotKey = value;
}

public HotKeySettingsTabViewModel(SettingsService settingsService)
: base(settingsService, 4, "Hotkeys") { }
}
13 changes: 3 additions & 10 deletions LightBulb/ViewModels/Framework/DialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, UIElement> _dialogScreenViewCache = new();
private readonly SemaphoreSlim _dialogLock = new(1, 1);

public DialogManager(IViewManager viewManager)
{
_viewManager = viewManager;
}

public UIElement GetViewForDialogScreen<T>(DialogScreen<T> 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
Expand Down

0 comments on commit ba8354d

Please sign in to comment.