mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
tweak plugin settings (#9522)
This commit is contained in:
@@ -100,7 +100,6 @@ namespace PowerLauncher
|
||||
StringMatcher.Instance = _stringMatcher;
|
||||
_stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
|
||||
|
||||
PluginManager.LoadPlugins(_settings.PluginSettings);
|
||||
_mainVM = new MainViewModel(_settings);
|
||||
_mainWindow = new MainWindow(_settings, _mainVM);
|
||||
API = new PublicAPIInstance(_settingsVM, _mainVM, _themeManager);
|
||||
|
||||
@@ -81,12 +81,6 @@ namespace PowerLauncher.Plugin
|
||||
{
|
||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||
metadata.PluginDirectory = pluginDirectory;
|
||||
|
||||
// for plugins which doesn't has ActionKeywords key
|
||||
metadata.SetActionKeywords(metadata.GetActionKeywords() ?? new List<string> { metadata.ActionKeyword });
|
||||
|
||||
// for plugin still use old ActionKeyword
|
||||
metadata.ActionKeyword = metadata.GetActionKeywords()?[0];
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -28,22 +28,54 @@ namespace PowerLauncher.Plugin
|
||||
|
||||
private static IEnumerable<PluginPair> _contextMenuPlugins = new List<PluginPair>();
|
||||
|
||||
private static List<PluginPair> _allPlugins;
|
||||
|
||||
// should be only used in tests
|
||||
public static void SetAllPlugins(List<PluginPair> plugins)
|
||||
{
|
||||
_allPlugins = plugins;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets directories that will hold Wox plugin directory
|
||||
/// </summary>
|
||||
public static List<PluginPair> AllPlugins { get; private set; } = new List<PluginPair>();
|
||||
public static List<PluginPair> AllPlugins
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_allPlugins == null)
|
||||
{
|
||||
_allPlugins = PluginsLoader.Plugins(PluginConfig.Parse(Directories));
|
||||
}
|
||||
|
||||
return _allPlugins;
|
||||
}
|
||||
}
|
||||
|
||||
public static IPublicAPI API { get; private set; }
|
||||
|
||||
public static readonly List<PluginPair> GlobalPlugins = new List<PluginPair>();
|
||||
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new Dictionary<string, PluginPair>();
|
||||
public static List<PluginPair> GlobalPlugins
|
||||
{
|
||||
get
|
||||
{
|
||||
return AllPlugins.Where(x => x.Metadata.IsGlobal).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<string, PluginPair> NonGlobalPlugins
|
||||
{
|
||||
get
|
||||
{
|
||||
return AllPlugins
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x.Metadata.ActionKeyword))
|
||||
.GroupBy(x => x.Metadata.ActionKeyword)
|
||||
.Select(x => x.First())
|
||||
.ToDictionary(x => x.Metadata.ActionKeyword);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly string[] Directories = { Constant.PreinstalledDirectory, Constant.PluginsDirectory };
|
||||
|
||||
// todo happlebao, this should not be public, the indicator function should be embedded
|
||||
public static PluginSettings Settings { get; set; }
|
||||
|
||||
private static List<PluginMetadata> _metadatas;
|
||||
|
||||
private static void ValidateUserDirectory()
|
||||
{
|
||||
if (!Directory.Exists(Constant.PluginsDirectory))
|
||||
@@ -75,19 +107,6 @@ namespace PowerLauncher.Plugin
|
||||
ValidateUserDirectory();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// because InitializePlugins needs API, so LoadPlugins needs to be called first
|
||||
/// todo happlebao The API should be removed
|
||||
/// </summary>
|
||||
/// <param name="settings">Plugin settings</param>
|
||||
public static void LoadPlugins(PluginSettings settings)
|
||||
{
|
||||
_metadatas = PluginConfig.Parse(Directories);
|
||||
Settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
Settings.UpdatePluginSettings(_metadatas);
|
||||
AllPlugins = PluginsLoader.Plugins(_metadatas);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call initialize for all plugins
|
||||
/// </summary>
|
||||
@@ -120,18 +139,6 @@ namespace PowerLauncher.Plugin
|
||||
});
|
||||
|
||||
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
|
||||
foreach (var plugin in AllPlugins)
|
||||
{
|
||||
if (IsGlobalPlugin(plugin.Metadata))
|
||||
{
|
||||
GlobalPlugins.Add(plugin);
|
||||
}
|
||||
|
||||
// Plugins may have multiple ActionKeywords, eg. WebSearch
|
||||
plugin.Metadata.GetActionKeywords().Where(x => x != Query.GlobalPluginWildcardSign)
|
||||
.ToList()
|
||||
.ForEach(x => NonGlobalPlugins[x] = plugin);
|
||||
}
|
||||
|
||||
if (failedPlugins.Any())
|
||||
{
|
||||
@@ -145,24 +152,6 @@ namespace PowerLauncher.Plugin
|
||||
PluginInstaller.Install(path);
|
||||
}
|
||||
|
||||
public static List<PluginPair> ValidPluginsForQuery(Query query)
|
||||
{
|
||||
if (query == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(query));
|
||||
}
|
||||
|
||||
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))
|
||||
{
|
||||
var plugin = NonGlobalPlugins[query.ActionKeyword];
|
||||
return new List<PluginPair> { plugin };
|
||||
}
|
||||
else
|
||||
{
|
||||
return GlobalPlugins;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
|
||||
public static List<Result> QueryForPlugin(PluginPair pair, Query query, bool delayedExecution = false)
|
||||
{
|
||||
@@ -245,11 +234,6 @@ namespace PowerLauncher.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsGlobalPlugin(PluginMetadata metadata)
|
||||
{
|
||||
return metadata.GetActionKeywords().Contains(Query.GlobalPluginWildcardSign);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// get specified plugin, return null if not found
|
||||
/// </summary>
|
||||
@@ -293,72 +277,6 @@ namespace PowerLauncher.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
public static bool ActionKeywordRegistered(string actionKeyword)
|
||||
{
|
||||
if (actionKeyword != Query.GlobalPluginWildcardSign &&
|
||||
NonGlobalPlugins.ContainsKey(actionKeyword))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// used to add action keyword for multiple action keyword plugin
|
||||
/// e.g. web search
|
||||
/// </summary>
|
||||
public static void AddActionKeyword(string id, string newActionKeyword)
|
||||
{
|
||||
var plugin = GetPluginForId(id);
|
||||
if (newActionKeyword == Query.GlobalPluginWildcardSign)
|
||||
{
|
||||
GlobalPlugins.Add(plugin);
|
||||
}
|
||||
else
|
||||
{
|
||||
NonGlobalPlugins[newActionKeyword] = plugin;
|
||||
}
|
||||
|
||||
plugin.Metadata.GetActionKeywords().Add(newActionKeyword);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// used to add action keyword for multiple action keyword plugin
|
||||
/// e.g. web search
|
||||
/// </summary>
|
||||
public static void RemoveActionKeyword(string id, string oldActionkeyword)
|
||||
{
|
||||
var plugin = GetPluginForId(id);
|
||||
if (oldActionkeyword == Query.GlobalPluginWildcardSign
|
||||
&& // Plugins may have multiple ActionKeywords that are global, eg. WebSearch
|
||||
plugin.Metadata.GetActionKeywords()
|
||||
.Where(x => x == Query.GlobalPluginWildcardSign)
|
||||
.ToList()
|
||||
.Count == 1)
|
||||
{
|
||||
GlobalPlugins.Remove(plugin);
|
||||
}
|
||||
|
||||
if (oldActionkeyword != Query.GlobalPluginWildcardSign)
|
||||
{
|
||||
NonGlobalPlugins.Remove(oldActionkeyword);
|
||||
}
|
||||
|
||||
plugin.Metadata.GetActionKeywords().Remove(oldActionkeyword);
|
||||
}
|
||||
|
||||
public static void ReplaceActionKeyword(string id, string oldActionKeyword, string newActionKeyword)
|
||||
{
|
||||
if (oldActionKeyword != newActionKeyword)
|
||||
{
|
||||
AddActionKeyword(id, newActionKeyword);
|
||||
RemoveActionKeyword(id, oldActionKeyword);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Dispose()
|
||||
{
|
||||
foreach (var plugin in AllPlugins)
|
||||
|
||||
@@ -12,18 +12,13 @@ namespace PowerLauncher.Plugin
|
||||
{
|
||||
public static class QueryBuilder
|
||||
{
|
||||
public static Dictionary<PluginPair, Query> Build(ref string text, Dictionary<string, PluginPair> nonGlobalPlugins)
|
||||
public static Dictionary<PluginPair, Query> Build(ref string text)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
}
|
||||
|
||||
if (nonGlobalPlugins == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(nonGlobalPlugins));
|
||||
}
|
||||
|
||||
// replace multiple white spaces with one white space
|
||||
var terms = text.Split(new[] { Query.TermSeparator }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (terms.Length == 0)
|
||||
@@ -41,12 +36,12 @@ namespace PowerLauncher.Plugin
|
||||
|
||||
string possibleActionKeyword = terms[0];
|
||||
|
||||
foreach (string pluginActionKeyword in nonGlobalPlugins.Keys)
|
||||
foreach (string pluginActionKeyword in PluginManager.NonGlobalPlugins.Keys)
|
||||
{
|
||||
// Using Ordinal since this is used internally
|
||||
if (possibleActionKeyword.StartsWith(pluginActionKeyword, StringComparison.Ordinal))
|
||||
{
|
||||
if (nonGlobalPlugins.TryGetValue(pluginActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
|
||||
if (PluginManager.NonGlobalPlugins.TryGetValue(pluginActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
|
||||
{
|
||||
// The search string is the raw query excluding the action keyword
|
||||
string search = rawQuery.Substring(pluginActionKeyword.Length).Trim();
|
||||
@@ -75,8 +70,6 @@ namespace PowerLauncher.Plugin
|
||||
// add the global plugins to the list.
|
||||
if (pluginQueryPair.Count == 0)
|
||||
{
|
||||
var globalplugins = PluginManager.GlobalPlugins;
|
||||
|
||||
foreach (PluginPair globalPlugin in PluginManager.GlobalPlugins)
|
||||
{
|
||||
if (!pluginQueryPair.ContainsKey(globalPlugin))
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Windows.Input;
|
||||
using Microsoft.PowerToys.Common.UI;
|
||||
@@ -54,6 +56,7 @@ namespace PowerLauncher
|
||||
Log.Info("PT Run settings.json was missing, creating a new one", GetType());
|
||||
|
||||
var defaultSettings = new PowerLauncherSettings();
|
||||
defaultSettings.Plugins = GetPluginsSettings();
|
||||
defaultSettings.Save(_settingsUtils);
|
||||
}
|
||||
}
|
||||
@@ -72,6 +75,26 @@ namespace PowerLauncher
|
||||
|
||||
var overloadSettings = _settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
|
||||
if (overloadSettings.Plugins == null || !overloadSettings.Plugins.Any())
|
||||
{
|
||||
// Needed to be consistent with old settings
|
||||
overloadSettings.Plugins = GetPluginsSettings();
|
||||
_settingsUtils.SaveSettings(overloadSettings.ToJsonString(), PowerLauncherSettings.ModuleName);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var setting in overloadSettings.Plugins)
|
||||
{
|
||||
var plugin = PluginManager.AllPlugins.FirstOrDefault(x => x.Metadata.ID == setting.Id);
|
||||
if (plugin != null)
|
||||
{
|
||||
plugin.Metadata.Disabled = setting.Disabled;
|
||||
plugin.Metadata.ActionKeyword = setting.ActionKeyword;
|
||||
plugin.Metadata.IsGlobal = setting.IsGlobal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher);
|
||||
if (_settings.Hotkey != openPowerlauncher)
|
||||
{
|
||||
@@ -160,5 +183,21 @@ namespace PowerLauncher
|
||||
HotkeyModel model = new HotkeyModel(hotkey.Alt, hotkey.Shift, hotkey.Win, hotkey.Ctrl, key);
|
||||
return model.ToString();
|
||||
}
|
||||
|
||||
private static IEnumerable<PowerLauncherPluginSettings> GetPluginsSettings()
|
||||
{
|
||||
return PluginManager.AllPlugins.Select(x => x.Metadata).Select(x => new PowerLauncherPluginSettings
|
||||
{
|
||||
Id = x.ID,
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
Author = x.Author,
|
||||
Disabled = x.Disabled,
|
||||
IsGlobal = x.IsGlobal,
|
||||
ActionKeyword = x.ActionKeyword,
|
||||
IconPathDark = x.IcoPathDark,
|
||||
IconPathLight = x.IcoPathLight,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -539,7 +539,7 @@ namespace PowerLauncher.ViewModel
|
||||
_updateToken = currentCancellationToken;
|
||||
var queryText = QueryText.Trim();
|
||||
|
||||
var pluginQueryPairs = QueryBuilder.Build(ref queryText, PluginManager.NonGlobalPlugins);
|
||||
var pluginQueryPairs = QueryBuilder.Build(ref queryText);
|
||||
if (pluginQueryPairs != null && pluginQueryPairs.Count > 0)
|
||||
{
|
||||
_currentQuery = queryText;
|
||||
@@ -885,7 +885,7 @@ namespace PowerLauncher.ViewModel
|
||||
|
||||
// Fix Cold start for plugins
|
||||
string s = "m";
|
||||
var pluginQueryPairs = QueryBuilder.Build(ref s, PluginManager.NonGlobalPlugins);
|
||||
var pluginQueryPairs = QueryBuilder.Build(ref s);
|
||||
|
||||
// To execute a query corresponding to each plugin
|
||||
foreach (KeyValuePair<PluginPair, Query> pluginQueryItem in pluginQueryPairs)
|
||||
|
||||
Reference in New Issue
Block a user