diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerPreviewViewModel.cs similarity index 70% rename from src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs rename to src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerPreviewViewModel.cs index 760dcd3af7..7933a86f3a 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerPreviewViewModel.cs @@ -2,12 +2,12 @@ // 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.Runtime.CompilerServices; -using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.Lib; -using Microsoft.PowerToys.Settings.UI.Views; +using Microsoft.PowerToys.Settings.UI.Lib.Helpers; -namespace Microsoft.PowerToys.Settings.UI.ViewModels +namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels { public class PowerPreviewViewModel : Observable { @@ -15,21 +15,31 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private PowerPreviewSettings Settings { get; set; } - public PowerPreviewViewModel() + private Func SendConfigMSG { get; } + + public string SettingsConfigFileFolder = string.Empty; + + public PowerPreviewViewModel(Func ipcMSGCallBackFunc, string configFileSubfolder = "") { + // Update Settings file folder: + SettingsConfigFileFolder = configFileSubfolder; + try { - Settings = SettingsUtils.GetSettings(ModuleName); + Settings = SettingsUtils.GetSettings(GetSettingsSubPath()); } catch { Settings = new PowerPreviewSettings(); - SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName); + SettingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath()); } - _svgRenderIsEnabled = Settings.Properties.EnableSvgPreview; - _svgThumbnailIsEnabled = Settings.Properties.EnableSvgThumbnail; - _mdRenderIsEnabled = Settings.Properties.EnableMdPreview; + // set the callback functions value to hangle outgoing IPC message. + SendConfigMSG = ipcMSGCallBackFunc; + + this._svgRenderIsEnabled = Settings.Properties.EnableSvgPreview; + this._svgThumbnailIsEnabled = Settings.Properties.EnableSvgThumbnail; + this._mdRenderIsEnabled = Settings.Properties.EnableMdPreview; } private bool _svgRenderIsEnabled = false; @@ -90,16 +100,21 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } } + public string GetSettingsSubPath() + { + return SettingsConfigFileFolder + "\\" + ModuleName; + } + private void RaisePropertyChanged([CallerMemberName] string propertyName = null) { // Notify UI of property change OnPropertyChanged(propertyName); - if (ShellPage.DefaultSndMSGCallback != null) + if (SendConfigMSG != null) { SndPowerPreviewSettings snd = new SndPowerPreviewSettings(Settings); SndModuleSettings ipcMessage = new SndModuleSettings(snd); - ShellPage.DefaultSndMSGCallback(ipcMessage.ToJsonString()); + SendConfigMSG(ipcMessage.ToJsonString()); } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs new file mode 100644 index 0000000000..44b81baa4b --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs @@ -0,0 +1,105 @@ +// 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.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 PowerPreview + { + public const string Module = "Test\\File Explorer"; + + [TestInitialize] + public void Setup() + { + // initialize creation of test settings file. + GeneralSettings generalSettings = new GeneralSettings(); + PowerPreviewSettings powerpreview = new PowerPreviewSettings(); + + SettingsUtils.SaveSettings(generalSettings.ToJsonString()); + SettingsUtils.SaveSettings(powerpreview.ToJsonString(), powerpreview.Name); + } + + [TestCleanup] + public void CleanUp() + { + // delete folder created. + string generalSettings_file_name = string.Empty; + if (SettingsUtils.SettingsFolderExists(generalSettings_file_name)) + { + DeleteFolder(generalSettings_file_name); + } + + if (SettingsUtils.SettingsFolderExists(Module)) + { + DeleteFolder(Module); + } + } + + public void DeleteFolder(string powertoy) + { + Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true); + } + + [TestMethod] + public void SVGRenderIsEnabled_ShouldPrevHandler_WhenSuccessful() + { + // Assert + Func SendMockIPCConfigMSG = msg => + { + SndModuleSettings snd = JsonSerializer.Deserialize>(msg); + Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.Properties.EnableSvgPreview); + return 0; + }; + + // arrange + PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SendMockIPCConfigMSG, Module); + + // act + viewModel.SVGRenderIsEnabled = true; + } + + [TestMethod] + public void SVGThumbnailIsEnabled_ShouldPrevHandler_WhenSuccessful() + { + // Assert + Func SendMockIPCConfigMSG = msg => + { + SndModuleSettings snd = JsonSerializer.Deserialize>(msg); + Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.Properties.EnableSvgThumbnail); + return 0; + }; + + // arrange + PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SendMockIPCConfigMSG, Module); + + // act + viewModel.SVGThumbnailIsEnabled = true; + } + + [TestMethod] + public void MDRenderIsEnabled_ShouldPrevHandler_WhenSuccessful() + { + // Assert + Func SendMockIPCConfigMSG = msg => + { + SndModuleSettings snd = JsonSerializer.Deserialize>(msg); + Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.Properties.EnableMdPreview); + return 0; + }; + + // arrange + PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SendMockIPCConfigMSG, Module);; + + // act + viewModel.MDRenderIsEnabled = 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 12cd95b2a6..f2e4012bd7 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 @@ -1,313 +1,312 @@ - - - - - - - Microsoft.PowerToys.Settings.UI - PowerToys Settings UI - Microsoft Corp. - Copyright (C) 2020 Microsoft Corp. - PowerToys - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Debug - x64 - {A7D5099E-F0FD-4BF3-8522-5A682759F915} - AppContainerExe - Properties - Microsoft.PowerToys.Settings.UI - Microsoft.PowerToys.Settings.UI - en-US - UAP - 10.0.18362.0 - 10.0.18362.0 - 14 - 512 - {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - true - false - - - true - ..\..\..\x64\Debug\SettingsUI\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - 8305;2008 - full - x64 - false - prompt - true - - - ..\..\..\x64\Release\SettingsUI\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - 8305;2008 - pdbonly - x64 - false - prompt - true - false - - - PackageReference - - - - GlobalSuppressions.cs - - - - - App.xaml - - - - - HotkeySettingsControl.xaml - - - - Code - - - - - - - - - - - - - - - - - - ColorPickerPage.xaml - - - GeneralPage.xaml - - - ImageResizerPage.xaml - - - KeyboardManagerPage.xaml - - - PowerLauncherPage.xaml - - - PowerPreviewPage.xaml - - - FancyZonesPage.xaml - - - PowerRenamePage.xaml - - - ShellPage.xaml - - - ShortcutGuidePage.xaml - - - - - - Designer - - - - - - - - - - - - - - - - - - - - - - - - - MSBuild:Compile - Designer - - - - - 6.2.10 - - - 6.1.0 - - - 6.1.1 - - - 2.5.0-prerelease.200708003 - - - 2.0.1 - - - 12.0.3 - - - 1.1.118 - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - - - StyleCop.json - - - - - {5d00d290-4016-4cfe-9e41-1e7c724509ba} - Telemetry - - - {b1bcc8c6-46b5-4bfa-8f22-20f32d99ec6a} - Microsoft.PowerToys.Settings.UI.Lib - - - - 14.0 - - - false - false - - - + + + + + + + Microsoft.PowerToys.Settings.UI + PowerToys Settings UI + Microsoft Corp. + Copyright (C) 2020 Microsoft Corp. + PowerToys + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Debug + x64 + {A7D5099E-F0FD-4BF3-8522-5A682759F915} + AppContainerExe + Properties + Microsoft.PowerToys.Settings.UI + Microsoft.PowerToys.Settings.UI + en-US + UAP + 10.0.18362.0 + 10.0.18362.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + true + false + + + true + ..\..\..\x64\Debug\SettingsUI\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + 8305;2008 + full + x64 + false + prompt + true + + + ..\..\..\x64\Release\SettingsUI\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + 8305;2008 + pdbonly + x64 + false + prompt + true + false + + + PackageReference + + + + GlobalSuppressions.cs + + + + + App.xaml + + + + + HotkeySettingsControl.xaml + + + + Code + + + + + + + + + + + + + + + + + ColorPickerPage.xaml + + + GeneralPage.xaml + + + ImageResizerPage.xaml + + + KeyboardManagerPage.xaml + + + PowerLauncherPage.xaml + + + PowerPreviewPage.xaml + + + FancyZonesPage.xaml + + + PowerRenamePage.xaml + + + ShellPage.xaml + + + ShortcutGuidePage.xaml + + + + + + Designer + + + + + + + + + + + + + + + + + + + + + + + + + MSBuild:Compile + Designer + + + + + 6.2.10 + + + 6.1.0 + + + 6.1.1 + + + 2.5.0-prerelease.200708003 + + + 2.0.1 + + + 12.0.3 + + + 1.1.118 + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + + + StyleCop.json + + + + + {5d00d290-4016-4cfe-9e41-1e7c724509ba} + Telemetry + + + {b1bcc8c6-46b5-4bfa-8f22-20f32d99ec6a} + Microsoft.PowerToys.Settings.UI.Lib + + + + 14.0 + + + false + false + + + \ No newline at end of file diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml index 9e99b758ba..1f1f0eaf27 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml @@ -76,7 +76,7 @@ HorizontalAlignment="Left" Margin="{StaticResource SmallTopBottomMargin}" RelativePanel.Below="DescriptionPanel"> - + - + 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 09d1baf967..8019726e39 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.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 @@ -12,13 +12,13 @@ namespace Microsoft.PowerToys.Settings.UI.Views /// public sealed partial class PowerPreviewPage : Page { - public PowerPreviewViewModel ViewModel { get; set; } + public PowerPreviewViewModel viewModel { get; set; } public PowerPreviewPage() { - InitializeComponent(); - ViewModel = new PowerPreviewViewModel(); - PowerPreviewView.DataContext = ViewModel; + this.InitializeComponent(); + viewModel = new PowerPreviewViewModel(ShellPage.SendDefaultIPCMessage); + DataContext = viewModel; } } }