PowerToys/src/core/Microsoft.PowerToys.Settings.UI.Lib/ViewModels/PowerPreviewViewModel.cs

129 lines
4.1 KiB
C#
Raw Normal View History

// 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.Runtime.CompilerServices;
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
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<GeneralSettings> 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 <T>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
2020-09-24 04:20:32 +08:00
using Microsoft.PowerToys.Settings.UI.Lib.Interface;
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
{
public class PowerPreviewViewModel : Observable
{
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<GeneralSettings> 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 <T>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
2020-09-24 04:20:32 +08:00
private const string ModuleName = PowerPreviewSettings.ModuleName;
private PowerPreviewSettings Settings { get; set; }
private Func<string, int> SendConfigMSG { get; }
private string _settingsConfigFileFolder = string.Empty;
[File Explorer] Fix enable/disable for File Explorer PowerToy (#6883) * Commented out enable/disable for File Explorer * Revert UI changes * Disable the toggles if PT is not running elevated * Fixed compilation errors in tests * Cleaned up preview pane code to separate thumbnail and preview panes as separate classes * Fixed broken settings format and added elevation check and registry updated required logic. Preview Handler tested manually working, Thumbnail Enable/Disable needs to be fixed * Updated Thumbnail enable/disable logic and added warning messages * Update tests for File Explorer * Fixed RegGetValue failing in Release config * Renamed new classes * Split wrappers for disable to work * Modified enabled flag check to also check if user is on new settings. Fixed casing issue in powerpreview.h that caused a dialog prompt on first launch after install * Update fontweight and margin * Fixed release build not working * Move UseNewSettings usage to powerpreview.cpp to avoid tests breaking. For new settings the enable check is done in constructor and for old settings it is done in enable * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Moved dup code to method * Use correct versions of general settings for backwards compat test Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>
2020-10-10 05:45:30 +08:00
private GeneralSettings GeneralSettingsConfig { get; set; }
public PowerPreviewViewModel(ISettingsRepository<PowerPreviewSettings> moduleSettingsRepository, ISettingsRepository<GeneralSettings> generalSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
{
// Update Settings file folder:
_settingsConfigFileFolder = configFileSubfolder;
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<GeneralSettings> 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 <T>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
2020-09-24 04:20:32 +08:00
[File Explorer] Fix enable/disable for File Explorer PowerToy (#6883) * Commented out enable/disable for File Explorer * Revert UI changes * Disable the toggles if PT is not running elevated * Fixed compilation errors in tests * Cleaned up preview pane code to separate thumbnail and preview panes as separate classes * Fixed broken settings format and added elevation check and registry updated required logic. Preview Handler tested manually working, Thumbnail Enable/Disable needs to be fixed * Updated Thumbnail enable/disable logic and added warning messages * Update tests for File Explorer * Fixed RegGetValue failing in Release config * Renamed new classes * Split wrappers for disable to work * Modified enabled flag check to also check if user is on new settings. Fixed casing issue in powerpreview.h that caused a dialog prompt on first launch after install * Update fontweight and margin * Fixed release build not working * Move UseNewSettings usage to powerpreview.cpp to avoid tests breaking. For new settings the enable check is done in constructor and for old settings it is done in enable * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Moved dup code to method * Use correct versions of general settings for backwards compat test Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>
2020-10-10 05:45:30 +08:00
// To obtain the general Settings configurations of PowerToys
GeneralSettingsConfig = generalSettingsRepository.SettingsConfig;
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<GeneralSettings> 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 <T>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
2020-09-24 04:20:32 +08:00
// 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;
_svgRenderIsEnabled = Settings.Properties.EnableSvgPreview;
_svgThumbnailIsEnabled = Settings.Properties.EnableSvgThumbnail;
_mdRenderIsEnabled = Settings.Properties.EnableMdPreview;
}
private bool _svgRenderIsEnabled = false;
private bool _mdRenderIsEnabled = false;
private bool _svgThumbnailIsEnabled = false;
public bool SVGRenderIsEnabled
{
get
{
return _svgRenderIsEnabled;
}
set
{
if (value != _svgRenderIsEnabled)
{
_svgRenderIsEnabled = value;
Settings.Properties.EnableSvgPreview = value;
RaisePropertyChanged();
}
}
}
public bool SVGThumbnailIsEnabled
{
get
{
return _svgThumbnailIsEnabled;
}
set
{
if (value != _svgThumbnailIsEnabled)
{
_svgThumbnailIsEnabled = value;
Settings.Properties.EnableSvgThumbnail = value;
RaisePropertyChanged();
}
}
}
public bool MDRenderIsEnabled
{
get
{
return _mdRenderIsEnabled;
}
set
{
if (value != _mdRenderIsEnabled)
{
_mdRenderIsEnabled = value;
Settings.Properties.EnableMdPreview = value;
RaisePropertyChanged();
}
}
}
public string GetSettingsSubPath()
{
return _settingsConfigFileFolder + "\\" + ModuleName;
}
[File Explorer] Fix enable/disable for File Explorer PowerToy (#6883) * Commented out enable/disable for File Explorer * Revert UI changes * Disable the toggles if PT is not running elevated * Fixed compilation errors in tests * Cleaned up preview pane code to separate thumbnail and preview panes as separate classes * Fixed broken settings format and added elevation check and registry updated required logic. Preview Handler tested manually working, Thumbnail Enable/Disable needs to be fixed * Updated Thumbnail enable/disable logic and added warning messages * Update tests for File Explorer * Fixed RegGetValue failing in Release config * Renamed new classes * Split wrappers for disable to work * Modified enabled flag check to also check if user is on new settings. Fixed casing issue in powerpreview.h that caused a dialog prompt on first launch after install * Update fontweight and margin * Fixed release build not working * Move UseNewSettings usage to powerpreview.cpp to avoid tests breaking. For new settings the enable check is done in constructor and for old settings it is done in enable * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Moved dup code to method * Use correct versions of general settings for backwards compat test Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>
2020-10-10 05:45:30 +08:00
public bool IsElevated
{
get
{
return GeneralSettingsConfig.IsElevated;
}
}
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
// Notify UI of property change
OnPropertyChanged(propertyName);
if (SendConfigMSG != null)
{
SndPowerPreviewSettings snd = new SndPowerPreviewSettings(Settings);
SndModuleSettings<SndPowerPreviewSettings> ipcMessage = new SndModuleSettings<SndPowerPreviewSettings>(snd);
SendConfigMSG(ipcMessage.ToJsonString());
}
}
}
}