From 399b3c39165505b71e220bb56b3d6fdf73685aff Mon Sep 17 00:00:00 2001 From: Patrick Demichiel Date: Sun, 17 Apr 2022 21:19:30 +0200 Subject: [PATCH] refactoring --- src/VolumeScroller/MainModel.cs | 26 +++++++---- src/VolumeScroller/MainViewModel.cs | 43 ++++++------------- .../{MainWindow.xaml => Window.xaml} | 6 ++- .../{MainWindow.xaml.cs => Window.xaml.cs} | 0 .../{Audio => Windows}/AudioController.cs | 0 .../AudioControllerNative.cs | 0 6 files changed, 35 insertions(+), 40 deletions(-) rename src/VolumeScroller/{MainWindow.xaml => Window.xaml} (94%) rename src/VolumeScroller/{MainWindow.xaml.cs => Window.xaml.cs} (100%) rename src/VolumeScroller/{Audio => Windows}/AudioController.cs (100%) rename src/VolumeScroller/{Audio => Windows}/AudioControllerNative.cs (100%) diff --git a/src/VolumeScroller/MainModel.cs b/src/VolumeScroller/MainModel.cs index 1e53c58..de8bcd9 100644 --- a/src/VolumeScroller/MainModel.cs +++ b/src/VolumeScroller/MainModel.cs @@ -1,13 +1,14 @@ -namespace VolumeScroller; +using Microsoft.Win32; + +namespace VolumeScroller; public class MainModel { public MainModel() { - ResetRunOnStartup(); } - public int Increment + public static int Increment { get => Properties.Settings.Default.Increment; set @@ -17,7 +18,7 @@ public int Increment } } - public bool TaskbarMustBeVisible + public static bool TaskbarMustBeVisible { get => Properties.Settings.Default.TaskbarMustBeVisible; set @@ -28,7 +29,19 @@ public bool TaskbarMustBeVisible } } - public bool RunOnStartup + public static string TaskBarIconPath => GetTaskbarIconPath(); + + private static string GetTaskbarIconPath() + { + RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); + var isLightTheme = (int)key.GetValue("SystemUsesLightTheme") == 1; + + return isLightTheme + ? "/Resources/VolumeScroller_light.ico" + : "/Resources/VolumeScroller_dark.ico"; + } + + public static bool RunOnStartup { get => Properties.Settings.Default.RunOnStartup; set @@ -38,7 +51,4 @@ public bool RunOnStartup new StartupManager(Process.GetCurrentProcess()).Set(value); } } - - public void ResetRunOnStartup() - => RunOnStartup = Properties.Settings.Default.RunOnStartup; } diff --git a/src/VolumeScroller/MainViewModel.cs b/src/VolumeScroller/MainViewModel.cs index 7bbd1b6..bd2f4be 100644 --- a/src/VolumeScroller/MainViewModel.cs +++ b/src/VolumeScroller/MainViewModel.cs @@ -4,53 +4,36 @@ namespace VolumeScroller; public class MainViewModel : ViewModelBase, IDisposable { - private readonly MainModel mainModel; private string taskBarIconPath; public MainViewModel(MainModel mainModel) - { - this.mainModel = mainModel; - TaskBarIconPath = GetTaskbarIconPath(); + { SystemEvents.UserPreferenceChanged += UserPreferenceChanged; } - public string TaskBarIconPath - { - get => taskBarIconPath; - set => SetProperty(ref taskBarIconPath, value); - } + public string TaskBarIconPath => MainModel.TaskBarIconPath; - public bool RunOnStartup + public static bool RunOnStartup { - get => mainModel.RunOnStartup; - set => mainModel.RunOnStartup = value; + get => MainModel.RunOnStartup; + set => MainModel.RunOnStartup = value; } - public bool TaskbarMustBeVisible + public static bool TaskbarMustBeVisible { - get => mainModel.TaskbarMustBeVisible; - set => mainModel.TaskbarMustBeVisible = value; + get => MainModel.TaskbarMustBeVisible; + set => MainModel.TaskbarMustBeVisible = value; } - public int Increment + public static int Increment { - get => mainModel.Increment * 2; - set => mainModel.Increment = value / 2; + get => MainModel.Increment * 2; + set => MainModel.Increment = value / 2; } public void Dispose() => SystemEvents.UserPreferenceChanged -= UserPreferenceChanged; - private void UserPreferenceChanged(object sender, EventArgs e) - => TaskBarIconPath = GetTaskbarIconPath(); - - private static string GetTaskbarIconPath() - { - RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); - var isLightTheme = (int)key.GetValue("SystemUsesLightTheme") == 1; - - return isLightTheme - ? "/Resources/VolumeScroller_light.ico" - : "/Resources/VolumeScroller_dark.ico"; - } + private void UserPreferenceChanged(object sender, EventArgs e) + => OnPropertyChanged(nameof(TaskBarIconPath)); } diff --git a/src/VolumeScroller/MainWindow.xaml b/src/VolumeScroller/Window.xaml similarity index 94% rename from src/VolumeScroller/MainWindow.xaml rename to src/VolumeScroller/Window.xaml index c39f481..b6d163a 100644 --- a/src/VolumeScroller/MainWindow.xaml +++ b/src/VolumeScroller/Window.xaml @@ -40,12 +40,13 @@ Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" /> -