From 5a07579b7300e71dc24736ae5732e66046742a6b Mon Sep 17 00:00:00 2001 From: Alekhya Date: Wed, 23 Sep 2020 13:20:32 -0700 Subject: [PATCH] Fix for settings crash on toggling enable button for any PowerToy (#6620) * Removed getSettings from moduleEnabled so that it doesn't have to reopen the file everytime * To Lower to while checking the theme * color picker doesn't reopen the file * use the generalView model config directly for FZ * Made the same change for all the viewmodels so that they access the general view model value directly instead of opening and reading the value from a file each time * removed unused variable * Fix initialization in tests * should read the file only if general settings does not exist * Added interfaces for all the powertoys to use the generalSettings singleton class * Runner is responding to changes in settings, only issue is that every time the general settings page is loaded the information is being read because isInitialized property could not be ignored during serialization * All tests pass * Settings and runner are working as expected with the settings cache * added a null check to read from the file only when the settings process is started * use converter to deserialize an interface * Renamed generalSettings within the cache to be called CommonSettingsConfig * All tests pass, had to initialize the common settings config instance * Added few comments to newly created classes * encapsulating load and store of general view model * reading from file is encapsulated * settings and runner wotk with generic singleton * Shortcut guide works as expected * Fancyzones, shortcutguide and power preview use the settings repository and work as expected * referencing GeneralSettings instead of the settingsRepository within viewmodels * unified access to General settings and removed the IGeneralSettingsData interface * Passing settings to viewmodel as a parameter * removed ISettingsConfig interface from the viewmodels which are not using the singleton to access settings * have to use ISettingsConfig to use GetSettings * refactored tests, all tests pass * Added test for settingsRepository that a single instance is created * Added comments and removed unnecessary headers/code * added settings repository tests * moq for each settings file * Img resizer tests pass * General tests pass * FancyZones tests pass * PowerPreview tests pass * PowerRename tests pass * shortcut guide tests pass * Added GetModuleName to ISettingsConfig * unify the way the Modulename is accessed. It was redeclared in multiple places and this would cause an issue if the name is changed only in one place. All the module names are accessed using the Settings.ModuleName, eg: ShortcutGuideSettings.ModuleName. * create PTRun settings file if it does not exist * GetFile is now a private function. Modified the logic of KBM default.json access and PT Run so that we can re-use GetSettings instead of GetFile. * Added UpgradeSettingsConfiguration to the ISettingsConfig interface so that the settings file can be upgraded based on some condition. Presently, only the GeneralSettings file is utilizing this to change the PT Version number based on the old PT version and the current PT version that it receives from the helper function. Verified that if the PT version is lower in the general settings.json file, settings saves the file with the new version info. * The naming for the PowerToys was inconsistent and the variables were redeclared in multiple places. To have the settings.ModuleName as the main name, all other places should refer to that name. In the tests file the module name for ImgResizer was 'ImageResizer' and not 'Image Resizer'. * renamed lock * Remove unnecessary GetSettingsFileNAme function. It is no longer in use because the code does not use types to create a new BasePTModule object --- .../ColorPickerSettings.cs | 16 ++++- .../FancyZonesSettings.cs | 20 +++++- .../GeneralSettings.cs | 32 ++++++++- .../ISettingsUtils.cs | 5 +- .../ImageResizerSettings.cs | 14 +++- .../Interface/ISettingsConfig.cs | 16 +++++ .../Interface/ISettingsRepository`1.cs | 11 +++ .../KeyboardManagerProfile.cs | 20 +++++- .../KeyboardManagerSettings.cs | 19 ++++-- .../KeysDataModel.cs | 6 ++ .../PowerLauncherSettings.cs | 16 ++++- .../PowerPreviewSettings.cs | 15 ++-- .../PowerRenameLocalProperties.cs | 16 ++++- .../PowerRenameSettings.cs | 14 +++- .../RemapKeysDataModel.cs | 6 ++ .../SettingsRepository`1.cs | 66 ++++++++++++++++++ .../SettingsUtils.cs | 31 ++++++++- .../ShortcutGuideSettings.cs | 14 +++- .../ShortcutsKeyDataModel.cs | 6 ++ .../ViewModels/ColorPickerViewModel.cs | 25 ++++--- .../ViewModels/FancyZonesViewModel.cs | 42 ++++-------- .../ViewModels/GeneralViewModel.cs | 61 ++++++----------- .../ViewModels/ImageResizerViewModel.cs | 32 ++++----- .../ViewModels/KeyboardManagerViewModel.cs | 46 +++++++------ .../ViewModels/PowerLauncherViewModel.cs | 28 ++++---- .../ViewModels/PowerPreviewViewModel.cs | 21 ++---- .../ViewModels/PowerRenameViewModel.cs | 30 +++----- .../ViewModels/ShortcutGuideViewModel.cs | 44 ++++-------- .../Mocks/ISettingsUtilsMocks.cs | 15 ++-- .../ModelsTests/BasePTSettingsTest.cs | 13 +++- .../ModelsTests/SettingsRepositoryTest.cs | 68 +++++++++++++++++++ .../ViewModelTests/ColorPicker.cs | 8 +-- .../ViewModelTests/FancyZones.cs | 47 ++++++++----- .../ViewModelTests/General.cs | 14 ++++ .../ViewModelTests/ImageResizer.cs | 50 ++++++++------ .../ViewModelTests/KeyboardManager.cs | 2 +- .../ViewModelTests/PowerPreview.cs | 14 +++- .../ViewModelTests/PowerRename.cs | 33 ++++++--- .../ViewModelTests/ShortcutGuide.cs | 19 ++++-- .../ModuleEnabledToForegroundConverter.cs | 6 +- .../Views/ColorPickerPage.xaml.cs | 2 +- .../Views/FancyZonesPage.xaml.cs | 2 +- .../Views/GeneralPage.xaml.cs | 1 + .../Views/ImageResizerPage.xaml.cs | 3 +- .../Views/KeyboardManagerPage.xaml.cs | 2 +- .../Views/PowerLauncherPage.xaml.cs | 2 +- .../Views/PowerPreviewPage.xaml.cs | 2 +- .../Views/PowerRenamePage.xaml.cs | 2 +- .../Views/ShellPage.xaml.cs | 2 + .../Views/ShortcutGuidePage.xaml.cs | 3 +- 50 files changed, 668 insertions(+), 314 deletions(-) create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/Interface/ISettingsConfig.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/Interface/ISettingsRepository`1.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsRepository`1.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ModelsTests/SettingsRepositoryTest.cs diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ColorPickerSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ColorPickerSettings.cs index 4e1a2be279..849bae1909 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ColorPickerSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ColorPickerSettings.cs @@ -2,14 +2,13 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Text.Json; using System.Text.Json.Serialization; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class ColorPickerSettings : BasePTModuleSettings + public class ColorPickerSettings : BasePTModuleSettings, ISettingsConfig { public const string ModuleName = "ColorPicker"; @@ -33,5 +32,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName); } + + public string GetModuleName() + { + return Name; + } + + // This can be utilized in the future if the settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/FancyZonesSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/FancyZonesSettings.cs index e3e49aa9f9..570b75cc97 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/FancyZonesSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/FancyZonesSettings.cs @@ -3,19 +3,33 @@ // See the LICENSE file in the project root for more information. using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class FancyZonesSettings : BasePTModuleSettings + public class FancyZonesSettings : BasePTModuleSettings, ISettingsConfig { + public const string ModuleName = "FancyZones"; + public FancyZonesSettings() { - Version = string.Empty; - Name = string.Empty; + Version = "1.0"; + Name = ModuleName; Properties = new FZConfigProperties(); } [JsonPropertyName("properties")] public FZConfigProperties Properties { get; set; } + + public string GetModuleName() + { + return Name; + } + + // This can be utilized in the future if the settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs index 6b8a4932b0..637e531516 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs @@ -2,12 +2,15 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Text.Json; using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; +using Microsoft.PowerToys.Settings.UI.Lib.Utilities; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class GeneralSettings + public class GeneralSettings : ISettingsConfig { // Gets or sets a value indicating whether packaged. [JsonPropertyName("packaged")] @@ -82,5 +85,32 @@ namespace Microsoft.PowerToys.Settings.UI.Lib { return interop.CommonManaged.GetProductVersion(); } + + // This function is to implement the ISettingsConfig interface. + // This interface helps in getting the settings configurations. + public string GetModuleName() + { + // The SettingsUtils functions access general settings when the module name is an empty string. + return string.Empty; + } + + public bool UpgradeSettingsConfiguration() + { + try + { + if (Helper.CompareVersions(PowertoysVersion, Helper.GetProductVersion()) < 0) + { + // Update settings + PowertoysVersion = Helper.GetProductVersion(); + return true; + } + } + catch (FormatException) + { + // If there is an issue with the version number format, don't migrate settings. + } + + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ISettingsUtils.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ISettingsUtils.cs index 3430b0b18c..c5083f3a16 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ISettingsUtils.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ISettingsUtils.cs @@ -2,11 +2,14 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using Microsoft.PowerToys.Settings.UI.Lib.Interface; + namespace Microsoft.PowerToys.Settings.UI.Lib { public interface ISettingsUtils { - T GetSettings(string powertoy = "", string fileName = "settings.json"); + T GetSettings(string powertoy = "", string fileName = "settings.json") + where T : ISettingsConfig, new(); void SaveSettings(string jsonSettings, string powertoy = "", string fileName = "settings.json"); diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerSettings.cs index fcf512f591..103d98f655 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ImageResizerSettings.cs @@ -4,10 +4,11 @@ using System.Text.Json; using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class ImageResizerSettings : BasePTModuleSettings + public class ImageResizerSettings : BasePTModuleSettings, ISettingsConfig { public const string ModuleName = "Image Resizer"; @@ -29,5 +30,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib }; return JsonSerializer.Serialize(this, options); } + + public string GetModuleName() + { + return Name; + } + + // This can be utilized in the future if the settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/Interface/ISettingsConfig.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Interface/ISettingsConfig.cs new file mode 100644 index 0000000000..fa0291568f --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Interface/ISettingsConfig.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.PowerToys.Settings.UI.Lib.Interface +{ + // Common interface to be implemented by all the objects which get and store settings properties. + public interface ISettingsConfig + { + string ToJsonString(); + + string GetModuleName(); + + bool UpgradeSettingsConfiguration(); + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/Interface/ISettingsRepository`1.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Interface/ISettingsRepository`1.cs new file mode 100644 index 0000000000..6caadb85f9 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Interface/ISettingsRepository`1.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.PowerToys.Settings.UI.Lib.Interface +{ + public interface ISettingsRepository + { + T SettingsConfig { get; set; } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeyboardManagerProfile.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeyboardManagerProfile.cs index 52a0c2d764..66246e2cc4 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeyboardManagerProfile.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeyboardManagerProfile.cs @@ -2,11 +2,13 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Text.Json; using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class KeyboardManagerProfile + public class KeyboardManagerProfile : ISettingsConfig { [JsonPropertyName("remapKeys")] public RemapKeysDataModel RemapKeys { get; set; } @@ -19,5 +21,21 @@ namespace Microsoft.PowerToys.Settings.UI.Lib RemapKeys = new RemapKeysDataModel(); RemapShortcuts = new ShortcutsKeyDataModel(); } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this); + } + + public string GetModuleName() + { + return KeyboardManagerSettings.ModuleName; + } + + // This can be utilized in the future if the default.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeyboardManagerSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeyboardManagerSettings.cs index 12ff998159..ffe032a47e 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeyboardManagerSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeyboardManagerSettings.cs @@ -3,11 +3,14 @@ // See the LICENSE file in the project root for more information. using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class KeyboardManagerSettings : BasePTModuleSettings + public class KeyboardManagerSettings : BasePTModuleSettings, ISettingsConfig { + public const string ModuleName = "Keyboard Manager"; + [JsonPropertyName("properties")] public KeyboardManagerProperties Properties { get; set; } @@ -15,14 +18,18 @@ namespace Microsoft.PowerToys.Settings.UI.Lib { Properties = new KeyboardManagerProperties(); Version = "1"; - Name = "_unset_"; + Name = ModuleName; } - public KeyboardManagerSettings(string ptName) + public string GetModuleName() { - Properties = new KeyboardManagerProperties(); - Version = "1"; - Name = ptName; + return Name; + } + + // This can be utilized in the future if the settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeysDataModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeysDataModel.cs index 286e5510ae..47f211b26d 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeysDataModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeysDataModel.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; +using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.PowerToys.Settings.UI.Lib.Utilities; @@ -35,5 +36,10 @@ namespace Microsoft.PowerToys.Settings.UI.Lib { return MapKeys(NewRemapKeys); } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this); + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerLauncherSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerLauncherSettings.cs index 7eddad108f..124657664c 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerLauncherSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerLauncherSettings.cs @@ -2,14 +2,13 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Text.Json; using System.Text.Json.Serialization; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class PowerLauncherSettings : BasePTModuleSettings + public class PowerLauncherSettings : BasePTModuleSettings, ISettingsConfig { public const string ModuleName = "PowerToys Run"; @@ -33,5 +32,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName); } + + public string GetModuleName() + { + return Name; + } + + // This can be utilized in the future if the settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewSettings.cs index afe67754f5..e56be7273d 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewSettings.cs @@ -3,10 +3,11 @@ // See the LICENSE file in the project root for more information. using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class PowerPreviewSettings : BasePTModuleSettings + public class PowerPreviewSettings : BasePTModuleSettings, ISettingsConfig { public const string ModuleName = "File Explorer"; @@ -20,11 +21,15 @@ namespace Microsoft.PowerToys.Settings.UI.Lib Name = ModuleName; } - public PowerPreviewSettings(string ptName) + public string GetModuleName() { - Properties = new PowerPreviewProperties(); - Version = "1"; - Name = ptName; + return Name; + } + + // This can be utilized in the future if the settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerRenameLocalProperties.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerRenameLocalProperties.cs index a0ba1966fe..3136edd2a6 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerRenameLocalProperties.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerRenameLocalProperties.cs @@ -3,10 +3,11 @@ // See the LICENSE file in the project root for more information. using System.Text.Json; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class PowerRenameLocalProperties + public class PowerRenameLocalProperties : ISettingsConfig { public PowerRenameLocalProperties() { @@ -51,5 +52,18 @@ namespace Microsoft.PowerToys.Settings.UI.Lib { return JsonSerializer.Serialize(this); } + + // This function is required to implement the ISettingsConfig interface and obtain the settings configurations. + public string GetModuleName() + { + string moduleName = PowerRenameSettings.ModuleName; + return moduleName; + } + + // This can be utilized in the future if the settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerRenameSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerRenameSettings.cs index a3be125bc5..0d7dc58a98 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerRenameSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerRenameSettings.cs @@ -3,10 +3,11 @@ // See the LICENSE file in the project root for more information. using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class PowerRenameSettings : BasePTModuleSettings + public class PowerRenameSettings : BasePTModuleSettings, ISettingsConfig { public const string ModuleName = "PowerRename"; @@ -39,5 +40,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib Version = "1"; Name = ptName; } + + public string GetModuleName() + { + return Name; + } + + // This can be utilized in the future if the power-rename-settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/RemapKeysDataModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/RemapKeysDataModel.cs index 4f359197ae..873d5d7d64 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/RemapKeysDataModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/RemapKeysDataModel.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Text.Json; using System.Text.Json.Serialization; namespace Microsoft.PowerToys.Settings.UI.Lib @@ -16,5 +17,10 @@ namespace Microsoft.PowerToys.Settings.UI.Lib { InProcessRemapKeys = new List(); } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this); + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsRepository`1.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsRepository`1.cs new file mode 100644 index 0000000000..3bb7df8518 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsRepository`1.cs @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + // This Singleton class is a wrapper around the settings configurations that are accessed by viewmodels. + // This class can have only one instance and therefore the settings configurations are common to all. + public class SettingsRepository : ISettingsRepository + where T : class, ISettingsConfig, new() + { + private static readonly object _SettingsRepoLock = new object(); + + private static ISettingsUtils _settingsUtils; + + private static SettingsRepository settingsRepository; + + private T settingsConfig; + + public static SettingsRepository GetInstance(ISettingsUtils settingsUtils) + { + // To ensure that only one instance of Settings Repository is created in a multi-threaded environment. + lock (_SettingsRepoLock) + { + if (settingsRepository == null) + { + settingsRepository = new SettingsRepository(); + _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); + } + + return settingsRepository; + } + } + + // The Singleton class must have a private constructor so that it cannot be instantiated by any other object other than itself. + private SettingsRepository() + { + } + + // Settings configurations shared across all viewmodels + public T SettingsConfig + { + get + { + if (settingsConfig == null) + { + T settingsItem = new T(); + settingsConfig = _settingsUtils.GetSettings(settingsItem.GetModuleName()); + } + + return settingsConfig; + } + + set + { + if (value != null) + { + settingsConfig = value; + } + } + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsUtils.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsUtils.cs index b0096bbb9c..c939f71536 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsUtils.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsUtils.cs @@ -4,6 +4,7 @@ using System; using System.Text.Json; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; using Microsoft.PowerToys.Settings.UI.Lib.Utilities; namespace Microsoft.PowerToys.Settings.UI.Lib @@ -59,16 +60,42 @@ namespace Microsoft.PowerToys.Settings.UI.Lib /// /// Get a Deserialized object of the json settings string. + /// This function creates a file in the powertoy folder if it does not exist and returns an object with default properties. /// /// Deserialized json settings object. public T GetSettings(string powertoy = DefaultModuleName, string fileName = DefaultFileName) + where T : ISettingsConfig, new() + { + if (SettingsExists(powertoy, fileName)) + { + // Given the file already exists, to deserialize the file and read it's content. + T deserializedSettings = GetFile(powertoy, fileName); + + // IF the file needs to be modified, to save the new configurations accordingly. + if (!deserializedSettings.UpgradeSettingsConfiguration()) + { + SaveSettings(deserializedSettings.ToJsonString(), powertoy, fileName); + } + + return deserializedSettings; + } + else + { + // If the settings file does not exist, to create a new object with default parameters and save it to a newly created settings file. + T newSettingsItem = new T(); + SaveSettings(newSettingsItem.ToJsonString(), powertoy, fileName); + return newSettingsItem; + } + } + + // Given the powerToy folder name and filename to be accessed, this function deserializes and returns the file. + private T GetFile(string powertoyFolderName = DefaultModuleName, string fileName = DefaultFileName) { // Adding Trim('\0') to overcome possible NTFS file corruption. // Look at issue https://github.com/microsoft/PowerToys/issues/6413 you'll see the file has a large sum of \0 to fill up a 4096 byte buffer for writing to disk // This, while not totally ideal, does work around the problem by trimming the end. // The file itself did write the content correctly but something is off with the actual end of the file, hence the 0x00 bug - var jsonSettingsString = _ioProvider.ReadAllText(GetSettingsPath(powertoy, fileName)).Trim('\0'); - + var jsonSettingsString = _ioProvider.ReadAllText(GetSettingsPath(powertoyFolderName, fileName)).Trim('\0'); return JsonSerializer.Deserialize(jsonSettingsString); } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutGuideSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutGuideSettings.cs index 6b5e41e631..343649fd80 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutGuideSettings.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutGuideSettings.cs @@ -3,10 +3,11 @@ // See the LICENSE file in the project root for more information. using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib { - public class ShortcutGuideSettings : BasePTModuleSettings + public class ShortcutGuideSettings : BasePTModuleSettings, ISettingsConfig { public const string ModuleName = "Shortcut Guide"; @@ -19,5 +20,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib Properties = new ShortcutGuideProperties(); Version = "1.0"; } + + public string GetModuleName() + { + return Name; + } + + // This can be utilized in the future if the settings.json file is to be modified/deleted. + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutsKeyDataModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutsKeyDataModel.cs index 0e1e131fe3..5195c6c1e9 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutsKeyDataModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutsKeyDataModel.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Text.Json; using System.Text.Json.Serialization; namespace Microsoft.PowerToys.Settings.UI.Lib @@ -20,5 +21,10 @@ namespace Microsoft.PowerToys.Settings.UI.Lib GlobalRemapShortcuts = new List(); AppSpecificRemapShortcuts = new List(); } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this); + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ColorPickerViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ColorPickerViewModel.cs index 6d49d42355..5df080b77d 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ColorPickerViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ColorPickerViewModel.cs @@ -5,20 +5,27 @@ using System; using System.Text.Json; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class ColorPickerViewModel : Observable { + private GeneralSettings GeneralSettingsConfig { get; set; } + private readonly ISettingsUtils _settingsUtils; + private ColorPickerSettings _colorPickerSettings; + private bool _isEnabled; private Func SendConfigMSG { get; } - public ColorPickerViewModel(ISettingsUtils settingsUtils, Func ipcMSGCallBackFunc) + public ColorPickerViewModel(ISettingsUtils settingsUtils, ISettingsRepository settingsRepository, Func ipcMSGCallBackFunc) { + // Obtain the general PowerToy settings configurations + GeneralSettingsConfig = settingsRepository.SettingsConfig; + _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); if (_settingsUtils.SettingsExists(ColorPickerSettings.ModuleName)) { @@ -29,11 +36,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels _colorPickerSettings = new ColorPickerSettings(); } - if (_settingsUtils.SettingsExists()) - { - var generalSettings = _settingsUtils.GetSettings(); - _isEnabled = generalSettings.Enabled.ColorPicker; - } + _isEnabled = GeneralSettingsConfig.Enabled.ColorPicker; // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; @@ -53,10 +56,10 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels _isEnabled = value; OnPropertyChanged(nameof(IsEnabled)); - // grab the latest version of settings - var generalSettings = _settingsUtils.GetSettings(); - generalSettings.Enabled.ColorPicker = value; - OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(generalSettings); + // Set the status of ColorPicker in the general settings + GeneralSettingsConfig.Enabled.ColorPicker = value; + OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig); + SendConfigMSG(outgoing.ToString()); } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/FancyZonesViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/FancyZonesViewModel.cs index 46929bb2a5..65eb24ad3b 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/FancyZonesViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/FancyZonesViewModel.cs @@ -6,16 +6,16 @@ using System; using System.Drawing; using System.Runtime.CompilerServices; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; using Microsoft.PowerToys.Settings.UI.Lib.ViewModels.Commands; namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class FancyZonesViewModel : Observable { - private readonly ISettingsUtils _settingsUtils; + private GeneralSettings GeneralSettingsConfig { get; set; } - private const string ModuleName = "FancyZones"; + private const string ModuleName = FancyZonesSettings.ModuleName; public ButtonClickCommand LaunchEditorEventHandler { get; set; } @@ -25,20 +25,14 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels private string settingsConfigFileFolder = string.Empty; - public FancyZonesViewModel(ISettingsUtils settingsUtils, Func ipcMSGCallBackFunc, string configFileSubfolder = "") + public FancyZonesViewModel(ISettingsRepository settingsRepository, ISettingsRepository moduleSettingsRepository, Func ipcMSGCallBackFunc, string configFileSubfolder = "") { + // To obtain the general settings configurations of PowerToys Settings. + GeneralSettingsConfig = settingsRepository.SettingsConfig; settingsConfigFileFolder = configFileSubfolder; - _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); - try - { - Settings = _settingsUtils.GetSettings(GetSettingsSubPath()); - } - catch - { - Settings = new FancyZonesSettings(); - _settingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath()); - } + // To obtain the settings configurations of Fancy zones. + Settings = moduleSettingsRepository.SettingsConfig; LaunchEditorEventHandler = new ButtonClickCommand(LaunchEditor); @@ -72,18 +66,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels string highlightColor = Settings.Properties.FancyzonesZoneHighlightColor.Value; _zoneHighlightColor = highlightColor != string.Empty ? highlightColor : "#0078D7"; - GeneralSettings generalSettings; - try - { - generalSettings = _settingsUtils.GetSettings(string.Empty); - } - catch - { - generalSettings = new GeneralSettings(); - _settingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty); - } - - _isEnabled = generalSettings.Enabled.FancyZones; + _isEnabled = GeneralSettingsConfig.Enabled.FancyZones; } private bool _isEnabled; @@ -121,9 +104,10 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels if (value != _isEnabled) { _isEnabled = value; - GeneralSettings generalSettings = _settingsUtils.GetSettings(string.Empty); - generalSettings.Enabled.FancyZones = value; - OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings); + + // Set the status of FancyZones in the general settings configuration + GeneralSettingsConfig.Enabled.FancyZones = value; + OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig); SendConfigMSG(snd.ToString()); OnPropertyChanged("IsEnabled"); diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/GeneralViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/GeneralViewModel.cs index 19864f120c..380fd28e8e 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/GeneralViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/GeneralViewModel.cs @@ -3,9 +3,9 @@ // See the LICENSE file in the project root for more information. using System; -using System.Diagnostics; using System.Runtime.CompilerServices; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; using Microsoft.PowerToys.Settings.UI.Lib.Utilities; using Microsoft.PowerToys.Settings.UI.Lib.ViewModels.Commands; @@ -13,9 +13,9 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class GeneralViewModel : Observable { - private readonly ISettingsUtils _settingsUtils; + private GeneralSettings GeneralSettingsConfig { get; set; } - private GeneralSettings GeneralSettingsConfigs { get; set; } + private readonly ISettingsUtils _settingsUtils; public ButtonClickCommand CheckFoUpdatesEventHandler { get; set; } @@ -35,33 +35,14 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels private string _settingsConfigFileFolder = string.Empty; - public GeneralViewModel(ISettingsUtils settingsUtils, string runAsAdminText, string runAsUserText, bool isElevated, bool isAdmin, Func updateTheme, Func ipcMSGCallBackFunc, Func ipcMSGRestartAsAdminMSGCallBackFunc, Func ipcMSGCheckForUpdatesCallBackFunc, string configFileSubfolder = "") + public GeneralViewModel(ISettingsUtils settingsUtils, ISettingsRepository settingsRepository, string runAsAdminText, string runAsUserText, bool isElevated, bool isAdmin, Func updateTheme, Func ipcMSGCallBackFunc, Func ipcMSGRestartAsAdminMSGCallBackFunc, Func ipcMSGCheckForUpdatesCallBackFunc, string configFileSubfolder = "") { CheckFoUpdatesEventHandler = new ButtonClickCommand(CheckForUpdates_Click); RestartElevatedButtonEventHandler = new ButtonClickCommand(Restart_Elevated); _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); - try - { - GeneralSettingsConfigs = _settingsUtils.GetSettings(string.Empty); - - if (Helper.CompareVersions(GeneralSettingsConfigs.PowertoysVersion, Helper.GetProductVersion()) < 0) - { - // Update settings - GeneralSettingsConfigs.PowertoysVersion = Helper.GetProductVersion(); - _settingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty); - } - } - catch (FormatException e) - { - // If there is an issue with the version number format, don't migrate settings. - Debug.WriteLine(e.Message); - } - catch - { - GeneralSettingsConfigs = new GeneralSettings(); - _settingsUtils.SaveSettings(GeneralSettingsConfigs.ToJsonString(), string.Empty); - } + // To obtain the general settings configuration of PowerToys if it exists, else to create a new file and return the default configurations. + GeneralSettingsConfig = settingsRepository.SettingsConfig; // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; @@ -70,12 +51,12 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels // set the callback function value to update the UI theme. UpdateUIThemeCallBack = updateTheme; - UpdateUIThemeCallBack(GeneralSettingsConfigs.Theme.ToLower()); + UpdateUIThemeCallBack(GeneralSettingsConfig.Theme.ToLower()); // Update Settings file folder: _settingsConfigFileFolder = configFileSubfolder; - switch (GeneralSettingsConfigs.Theme.ToLower()) + switch (GeneralSettingsConfig.Theme.ToLower()) { case "light": _isLightThemeRadioButtonChecked = true; @@ -88,10 +69,10 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels break; } - _startup = GeneralSettingsConfigs.Startup; - _autoDownloadUpdates = GeneralSettingsConfigs.AutoDownloadUpdates; + _startup = GeneralSettingsConfig.Startup; + _autoDownloadUpdates = GeneralSettingsConfig.AutoDownloadUpdates; _isElevated = isElevated; - _runElevated = GeneralSettingsConfigs.RunElevated; + _runElevated = GeneralSettingsConfig.RunElevated; RunningAsUserDefaultText = runAsUserText; RunningAsAdminDefaultText = runAsAdminText; @@ -142,7 +123,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels if (_startup != value) { _startup = value; - GeneralSettingsConfigs.Startup = value; + GeneralSettingsConfig.Startup = value; RaisePropertyChanged(); } } @@ -214,7 +195,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels if (_runElevated != value) { _runElevated = value; - GeneralSettingsConfigs.RunElevated = value; + GeneralSettingsConfig.RunElevated = value; RaisePropertyChanged(); } } @@ -241,7 +222,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels if (_autoDownloadUpdates != value) { _autoDownloadUpdates = value; - GeneralSettingsConfigs.AutoDownloadUpdates = value; + GeneralSettingsConfig.AutoDownloadUpdates = value; RaisePropertyChanged(); } } @@ -258,11 +239,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { if (value == true) { - GeneralSettingsConfigs.Theme = "dark"; + GeneralSettingsConfig.Theme = "dark"; _isDarkThemeRadioButtonChecked = value; try { - UpdateUIThemeCallBack(GeneralSettingsConfigs.Theme); + UpdateUIThemeCallBack(GeneralSettingsConfig.Theme); } catch { @@ -284,11 +265,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { if (value == true) { - GeneralSettingsConfigs.Theme = "light"; + GeneralSettingsConfig.Theme = "light"; _isLightThemeRadioButtonChecked = value; try { - UpdateUIThemeCallBack(GeneralSettingsConfigs.Theme); + UpdateUIThemeCallBack(GeneralSettingsConfig.Theme); } catch { @@ -310,11 +291,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { if (value == true) { - GeneralSettingsConfigs.Theme = "system"; + GeneralSettingsConfig.Theme = "system"; _isSystemThemeRadioButtonChecked = value; try { - UpdateUIThemeCallBack(GeneralSettingsConfigs.Theme); + UpdateUIThemeCallBack(GeneralSettingsConfig.Theme); } catch { @@ -355,7 +336,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { // Notify UI of property change OnPropertyChanged(propertyName); - OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(GeneralSettingsConfigs); + OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(GeneralSettingsConfig); SendConfigMSG(outsettings.ToString()); } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ImageResizerViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ImageResizerViewModel.cs index 5110092799..ea12e18629 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ImageResizerViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ImageResizerViewModel.cs @@ -7,24 +7,29 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class ImageResizerViewModel : Observable { + private GeneralSettings GeneralSettingsConfig { get; set; } + private readonly ISettingsUtils _settingsUtils; private ImageResizerSettings Settings { get; set; } - private const string ModuleName = "ImageResizer"; + private const string ModuleName = ImageResizerSettings.ModuleName; private Func SendConfigMSG { get; } - public ImageResizerViewModel(ISettingsUtils settingsUtils, Func ipcMSGCallBackFunc) + public ImageResizerViewModel(ISettingsUtils settingsUtils, ISettingsRepository settingsRepository, Func ipcMSGCallBackFunc) { _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); + // To obtain the general settings configurations of PowerToys. + GeneralSettingsConfig = settingsRepository.SettingsConfig; + try { Settings = _settingsUtils.GetSettings(ModuleName); @@ -35,22 +40,10 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels _settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName); } - GeneralSettings generalSettings; - - try - { - generalSettings = _settingsUtils.GetSettings(string.Empty); - } - catch - { - generalSettings = new GeneralSettings(); - _settingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty); - } - // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; - _isEnabled = generalSettings.Enabled.ImageResizer; + _isEnabled = GeneralSettingsConfig.Enabled.ImageResizer; _advancedSizes = Settings.Properties.ImageresizerSizes.Value; _jpegQualityLevel = Settings.Properties.ImageresizerJpegQualityLevel.Value; _pngInterlaceOption = Settings.Properties.ImageresizerPngInterlaceOption.Value; @@ -88,10 +81,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { if (value != _isEnabled) { + // To set the status of ImageResizer in the General PowerToys settings. _isEnabled = value; - GeneralSettings generalSettings = _settingsUtils.GetSettings(string.Empty); - generalSettings.Enabled.ImageResizer = value; - OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings); + GeneralSettingsConfig.Enabled.ImageResizer = value; + OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig); + SendConfigMSG(snd.ToString()); OnPropertyChanged("IsEnabled"); } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/KeyboardManagerViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/KeyboardManagerViewModel.cs index 7f2df05c49..0e6d36f519 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/KeyboardManagerViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/KeyboardManagerViewModel.cs @@ -4,11 +4,13 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Input; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; using Microsoft.PowerToys.Settings.UI.Lib.Utilities; using Microsoft.PowerToys.Settings.UI.Lib.ViewModels.Commands; @@ -16,9 +18,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class KeyboardManagerViewModel : Observable { + private GeneralSettings GeneralSettingsConfig { get; set; } + private readonly ISettingsUtils _settingsUtils; - private const string PowerToyName = "Keyboard Manager"; + private const string PowerToyName = KeyboardManagerSettings.ModuleName; private const string RemapKeyboardActionName = "RemapKeyboard"; private const string RemapKeyboardActionValue = "Open Remap Keyboard Window"; private const string EditShortcutActionName = "EditShortcut"; @@ -32,14 +36,15 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels private ICommand _remapKeyboardCommand; private ICommand _editShortcutCommand; private KeyboardManagerProfile _profile; - private GeneralSettings _generalSettings; private Func SendConfigMSG { get; } private Func, int> FilterRemapKeysList { get; } - public KeyboardManagerViewModel(ISettingsUtils settingsUtils, Func ipcMSGCallBackFunc, Func, int> filterRemapKeysList) + public KeyboardManagerViewModel(ISettingsUtils settingsUtils, ISettingsRepository settingsRepository, Func ipcMSGCallBackFunc, Func, int> filterRemapKeysList) { + GeneralSettingsConfig = settingsRepository.SettingsConfig; + // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; FilterRemapKeysList = filterRemapKeysList; @@ -59,35 +64,25 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels } else { - Settings = new KeyboardManagerSettings(PowerToyName); + Settings = new KeyboardManagerSettings(); _settingsUtils.SaveSettings(Settings.ToJsonString(), PowerToyName); } - - if (_settingsUtils.SettingsExists()) - { - _generalSettings = _settingsUtils.GetSettings(string.Empty); - } - else - { - _generalSettings = new GeneralSettings(); - _settingsUtils.SaveSettings(_generalSettings.ToJsonString(), string.Empty); - } } public bool Enabled { get { - return _generalSettings.Enabled.KeyboardManager; + return GeneralSettingsConfig.Enabled.KeyboardManager; } set { - if (_generalSettings.Enabled.KeyboardManager != value) + if (GeneralSettingsConfig.Enabled.KeyboardManager != value) { - _generalSettings.Enabled.KeyboardManager = value; + GeneralSettingsConfig.Enabled.KeyboardManager = value; OnPropertyChanged(nameof(Enabled)); - OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(_generalSettings); + OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig); SendConfigMSG(outgoing.ToString()); } @@ -177,8 +172,19 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels // update the UI element here. try { - _profile = _settingsUtils.GetSettings(PowerToyName, Settings.Properties.ActiveConfiguration.Value + JsonFileType); - FilterRemapKeysList(_profile.RemapKeys.InProcessRemapKeys); + string fileName = Settings.Properties.ActiveConfiguration.Value + JsonFileType; + + if (_settingsUtils.SettingsExists(PowerToyName, fileName)) + { + _profile = _settingsUtils.GetSettings(PowerToyName, fileName); + } + else + { + // The KBM process out of runner creates the default.json file if it does not exist. + success = false; + } + + FilterRemapKeysList(_profile?.RemapKeys?.InProcessRemapKeys); } finally { diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerLauncherViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerLauncherViewModel.cs index 73e15ae558..82af7c1b5d 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerLauncherViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerLauncherViewModel.cs @@ -6,16 +6,17 @@ using System; using System.Runtime.CompilerServices; using System.Text.Json; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class PowerLauncherViewModel : Observable { + private GeneralSettings GeneralSettingsConfig { get; set; } + private readonly ISettingsUtils _settingsUtils; private PowerLauncherSettings settings; - private GeneralSettings generalSettings; public delegate void SendCallback(PowerLauncherSettings settings); @@ -23,10 +24,13 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels private Func SendConfigMSG { get; } - public PowerLauncherViewModel(ISettingsUtils settingsUtils, Func ipcMSGCallBackFunc, int defaultKeyCode) + public PowerLauncherViewModel(ISettingsUtils settingsUtils, ISettingsRepository settingsRepository, Func ipcMSGCallBackFunc, int defaultKeyCode) { _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); + // To obtain the general Settings configurations of PowerToys + GeneralSettingsConfig = settingsRepository.SettingsConfig; + // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; callback = (PowerLauncherSettings settings) => @@ -35,6 +39,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels SendConfigMSG( string.Format("{{ \"powertoys\": {{ \"{0}\": {1} }} }}", PowerLauncherSettings.ModuleName, JsonSerializer.Serialize(settings))); }; + if (_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName)) { settings = _settingsUtils.GetSettings(PowerLauncherSettings.ModuleName); @@ -47,15 +52,6 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels settings.Properties.MaximumNumberOfResults = 4; callback(settings); } - - if (_settingsUtils.SettingsExists()) - { - generalSettings = _settingsUtils.GetSettings(); - } - else - { - generalSettings = new GeneralSettings(); - } } public PowerLauncherViewModel(PowerLauncherSettings settings, SendCallback callback) @@ -76,16 +72,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { get { - return generalSettings.Enabled.PowerLauncher; + return GeneralSettingsConfig.Enabled.PowerLauncher; } set { - if (generalSettings.Enabled.PowerLauncher != value) + if (GeneralSettingsConfig.Enabled.PowerLauncher != value) { - generalSettings.Enabled.PowerLauncher = value; + GeneralSettingsConfig.Enabled.PowerLauncher = value; OnPropertyChanged(nameof(EnablePowerLauncher)); - OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(generalSettings); + OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig); SendConfigMSG(outgoing.ToString()); } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerPreviewViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerPreviewViewModel.cs index 7415c8eb65..0689bc9ca3 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerPreviewViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerPreviewViewModel.cs @@ -5,15 +5,13 @@ using System; using System.Runtime.CompilerServices; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class PowerPreviewViewModel : Observable { - private readonly ISettingsUtils _settingsUtils; - - private const string ModuleName = "File Explorer"; + private const string ModuleName = PowerPreviewSettings.ModuleName; private PowerPreviewSettings Settings { get; set; } @@ -21,21 +19,14 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels private string _settingsConfigFileFolder = string.Empty; - public PowerPreviewViewModel(ISettingsUtils settingsUtils, Func ipcMSGCallBackFunc, string configFileSubfolder = "") + public PowerPreviewViewModel(ISettingsRepository moduleSettingsRepository, Func ipcMSGCallBackFunc, string configFileSubfolder = "") { // Update Settings file folder: _settingsConfigFileFolder = configFileSubfolder; - _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); - try - { - Settings = _settingsUtils.GetSettings(GetSettingsSubPath()); - } - catch - { - Settings = new PowerPreviewSettings(); - _settingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath()); - } + // To obtain the PowerPreview settings if it exists. + // If the file does not exist, to create a new one and return the default settings configurations. + Settings = moduleSettingsRepository.SettingsConfig; // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerRenameViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerRenameViewModel.cs index 727dfd676b..2a6aaa2bf3 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerRenameViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerRenameViewModel.cs @@ -5,15 +5,17 @@ using System; using System.Runtime.CompilerServices; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class PowerRenameViewModel : Observable { + private GeneralSettings GeneralSettingsConfig { get; set; } + private readonly ISettingsUtils _settingsUtils; - private const string ModuleName = "PowerRename"; + private const string ModuleName = PowerRenameSettings.ModuleName; private string _settingsConfigFileFolder = string.Empty; @@ -21,12 +23,14 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels private Func SendConfigMSG { get; } - public PowerRenameViewModel(ISettingsUtils settingsUtils, Func ipcMSGCallBackFunc, string configFileSubfolder = "") + public PowerRenameViewModel(ISettingsUtils settingsUtils, ISettingsRepository settingsRepository, Func ipcMSGCallBackFunc, string configFileSubfolder = "") { // Update Settings file folder: _settingsConfigFileFolder = configFileSubfolder; _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); + GeneralSettingsConfig = settingsRepository.SettingsConfig; + try { PowerRenameLocalProperties localSettings = _settingsUtils.GetSettings(GetSettingsSubPath(), "power-rename-settings.json"); @@ -47,19 +51,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels _powerRenameRestoreFlagsOnLaunch = Settings.Properties.PersistState.Value; _powerRenameMaxDispListNumValue = Settings.Properties.MaxMRUSize.Value; _autoComplete = Settings.Properties.MRUEnabled.Value; - - GeneralSettings generalSettings; - try - { - generalSettings = _settingsUtils.GetSettings(string.Empty); - } - catch - { - generalSettings = new GeneralSettings(); - _settingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty); - } - - _powerRenameEnabled = generalSettings.Enabled.PowerRename; + _powerRenameEnabled = GeneralSettingsConfig.Enabled.PowerRename; } private bool _powerRenameEnabled = false; @@ -80,9 +72,9 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { if (value != _powerRenameEnabled) { - GeneralSettings generalSettings = _settingsUtils.GetSettings(string.Empty); - generalSettings.Enabled.PowerRename = value; - OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings); + GeneralSettingsConfig.Enabled.PowerRename = value; + OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig); + SendConfigMSG(snd.ToString()); _powerRenameEnabled = value; diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ShortcutGuideViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ShortcutGuideViewModel.cs index 6c4f5d645e..94806854af 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ShortcutGuideViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ShortcutGuideViewModel.cs @@ -5,54 +5,38 @@ using System; using System.Runtime.CompilerServices; using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class ShortcutGuideViewModel : Observable { - private readonly ISettingsUtils _settingsUtils; + private GeneralSettings GeneralSettingsConfig { get; set; } private ShortcutGuideSettings Settings { get; set; } - private const string ModuleName = "Shortcut Guide"; + private const string ModuleName = ShortcutGuideSettings.ModuleName; private Func SendConfigMSG { get; } private string _settingsConfigFileFolder = string.Empty; - public ShortcutGuideViewModel(ISettingsUtils settingsUtils, Func ipcMSGCallBackFunc, string configFileSubfolder = "") + public ShortcutGuideViewModel(ISettingsRepository settingsRepository, ISettingsRepository moduleSettingsRepository, Func ipcMSGCallBackFunc, string configFileSubfolder = "") { // Update Settings file folder: _settingsConfigFileFolder = configFileSubfolder; - _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); - try - { - Settings = _settingsUtils.GetSettings(GetSettingsSubPath()); - } - catch - { - Settings = new ShortcutGuideSettings(); - _settingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath()); - } + // To obtain the general PowerToys settings. + GeneralSettingsConfig = settingsRepository.SettingsConfig; - GeneralSettings generalSettings; - - try - { - generalSettings = _settingsUtils.GetSettings(string.Empty); - } - catch - { - generalSettings = new GeneralSettings(); - _settingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty); - } + // To obtain the shortcut guide settings, if the file exists. + // If not, to create a file with the default settings and to return the default configurations. + Settings = moduleSettingsRepository.SettingsConfig; // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; - _isEnabled = generalSettings.Enabled.ShortcutGuide; + _isEnabled = GeneralSettingsConfig.Enabled.ShortcutGuide; _pressTime = Settings.Properties.PressTime.Value; _opacity = Settings.Properties.OverlayOpacity.Value; @@ -91,9 +75,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels if (value != _isEnabled) { _isEnabled = value; - GeneralSettings generalSettings = _settingsUtils.GetSettings(string.Empty); - generalSettings.Enabled.ShortcutGuide = value; - OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings); + + // To update the status of shortcut guide in General PowerToy settings. + GeneralSettingsConfig.Enabled.ShortcutGuide = value; + OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig); + SendConfigMSG(snd.ToString()); OnPropertyChanged("IsEnabled"); } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/Mocks/ISettingsUtilsMocks.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/Mocks/ISettingsUtilsMocks.cs index e1e200ce96..b1c47f15b1 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/Mocks/ISettingsUtilsMocks.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/Mocks/ISettingsUtilsMocks.cs @@ -1,24 +1,19 @@ using Microsoft.PowerToys.Settings.UI.Lib; -using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; using Moq; using System; -using System.Collections.Generic; -using System.Text; namespace Microsoft.PowerToys.Settings.UI.UnitTests.Mocks { internal static class ISettingsUtilsMocks { //Stubs out empty values for imageresizersettings and general settings as needed by the imageresizer viewmodel - internal static Mock GetStubSettingsUtils() + internal static Mock GetStubSettingsUtils() + where T : ISettingsConfig, new() { var settingsUtils = new Mock(); - settingsUtils.Setup(x => x.GetSettings(It.IsAny(), It.IsAny())) - .Returns(new InvocationFunc(invocation => - { - var typeArgument = invocation.Method.GetGenericArguments()[0]; - return Activator.CreateInstance(typeArgument); - })); + settingsUtils.Setup(x => x.GetSettings(It.IsAny(), It.IsAny())) + .Returns(new T()); return settingsUtils; } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ModelsTests/BasePTSettingsTest.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ModelsTests/BasePTSettingsTest.cs index 2bec4be724..023ee7e851 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ModelsTests/BasePTSettingsTest.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ModelsTests/BasePTSettingsTest.cs @@ -3,15 +3,26 @@ // See the LICENSE file in the project root for more information. using Microsoft.PowerToys.Settings.UI.Lib; +using Microsoft.PowerToys.Settings.UI.Lib.Interface; namespace Microsoft.PowerToys.Settings.UnitTest { - public class BasePTSettingsTest : BasePTModuleSettings + public class BasePTSettingsTest : BasePTModuleSettings, ISettingsConfig { public BasePTSettingsTest() { Name = string.Empty; Version = string.Empty; } + + public string GetModuleName() + { + return Name; + } + + public bool UpgradeSettingsConfiguration() + { + return false; + } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ModelsTests/SettingsRepositoryTest.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ModelsTests/SettingsRepositoryTest.cs new file mode 100644 index 0000000000..67aa719da8 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ModelsTests/SettingsRepositoryTest.cs @@ -0,0 +1,68 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using Microsoft.PowerToys.Settings.UI.Lib; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks; + +namespace CommonLibTest +{ + [TestClass] + public class SettingsRepositoryTest + { + private Task> GetSettingsRepository(ISettingsUtils settingsUtils) + { + + return Task.Run(() => + { + return SettingsRepository.GetInstance(settingsUtils); + }); + } + + + [TestMethod] + public void SettingsRepositoryInstanceWhenCalledMustReturnSameObject() + { + // The singleton class Settings Repository must always have a single instance + var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + + // Arrange and Act + SettingsRepository firstInstance = SettingsRepository.GetInstance(mockSettingsUtils.Object); + SettingsRepository secondInstance = SettingsRepository.GetInstance(mockSettingsUtils.Object); + + // Assert + Assert.IsTrue(object.ReferenceEquals(firstInstance, secondInstance)); + } + + [TestMethod] + public void SettingsRepositoryInstanceMustBeTheSameAcrossThreads() + { + // Multiple tasks try to access and initialize the settings repository class, however they must all access the same settings Repository object. + + // Arrange + var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + List>> settingsRepoTasks = new List>>(); + int numberOfTasks = 100; + + for(int i = 0; i < numberOfTasks; i++) + { + settingsRepoTasks.Add(GetSettingsRepository(mockSettingsUtils.Object)); + } + + // Act + Task.WaitAll(settingsRepoTasks.ToArray()); + + // Assert + for(int i=0; i< numberOfTasks-1; i++) + { + Assert.IsTrue(object.ReferenceEquals(settingsRepoTasks[i].Result, settingsRepoTasks[i + 1].Result)); + } + + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs index 0d31233992..4cb73efd6a 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs @@ -2,19 +2,13 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.IO; -using System.Text.Json; -using Microsoft.PowerToys.Settings.UI.Lib; -using Microsoft.PowerToys.Settings.UI.Lib.ViewModels; -using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; namespace ViewModelTests { [TestClass] public class ColorPicker { - + } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/FancyZones.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/FancyZones.cs index a4e1a4984a..c4cb98925e 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/FancyZones.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/FancyZones.cs @@ -20,6 +20,17 @@ namespace ViewModelTests { public const string FancyZonesTestFolderName = "Test\\FancyZones"; + private Mock mockGeneralSettingsUtils; + + private Mock mockFancyZonesSettingsUtils; + + [TestInitialize] + public void SetUp_StubSettingUtils() + { + mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + mockFancyZonesSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + } + [TestMethod] public void IsEnabled_ShouldDisableModule_WhenSuccessful() { @@ -31,7 +42,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsTrue(viewModel.IsEnabled); // check if the module is enabled. // act @@ -50,7 +61,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsTrue(viewModel.ShiftDrag); // check if value was initialized to false. // act @@ -69,7 +80,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.OverrideSnapHotkeys); // check if value was initialized to false. // act @@ -88,7 +99,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.MoveWindowsBasedOnPosition); // check if value was initialized to false. // act @@ -107,7 +118,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.MakeDraggedWindowsTransparent); // check if value was initialized to false. // act @@ -126,7 +137,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.MouseSwitch); // check if value was initialized to false. // act @@ -145,7 +156,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.DisplayChangeMoveWindows); // check if value was initialized to false. // act @@ -164,7 +175,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.ZoneSetChangeMoveWindows); // check if value was initialized to false. // act @@ -183,7 +194,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.AppLastZoneMoveWindows); // check if value was initialized to false. // act @@ -201,7 +212,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.OpenWindowOnActiveMonitor); // check if value was initialized to false. // act @@ -220,7 +231,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.RestoreSize); // check if value was initialized to false. // act @@ -239,7 +250,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsTrue(viewModel.UseCursorPosEditorStartupScreen); // check if value was initialized to false. // act @@ -258,7 +269,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.IsFalse(viewModel.ShowOnAllMonitors); // check if value was initialized to false. // act @@ -277,7 +288,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.AreEqual(ConfigDefaults.DefaultFancyZonesZoneHighlightColor, viewModel.ZoneHighlightColor); // act @@ -296,7 +307,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.AreEqual(ConfigDefaults.DefaultFancyzonesBorderColor, viewModel.ZoneBorderColor); // act @@ -315,7 +326,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.AreEqual(ConfigDefaults.DefaultFancyZonesInActiveColor, viewModel.ZoneInActiveColor); // act @@ -334,7 +345,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.AreEqual(string.Empty, viewModel.ExcludedApps); // act @@ -353,7 +364,7 @@ namespace ViewModelTests }; // arrange - FancyZonesViewModel viewModel = new FancyZonesViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, FancyZonesTestFolderName); + FancyZonesViewModel viewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockFancyZonesSettingsUtils.Object), SendMockIPCConfigMSG, FancyZonesTestFolderName); Assert.AreEqual(50, viewModel.HighlightOpacity); // act diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/General.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/General.cs index f696f24f97..04f724c131 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/General.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/General.cs @@ -8,6 +8,7 @@ using System.Text.Json; using Microsoft.PowerToys.Settings.UI.Lib; using Microsoft.PowerToys.Settings.UI.Lib.ViewModels; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.PowerToys.Settings.UI.UnitTests.Mocks; using Moq; using NuGet.Frameworks; @@ -18,6 +19,14 @@ namespace ViewModelTests { public const string generalSettings_file_name = "Test\\GenealSettings"; + private Mock mockGeneralSettingsUtils; + + [TestInitialize] + public void SetUp_StubSettingUtils() + { + mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + } + [TestMethod] public void IsElevated_ShouldUpdateRunasAdminStatusAttrs_WhenSuccessful() { @@ -27,6 +36,7 @@ namespace ViewModelTests Func SendCheckForUpdatesIPCMessage = msg => { return 0; }; GeneralViewModel viewModel = new GeneralViewModel( new Mock().Object, + SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), "GeneralSettings_RunningAsAdminText", "GeneralSettings_RunningAsUserText", false, @@ -64,6 +74,7 @@ namespace ViewModelTests Func SendCheckForUpdatesIPCMessage = msg => { return 0; }; GeneralViewModel viewModel = new GeneralViewModel( new Mock().Object, + SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), "GeneralSettings_RunningAsAdminText", "GeneralSettings_RunningAsUserText", false, @@ -96,6 +107,7 @@ namespace ViewModelTests // Arrange GeneralViewModel viewModel = new GeneralViewModel( new Mock().Object, + SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), "GeneralSettings_RunningAsAdminText", "GeneralSettings_RunningAsUserText", false, @@ -129,6 +141,7 @@ namespace ViewModelTests Func SendCheckForUpdatesIPCMessage = msg => { return 0; }; viewModel = new GeneralViewModel( new Mock().Object, + SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), "GeneralSettings_RunningAsAdminText", "GeneralSettings_RunningAsUserText", false, @@ -160,6 +173,7 @@ namespace ViewModelTests Func SendCheckForUpdatesIPCMessage = msg => { return 0; }; GeneralViewModel viewModel = new GeneralViewModel( new Mock().Object, + SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), "GeneralSettings_RunningAsAdminText", "GeneralSettings_RunningAsUserText", false, diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ImageResizer.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ImageResizer.cs index 0e4ef3e16f..c966b48153 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ImageResizer.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ImageResizer.cs @@ -18,13 +18,23 @@ namespace ViewModelTests [TestClass] public class ImageResizer { - public const string Module = "ImageResizer"; + // To have a consistent name. + public const string Module = ImageResizerSettings.ModuleName; + + private Mock mockGeneralSettingsUtils; + + private Mock mockImgResizerSettingsUtils; + + [TestInitialize] + public void SetUp_StubSettingUtils() + { + mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + mockImgResizerSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + } [TestMethod] public void IsEnabled_ShouldEnableModule_WhenSuccessful() { - var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); - // Assert Func SendMockIPCConfigMSG = msg => { @@ -34,7 +44,7 @@ namespace ViewModelTests }; // arrange - ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(mockImgResizerSettingsUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); // act viewModel.IsEnabled = true; @@ -47,13 +57,13 @@ namespace ViewModelTests var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists(); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object); Func SendMockIPCConfigMSG = msg => { return 0; }; - ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); // act viewModel.JPEGQualityLevel = 10; // Assert - viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); Assert.AreEqual(10, viewModel.JPEGQualityLevel); } @@ -64,13 +74,13 @@ namespace ViewModelTests var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists(); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object); Func SendMockIPCConfigMSG = msg => { return 0; }; - ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); // act viewModel.PngInterlaceOption = 10; // Assert - viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); Assert.AreEqual(10, viewModel.PngInterlaceOption); } @@ -81,13 +91,13 @@ namespace ViewModelTests var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists(); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object); Func SendMockIPCConfigMSG = msg => { return 0; }; - ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); // act viewModel.TiffCompressOption = 10; // Assert - viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); Assert.AreEqual(10, viewModel.TiffCompressOption); } @@ -98,14 +108,14 @@ namespace ViewModelTests var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists(); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object); Func SendMockIPCConfigMSG = msg => { return 0; }; - ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); string expectedValue = "%1 (%3)"; // act viewModel.FileName = expectedValue; // Assert - viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); Assert.AreEqual(expectedValue, viewModel.FileName); } @@ -113,7 +123,7 @@ namespace ViewModelTests public void KeepDateModified_ShouldUpdateValue_WhenSuccessful() { // arrange - var settingUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + var settingUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); var expectedSettingsString = new ImageResizerSettings() { Properties = new ImageResizerProperties() { ImageresizerKeepDateModified = new BoolProperty() { Value = true } } }.ToJsonString(); settingUtils.Setup(x => x.SaveSettings( @@ -123,7 +133,7 @@ namespace ViewModelTests .Verifiable(); Func SendMockIPCConfigMSG = msg => { return 0; }; - ImageResizerViewModel viewModel = new ImageResizerViewModel(settingUtils.Object, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(settingUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); // act viewModel.KeepDateModified = true; @@ -139,13 +149,13 @@ namespace ViewModelTests var mockIOProvider = IIOProviderMocks.GetMockIOProviderForSaveLoadExists(); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object); Func SendMockIPCConfigMSG = msg => { return 0; }; - ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); // act viewModel.Encoder = 3; // Assert - viewModel = new ImageResizerViewModel(mockSettingsUtils, SendMockIPCConfigMSG); + viewModel = new ImageResizerViewModel(mockSettingsUtils, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); Assert.AreEqual("163bcc30-e2e9-4f0b-961d-a3e9fdb788a3", viewModel.GetEncoderGuid(viewModel.Encoder)); Assert.AreEqual(3, viewModel.Encoder); } @@ -154,9 +164,9 @@ namespace ViewModelTests public void AddRow_ShouldAddEmptyImageSize_WhenSuccessful() { // arrange - var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); Func SendMockIPCConfigMSG = msg => { return 0; }; - ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); int sizeOfOriginalArray = viewModel.Sizes.Count; // act @@ -170,9 +180,9 @@ namespace ViewModelTests public void DeleteImageSize_ShouldDeleteImageSize_WhenSuccessful() { // arrange - var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); Func SendMockIPCConfigMSG = msg => { return 0; }; - ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SendMockIPCConfigMSG); + ImageResizerViewModel viewModel = new ImageResizerViewModel(mockSettingsUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG); int sizeOfOriginalArray = viewModel.Sizes.Count; ImageSize deleteCandidate = viewModel.Sizes.Where(x => x.Id == 0).First(); diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/KeyboardManager.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/KeyboardManager.cs index 970fe95512..9c730eba9a 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/KeyboardManager.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/KeyboardManager.cs @@ -13,7 +13,7 @@ namespace ViewModelTests [TestClass] public class KeyboardManager { - public const string Module = "Keyboard Manager"; + public const string Module = KeyboardManagerSettings.ModuleName; [TestInitialize] public void Setup() diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs index 7833d2ff7e..6b3a78ac2a 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs @@ -18,6 +18,14 @@ namespace ViewModelTests { public const string Module = "Test\\File Explorer"; + private Mock mockPowerPreviewSettingsUtils; + + [TestInitialize] + public void SetUp_StubSettingUtils() + { + mockPowerPreviewSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + } + [TestMethod] public void SVGRenderIsEnabled_ShouldPrevHandler_WhenSuccessful() { @@ -30,7 +38,7 @@ namespace ViewModelTests }; // arrange - PowerPreviewViewModel viewModel = new PowerPreviewViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, Module); + PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SettingsRepository.GetInstance(mockPowerPreviewSettingsUtils.Object), SendMockIPCConfigMSG, Module); // act viewModel.SVGRenderIsEnabled = true; @@ -48,7 +56,7 @@ namespace ViewModelTests }; // arrange - PowerPreviewViewModel viewModel = new PowerPreviewViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, Module); + PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SettingsRepository.GetInstance(mockPowerPreviewSettingsUtils.Object), SendMockIPCConfigMSG, Module); // act viewModel.SVGThumbnailIsEnabled = true; @@ -66,7 +74,7 @@ namespace ViewModelTests }; // arrange - PowerPreviewViewModel viewModel = new PowerPreviewViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, Module);; + PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SettingsRepository.GetInstance(mockPowerPreviewSettingsUtils.Object), SendMockIPCConfigMSG, Module); ; // act viewModel.MDRenderIsEnabled = true; diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerRename.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerRename.cs index 9f9a9d809e..41b614732e 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerRename.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerRename.cs @@ -16,9 +16,20 @@ namespace ViewModelTests [TestClass] public class PowerRename { - public const string ModuleName = "PowerRename"; + public const string ModuleName = PowerRenameSettings.ModuleName; public const string generalSettings_file_name = "Test\\PowerRename"; + private Mock mockGeneralSettingsUtils; + + private Mock mockPowerRenamePropertiesUtils; + + [TestInitialize] + public void SetUp_StubSettingUtils() + { + mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + mockPowerRenamePropertiesUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + } + [TestMethod] public void IsEnabled_ShouldEnableModule_WhenSuccessful() { @@ -31,7 +42,7 @@ namespace ViewModelTests }; // arrange - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); // act viewModel.IsEnabled = true; @@ -49,7 +60,7 @@ namespace ViewModelTests }; // arrange - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); // act viewModel.MRUEnabled = true; @@ -59,7 +70,7 @@ namespace ViewModelTests public void WhenIsEnabledIsOffAndMRUEnabledIsOffGlobalAndMruShouldBeOff() { Func SendMockIPCConfigMSG = msg => { return 0; }; - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); viewModel.IsEnabled = false; viewModel.MRUEnabled = false; @@ -71,7 +82,7 @@ namespace ViewModelTests public void WhenIsEnabledIsOffAndMRUEnabledIsOnGlobalAndMruShouldBeOff() { Func SendMockIPCConfigMSG = msg => { return 0; }; - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); viewModel.IsEnabled = false; viewModel.MRUEnabled = true; @@ -83,7 +94,7 @@ namespace ViewModelTests public void WhenIsEnabledIsOnAndMRUEnabledIsOffGlobalAndMruShouldBeOff() { Func SendMockIPCConfigMSG = msg => { return 0; }; - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); viewModel.IsEnabled = true; viewModel.MRUEnabled = false; @@ -95,7 +106,7 @@ namespace ViewModelTests public void WhenIsEnabledIsOnAndMRUEnabledIsOnGlobalAndMruShouldBeOn() { Func SendMockIPCConfigMSG = msg => { return 0; }; - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); viewModel.IsEnabled = true; viewModel.MRUEnabled = true; @@ -115,7 +126,7 @@ namespace ViewModelTests }; // arrange - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); // act viewModel.EnabledOnContextMenu = true; @@ -133,7 +144,7 @@ namespace ViewModelTests }; // arrange - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); // act viewModel.EnabledOnContextMenu = true; @@ -151,7 +162,7 @@ namespace ViewModelTests }; // arrange - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); // act viewModel.RestoreFlagsOnLaunch = true; @@ -169,7 +180,7 @@ namespace ViewModelTests }; // arrange - PowerRenameViewModel viewModel = new PowerRenameViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, generalSettings_file_name); + PowerRenameViewModel viewModel = new PowerRenameViewModel(mockPowerRenamePropertiesUtils.Object, SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SendMockIPCConfigMSG, generalSettings_file_name); // act viewModel.MaxDispListNum = 20; diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs index d788154ffd..24928c1006 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs @@ -18,6 +18,17 @@ namespace ViewModelTests { public const string ShortCutGuideTestFolderName = "Test\\ShortCutGuide"; + private Mock mockGeneralSettingsUtils; + + private Mock mockShortcutGuideSettingsUtils; + + [TestInitialize] + public void SetUp_StubSettingUtils() + { + mockGeneralSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + mockShortcutGuideSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils(); + } + [TestMethod] public void IsEnabled_ShouldEnableModule_WhenSuccessful() { @@ -31,7 +42,7 @@ namespace ViewModelTests }; // Arrange - ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, ShortCutGuideTestFolderName); + ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockShortcutGuideSettingsUtils.Object), SendMockIPCConfigMSG, ShortCutGuideTestFolderName); // Act viewModel.IsEnabled = true; @@ -50,7 +61,7 @@ namespace ViewModelTests }; // Arrange - ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, ShortCutGuideTestFolderName); + ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockShortcutGuideSettingsUtils.Object), SendMockIPCConfigMSG, ShortCutGuideTestFolderName); Assert.AreEqual(1, viewModel.ThemeIndex); // Act @@ -70,7 +81,7 @@ namespace ViewModelTests }; // Arrange - ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, ShortCutGuideTestFolderName); + ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockShortcutGuideSettingsUtils.Object), SendMockIPCConfigMSG, ShortCutGuideTestFolderName); Assert.AreEqual(900, viewModel.PressTime); // Act @@ -92,7 +103,7 @@ namespace ViewModelTests }; // Arrange - ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(ISettingsUtilsMocks.GetStubSettingsUtils().Object, SendMockIPCConfigMSG, ShortCutGuideTestFolderName); + ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(SettingsRepository.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository.GetInstance(mockShortcutGuideSettingsUtils.Object), SendMockIPCConfigMSG, ShortCutGuideTestFolderName); Assert.AreEqual(90, viewModel.OverlayOpacity); // Act diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Converters/ModuleEnabledToForegroundConverter.cs b/src/core/Microsoft.PowerToys.Settings.UI/Converters/ModuleEnabledToForegroundConverter.cs index 72cf1129c4..415fa97ba7 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Converters/ModuleEnabledToForegroundConverter.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Converters/ModuleEnabledToForegroundConverter.cs @@ -15,15 +15,15 @@ namespace Microsoft.PowerToys.Settings.UI.Converters { private readonly ISettingsUtils settingsUtils = new SettingsUtils(new SystemIOProvider()); + private string selectedTheme = string.Empty; + public object Convert(object value, Type targetType, object parameter, string language) { bool isEnabled = (bool)value; - GeneralSettings generalSettings = settingsUtils.GetSettings(string.Empty); var defaultTheme = new Windows.UI.ViewManagement.UISettings(); var uiTheme = defaultTheme.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background).ToString(); - - string selectedTheme = generalSettings.Theme.ToLower(); + selectedTheme = SettingsRepository.GetInstance(settingsUtils).SettingsConfig.Theme.ToLower(); if (selectedTheme == "dark" || (selectedTheme == "system" && uiTheme == "#FF000000")) { diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/ColorPickerPage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/ColorPickerPage.xaml.cs index 6e47d7c8cf..929fc9398c 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/ColorPickerPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/ColorPickerPage.xaml.cs @@ -16,7 +16,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views public ColorPickerPage() { var settingsUtils = new SettingsUtils(new SystemIOProvider()); - ViewModel = new ColorPickerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage); + ViewModel = new ColorPickerViewModel(settingsUtils, SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; InitializeComponent(); } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/FancyZonesPage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/FancyZonesPage.xaml.cs index 56b513e517..e022e63fd9 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/FancyZonesPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/FancyZonesPage.xaml.cs @@ -17,7 +17,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views { InitializeComponent(); var settingsUtils = new SettingsUtils(new SystemIOProvider()); - ViewModel = new FancyZonesViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage); + ViewModel = new FancyZonesViewModel(SettingsRepository.GetInstance(settingsUtils), SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml.cs index 459679c753..1546469ec2 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml.cs @@ -37,6 +37,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views ViewModel = new GeneralViewModel( settingsUtils, + SettingsRepository.GetInstance(settingsUtils), loader.GetString("GeneralSettings_RunningAsAdminText"), loader.GetString("GeneralSettings_RunningAsUserText"), ShellPage.IsElevated, diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/ImageResizerPage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/ImageResizerPage.xaml.cs index 7f8281679d..b98c2d706d 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/ImageResizerPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/ImageResizerPage.xaml.cs @@ -17,9 +17,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views public ImageResizerPage() { InitializeComponent(); - var settingsUtils = new SettingsUtils(new SystemIOProvider()); - ViewModel = new ImageResizerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage); + ViewModel = new ImageResizerViewModel(settingsUtils, SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml.cs index 2333badd6b..2220c43804 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml.cs @@ -32,7 +32,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views dispatcher = Window.Current.Dispatcher; var settingsUtils = new SettingsUtils(new SystemIOProvider()); - ViewModel = new KeyboardManagerViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage, FilterRemapKeysList); + ViewModel = new KeyboardManagerViewModel(settingsUtils, SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, FilterRemapKeysList); watcher = Helper.GetFileWatcher( PowerToyName, diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerLauncherPage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerLauncherPage.xaml.cs index b41fa6f596..681af560bb 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerLauncherPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerLauncherPage.xaml.cs @@ -22,7 +22,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views { InitializeComponent(); var settingsUtils = new SettingsUtils(new SystemIOProvider()); - ViewModel = new PowerLauncherViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage, (int)Windows.System.VirtualKey.Space); + ViewModel = new PowerLauncherViewModel(settingsUtils, SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, (int)Windows.System.VirtualKey.Space); DataContext = ViewModel; var loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(); diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml.cs index fe71b790a4..5fcec859d3 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml.cs @@ -20,7 +20,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views { InitializeComponent(); var settingsUtils = new SettingsUtils(new SystemIOProvider()); - ViewModel = new PowerPreviewViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage); + ViewModel = new PowerPreviewViewModel(SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerRenamePage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerRenamePage.xaml.cs index 7f44b3672a..ff1a20be02 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerRenamePage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerRenamePage.xaml.cs @@ -17,7 +17,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views { InitializeComponent(); var settingsUtils = new SettingsUtils(new SystemIOProvider()); - ViewModel = new PowerRenameViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage); + ViewModel = new PowerRenameViewModel(settingsUtils, SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/ShellPage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/ShellPage.xaml.cs index eb61947599..972a1595ca 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/ShellPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/ShellPage.xaml.cs @@ -2,7 +2,9 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; +using Microsoft.PowerToys.Settings.UI.Lib; using Microsoft.PowerToys.Settings.UI.ViewModels; using Windows.Data.Json; using Windows.UI.Xaml.Controls; diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml.cs index c968dc8cd9..2b0dec506d 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/ShortcutGuidePage.xaml.cs @@ -16,8 +16,9 @@ namespace Microsoft.PowerToys.Settings.UI.Views public ShortcutGuidePage() { InitializeComponent(); + var settingsUtils = new SettingsUtils(new SystemIOProvider()); - ViewModel = new ShortcutGuideViewModel(settingsUtils, ShellPage.SendDefaultIPCMessage); + ViewModel = new ShortcutGuideViewModel(SettingsRepository.GetInstance(settingsUtils), SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; } }