Use Flow API to search

This commit is contained in:
Kevin Zhang 2021-07-19 17:17:09 +08:00
parent 9e9820bce9
commit 2c45ec61f5
4 changed files with 61 additions and 55 deletions

View file

@ -8,6 +8,7 @@ using System.Diagnostics;
using System.Linq;
using System.Text;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedModels;
using Microsoft.PowerToys.Run.Plugin.WindowsSettings.Properties;
namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings.Helper
@ -17,6 +18,10 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings.Helper
/// </summary>
internal static class ResultHelper
{
private static IPublicAPI? _api;
public static void Init(IPublicAPI api) => _api = api;
/// <summary>
/// Return a list with <see cref="Result"/>s, based on the given list.
/// </summary>
@ -25,32 +30,64 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings.Helper
/// <returns>A list with <see cref="Result"/>.</returns>
internal static List<Result> GetResultList(
in IEnumerable<WindowsSetting> list,
string query,
in string iconPath)
Query query,
string iconPath)
{
var resultList = new List<Result>(list.Count());
var resultList = new List<Result>();
foreach (var entry in list)
{
var result = new Result
{
Action = (_) => DoOpenSettingsAction(entry),
IcoPath = iconPath,
SubTitle = $"{Resources.Area} \"{entry.Area}\" {Resources.SubtitlePreposition} {entry.Type}",
Title = entry.Name,
ContextData = entry,
};
var result = Predicate();
if (result is null)
continue;
AddOptionalToolTip(entry, result);
resultList.Add(result);
}
SetScores(resultList, query);
Result NewSettingResult(int score) => new Result()
{
Action = _ => DoOpenSettingsAction(entry),
IcoPath = iconPath,
SubTitle = $"{Resources.Area} \"{entry.Area}\" {Resources.SubtitlePreposition} {entry.Type}",
Title = entry.Name,
ContextData = entry,
Score = score
};
Result? Predicate()
{
Debug.Assert(_api != null, nameof(_api) + " != null");
var nameMatch = _api.FuzzySearch(query.Search, entry.Name);
if (nameMatch.IsSearchPrecisionScoreMet())
{
var settingResult = NewSettingResult(nameMatch.Score);
settingResult.TitleHighlightData = nameMatch.MatchData;
return settingResult;
}
var areaMatch = _api.FuzzySearch(query.Search, entry.Area);
if (areaMatch.IsSearchPrecisionScoreMet())
{
var settingResult = NewSettingResult(areaMatch.Score);
return settingResult;
}
return entry.AltNames?
.Select(altName => _api.FuzzySearch(query.Search, altName))
.Where(match => match.IsSearchPrecisionScoreMet())
.Select(altNameMatch => NewSettingResult(altNameMatch.Score))
.FirstOrDefault();
}
}
return resultList;
}
/// <summary>
/// Add a tool-tip to the given <see cref="Result"/>, based o the given <see cref="IWindowsSetting"/>.
/// </summary>
@ -102,7 +139,7 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings.Helper
if (command.Contains(' '))
{
var commandSplit = command.Split(' ');
var file = commandSplit.FirstOrDefault();
var file = commandSplit.First();
var arguments = command[file.Length..].TrimStart();
processStartInfo = new ProcessStartInfo(file, arguments)

View file

@ -64,7 +64,7 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings.Helper
/// <returns>A registry value or <see cref="uint.MinValue"/> on error.</returns>
private static uint GetNumericRegistryValue(in string registryKey, in string valueName)
{
object registryValueData;
object? registryValueData;
try
{

6
Log.cs
View file

@ -6,7 +6,7 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings
{
public static class Log
{
private static IPublicAPI _api;
private static IPublicAPI? _api;
public static void Init(IPublicAPI api)
{
@ -14,11 +14,11 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings
}
public static void Exception(string message, Exception exception, Type type, [CallerMemberName] string methodName = "")
{
_api.LogException(type.FullName, message, exception, methodName);
_api?.LogException(type.FullName, message, exception, methodName);
}
public static void Warn(string message, Type type, [CallerMemberName] string methodName = "")
{
_api.LogWarn(type.FullName, message, methodName);
_api?.LogWarn(type.FullName, message, methodName);
}
}
}

41
Main.cs
View file

@ -82,6 +82,9 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings
_settingsList = JsonSettingsListHelper.ReadAllPossibleSettings();
_settingsList = UnsupportedSettingsHelper.FilterByBuild(_settingsList);
Log.Init(_context.API);
ResultHelper.Init(_context.API);
TranslationHelper.TranslateAllSettings(_settingsList);
}
@ -97,45 +100,11 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings
return new List<Result>(0);
}
var filteredList = _settingsList
.Where(Predicate)
.OrderBy(found => found.Name);
var newList = ResultHelper.GetResultList(filteredList, query.Search, _defaultIconPath);
var newList = ResultHelper.GetResultList(_settingsList, query, _defaultIconPath);
return newList;
bool Predicate(WindowsSetting found)
{
if (found.Name.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
// Search for Area only by key char
if (found.Area.Contains(query.Search.Replace(":", string.Empty), StringComparison.CurrentCultureIgnoreCase)
&& query.Search.EndsWith(":"))
{
return true;
}
if (found.Area.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
if (found.AltNames is not null)
{
foreach (var altName in found.AltNames)
{
if (altName.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
}
}
return false;
}
}
/// <summary>