mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +01:00
[PTRun]Optimize loop gathering results and memory consumption in WindowWalker (#31188)
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Plugin.WindowWalker.Properties;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Plugin;
|
||||
@@ -22,33 +23,23 @@ namespace Microsoft.Plugin.WindowWalker.Components
|
||||
/// <returns>List of results</returns>
|
||||
internal static List<Result> GetResultList(List<SearchResult> searchControllerResults, bool isKeywordSearch, string icon, string infoIcon)
|
||||
{
|
||||
bool addExplorerInfo = false;
|
||||
List<Result> resultsList = new List<Result>();
|
||||
|
||||
foreach (SearchResult x in searchControllerResults)
|
||||
if (searchControllerResults == null || searchControllerResults.Count == 0)
|
||||
{
|
||||
if (string.Equals(x.Result.Process.Name, "explorer.exe", StringComparison.OrdinalIgnoreCase) && x.Result.Process.IsShellProcess)
|
||||
{
|
||||
addExplorerInfo = true;
|
||||
}
|
||||
|
||||
resultsList.Add(new Result()
|
||||
{
|
||||
Title = x.Result.Title,
|
||||
IcoPath = icon,
|
||||
SubTitle = GetSubtitle(x.Result),
|
||||
ContextData = x.Result,
|
||||
Action = c =>
|
||||
{
|
||||
x.Result.SwitchToWindow();
|
||||
return true;
|
||||
},
|
||||
|
||||
// For debugging you can set the second parameter to true to see more information.
|
||||
ToolTipData = GetToolTip(x.Result, false),
|
||||
});
|
||||
return new List<Result>();
|
||||
}
|
||||
|
||||
List<Result> resultsList = new List<Result>(searchControllerResults.Count);
|
||||
bool addExplorerInfo = searchControllerResults.Any(x =>
|
||||
string.Equals(x.Result.Process.Name, "explorer.exe", StringComparison.OrdinalIgnoreCase) &&
|
||||
x.Result.Process.IsShellProcess);
|
||||
|
||||
// Process each SearchResult to convert it into a Result.
|
||||
// Using parallel processing if the operation is CPU-bound and the list is large.
|
||||
resultsList = searchControllerResults
|
||||
.AsParallel()
|
||||
.Select(x => CreateResultFromSearchResult(x, icon))
|
||||
.ToList();
|
||||
|
||||
if (addExplorerInfo && isKeywordSearch && !WindowWalkerSettings.Instance.HideExplorerSettingInfo)
|
||||
{
|
||||
resultsList.Add(GetExplorerInfoResult(infoIcon));
|
||||
@@ -57,6 +48,31 @@ namespace Microsoft.Plugin.WindowWalker.Components
|
||||
return resultsList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a Result object from a given SearchResult.
|
||||
/// </summary>
|
||||
/// <param name="searchResult">The SearchResult object to convert.</param>
|
||||
/// <param name="icon">The path to the icon that should be used for the Result.</param>
|
||||
/// <returns>A Result object populated with data from the SearchResult.</returns>
|
||||
private static Result CreateResultFromSearchResult(SearchResult searchResult, string icon)
|
||||
{
|
||||
return new Result
|
||||
{
|
||||
Title = searchResult.Result.Title,
|
||||
IcoPath = icon,
|
||||
SubTitle = GetSubtitle(searchResult.Result),
|
||||
ContextData = searchResult.Result,
|
||||
Action = c =>
|
||||
{
|
||||
searchResult.Result.SwitchToWindow();
|
||||
return true;
|
||||
},
|
||||
|
||||
// For debugging you can set the second parameter to true to see more information.
|
||||
ToolTipData = GetToolTip(searchResult.Result, false),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the subtitle for a result
|
||||
/// </summary>
|
||||
|
||||
@@ -220,8 +220,7 @@ namespace PowerLauncher.Plugin
|
||||
|
||||
if (results != null)
|
||||
{
|
||||
UpdatePluginMetadata(results, metadata, query);
|
||||
UpdateResultWithActionKeyword(results, query);
|
||||
UpdateResults(results, metadata, query);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -233,14 +232,6 @@ namespace PowerLauncher.Plugin
|
||||
metadata.QueryCount += 1;
|
||||
metadata.AvgQueryTime = metadata.QueryCount == 1 ? milliseconds : (metadata.AvgQueryTime + milliseconds) / 2;
|
||||
|
||||
if (results != null)
|
||||
{
|
||||
foreach (var result in results)
|
||||
{
|
||||
result.Metadata = pair.Metadata;
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -251,10 +242,15 @@ namespace PowerLauncher.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Result> UpdateResultWithActionKeyword(List<Result> results, Query query)
|
||||
private static void UpdateResults(List<Result> results, PluginMetadata metadata, Query query)
|
||||
{
|
||||
foreach (Result result in results)
|
||||
{
|
||||
result.PluginDirectory = metadata.PluginDirectory;
|
||||
result.PluginID = metadata.ID;
|
||||
result.OriginQuery = query;
|
||||
result.Metadata = metadata;
|
||||
|
||||
if (string.IsNullOrEmpty(result.QueryTextDisplay))
|
||||
{
|
||||
result.QueryTextDisplay = result.Title;
|
||||
@@ -266,8 +262,6 @@ namespace PowerLauncher.Plugin
|
||||
result.QueryTextDisplay = string.Format(CultureInfo.CurrentCulture, "{0} {1}", query.ActionKeyword, result.QueryTextDisplay);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query)
|
||||
|
||||
Reference in New Issue
Block a user