diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ColorPickerViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ColorPickerViewModel.cs similarity index 88% rename from src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ColorPickerViewModel.cs rename to src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ColorPickerViewModel.cs index 7831504d82..e3ccd66207 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/ColorPickerViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/ColorPickerViewModel.cs @@ -1,20 +1,21 @@ // 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.Text.Json; +using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -using Microsoft.PowerToys.Settings.UI.Helpers; -using Microsoft.PowerToys.Settings.UI.Lib; -using Microsoft.PowerToys.Settings.UI.Views; - -namespace Microsoft.PowerToys.Settings.UI.ViewModels +namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class ColorPickerViewModel : Observable { private ColorPickerSettings _colorPickerSettings; private bool _isEnabled; - public ColorPickerViewModel() + private Func SendConfigMSG { get; } + + public ColorPickerViewModel(Func ipcMSGCallBackFunc) { if (SettingsUtils.SettingsExists(ColorPickerSettings.ModuleName)) { @@ -30,6 +31,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels var generalSettings = SettingsUtils.GetSettings(); _isEnabled = generalSettings.Enabled.ColorPicker; } + + // set the callback functions value to hangle outgoing IPC message. + SendConfigMSG = ipcMSGCallBackFunc; } public bool IsEnabled @@ -50,7 +54,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels var generalSettings = SettingsUtils.GetSettings(); generalSettings.Enabled.ColorPicker = value; OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(generalSettings); - ShellPage.DefaultSndMSGCallback(outgoing.ToString()); + SendConfigMSG(outgoing.ToString()); } } } @@ -111,7 +115,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private void NotifySettingsChanged() { - ShellPage.DefaultSndMSGCallback( + SendConfigMSG( string.Format("{{ \"powertoys\": {{ \"{0}\": {1} }} }}", ColorPickerSettings.ModuleName, JsonSerializer.Serialize(_colorPickerSettings))); } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs new file mode 100644 index 0000000000..6265b658a3 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs @@ -0,0 +1,63 @@ +// 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.IO; +using System.Text.Json; +using Microsoft.PowerToys.Settings.UI.Lib; +using Microsoft.PowerToys.Settings.UI.Lib.ViewModels; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ViewModelTests +{ + [TestClass] + public class ColorPicker + { + private const string ModuleName = "ColorPicker"; + + [TestInitialize] + public void Setup() + { + var generalSettings = new GeneralSettings(); + var colorPickerSettings = new ColorPickerSettings(); + + SettingsUtils.SaveSettings(generalSettings.ToJsonString()); + SettingsUtils.SaveSettings(colorPickerSettings.ToJsonString(), colorPickerSettings.Name, ModuleName + ".json"); + } + + [TestCleanup] + public void CleanUp() + { + string generalSettings_file_name = string.Empty; + if (SettingsUtils.SettingsFolderExists(generalSettings_file_name)) + { + DeleteFolder(generalSettings_file_name); + } + + if (SettingsUtils.SettingsFolderExists(ModuleName)) + { + DeleteFolder(ModuleName); + } + } + + [TestMethod] + public void ColorPickerIsEnabledByDefault() + { + var viewModel = new ColorPickerViewModel(ColorPickerIsEnabledByDefault_IPC); + + Assert.IsTrue(viewModel.IsEnabled); + } + + public int ColorPickerIsEnabledByDefault_IPC(string msg) + { + OutGoingGeneralSettings snd = JsonSerializer.Deserialize(msg); + Assert.IsTrue(snd.GeneralSettings.Enabled.ColorPicker); + return 0; + } + + private static void DeleteFolder(string powertoy) + { + Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true); + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Microsoft.PowerToys.Settings.UI.csproj b/src/core/Microsoft.PowerToys.Settings.UI/Microsoft.PowerToys.Settings.UI.csproj index 69d2c51208..883095b0d1 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Microsoft.PowerToys.Settings.UI.csproj +++ b/src/core/Microsoft.PowerToys.Settings.UI/Microsoft.PowerToys.Settings.UI.csproj @@ -108,7 +108,6 @@ - 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 b45bf5f2da..4d759ebde0 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/ColorPickerPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/ColorPickerPage.xaml.cs @@ -2,7 +2,7 @@ // 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.ViewModels; +using Microsoft.PowerToys.Settings.UI.Lib.ViewModels; using Windows.UI.Xaml.Controls; namespace Microsoft.PowerToys.Settings.UI.Views @@ -13,7 +13,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views public ColorPickerPage() { - ViewModel = new ColorPickerViewModel(); + ViewModel = new ColorPickerViewModel(ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; InitializeComponent(); }