2020-04-08 01:19:14 +08:00
// 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.
2020-03-27 23:58:53 +08:00
2020-08-15 05:26:41 +08:00
using System ;
2020-10-22 03:32:53 +08:00
using System.Diagnostics.CodeAnalysis ;
using System.IO ;
2020-04-18 06:25:08 +08:00
using System.Runtime.CompilerServices ;
2020-10-23 00:45:48 +08:00
using Microsoft.PowerToys.Settings.UI.Library.Helpers ;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces ;
using Microsoft.PowerToys.Settings.UI.Library.Utilities ;
2020-03-27 23:58:53 +08:00
2020-10-23 00:45:48 +08:00
namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
2020-03-27 23:58:53 +08:00
{
public class PowerRenameViewModel : Observable
{
2020-09-24 04:20:32 +08:00
private GeneralSettings GeneralSettingsConfig { get ; set ; }
2020-09-22 01:14:44 +08:00
private readonly ISettingsUtils _settingsUtils ;
2020-09-24 04:20:32 +08:00
private const string ModuleName = PowerRenameSettings . ModuleName ;
2020-04-18 06:25:08 +08:00
2020-08-20 06:59:10 +08:00
private string _settingsConfigFileFolder = string . Empty ;
2020-08-15 05:26:41 +08:00
2020-04-18 06:25:08 +08:00
private PowerRenameSettings Settings { get ; set ; }
2020-08-15 05:26:41 +08:00
private Func < string , int > SendConfigMSG { get ; }
2020-10-22 03:32:53 +08:00
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exceptions should not crash the program but will be logged until we can understand common exception scenarios")]
2020-09-24 04:20:32 +08:00
public PowerRenameViewModel ( ISettingsUtils settingsUtils , ISettingsRepository < GeneralSettings > settingsRepository , Func < string , int > ipcMSGCallBackFunc , string configFileSubfolder = "" )
2020-03-27 23:58:53 +08:00
{
2020-08-15 05:26:41 +08:00
// Update Settings file folder:
2020-08-20 06:59:10 +08:00
_settingsConfigFileFolder = configFileSubfolder ;
2020-09-22 01:14:44 +08:00
_settingsUtils = settingsUtils ? ? throw new ArgumentNullException ( nameof ( settingsUtils ) ) ;
2020-08-15 05:26:41 +08:00
2020-10-20 04:32:05 +08:00
if ( settingsRepository = = null )
{
throw new ArgumentNullException ( nameof ( settingsRepository ) ) ;
}
2020-09-24 04:20:32 +08:00
GeneralSettingsConfig = settingsRepository . SettingsConfig ;
2020-04-18 06:25:08 +08:00
try
{
2020-09-22 01:14:44 +08:00
PowerRenameLocalProperties localSettings = _settingsUtils . GetSettings < PowerRenameLocalProperties > ( GetSettingsSubPath ( ) , "power-rename-settings.json" ) ;
2020-04-18 06:25:08 +08:00
Settings = new PowerRenameSettings ( localSettings ) ;
}
2020-10-22 03:32:53 +08:00
catch ( Exception e )
2020-04-18 06:25:08 +08:00
{
2020-10-22 03:32:53 +08:00
Logger . LogError ( $"Exception encountered while reading {ModuleName} settings." , e ) ;
#if DEBUG
if ( e is ArgumentException | | e is ArgumentNullException | | e is PathTooLongException )
{
2020-10-23 00:45:48 +08:00
throw ;
2020-10-22 03:32:53 +08:00
}
#endif
2020-04-18 06:25:08 +08:00
PowerRenameLocalProperties localSettings = new PowerRenameLocalProperties ( ) ;
Settings = new PowerRenameSettings ( localSettings ) ;
2020-09-22 01:14:44 +08:00
_settingsUtils . SaveSettings ( localSettings . ToJsonString ( ) , GetSettingsSubPath ( ) , "power-rename-settings.json" ) ;
2020-04-18 06:25:08 +08:00
}
2020-08-15 05:26:41 +08:00
// set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc ;
2020-07-22 05:06:39 +08:00
_powerRenameEnabledOnContextMenu = Settings . Properties . ShowIcon . Value ;
_powerRenameEnabledOnContextExtendedMenu = Settings . Properties . ExtendedContextMenuOnly . Value ;
_powerRenameRestoreFlagsOnLaunch = Settings . Properties . PersistState . Value ;
_powerRenameMaxDispListNumValue = Settings . Properties . MaxMRUSize . Value ;
_autoComplete = Settings . Properties . MRUEnabled . Value ;
2020-11-10 02:13:43 +08:00
_powerRenameUseBoostLib = Settings . Properties . UseBoostLib . Value ;
2020-09-24 04:20:32 +08:00
_powerRenameEnabled = GeneralSettingsConfig . Enabled . PowerRename ;
2020-04-18 06:25:08 +08:00
}
2020-10-10 08:58:52 +08:00
private bool _powerRenameEnabled ;
private bool _powerRenameEnabledOnContextMenu ;
private bool _powerRenameEnabledOnContextExtendedMenu ;
private bool _powerRenameRestoreFlagsOnLaunch ;
private int _powerRenameMaxDispListNumValue ;
private bool _autoComplete ;
2020-11-10 02:13:43 +08:00
private bool _powerRenameUseBoostLib ;
2020-04-18 06:25:08 +08:00
public bool IsEnabled
{
get
{
return _powerRenameEnabled ;
}
set
{
if ( value ! = _powerRenameEnabled )
{
2020-09-24 04:20:32 +08:00
GeneralSettingsConfig . Enabled . PowerRename = value ;
OutGoingGeneralSettings snd = new OutGoingGeneralSettings ( GeneralSettingsConfig ) ;
2020-08-15 05:26:41 +08:00
SendConfigMSG ( snd . ToString ( ) ) ;
2020-04-18 06:25:08 +08:00
_powerRenameEnabled = value ;
2020-10-10 08:58:52 +08:00
OnPropertyChanged ( nameof ( IsEnabled ) ) ;
RaisePropertyChanged ( nameof ( GlobalAndMruEnabled ) ) ;
2020-04-18 06:25:08 +08:00
}
}
}
public bool MRUEnabled
{
get
{
return _autoComplete ;
}
set
{
if ( value ! = _autoComplete )
{
_autoComplete = value ;
2020-07-22 05:06:39 +08:00
Settings . Properties . MRUEnabled . Value = value ;
2020-04-18 06:25:08 +08:00
RaisePropertyChanged ( ) ;
2020-10-10 08:58:52 +08:00
RaisePropertyChanged ( nameof ( GlobalAndMruEnabled ) ) ;
2020-04-18 06:25:08 +08:00
}
}
}
2020-05-11 20:37:29 +08:00
public bool GlobalAndMruEnabled
{
get
{
return _autoComplete & & _powerRenameEnabled ;
}
}
2020-04-18 06:25:08 +08:00
public bool EnabledOnContextMenu
{
get
{
return _powerRenameEnabledOnContextMenu ;
}
set
{
if ( value ! = _powerRenameEnabledOnContextMenu )
{
_powerRenameEnabledOnContextMenu = value ;
2020-07-22 05:06:39 +08:00
Settings . Properties . ShowIcon . Value = value ;
2020-04-18 06:25:08 +08:00
RaisePropertyChanged ( ) ;
}
}
}
public bool EnabledOnContextExtendedMenu
{
get
{
return _powerRenameEnabledOnContextExtendedMenu ;
}
set
{
if ( value ! = _powerRenameEnabledOnContextExtendedMenu )
{
_powerRenameEnabledOnContextExtendedMenu = value ;
2020-07-22 05:06:39 +08:00
Settings . Properties . ExtendedContextMenuOnly . Value = value ;
2020-04-18 06:25:08 +08:00
RaisePropertyChanged ( ) ;
}
}
}
public bool RestoreFlagsOnLaunch
{
get
{
return _powerRenameRestoreFlagsOnLaunch ;
}
set
{
if ( value ! = _powerRenameRestoreFlagsOnLaunch )
{
_powerRenameRestoreFlagsOnLaunch = value ;
2020-07-22 05:06:39 +08:00
Settings . Properties . PersistState . Value = value ;
2020-04-18 06:25:08 +08:00
RaisePropertyChanged ( ) ;
}
}
}
public int MaxDispListNum
{
get
{
return _powerRenameMaxDispListNumValue ;
}
set
{
if ( value ! = _powerRenameMaxDispListNumValue )
{
_powerRenameMaxDispListNumValue = value ;
2020-07-22 05:06:39 +08:00
Settings . Properties . MaxMRUSize . Value = value ;
2020-04-18 06:25:08 +08:00
RaisePropertyChanged ( ) ;
}
}
}
2020-11-10 02:13:43 +08:00
public bool UseBoostLib
{
get
{
return _powerRenameUseBoostLib ;
}
set
{
if ( value ! = _powerRenameUseBoostLib )
{
_powerRenameUseBoostLib = value ;
Settings . Properties . UseBoostLib . Value = value ;
RaisePropertyChanged ( ) ;
}
}
}
2020-08-15 05:26:41 +08:00
public string GetSettingsSubPath ( )
{
2020-08-20 06:59:10 +08:00
return _settingsConfigFileFolder + "\\" + ModuleName ;
2020-08-15 05:26:41 +08:00
}
2020-04-18 06:25:08 +08:00
private void RaisePropertyChanged ( [ CallerMemberName ] string propertyName = null )
{
// Notify UI of property change
OnPropertyChanged ( propertyName ) ;
2020-08-15 05:26:41 +08:00
if ( SendConfigMSG ! = null )
2020-04-18 06:25:08 +08:00
{
SndPowerRenameSettings snd = new SndPowerRenameSettings ( Settings ) ;
SndModuleSettings < SndPowerRenameSettings > ipcMessage = new SndModuleSettings < SndPowerRenameSettings > ( snd ) ;
2020-08-15 05:26:41 +08:00
SendConfigMSG ( ipcMessage . ToJsonString ( ) ) ;
2020-04-18 06:25:08 +08:00
}
2020-03-27 23:58:53 +08:00
}
}
2020-07-23 04:27:17 +08:00
}