2020-08-15 04:35:06 +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.
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-02-10 21:12:42 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-08-15 04:35:06 +08:00
|
|
|
|
using System.IO;
|
2020-11-03 01:33:43 +08:00
|
|
|
|
using System.IO.Abstractions;
|
2021-02-10 21:12:42 +08:00
|
|
|
|
using System.Linq;
|
2020-08-15 04:35:06 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Windows.Input;
|
2021-01-05 22:54:32 +08:00
|
|
|
|
using Microsoft.PowerToys.Common.UI;
|
2020-10-23 00:45:48 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
2020-09-09 01:04:17 +08:00
|
|
|
|
using PowerLauncher.Helper;
|
2020-11-04 04:45:01 +08:00
|
|
|
|
using PowerLauncher.Plugin;
|
2020-08-15 04:35:06 +08:00
|
|
|
|
using Wox.Infrastructure.Hotkey;
|
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
|
|
|
|
using Wox.Plugin;
|
2020-10-24 04:06:22 +08:00
|
|
|
|
using Wox.Plugin.Logger;
|
2020-09-09 01:04:17 +08:00
|
|
|
|
using JsonException = System.Text.Json.JsonException;
|
2020-08-15 04:35:06 +08:00
|
|
|
|
|
|
|
|
|
namespace PowerLauncher
|
|
|
|
|
{
|
|
|
|
|
// Watch for /Local/Microsoft/PowerToys/Launcher/Settings.json changes
|
2021-04-05 22:57:22 +08:00
|
|
|
|
public class SettingsReader : BaseModel
|
2020-08-15 04:35:06 +08:00
|
|
|
|
{
|
2020-09-22 01:14:44 +08:00
|
|
|
|
private readonly ISettingsUtils _settingsUtils;
|
|
|
|
|
|
2020-08-15 04:35:06 +08:00
|
|
|
|
private const int MaxRetries = 10;
|
2021-04-05 22:57:22 +08:00
|
|
|
|
private static readonly object _readSyncObject = new object();
|
2020-10-30 08:52:35 +08:00
|
|
|
|
private readonly PowerToysRunSettings _settings;
|
2020-10-24 06:05:07 +08:00
|
|
|
|
private readonly ThemeManager _themeManager;
|
2020-08-15 04:35:06 +08:00
|
|
|
|
|
2021-04-05 22:57:22 +08:00
|
|
|
|
private IFileSystemWatcher _watcher;
|
|
|
|
|
|
|
|
|
|
public SettingsReader(PowerToysRunSettings settings, ThemeManager themeManager)
|
2020-08-15 04:35:06 +08:00
|
|
|
|
{
|
2020-11-03 01:33:43 +08:00
|
|
|
|
_settingsUtils = new SettingsUtils();
|
2020-08-15 04:35:06 +08:00
|
|
|
|
_settings = settings;
|
2020-10-24 06:05:07 +08:00
|
|
|
|
_themeManager = themeManager;
|
2020-08-15 04:35:06 +08:00
|
|
|
|
|
2020-11-03 18:30:19 +08:00
|
|
|
|
// Apply theme at startup
|
2021-01-15 19:19:52 +08:00
|
|
|
|
_themeManager.ChangeTheme(_settings.Theme, true);
|
2020-08-15 04:35:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 01:14:44 +08:00
|
|
|
|
public void CreateSettingsIfNotExists()
|
2020-09-09 01:04:17 +08:00
|
|
|
|
{
|
2020-09-22 01:14:44 +08:00
|
|
|
|
if (!_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
2020-09-09 01:04:17 +08:00
|
|
|
|
{
|
2020-09-24 07:32:06 +08:00
|
|
|
|
Log.Info("PT Run settings.json was missing, creating a new one", GetType());
|
2020-09-22 01:14:44 +08:00
|
|
|
|
|
2020-09-09 01:04:17 +08:00
|
|
|
|
var defaultSettings = new PowerLauncherSettings();
|
2021-02-26 19:21:58 +08:00
|
|
|
|
defaultSettings.Plugins = GetDefaultPluginsSettings();
|
2020-09-22 01:14:44 +08:00
|
|
|
|
defaultSettings.Save(_settingsUtils);
|
2020-09-09 01:04:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 22:57:22 +08:00
|
|
|
|
public void ReadSettingsOnChange()
|
|
|
|
|
{
|
|
|
|
|
_watcher = Microsoft.PowerToys.Settings.UI.Library.Utilities.Helper.GetFileWatcher(PowerLauncherSettings.ModuleName, "settings.json", ReadSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReadSettings()
|
2020-08-15 04:35:06 +08:00
|
|
|
|
{
|
2021-04-05 22:57:22 +08:00
|
|
|
|
Monitor.Enter(_readSyncObject);
|
2020-08-15 04:35:06 +08:00
|
|
|
|
var retry = true;
|
|
|
|
|
var retryCount = 0;
|
|
|
|
|
while (retry)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
retryCount++;
|
2020-09-09 01:04:17 +08:00
|
|
|
|
CreateSettingsIfNotExists();
|
2020-08-15 04:35:06 +08:00
|
|
|
|
|
2021-01-14 20:14:29 +08:00
|
|
|
|
var overloadSettings = _settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
2020-08-15 04:35:06 +08:00
|
|
|
|
|
2021-03-05 21:43:08 +08:00
|
|
|
|
if (overloadSettings.Plugins == null || overloadSettings.Plugins.Count() != PluginManager.AllPlugins.Count)
|
2021-02-10 21:12:42 +08:00
|
|
|
|
{
|
|
|
|
|
// Needed to be consistent with old settings
|
2021-03-05 21:43:08 +08:00
|
|
|
|
overloadSettings.Plugins = CombineWithDefaultSettings(overloadSettings.Plugins);
|
2021-02-10 21:12:42 +08:00
|
|
|
|
_settingsUtils.SaveSettings(overloadSettings.ToJsonString(), PowerLauncherSettings.ModuleName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var setting in overloadSettings.Plugins)
|
|
|
|
|
{
|
|
|
|
|
var plugin = PluginManager.AllPlugins.FirstOrDefault(x => x.Metadata.ID == setting.Id);
|
2021-04-05 22:57:22 +08:00
|
|
|
|
plugin?.Update(setting, App.API);
|
2021-02-10 21:12:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-15 04:35:06 +08:00
|
|
|
|
var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher);
|
|
|
|
|
if (_settings.Hotkey != openPowerlauncher)
|
|
|
|
|
{
|
|
|
|
|
_settings.Hotkey = openPowerlauncher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_settings.MaxResultsToShow != overloadSettings.Properties.MaximumNumberOfResults)
|
|
|
|
|
{
|
|
|
|
|
_settings.MaxResultsToShow = overloadSettings.Properties.MaximumNumberOfResults;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_settings.IgnoreHotkeysOnFullscreen != overloadSettings.Properties.IgnoreHotkeysInFullscreen)
|
|
|
|
|
{
|
|
|
|
|
_settings.IgnoreHotkeysOnFullscreen = overloadSettings.Properties.IgnoreHotkeysInFullscreen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_settings.ClearInputOnLaunch != overloadSettings.Properties.ClearInputOnLaunch)
|
|
|
|
|
{
|
|
|
|
|
_settings.ClearInputOnLaunch = overloadSettings.Properties.ClearInputOnLaunch;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 06:05:07 +08:00
|
|
|
|
if (_settings.Theme != overloadSettings.Properties.Theme)
|
|
|
|
|
{
|
|
|
|
|
_settings.Theme = overloadSettings.Properties.Theme;
|
2021-01-15 19:19:52 +08:00
|
|
|
|
_themeManager.ChangeTheme(_settings.Theme, true);
|
2020-10-24 06:05:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 01:20:49 +08:00
|
|
|
|
if (_settings.StartupPosition != overloadSettings.Properties.Position)
|
|
|
|
|
{
|
|
|
|
|
_settings.StartupPosition = overloadSettings.Properties.Position;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-15 04:35:06 +08:00
|
|
|
|
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;
|
2020-09-24 07:32:06 +08:00
|
|
|
|
Log.Exception($"Failed to Deserialize PowerToys settings, Retrying {e.Message}", e, GetType());
|
2020-09-09 01:04:17 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(1000);
|
2020-08-15 04:35:06 +08:00
|
|
|
|
}
|
2020-09-09 01:04:17 +08:00
|
|
|
|
}
|
|
|
|
|
catch (JsonException e)
|
|
|
|
|
{
|
|
|
|
|
if (retryCount > MaxRetries)
|
|
|
|
|
{
|
|
|
|
|
retry = false;
|
2020-09-24 07:32:06 +08:00
|
|
|
|
Log.Exception($"Failed to Deserialize PowerToys settings, Creating new settings as file could be corrupted {e.Message}", e, GetType());
|
2020-08-15 04:35:06 +08:00
|
|
|
|
|
2020-09-09 01:04:17 +08:00
|
|
|
|
// Settings.json could possibly be corrupted. To mitigate this we delete the
|
|
|
|
|
// current file and replace it with a correct json value.
|
2020-09-22 01:14:44 +08:00
|
|
|
|
_settingsUtils.DeleteSettings(PowerLauncherSettings.ModuleName);
|
2020-09-09 01:04:17 +08:00
|
|
|
|
CreateSettingsIfNotExists();
|
|
|
|
|
ErrorReporting.ShowMessageBox(Properties.Resources.deseralization_error_title, Properties.Resources.deseralization_error_message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
2020-08-15 04:35:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 22:57:22 +08:00
|
|
|
|
Monitor.Exit(_readSyncObject);
|
2020-08-15 04:35:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ConvertHotkey(HotkeySettings hotkey)
|
|
|
|
|
{
|
|
|
|
|
Key key = KeyInterop.KeyFromVirtualKey(hotkey.Code);
|
|
|
|
|
HotkeyModel model = new HotkeyModel(hotkey.Alt, hotkey.Shift, hotkey.Win, hotkey.Ctrl, key);
|
|
|
|
|
return model.ToString();
|
|
|
|
|
}
|
2021-02-10 21:12:42 +08:00
|
|
|
|
|
2021-03-05 21:43:08 +08:00
|
|
|
|
private static List<PowerLauncherPluginSettings> CombineWithDefaultSettings(IEnumerable<PowerLauncherPluginSettings> plugins)
|
|
|
|
|
{
|
|
|
|
|
var results = GetDefaultPluginsSettings().ToDictionary(x => x.Id);
|
|
|
|
|
foreach (var plugin in plugins)
|
|
|
|
|
{
|
|
|
|
|
if (results.ContainsKey(plugin.Id))
|
|
|
|
|
{
|
|
|
|
|
results[plugin.Id] = plugin;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results.Values.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 19:21:58 +08:00
|
|
|
|
private static IEnumerable<PowerLauncherPluginSettings> GetDefaultPluginsSettings()
|
2021-02-10 21:12:42 +08:00
|
|
|
|
{
|
2021-02-26 19:21:58 +08:00
|
|
|
|
return PluginManager.AllPlugins.Select(x => new PowerLauncherPluginSettings()
|
2021-02-10 21:12:42 +08:00
|
|
|
|
{
|
2021-02-26 19:21:58 +08:00
|
|
|
|
Id = x.Metadata.ID,
|
|
|
|
|
Name = x.Plugin.Name,
|
|
|
|
|
Description = x.Plugin.Description,
|
|
|
|
|
Author = x.Metadata.Author,
|
|
|
|
|
Disabled = x.Metadata.Disabled,
|
|
|
|
|
IsGlobal = x.Metadata.IsGlobal,
|
|
|
|
|
ActionKeyword = x.Metadata.ActionKeyword,
|
|
|
|
|
IconPathDark = x.Metadata.IcoPathDark,
|
|
|
|
|
IconPathLight = x.Metadata.IcoPathLight,
|
|
|
|
|
AdditionalOptions = x.Plugin is ISettingProvider ? (x.Plugin as ISettingProvider).AdditionalOptions : new List<PluginAdditionalOption>(),
|
2021-02-10 21:12:42 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-08-15 04:35:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|