public KeyboardManagerViewModel()
        {
            dispatcher = Window.Current.Dispatcher;
            if (SettingsUtils.SettingsExists(PowerToyName))
            {
                // Todo: Be more resillent while reading and saving settings.
                settings = SettingsUtils.GetSettings <KeyboardManagerSettings>(PowerToyName);

                // Load profile.
                if (!LoadProfile())
                {
                    profile = new KeyboardManagerProfile();
                }
            }
            else
            {
                settings = new KeyboardManagerSettings(PowerToyName);
                SettingsUtils.SaveSettings(settings.ToJsonString(), PowerToyName);
            }

            if (SettingsUtils.SettingsExists())
            {
                generalSettings = SettingsUtils.GetSettings <GeneralSettings>(string.Empty);
            }
            else
            {
                generalSettings = new GeneralSettings();
                SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
            }

            watcher = Helper.GetFileWatcher(
                PowerToyName,
                settings.Properties.ActiveConfiguration.Value + JsonFileType,
                OnConfigFileUpdate);
        }
Esempio n. 2
0
        public MouseUtilsPage()
        {
            try
            {
                // By mistake, the first release of Find My Mouse was saving settings in two places at the same time.
                // Delete the wrong path for Find My Mouse settings.
                var tempSettingsUtils = new SettingsUtils();
                if (tempSettingsUtils.SettingsExists("Find My Mouse"))
                {
                    var settingsFilePath = tempSettingsUtils.GetSettingsFilePath("Find My Mouse");
                    System.IO.File.Delete(settingsFilePath);
                    tempSettingsUtils.DeleteSettings("Find My Mouse");
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (System.Exception)
#pragma warning restore CA1031 // Do not catch general exception types
            {
            }

            var settingsUtils = new SettingsUtils();
            ViewModel = new MouseUtilsViewModel(
                settingsUtils,
                SettingsRepository <GeneralSettings> .GetInstance(settingsUtils),
                SettingsRepository <FindMyMouseSettings> .GetInstance(settingsUtils),
                SettingsRepository <MouseHighlighterSettings> .GetInstance(settingsUtils),
                SettingsRepository <MousePointerCrosshairsSettings> .GetInstance(settingsUtils),
                ShellPage.SendDefaultIPCMessage);

            DataContext = ViewModel;
            InitializeComponent();
        }
        public KeyboardManagerViewModel(Func <string, int> ipcMSGCallBackFunc, Func <List <KeysDataModel>, int> filterRemapKeysList)
        {
            // set the callback functions value to hangle outgoing IPC message.
            SendConfigMSG       = ipcMSGCallBackFunc;
            FilterRemapKeysList = filterRemapKeysList;

            if (SettingsUtils.SettingsExists(PowerToyName))
            {
                // Todo: Be more resilient while reading and saving settings.
                settings = SettingsUtils.GetSettings <KeyboardManagerSettings>(PowerToyName);

                // Load profile.
                if (!LoadProfile())
                {
                    profile = new KeyboardManagerProfile();
                }
            }
            else
            {
                settings = new KeyboardManagerSettings(PowerToyName);
                SettingsUtils.SaveSettings(settings.ToJsonString(), PowerToyName);
            }

            if (SettingsUtils.SettingsExists())
            {
                generalSettings = SettingsUtils.GetSettings <GeneralSettings>(string.Empty);
            }
            else
            {
                generalSettings = new GeneralSettings();
                SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
            }
        }
Esempio n. 4
0
        public PowerLauncherViewModel()
        {
            callback = (PowerLauncherSettings settings) =>
            {
                // Propagate changes to Power Launcher through IPC
                ShellPage.DefaultSndMSGCallback(
                    string.Format("{{ \"powertoys\": {{ \"{0}\": {1} }} }}", PowerLauncherSettings.POWERTOYNAME, JsonSerializer.Serialize(settings)));
            };
            if (SettingsUtils.SettingsExists(PowerLauncherSettings.POWERTOYNAME))
            {
                settings = SettingsUtils.GetSettings <PowerLauncherSettings>(PowerLauncherSettings.POWERTOYNAME);
            }
            else
            {
                settings = new PowerLauncherSettings();
                settings.properties.open_powerlauncher.Alt    = true;
                settings.properties.open_powerlauncher.Code   = (int)Windows.System.VirtualKey.Space;
                settings.properties.maximum_number_of_results = 4;
                callback(settings);
            }

            if (SettingsUtils.SettingsExists())
            {
                generalSettings = SettingsUtils.GetSettings <GeneralSettings>();
            }
            else
            {
                generalSettings = new GeneralSettings();
            }
        }
Esempio n. 5
0
        public PowerLauncherViewModel(Func <string, int> ipcMSGCallBackFunc, int defaultKeyCode)
        {
            // set the callback functions value to hangle outgoing IPC message.
            SendConfigMSG = ipcMSGCallBackFunc;

            callback = (PowerLauncherSettings settings) =>
            {
                // Propagate changes to Power Launcher through IPC
                SendConfigMSG(
                    string.Format("{{ \"powertoys\": {{ \"{0}\": {1} }} }}", PowerLauncherSettings.ModuleName, JsonSerializer.Serialize(settings)));
            };

            if (SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
            {
                settings = SettingsUtils.GetSettings <PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
            }
            else
            {
                settings = new PowerLauncherSettings();
                settings.Properties.OpenPowerLauncher.Alt  = true;
                settings.Properties.OpenPowerLauncher.Code = defaultKeyCode;
                settings.Properties.MaximumNumberOfResults = 4;
                callback(settings);
            }

            if (SettingsUtils.SettingsExists())
            {
                generalSettings = SettingsUtils.GetSettings <GeneralSettings>();
            }
            else
            {
                generalSettings = new GeneralSettings();
            }
        }
Esempio n. 6
0
        private void LoadSettingsFromJson()
        {
            // TODO this IO call should by Async, update GetFileWatcher helper to support async
            lock (_loadingSettingsLock)
            {
                {
                    var retry      = true;
                    var retryCount = 0;

                    while (retry)
                    {
                        try
                        {
                            retryCount++;

                            if (!SettingsUtils.SettingsExists(ColorPickerModuleName))
                            {
                                Logger.LogInfo("ColorPicker settings.json was missing, creating a new one");
                                var defaultColorPickerSettings = new ColorPickerSettings();
                                defaultColorPickerSettings.Save();
                            }

                            var settings = SettingsUtils.GetSettings <ColorPickerSettings>(ColorPickerModuleName);
                            if (settings != null)
                            {
                                ChangeCursor.Value              = settings.Properties.ChangeCursor;
                                ActivationShortcut.Value        = settings.Properties.ActivationShortcut.ToString();
                                CopiedColorRepresentation.Value = settings.Properties.CopiedColorRepresentation;
                            }

                            retry = false;
                        }
                        catch (IOException ex)
                        {
                            if (retryCount > MaxNumberOfRetry)
                            {
                                retry = false;
                            }

                            Logger.LogError("Failed to read changed settings", ex);
                            Thread.Sleep(500);
                        }
#pragma warning disable CA1031 // Do not catch general exception types
                        catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                        {
                            if (retryCount > MaxNumberOfRetry)
                            {
                                retry = false;
                            }

                            Logger.LogError("Failed to read changed settings", ex);
                            Thread.Sleep(500);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
 public static void CreateSettingsIfNotExists()
 {
     if (!SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
     {
         Log.Info("|SettingsWatcher.OverloadSettings|PT Run settings.json was missing, creating a new one");
         var defaultSettings = new PowerLauncherSettings();
         defaultSettings.Save();
     }
 }
        public void SettingsFolderExistsShouldReturnFalseWhenFilePathIsNotFound()
        {
            // Arrange
            var    mockFileSystem   = new MockFileSystem();
            var    settingsUtils    = new SettingsUtils(mockFileSystem);
            string file_name_random = "test\\" + RandomString();
            string file_name_exists = "test\\exists";
            string file_contents_correct_json_content = "{\"name\":\"powertoy module name\",\"version\":\"powertoy version\"}";

            // Act
            bool pathNotFound = settingsUtils.SettingsExists(file_name_random);

            settingsUtils.SaveSettings(file_contents_correct_json_content, file_name_exists);
            bool pathFound = settingsUtils.SettingsExists(file_name_exists);

            // Assert
            Assert.IsFalse(pathNotFound);
            Assert.IsTrue(pathFound);
        }
Esempio n. 9
0
        public ColorPickerViewModel()
        {
            if (SettingsUtils.SettingsExists(ColorPickerSettings.ModuleName))
            {
                _colorPickerSettings = SettingsUtils.GetSettings <ColorPickerSettings>(ColorPickerSettings.ModuleName);
            }
            else
            {
                _colorPickerSettings = new ColorPickerSettings();
            }

            if (SettingsUtils.SettingsExists())
            {
                var generalSettings = SettingsUtils.GetSettings <GeneralSettings>();
                _isEnabled = generalSettings.Enabled.ColorPicker;
            }
        }
Esempio n. 10
0
        public ColorPickerViewModel(Func <string, int> ipcMSGCallBackFunc)
        {
            if (SettingsUtils.SettingsExists(ColorPickerSettings.ModuleName))
            {
                _colorPickerSettings = SettingsUtils.GetSettings <ColorPickerSettings>(ColorPickerSettings.ModuleName);
            }
            else
            {
                _colorPickerSettings = new ColorPickerSettings();
            }

            if (SettingsUtils.SettingsExists())
            {
                var generalSettings = SettingsUtils.GetSettings <GeneralSettings>();
                _isEnabled = generalSettings.Enabled.ColorPicker;
            }

            // set the callback functions value to hangle outgoing IPC message.
            SendConfigMSG = ipcMSGCallBackFunc;
        }
Esempio n. 11
0
        public void OverloadSettings()
        {
            Monitor.Enter(_watcherSyncObject);
            var retry      = true;
            var retryCount = 0;

            while (retry)
            {
                try
                {
                    retryCount++;
                    if (!SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
                    {
                        Debug.WriteLine("PT Run settings.json was missing, creating a new one");

                        var defaultSettings = new PowerLauncherSettings();
                        defaultSettings.Save();
                    }

                    var overloadSettings = SettingsUtils.GetSettings <PowerLauncherSettings>(PowerLauncherSettings.ModuleName);

                    var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher);
                    if (_settings.Hotkey != openPowerlauncher)
                    {
                        _settings.Hotkey = openPowerlauncher;
                    }

                    var shell = PluginManager.AllPlugins.Find(pp => pp.Metadata.Name == "Shell");
                    if (shell != null)
                    {
                        var shellSettings = shell.Plugin as ISettingProvider;
                        shellSettings.UpdateSettings(overloadSettings);
                    }

                    if (_settings.MaxResultsToShow != overloadSettings.Properties.MaximumNumberOfResults)
                    {
                        _settings.MaxResultsToShow = overloadSettings.Properties.MaximumNumberOfResults;
                    }

                    if (_settings.IgnoreHotkeysOnFullscreen != overloadSettings.Properties.IgnoreHotkeysInFullscreen)
                    {
                        _settings.IgnoreHotkeysOnFullscreen = overloadSettings.Properties.IgnoreHotkeysInFullscreen;
                    }

                    var indexer = PluginManager.AllPlugins.Find(p => p.Metadata.Name.Equals("Windows Indexer Plugin", StringComparison.OrdinalIgnoreCase));
                    if (indexer != null)
                    {
                        var indexerSettings = indexer.Plugin as ISettingProvider;
                        indexerSettings.UpdateSettings(overloadSettings);
                    }

                    if (_settings.ClearInputOnLaunch != overloadSettings.Properties.ClearInputOnLaunch)
                    {
                        _settings.ClearInputOnLaunch = overloadSettings.Properties.ClearInputOnLaunch;
                    }

                    retry = false;
                }

                // the settings application can hold a lock on the settings.json file which will result in a IOException.
                // This should be changed to properly synch with the settings app instead of retrying.
                catch (IOException e)
                {
                    if (retryCount > _maxRetries)
                    {
                        retry = false;
                    }

                    Thread.Sleep(1000);
                    Debug.WriteLine(e.Message);
                }
            }

            Monitor.Exit(_watcherSyncObject);
        }