[Settings] update additional options for launcher plugins (#12920)

This commit is contained in:
Davide Giacometti 2021-09-07 00:19:00 +02:00 committed by GitHub
parent 2fe6282157
commit f00bf7cf19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,6 @@
// 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.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
@ -39,6 +38,10 @@ namespace PowerLauncher
_settings = settings;
_themeManager = themeManager;
var overloadSettings = _settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
UpdateSettings(overloadSettings);
_settingsUtils.SaveSettings(overloadSettings.ToJsonString(), PowerLauncherSettings.ModuleName);
// Apply theme at startup
_themeManager.ChangeTheme(_settings.Theme, true);
}
@ -85,19 +88,10 @@ namespace PowerLauncher
Log.Info($"Successfully read new settings. retryCount={retryCount}", GetType());
}
if (overloadSettings.Plugins == null || overloadSettings.Plugins.Count() != PluginManager.AllPlugins.Count)
foreach (var setting in overloadSettings.Plugins)
{
// Needed to be consistent with old settings
overloadSettings.Plugins = CombineWithDefaultSettings(overloadSettings.Plugins);
_settingsUtils.SaveSettings(overloadSettings.ToJsonString(), PowerLauncherSettings.ModuleName);
}
else
{
foreach (var setting in overloadSettings.Plugins)
{
var plugin = PluginManager.AllPlugins.FirstOrDefault(x => x.Metadata.ID == setting.Id);
plugin?.Update(setting, App.API);
}
var plugin = PluginManager.AllPlugins.FirstOrDefault(x => x.Metadata.ID == setting.Id);
plugin?.Update(setting, App.API);
}
var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher);
@ -179,20 +173,6 @@ namespace PowerLauncher
return model.ToString();
}
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();
}
private static string GetIcon(PluginMetadata metadata, string iconPath)
{
var pluginDirectory = Path.GetFileName(metadata.PluginDirectory);
@ -215,5 +195,40 @@ namespace PowerLauncher
AdditionalOptions = x.Plugin is ISettingProvider ? (x.Plugin as ISettingProvider).AdditionalOptions : new List<PluginAdditionalOption>(),
});
}
/// <summary>
/// Add new plugins and updates additional options for existing ones
/// </summary>
private static void UpdateSettings(PowerLauncherSettings settings)
{
var defaultPlugins = GetDefaultPluginsSettings().ToDictionary(x => x.Id);
foreach (PowerLauncherPluginSettings plugin in settings.Plugins)
{
if (defaultPlugins.ContainsKey(plugin.Id))
{
var additionalOptions = CombineAdditionalOptions(defaultPlugins[plugin.Id].AdditionalOptions, plugin.AdditionalOptions);
plugin.Name = defaultPlugins[plugin.Id].Name;
plugin.Description = defaultPlugins[plugin.Id].Description;
defaultPlugins[plugin.Id] = plugin;
defaultPlugins[plugin.Id].AdditionalOptions = additionalOptions;
}
}
settings.Plugins = defaultPlugins.Values.ToList();
}
private static IEnumerable<PluginAdditionalOption> CombineAdditionalOptions(IEnumerable<PluginAdditionalOption> defaultAdditionalOptions, IEnumerable<PluginAdditionalOption> additionalOptions)
{
var defaultOptions = defaultAdditionalOptions.ToDictionary(x => x.Key);
foreach (var option in additionalOptions)
{
if (defaultOptions.ContainsKey(option.Key))
{
defaultOptions[option.Key].Value = option.Value;
}
}
return defaultOptions.Values;
}
}
}