From 8977ab9a44c9c1d7086266c976857fbac0389e18 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 28 Mar 2024 05:05:49 +0200 Subject: [PATCH] Clean up --- LightBulb.WindowsApi/MessageBox.cs | 7 +++++ LightBulb.WindowsApi/NativeMethods.User32.cs | 3 ++ LightBulb/App.axaml.cs | 2 +- LightBulb/Program.cs | 6 ++-- LightBulb/Services/SettingsService.cs | 4 +-- LightBulb/Services/UpdateService.cs | 4 +-- LightBulb/StartupOptions.cs | 9 ++---- .../Utils/Extensions/AvaloniaExtensions.cs | 12 ++++---- .../NotifyPropertyChangedExtensions.cs | 28 +++++++++++++++++-- LightBulb/Utils/NativeMethods.cs | 12 -------- .../Components/DashboardViewModel.cs | 15 ++++++++++ LightBulb/ViewModels/MainViewModel.cs | 7 +---- LightBulb/Views/MainView.axaml | 2 +- 13 files changed, 68 insertions(+), 43 deletions(-) create mode 100644 LightBulb.WindowsApi/MessageBox.cs delete mode 100644 LightBulb/Utils/NativeMethods.cs diff --git a/LightBulb.WindowsApi/MessageBox.cs b/LightBulb.WindowsApi/MessageBox.cs new file mode 100644 index 00000000..1ce5d09a --- /dev/null +++ b/LightBulb.WindowsApi/MessageBox.cs @@ -0,0 +1,7 @@ +namespace LightBulb.WindowsApi; + +public static class MessageBox +{ + public static bool ShowError(string title, string message) => + NativeMethods.MessageBox(0, message, title, 0x10) == 0; +} diff --git a/LightBulb.WindowsApi/NativeMethods.User32.cs b/LightBulb.WindowsApi/NativeMethods.User32.cs index f9f6035b..88285b60 100644 --- a/LightBulb.WindowsApi/NativeMethods.User32.cs +++ b/LightBulb.WindowsApi/NativeMethods.User32.cs @@ -71,4 +71,7 @@ uint dwFlags [DllImport(User32, SetLastError = true)] public static extern bool UnhookWinEvent(nint hWinEventHook); + + [DllImport(User32, CharSet = CharSet.Auto)] + public static extern int MessageBox(nint hWnd, string text, string caption, uint type); } diff --git a/LightBulb/App.axaml.cs b/LightBulb/App.axaml.cs index ed1caff6..9db9a811 100644 --- a/LightBulb/App.axaml.cs +++ b/LightBulb/App.axaml.cs @@ -126,7 +126,7 @@ private void DisableTemporarily1MinuteMenuItem_OnClick(object? sender, EventArgs _mainViewModel.Dashboard.DisableTemporarilyCommand.Execute(TimeSpan.FromMinutes(1)); private void ExitMenuItem_OnClick(object? sender, EventArgs args) => - ApplicationLifetime?.TryShutdown(); + ApplicationLifetime?.Shutdown(); public void Dispose() => _services.Dispose(); } diff --git a/LightBulb/Program.cs b/LightBulb/Program.cs index 8257fd63..8a3c5373 100644 --- a/LightBulb/Program.cs +++ b/LightBulb/Program.cs @@ -3,7 +3,7 @@ using System.Reflection; using System.Threading; using Avalonia; -using LightBulb.Utils; +using LightBulb.WindowsApi; namespace LightBulb; @@ -49,9 +49,7 @@ out var isOnlyRunningInstance } catch (Exception ex) { - if (OperatingSystem.IsWindows()) - _ = NativeMethods.Windows.MessageBox(0, ex.ToString(), "Fatal Error", 0x10); - + MessageBox.ShowError("Fatal Error", ex.ToString()); throw; } finally diff --git a/LightBulb/Services/SettingsService.cs b/LightBulb/Services/SettingsService.cs index 637e022e..d026bc15 100644 --- a/LightBulb/Services/SettingsService.cs +++ b/LightBulb/Services/SettingsService.cs @@ -27,7 +27,7 @@ public partial class SettingsService() : SettingsBase(GetFilePath()) new( RegistryHive.CurrentUser, @"Software\Microsoft\Windows\CurrentVersion\Run", - "LightBulb", + Program.Name, $"\"{Program.ExecutableFilePath}\" {StartupOptions.IsInitiallyHiddenArgument}" ); @@ -183,7 +183,7 @@ private static string GetFilePath() { return Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - "LightBulb", + Program.Name, "Settings.json" ); } diff --git a/LightBulb/Services/UpdateService.cs b/LightBulb/Services/UpdateService.cs index caa43677..d3c7d1c0 100644 --- a/LightBulb/Services/UpdateService.cs +++ b/LightBulb/Services/UpdateService.cs @@ -57,9 +57,7 @@ public void FinalizePendingUpdates() return; _updateManager.LaunchUpdater(lastPreparedUpdate); - - if (Application.Current?.ApplicationLifetime?.TryShutdown(2) != true) - Environment.Exit(2); + Application.Current?.ApplicationLifetime?.Shutdown(2); } catch (UpdaterAlreadyLaunchedException) { diff --git a/LightBulb/StartupOptions.cs b/LightBulb/StartupOptions.cs index 1ed51fa3..de81eecf 100644 --- a/LightBulb/StartupOptions.cs +++ b/LightBulb/StartupOptions.cs @@ -13,6 +13,9 @@ public partial class StartupOptions { public static string IsInitiallyHiddenArgument { get; } = "--start-hidden"; + public static StartupOptions Current { get; } = + Parse(Environment.GetCommandLineArgs().Skip(1).ToArray()); + public static StartupOptions Parse(IReadOnlyList commandLineArgs) => new() { @@ -22,9 +25,3 @@ public static StartupOptions Parse(IReadOnlyList commandLineArgs) => ) }; } - -public partial class StartupOptions -{ - public static StartupOptions Current { get; } = - Parse(Environment.GetCommandLineArgs().Skip(1).ToArray()); -} diff --git a/LightBulb/Utils/Extensions/AvaloniaExtensions.cs b/LightBulb/Utils/Extensions/AvaloniaExtensions.cs index 81ae4438..c89de95e 100644 --- a/LightBulb/Utils/Extensions/AvaloniaExtensions.cs +++ b/LightBulb/Utils/Extensions/AvaloniaExtensions.cs @@ -1,4 +1,5 @@ -using Avalonia.Controls; +using System; +using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; namespace LightBulb.Utils.Extensions; @@ -13,19 +14,20 @@ internal static class AvaloniaExtensions return null; } - public static bool TryShutdown(this IApplicationLifetime lifetime, int exitCode = 0) + public static void Shutdown(this IApplicationLifetime lifetime, int exitCode = 0) { if (lifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime) { - return desktopLifetime.TryShutdown(exitCode); + desktopLifetime.TryShutdown(exitCode); + return; } if (lifetime is IControlledApplicationLifetime controlledLifetime) { controlledLifetime.Shutdown(exitCode); - return true; + return; } - return false; + Environment.Exit(exitCode); } } diff --git a/LightBulb/Utils/Extensions/NotifyPropertyChangedExtensions.cs b/LightBulb/Utils/Extensions/NotifyPropertyChangedExtensions.cs index a57926b2..b91edf17 100644 --- a/LightBulb/Utils/Extensions/NotifyPropertyChangedExtensions.cs +++ b/LightBulb/Utils/Extensions/NotifyPropertyChangedExtensions.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; +using System.Linq; using System.Linq.Expressions; using System.Reactive.Disposables; using System.Reflection; @@ -10,15 +12,20 @@ namespace LightBulb.Utils.Extensions; internal static class NotifyPropertyChangedExtensions { - public static IDisposable WatchProperty( + public static IDisposable WatchProperty( this TOwner owner, - Expression> propertyExpression, + Expression> propertyExpression, Action handle, bool watchInitialValue = true ) where TOwner : INotifyPropertyChanged { - if (propertyExpression.Body is not MemberExpression { Member: PropertyInfo property }) + var memberExpression = + propertyExpression.Body as MemberExpression + // If the property is not of type object, it will be wrapped in an unary conversion expression + ?? (propertyExpression.Body as UnaryExpression)?.Operand as MemberExpression; + + if (memberExpression?.Member is not PropertyInfo property) throw new ArgumentException("Provided expression must reference a property."); void OnPropertyChanged(object? sender, PropertyChangedEventArgs args) @@ -40,6 +47,21 @@ void OnPropertyChanged(object? sender, PropertyChangedEventArgs args) return Disposable.Create(() => owner.PropertyChanged -= OnPropertyChanged); } + public static IDisposable WatchProperties( + this TOwner owner, + IReadOnlyList>> propertyExpressions, + Action handle, + bool watchInitialValue = true + ) + where TOwner : INotifyPropertyChanged + { + var watchers = propertyExpressions + .Select(x => WatchProperty(owner, x, handle, watchInitialValue)) + .ToArray(); + + return Disposable.Create(() => watchers.DisposeAll()); + } + public static IDisposable WatchAllProperties( this TOwner owner, Action handle, diff --git a/LightBulb/Utils/NativeMethods.cs b/LightBulb/Utils/NativeMethods.cs deleted file mode 100644 index 109f4a6e..00000000 --- a/LightBulb/Utils/NativeMethods.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Runtime.InteropServices; - -namespace LightBulb.Utils; - -internal static class NativeMethods -{ - public static class Windows - { - [DllImport("user32.dll", CharSet = CharSet.Auto)] - public static extern int MessageBox(nint hWnd, string text, string caption, uint type); - } -} diff --git a/LightBulb/ViewModels/Components/DashboardViewModel.cs b/LightBulb/ViewModels/Components/DashboardViewModel.cs index e28e3255..f5d1724a 100644 --- a/LightBulb/ViewModels/Components/DashboardViewModel.cs +++ b/LightBulb/ViewModels/Components/DashboardViewModel.cs @@ -93,6 +93,21 @@ ExternalApplicationService externalApplicationService ) ); + _eventRoot.Add( + // Re-register hotkeys when they get updated + settingsService.WatchProperties( + [ + o => o.ToggleHotKey, + o => o.IncreaseTemperatureOffsetHotKey, + o => o.DecreaseTemperatureOffsetHotKey, + o => o.IncreaseBrightnessOffsetHotKey, + o => o.DecreaseBrightnessOffsetHotKey, + o => o.ResetConfigurationOffsetHotKey + ], + RegisterHotKeys + ) + ); + _updateConfigurationTimer = new Timer(TimeSpan.FromMilliseconds(50), UpdateConfiguration); _updateInstantTimer = new Timer(TimeSpan.FromMilliseconds(50), UpdateInstant); _updateIsPausedTimer = new Timer(TimeSpan.FromSeconds(1), UpdateIsPaused); diff --git a/LightBulb/ViewModels/MainViewModel.cs b/LightBulb/ViewModels/MainViewModel.cs index 62b9efcc..d70ecf4e 100644 --- a/LightBulb/ViewModels/MainViewModel.cs +++ b/LightBulb/ViewModels/MainViewModel.cs @@ -112,14 +112,9 @@ private async Task InitializeAsync() } [RelayCommand] - private async Task ShowSettingsAsync() - { + private async Task ShowSettingsAsync() => await dialogManager.ShowDialogAsync(viewModelManager.CreateSettingsViewModel()); - // Re-initialize timers, hotkeys, and other stateful components - Dashboard.InitializeCommand.Execute(null); - } - [RelayCommand] private void ShowAbout() => ProcessEx.StartShellExecute(Program.ProjectUrl); diff --git a/LightBulb/Views/MainView.axaml b/LightBulb/Views/MainView.axaml index 439b0c95..b56ccd54 100644 --- a/LightBulb/Views/MainView.axaml +++ b/LightBulb/Views/MainView.axaml @@ -7,7 +7,7 @@ xmlns:materialIcons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia" xmlns:viewModels="clr-namespace:LightBulb.ViewModels" x:Name="Window" - Title="LightBulb" + Title="{x:Static lightBulb:Program.Name}" Width="450" Height="350" CanResize="False"