From 51a2becce3337289a21ce27a4d96cd9f9a23fd82 Mon Sep 17 00:00:00 2001 From: Kamil Cholewa Date: Tue, 23 Jul 2024 08:56:37 +0200 Subject: [PATCH 1/8] Adding localization feature. Adding Polish language. --- MSI.Keyboard.Illuminator/App.axaml | 3 +- MSI.Keyboard.Illuminator/App.axaml.cs | 20 +- .../MSI.Keyboard.Illuminator.csproj | 18 +- .../Resources/Resources.Designer.cs | 328 ++++++++++++++++++ .../Resources/Resources.pl-PL.resx | 211 +++++++++++ .../Resources/Resources.resx | 211 +++++++++++ .../ViewModels/ColorProfilesViewModel.cs | 2 +- .../ViewModels/TrayViewModel.cs | 8 +- .../Views/ColorProfileView.axaml | 16 +- .../Views/ColorProfilesView.axaml | 23 +- .../Views/ColorProfilesWindow.axaml | 5 +- .../Views/MessageWindow.axaml | 6 +- README.md | 1 + 13 files changed, 813 insertions(+), 39 deletions(-) create mode 100644 MSI.Keyboard.Illuminator/Resources/Resources.Designer.cs create mode 100644 MSI.Keyboard.Illuminator/Resources/Resources.pl-PL.resx create mode 100644 MSI.Keyboard.Illuminator/Resources/Resources.resx diff --git a/MSI.Keyboard.Illuminator/App.axaml b/MSI.Keyboard.Illuminator/App.axaml index bbb41bd..f089ada 100644 --- a/MSI.Keyboard.Illuminator/App.axaml +++ b/MSI.Keyboard.Illuminator/App.axaml @@ -1,6 +1,7 @@ @@ -17,7 +18,7 @@ diff --git a/MSI.Keyboard.Illuminator/App.axaml.cs b/MSI.Keyboard.Illuminator/App.axaml.cs index a4a7213..a43c537 100644 --- a/MSI.Keyboard.Illuminator/App.axaml.cs +++ b/MSI.Keyboard.Illuminator/App.axaml.cs @@ -62,10 +62,8 @@ protected void WarnIfDeviceIsNotSupported() return; WindowHelper.ShowMessageWindow( - "MSI keyboard not found!", - "The supported \"MSI EPF USB\" SteelSeries keyboard has not been found!" + - Environment.NewLine + - "Exit an application as it is is not going to work properly anyway."); + Illuminator.Resources.Resources.DeviceNotFoundErrorTitle, + Illuminator.Resources.Resources.DeviceNotFoundErrorMessage); } protected static IAppSettingsManager GetAppSettingsManager(params string[] args) @@ -73,7 +71,7 @@ protected static IAppSettingsManager GetAppSettingsManager(params string[] args) var fileOption = new Option( name: "--settings", getDefaultValue: () => new FileInfo("appsettings.xml"), - description: "A full path to the settings file."); + description: Illuminator.Resources.Resources.SettingsParameterDescription); var settingsFile = fileOption.Parse(args).GetValueForOption(fileOption); @@ -92,11 +90,11 @@ protected void InitializeSettings() { // supress and use default settings } - catch (Exception ex) + catch (Exception) { WindowHelper.ShowMessageWindow( - "Loading application settings failed!", - ex.Message); + Illuminator.Resources.Resources.AppSettingsLoadingErrorTitle, + Illuminator.Resources.Resources.AppSettingsLoadingErrorMessage); } } @@ -106,11 +104,11 @@ protected void FinalizeSettings() { appSettingsManager.SaveSettings(); } - catch (Exception ex) + catch (Exception) { WindowHelper.ShowMessageWindow( - "Saving application settings failed!", - ex.Message); + Illuminator.Resources.Resources.AppSettingsSavingErrorTitle, + Illuminator.Resources.Resources.AppSettingsSavingErrorMessage); } } } \ No newline at end of file diff --git a/MSI.Keyboard.Illuminator/MSI.Keyboard.Illuminator.csproj b/MSI.Keyboard.Illuminator/MSI.Keyboard.Illuminator.csproj index ad7d997..59b564f 100644 --- a/MSI.Keyboard.Illuminator/MSI.Keyboard.Illuminator.csproj +++ b/MSI.Keyboard.Illuminator/MSI.Keyboard.Illuminator.csproj @@ -20,10 +20,6 @@ false - - - - @@ -35,4 +31,18 @@ + + + + + True + True + Resources.resx + + + PublicResXFileCodeGenerator + Resources.Designer.cs + + + diff --git a/MSI.Keyboard.Illuminator/Resources/Resources.Designer.cs b/MSI.Keyboard.Illuminator/Resources/Resources.Designer.cs new file mode 100644 index 0000000..2ef9d07 --- /dev/null +++ b/MSI.Keyboard.Illuminator/Resources/Resources.Designer.cs @@ -0,0 +1,328 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace MSI.Keyboard.Illuminator.Resources { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MSI.Keyboard.Illuminator.Resources.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Loading of an application settings has failed! + ///Make sure a file is not corrupted.. + /// + public static string AppSettingsLoadingErrorMessage { + get { + return ResourceManager.GetString("AppSettingsLoadingErrorMessage", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Loading application settings failed!. + /// + public static string AppSettingsLoadingErrorTitle { + get { + return ResourceManager.GetString("AppSettingsLoadingErrorTitle", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Saving of an application settings has failed! + ///Make sure a target directory exists and You have an access to it.. + /// + public static string AppSettingsSavingErrorMessage { + get { + return ResourceManager.GetString("AppSettingsSavingErrorMessage", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Saving application settings failed!. + /// + public static string AppSettingsSavingErrorTitle { + get { + return ResourceManager.GetString("AppSettingsSavingErrorTitle", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Change MSI keyboard colors!. + /// + public static string AppTrayIconTooltipText { + get { + return ResourceManager.GetString("AppTrayIconTooltipText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Changing keyboard colors has failed! + ///Make sure the supported "MSI EPF USB" SteelSeries keyboard is connected!. + /// + public static string ColorChangeErrorMessage { + get { + return ResourceManager.GetString("ColorChangeErrorMessage", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Changing keyboard colors failed!. + /// + public static string ColorChangeErrorTitle { + get { + return ResourceManager.GetString("ColorChangeErrorTitle", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Add new color profile. + /// + public static string ColorProfileAddTooltipText { + get { + return ResourceManager.GetString("ColorProfileAddTooltipText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Blinking. + /// + public static string ColorProfileBlinkingModeLabelText { + get { + return ResourceManager.GetString("ColorProfileBlinkingModeLabelText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Center. + /// + public static string ColorProfileCenterLabelText { + get { + return ResourceManager.GetString("ColorProfileCenterLabelText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Left. + /// + public static string ColorProfileLeftLabelText { + get { + return ResourceManager.GetString("ColorProfileLeftLabelText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Move selected color profile down. + /// + public static string ColorProfileMoveDownTooltipText { + get { + return ResourceManager.GetString("ColorProfileMoveDownTooltipText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Move selected color profile up. + /// + public static string ColorProfileMoveUpTooltipText { + get { + return ResourceManager.GetString("ColorProfileMoveUpTooltipText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Name. + /// + public static string ColorProfileNameLabelText { + get { + return ResourceManager.GetString("ColorProfileNameLabelText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Remove selected color profile. + /// + public static string ColorProfileRemoveTooltipText { + get { + return ResourceManager.GetString("ColorProfileRemoveTooltipText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Right. + /// + public static string ColorProfileRightLabelText { + get { + return ResourceManager.GetString("ColorProfileRightLabelText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Profiles. + /// + public static string ColorProfilesButtonText { + get { + return ResourceManager.GetString("ColorProfilesButtonText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Cancel. + /// + public static string ColorProfilesCancelButtonText { + get { + return ResourceManager.GetString("ColorProfilesCancelButtonText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Cancel all color profile changes. + /// + public static string ColorProfilesCancelButtonTooltipText { + get { + return ResourceManager.GetString("ColorProfilesCancelButtonTooltipText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Save. + /// + public static string ColorProfilesSaveButtonText { + get { + return ResourceManager.GetString("ColorProfilesSaveButtonText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Save all color profile changes. + /// + public static string ColorProfilesSaveButtonTooltipText { + get { + return ResourceManager.GetString("ColorProfilesSaveButtonTooltipText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Edit color profiles. + /// + public static string ColorProfilesWindowTitle { + get { + return ResourceManager.GetString("ColorProfilesWindowTitle", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Profile. + /// + public static string DefaultProfileName { + get { + return ResourceManager.GetString("DefaultProfileName", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The supported "MSI EPF USB" SteelSeries keyboard has not been found! + ///Exit an application as it is is not going to work properly anyway.. + /// + public static string DeviceNotFoundErrorMessage { + get { + return ResourceManager.GetString("DeviceNotFoundErrorMessage", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to MSI keyboard not found!. + /// + public static string DeviceNotFoundErrorTitle { + get { + return ResourceManager.GetString("DeviceNotFoundErrorTitle", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Exit. + /// + public static string ExitButtonText { + get { + return ResourceManager.GetString("ExitButtonText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Close. + /// + public static string MessageCloseButtonText { + get { + return ResourceManager.GetString("MessageCloseButtonText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Close a window. + /// + public static string MessageCloseButtonTooltipText { + get { + return ResourceManager.GetString("MessageCloseButtonTooltipText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A full path to the settings file.. + /// + public static string SettingsParameterDescription { + get { + return ResourceManager.GetString("SettingsParameterDescription", resourceCulture); + } + } + } +} diff --git a/MSI.Keyboard.Illuminator/Resources/Resources.pl-PL.resx b/MSI.Keyboard.Illuminator/Resources/Resources.pl-PL.resx new file mode 100644 index 0000000..fba0bf3 --- /dev/null +++ b/MSI.Keyboard.Illuminator/Resources/Resources.pl-PL.resx @@ -0,0 +1,211 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Ładowanie ustawień aplikacji się nie powiodło! +Upewnij się, że plik nie jest uszkodzony. + + + Błąd ładowania ustawień aplikacji! + + + Zapisywanie ustawień aplikacji się nie powiodło! +Upewnij się, że katalog docelowy istnieje oraz masz do niego uprawnienia. + + + Błąd zapisywania ustawień aplikacji! + + + Zmień kolory klawiatury MSI! + + + Zmiana kolorów klawiatury się nie powiodła! +Upewnij się, że klawiatura "MSI EPF USB" SteelSeries jest podłączona. + + + Błąd zmiany kolorów klawiatury! + + + Dodaj nowy profil kolorów + + + Miganie + + + Środkowy + + + Lewy + + + Przesuń zaznaczony profil kolorów w dół + + + Przesuń zaznaczony profil kolorów w górę + + + Nazwa + + + Usuń zaznaczony profil kolorów + + + Prawy + + + Profile + + + Anuluj + + + Anuluj zmiany w profilach kolorów + + + Zapisz + + + Zapisz zmiany w profilach kolorów + + + Edytuj profile kolorów + + + Profil + + + Klawiatura "MSI EPF USB" SteelSeries nie została wykryta! +Zamknij aplikację, gdyż i tak nie będzie działać poprawnie. + + + Nie wykryto klawiatury MSI! + + + Wyjdź + + + Zamknij + + + Zamknij okno + + + Pełna ścieżka do pliku z ustawieniami. + + \ No newline at end of file diff --git a/MSI.Keyboard.Illuminator/Resources/Resources.resx b/MSI.Keyboard.Illuminator/Resources/Resources.resx new file mode 100644 index 0000000..9bc06ef --- /dev/null +++ b/MSI.Keyboard.Illuminator/Resources/Resources.resx @@ -0,0 +1,211 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Loading of an application settings has failed! +Make sure a file is not corrupted. + + + Loading application settings failed! + + + Saving of an application settings has failed! +Make sure a target directory exists and You have an access to it. + + + Saving application settings failed! + + + Change MSI keyboard colors! + + + Changing keyboard colors has failed! +Make sure the supported "MSI EPF USB" SteelSeries keyboard is connected! + + + Changing keyboard colors failed! + + + Add new color profile + + + Blinking + + + Center + + + Left + + + Move selected color profile down + + + Move selected color profile up + + + Name + + + Remove selected color profile + + + Right + + + Profiles + + + Cancel + + + Cancel all color profile changes + + + Save + + + Save all color profile changes + + + Edit color profiles + + + Profile + + + The supported "MSI EPF USB" SteelSeries keyboard has not been found! +Exit an application as it is is not going to work properly anyway. + + + MSI keyboard not found! + + + Exit + + + Close + + + Close a window + + + A full path to the settings file. + + \ No newline at end of file diff --git a/MSI.Keyboard.Illuminator/ViewModels/ColorProfilesViewModel.cs b/MSI.Keyboard.Illuminator/ViewModels/ColorProfilesViewModel.cs index 9eeb84e..6be2e9a 100644 --- a/MSI.Keyboard.Illuminator/ViewModels/ColorProfilesViewModel.cs +++ b/MSI.Keyboard.Illuminator/ViewModels/ColorProfilesViewModel.cs @@ -64,7 +64,7 @@ public ColorProfilesViewModel( AddNewColorProfile = ReactiveCommand.Create(() => { var newColorProfile = ColorProfile.GetDefault(); - newColorProfile.Name = $"Profile {ColorProfileViewModels?.Count + 1 ?? 1}"; + newColorProfile.Name = $"{Resources.Resources.DefaultProfileName} {ColorProfileViewModels?.Count + 1 ?? 1}"; ColorProfileViewModels.Add(new ColorProfileViewModel(newColorProfile)); }); diff --git a/MSI.Keyboard.Illuminator/ViewModels/TrayViewModel.cs b/MSI.Keyboard.Illuminator/ViewModels/TrayViewModel.cs index 295c3fb..4b3c2b2 100644 --- a/MSI.Keyboard.Illuminator/ViewModels/TrayViewModel.cs +++ b/MSI.Keyboard.Illuminator/ViewModels/TrayViewModel.cs @@ -63,8 +63,8 @@ public TrayViewModel( SelectColorProfile.ThrownExceptions.Subscribe(ex => { WindowHelper.ShowMessageWindow( - "Changing keyboard colors failed!", - ex.Message); + Resources.Resources.ColorChangeErrorTitle, + Resources.Resources.ColorChangeErrorMessage); }); ShowColorProfiles = ReactiveCommand.Create(() => @@ -117,14 +117,14 @@ protected void GenerateTrayMenu() newTrayMenu.Add(new NativeMenuItem() { - Header = "Profiles", + Header = Resources.Resources.ColorProfilesButtonText, Command = ShowColorProfiles, Icon = AssetsHelper.GetImageFromAssets("palette16.png"), }); newTrayMenu.Add(new NativeMenuItemSeparator()); newTrayMenu.Add(new NativeMenuItem() { - Header = "Exit", + Header = Resources.Resources.ExitButtonText, Command = Exit, Icon = AssetsHelper.GetImageFromAssets("exit16.png"), }); diff --git a/MSI.Keyboard.Illuminator/Views/ColorProfileView.axaml b/MSI.Keyboard.Illuminator/Views/ColorProfileView.axaml index 44bafd6..78b88a2 100644 --- a/MSI.Keyboard.Illuminator/Views/ColorProfileView.axaml +++ b/MSI.Keyboard.Illuminator/Views/ColorProfileView.axaml @@ -3,6 +3,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="using:MSI.Keyboard.Illuminator.ViewModels" + xmlns:r="using:MSI.Keyboard.Illuminator.Resources" mc:Ignorable="d" x:Class="MSI.Keyboard.Illuminator.Views.ColorProfileView" x:DataType="vm:ColorProfileViewModel"> @@ -41,26 +42,31 @@ ColumnDefinitions="Auto,Auto" Margin="3"> - + - + - + - + - + diff --git a/MSI.Keyboard.Illuminator/Views/ColorProfilesView.axaml b/MSI.Keyboard.Illuminator/Views/ColorProfilesView.axaml index 4619c5b..a9234d1 100644 --- a/MSI.Keyboard.Illuminator/Views/ColorProfilesView.axaml +++ b/MSI.Keyboard.Illuminator/Views/ColorProfilesView.axaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:v="using:MSI.Keyboard.Illuminator.Views" xmlns:vm="using:MSI.Keyboard.Illuminator.ViewModels" + xmlns:r="using:MSI.Keyboard.Illuminator.Resources" mc:Ignorable="d" x:Class="MSI.Keyboard.Illuminator.Views.ColorProfilesView" x:DataType="vm:ColorProfilesViewModel"> @@ -23,7 +24,7 @@ + + + + + + + + Date: Tue, 27 Aug 2024 17:59:02 +0200 Subject: [PATCH 4/8] Fixing an issue with displaying a saving settings error dialog (application will NOT shut down until user closes error dialog). Refactoring a code related to saving setting. --- MSI.Keyboard.Illuminator/App.axaml.cs | 41 +--------- .../ViewModels/ColorProfilesViewModel.cs | 6 +- .../ViewModels/TrayViewModel.cs | 74 ++++++++++++++++--- 3 files changed, 69 insertions(+), 52 deletions(-) diff --git a/MSI.Keyboard.Illuminator/App.axaml.cs b/MSI.Keyboard.Illuminator/App.axaml.cs index a43c537..128fd84 100644 --- a/MSI.Keyboard.Illuminator/App.axaml.cs +++ b/MSI.Keyboard.Illuminator/App.axaml.cs @@ -8,7 +8,6 @@ using MSI.Keyboard.Illuminator.Services; using MSI.Keyboard.Illuminator.ViewModels; -using System; using System.CommandLine; using System.IO; @@ -35,16 +34,14 @@ public override void OnFrameworkInitializationCompleted() WarnIfDeviceIsNotSupported(); appSettingsManager = GetAppSettingsManager(application.Args); - InitializeSettings(); - application.Exit += (s, e) => FinalizeSettings(); var colorProfilesViewModel = new ColorProfilesViewModel(appSettingsManager); DataContext = new TrayViewModel( application, - colorProfilesViewModel, - keyboardService, - appSettingsManager); + keyboardService, + appSettingsManager, + colorProfilesViewModel); } base.OnFrameworkInitializationCompleted(); @@ -79,36 +76,4 @@ protected static IAppSettingsManager GetAppSettingsManager(params string[] args) return new AppSettingsManager(appSettingsStreamer); } - - protected void InitializeSettings() - { - try - { - appSettingsManager.LoadSettings(); - } - catch (FileNotFoundException) - { - // supress and use default settings - } - catch (Exception) - { - WindowHelper.ShowMessageWindow( - Illuminator.Resources.Resources.AppSettingsLoadingErrorTitle, - Illuminator.Resources.Resources.AppSettingsLoadingErrorMessage); - } - } - - protected void FinalizeSettings() - { - try - { - appSettingsManager.SaveSettings(); - } - catch (Exception) - { - WindowHelper.ShowMessageWindow( - Illuminator.Resources.Resources.AppSettingsSavingErrorTitle, - Illuminator.Resources.Resources.AppSettingsSavingErrorMessage); - } - } } \ No newline at end of file diff --git a/MSI.Keyboard.Illuminator/ViewModels/ColorProfilesViewModel.cs b/MSI.Keyboard.Illuminator/ViewModels/ColorProfilesViewModel.cs index 6be2e9a..e42d1b7 100644 --- a/MSI.Keyboard.Illuminator/ViewModels/ColorProfilesViewModel.cs +++ b/MSI.Keyboard.Illuminator/ViewModels/ColorProfilesViewModel.cs @@ -51,15 +51,13 @@ public ColorProfilesViewModel( Selection = new(); - LoadColorProfiles(); - Save = ReactiveCommand.Create(() => { appSettingsManager.UpdateColorProfiles( ColorProfileViewModels.Select(s => s.ColorProfile).Distinct()); }); - Cancel = ReactiveCommand.Create(LoadColorProfiles); + Cancel = ReactiveCommand.Create(() => { }); AddNewColorProfile = ReactiveCommand.Create(() => { @@ -110,7 +108,7 @@ void MoveSelectedColorProfile(bool moveUp) canMoveSelectedColorProfileDown); } - protected void LoadColorProfiles() + public void LoadColorProfiles() { var colorProfiles = appSettingsManager.GetColorProfiles() .Select(cp => new ColorProfileViewModel(cp)) diff --git a/MSI.Keyboard.Illuminator/ViewModels/TrayViewModel.cs b/MSI.Keyboard.Illuminator/ViewModels/TrayViewModel.cs index a732b36..6fdcd4b 100644 --- a/MSI.Keyboard.Illuminator/ViewModels/TrayViewModel.cs +++ b/MSI.Keyboard.Illuminator/ViewModels/TrayViewModel.cs @@ -10,6 +10,7 @@ using ReactiveUI; using System; +using System.IO; using System.Linq; using System.Reactive; using System.Reactive.Linq; @@ -18,6 +19,8 @@ namespace MSI.Keyboard.Illuminator.ViewModels; public class TrayViewModel : ReactiveObject { + protected readonly IClassicDesktopStyleApplicationLifetime application; + protected readonly IKeyboardService keyboardService; protected readonly IAppSettingsManager appSettingsManager; @@ -41,14 +44,17 @@ public NativeMenu TrayMenu public TrayViewModel( IClassicDesktopStyleApplicationLifetime application, - ColorProfilesViewModel colorProfilesViewModel, - IKeyboardService keyboardService, - IAppSettingsManager appSettingsManager) + IKeyboardService keyboardService, + IAppSettingsManager appSettingsManager, + ColorProfilesViewModel colorProfilesViewModel) { ColorProfilesViewModel = colorProfilesViewModel; + this.application = application; this.keyboardService = keyboardService; this.appSettingsManager = appSettingsManager; + InitializeSettings(); + SelectColorProfile = ReactiveCommand.CreateFromTask(async colorProfile => { var configuration = IlluminationConfigurationFactory @@ -69,24 +75,25 @@ public TrayViewModel( ShowColorProfiles = ReactiveCommand.Create(() => { - if (application.Windows.Any(w => w is ColorProfilesWindow)) + if (this.application.Windows.Any(w => w is ColorProfilesWindow)) return; - WindowHelper.ShowColorProfilesWindow(colorProfilesViewModel); + ColorProfilesViewModel.LoadColorProfiles(); + WindowHelper.ShowColorProfilesWindow(ColorProfilesViewModel); }); - Exit = ReactiveCommand.Create(() => application.Shutdown(0)); + Exit = ReactiveCommand.Create(() => FinalizeSettingsAndExit()); ColorProfilesViewModel.Save.Subscribe(x => { GenerateTrayMenu(); SelectColorProfileOnTrayMenu(appSettingsManager.GetActiveColorProfile()); - CloseAllColorProfilesWindows(application); + CloseAllColorProfilesWindows(); }); ColorProfilesViewModel.Cancel.Subscribe(x => - CloseAllColorProfilesWindows(application)); + CloseAllColorProfilesWindows()); GenerateTrayMenu(); @@ -151,8 +158,7 @@ protected void SelectColorProfileOnTrayMenu(ColorProfile colorProfile) /// /// Closes all windows. /// - protected static void CloseAllColorProfilesWindows( - IClassicDesktopStyleApplicationLifetime application) + protected void CloseAllColorProfilesWindows() { for (int i = application.Windows.Count - 1; i >= 0; i--) { @@ -160,4 +166,52 @@ protected static void CloseAllColorProfilesWindows( colorProfilesWindow?.Close(); } } + + /// + /// Initializes an application settings loading, shows error in case of any issues. + /// + protected void InitializeSettings() + { + try + { + appSettingsManager.LoadSettings(); + } + catch (FileNotFoundException) + { + // supress and use default settings + } + catch (Exception) + { + WindowHelper.ShowMessageWindow( + Resources.Resources.AppSettingsLoadingErrorTitle, + Resources.Resources.AppSettingsLoadingErrorMessage); + } + } + + /// + /// Initializes an application settings saving and shutdown, shows error in case of any issues. + /// + protected void FinalizeSettingsAndExit() + { + try + { + appSettingsManager.SaveSettings(); + application.Shutdown(0); + } + catch (Exception) + { + // in case of an issue show error window and postpone + // the shutdown until user closes that window + + var saveSettingsErrorWindow = WindowHelper.GetMessageWindow( + Resources.Resources.AppSettingsSavingErrorTitle, + Resources.Resources.AppSettingsSavingErrorMessage); + + saveSettingsErrorWindow.Closed += (_, _) => application.Shutdown(0); + saveSettingsErrorWindow.Show(); + + var emptyTrayMenu = new NativeMenu(); + TrayMenu = emptyTrayMenu; + } + } } From 77d4deadbbceee55946b0c858975641da79be4ed Mon Sep 17 00:00:00 2001 From: Kamil Cholewa Date: Tue, 27 Aug 2024 18:11:41 +0200 Subject: [PATCH 5/8] Simplifying message window code. --- .../ViewModels/MessageViewModel.cs | 10 +--------- MSI.Keyboard.Illuminator/Views/MessageWindow.axaml | 3 +-- MSI.Keyboard.Illuminator/Views/MessageWindow.axaml.cs | 3 +++ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/MSI.Keyboard.Illuminator/ViewModels/MessageViewModel.cs b/MSI.Keyboard.Illuminator/ViewModels/MessageViewModel.cs index c85bc0b..7485db9 100644 --- a/MSI.Keyboard.Illuminator/ViewModels/MessageViewModel.cs +++ b/MSI.Keyboard.Illuminator/ViewModels/MessageViewModel.cs @@ -1,8 +1,4 @@ -using Avalonia.Controls; - -using ReactiveUI; - -using System.Reactive; +using ReactiveUI; namespace MSI.Keyboard.Illuminator.ViewModels; @@ -24,13 +20,9 @@ public string Message set => this.RaiseAndSetIfChanged(ref message, value); } - public ReactiveCommand Close { get; } - public MessageViewModel(string title, string message) { Title = title; Message = message; - - Close = ReactiveCommand.Create(window => window?.Close()); } } diff --git a/MSI.Keyboard.Illuminator/Views/MessageWindow.axaml b/MSI.Keyboard.Illuminator/Views/MessageWindow.axaml index 0e5e4e0..c86de20 100644 --- a/MSI.Keyboard.Illuminator/Views/MessageWindow.axaml +++ b/MSI.Keyboard.Illuminator/Views/MessageWindow.axaml @@ -26,8 +26,7 @@ TextWrapping="Wrap"/>