mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
[PowerToys Run] Plugin manager (#9872)
This commit is contained in:
@@ -12,7 +12,6 @@ using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.Logger;
|
||||
|
||||
@@ -62,15 +61,11 @@ namespace PowerLauncher.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<string, PluginPair> NonGlobalPlugins
|
||||
public static IEnumerable<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);
|
||||
return AllPlugins.Where(x => !string.IsNullOrWhiteSpace(x.Metadata.ActionKeyword));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,66 +4,34 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Mono.Collections.Generic;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace PowerLauncher.Plugin
|
||||
{
|
||||
public static class QueryBuilder
|
||||
{
|
||||
public static Dictionary<PluginPair, Query> Build(ref string text)
|
||||
public static Dictionary<PluginPair, Query> Build(string text)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
}
|
||||
|
||||
// replace multiple white spaces with one white space
|
||||
var terms = text.Split(new[] { Query.TermSeparator }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (terms.Length == 0)
|
||||
{ // nothing was typed
|
||||
return null;
|
||||
}
|
||||
text = text.Trim();
|
||||
|
||||
// This Dictionary contains the corresponding query for each plugin
|
||||
Dictionary<PluginPair, Query> pluginQueryPair = new Dictionary<PluginPair, Query>();
|
||||
|
||||
var rawQuery = string.Join(Query.TermSeparator, terms);
|
||||
|
||||
// This is the query on removing extra spaces which would be executed by global Plugins
|
||||
text = rawQuery;
|
||||
|
||||
string possibleActionKeyword = terms[0];
|
||||
|
||||
foreach (string pluginActionKeyword in PluginManager.NonGlobalPlugins.Keys)
|
||||
foreach (var plugin in PluginManager.NonGlobalPlugins)
|
||||
{
|
||||
// Using Ordinal since this is used internally
|
||||
if (possibleActionKeyword.StartsWith(pluginActionKeyword, StringComparison.Ordinal))
|
||||
var pluginActionKeyword = plugin.Metadata.ActionKeyword;
|
||||
if (plugin.Metadata.Disabled || !text.StartsWith(pluginActionKeyword, StringComparison.Ordinal))
|
||||
{
|
||||
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();
|
||||
|
||||
// To set the terms of the query after removing the action keyword
|
||||
if (possibleActionKeyword.Length > pluginActionKeyword.Length)
|
||||
{
|
||||
// If the first term contains the action keyword, then set the remaining string to be the first term
|
||||
terms[0] = possibleActionKeyword.Substring(pluginActionKeyword.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the first term is the action keyword, then skip it.
|
||||
terms = terms.Skip(1).ToArray();
|
||||
}
|
||||
|
||||
// A new query is constructed for each plugin as they have different action keywords
|
||||
var query = new Query(rawQuery, search, new ReadOnlyCollection<string>(terms), pluginActionKeyword);
|
||||
|
||||
pluginQueryPair.TryAdd(pluginPair, query);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// A new query is constructed for each plugin
|
||||
var query = new Query(text, pluginActionKeyword);
|
||||
pluginQueryPair.TryAdd(plugin, query);
|
||||
}
|
||||
|
||||
// If the user has specified a matching action keyword, then do not
|
||||
@@ -72,9 +40,9 @@ namespace PowerLauncher.Plugin
|
||||
{
|
||||
foreach (PluginPair globalPlugin in PluginManager.GlobalPlugins)
|
||||
{
|
||||
if (!pluginQueryPair.ContainsKey(globalPlugin))
|
||||
if (!globalPlugin.Metadata.Disabled && !pluginQueryPair.ContainsKey(globalPlugin))
|
||||
{
|
||||
var query = new Query(rawQuery, rawQuery, new ReadOnlyCollection<string>(terms), string.Empty);
|
||||
var query = new Query(text);
|
||||
pluginQueryPair.Add(globalPlugin, query);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +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.Plugins = GetDefaultPluginsSettings();
|
||||
defaultSettings.Save(_settingsUtils);
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ namespace PowerLauncher
|
||||
if (overloadSettings.Plugins == null || !overloadSettings.Plugins.Any())
|
||||
{
|
||||
// Needed to be consistent with old settings
|
||||
overloadSettings.Plugins = GetPluginsSettings();
|
||||
overloadSettings.Plugins = GetDefaultPluginsSettings();
|
||||
_settingsUtils.SaveSettings(overloadSettings.ToJsonString(), PowerLauncherSettings.ModuleName);
|
||||
}
|
||||
else
|
||||
@@ -91,6 +91,10 @@ namespace PowerLauncher
|
||||
plugin.Metadata.Disabled = setting.Disabled;
|
||||
plugin.Metadata.ActionKeyword = setting.ActionKeyword;
|
||||
plugin.Metadata.IsGlobal = setting.IsGlobal;
|
||||
if (plugin.Plugin is ISettingProvider)
|
||||
{
|
||||
(plugin.Plugin as ISettingProvider).UpdateSettings(setting);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,13 +105,6 @@ namespace PowerLauncher
|
||||
_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;
|
||||
@@ -118,14 +115,6 @@ namespace PowerLauncher
|
||||
_settings.IgnoreHotkeysOnFullscreen = overloadSettings.Properties.IgnoreHotkeysInFullscreen;
|
||||
}
|
||||
|
||||
// Using OrdinalIgnoreCase since this is internal
|
||||
var indexer = PluginManager.AllPlugins.Find(p => p.Metadata.Name.Equals("Windows Indexer", 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;
|
||||
@@ -184,19 +173,20 @@ namespace PowerLauncher
|
||||
return model.ToString();
|
||||
}
|
||||
|
||||
private static IEnumerable<PowerLauncherPluginSettings> GetPluginsSettings()
|
||||
private static IEnumerable<PowerLauncherPluginSettings> GetDefaultPluginsSettings()
|
||||
{
|
||||
return PluginManager.AllPlugins.Select(x => x.Metadata).Select(x => new PowerLauncherPluginSettings
|
||||
return PluginManager.AllPlugins.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,
|
||||
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>(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,7 +493,7 @@ namespace PowerLauncher.ViewModel
|
||||
Title = string.Format(CultureInfo.InvariantCulture, title, h.Query),
|
||||
SubTitle = string.Format(CultureInfo.InvariantCulture, time, h.ExecutedDateTime),
|
||||
IcoPath = "Images\\history.png",
|
||||
OriginQuery = new Query { RawQuery = h.Query },
|
||||
OriginQuery = new Query(h.Query),
|
||||
Action = _ =>
|
||||
{
|
||||
SelectedResults = Results;
|
||||
@@ -531,9 +531,10 @@ namespace PowerLauncher.ViewModel
|
||||
_updateToken = currentCancellationToken;
|
||||
var queryText = QueryText.Trim();
|
||||
|
||||
var pluginQueryPairs = QueryBuilder.Build(ref queryText);
|
||||
var pluginQueryPairs = QueryBuilder.Build(queryText);
|
||||
if (pluginQueryPairs != null && pluginQueryPairs.Count > 0)
|
||||
{
|
||||
queryText = pluginQueryPairs.Values.First().RawQuery;
|
||||
_currentQuery = queryText;
|
||||
Task.Run(
|
||||
() =>
|
||||
@@ -557,13 +558,9 @@ namespace PowerLauncher.ViewModel
|
||||
{
|
||||
var plugin = pluginQueryItem.Key;
|
||||
var query = pluginQueryItem.Value;
|
||||
|
||||
if (!plugin.Metadata.Disabled)
|
||||
{
|
||||
var results = PluginManager.QueryForPlugin(plugin, query);
|
||||
resultPluginPair.Add((results, plugin.Metadata));
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
}
|
||||
var results = PluginManager.QueryForPlugin(plugin, query);
|
||||
resultPluginPair.Add((results, plugin.Metadata));
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
}
|
||||
|
||||
lock (_addResultsLock)
|
||||
@@ -595,39 +592,36 @@ namespace PowerLauncher.ViewModel
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!plugin.Metadata.Disabled)
|
||||
Query query;
|
||||
pluginQueryPairs.TryGetValue(plugin, out query);
|
||||
|
||||
var results = PluginManager.QueryForPlugin(plugin, query, true);
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
if ((results?.Count ?? 0) != 0)
|
||||
{
|
||||
Query query;
|
||||
pluginQueryPairs.TryGetValue(plugin, out query);
|
||||
|
||||
var results = PluginManager.QueryForPlugin(plugin, query, true);
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
if ((results?.Count ?? 0) != 0)
|
||||
lock (_addResultsLock)
|
||||
{
|
||||
lock (_addResultsLock)
|
||||
// Using CurrentCultureIgnoreCase since this is user facing
|
||||
if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
// Using CurrentCultureIgnoreCase since this is user facing
|
||||
if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
// Remove the original results from the plugin
|
||||
Results.Results.RemoveAll(r => r.Result.PluginID == plugin.Metadata.ID);
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
// Remove the original results from the plugin
|
||||
Results.Results.RemoveAll(r => r.Result.PluginID == plugin.Metadata.ID);
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
// Add the new results from the plugin
|
||||
UpdateResultView(results, queryText, currentCancellationToken);
|
||||
// Add the new results from the plugin
|
||||
UpdateResultView(results, queryText, currentCancellationToken);
|
||||
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
numResults = Results.Results.Count;
|
||||
Results.Sort();
|
||||
Results.SelectedItem = Results.Results.FirstOrDefault();
|
||||
}
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
numResults = Results.Results.Count;
|
||||
Results.Sort();
|
||||
Results.SelectedItem = Results.Results.FirstOrDefault();
|
||||
}
|
||||
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
UpdateResultsListViewAfterQuery(queryText, true);
|
||||
}
|
||||
|
||||
currentCancellationToken.ThrowIfCancellationRequested();
|
||||
UpdateResultsListViewAfterQuery(queryText, true);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
@@ -875,9 +869,8 @@ namespace PowerLauncher.ViewModel
|
||||
Results.Clear();
|
||||
MainWindowVisibility = System.Windows.Visibility.Collapsed;
|
||||
|
||||
// Fix Cold start for plugins
|
||||
string s = "m";
|
||||
var pluginQueryPairs = QueryBuilder.Build(ref s);
|
||||
// Fix Cold start for plugins, "m" is just a random string needed to query results
|
||||
var pluginQueryPairs = QueryBuilder.Build("m");
|
||||
|
||||
// To execute a query corresponding to each plugin
|
||||
foreach (KeyValuePair<PluginPair, Query> pluginQueryItem in pluginQueryPairs)
|
||||
|
||||
Reference in New Issue
Block a user