[PT Run] Settings plugin: Settings path filter and bug fixes (#13151)

* Create filter method

* adding call of new method

* solve bug #13150

* fixes and comment updates

* fix typos

* Update filter method

* Improve performance on empty queries

* fix typos
This commit is contained in:
Heiko 2021-09-15 19:37:36 +02:00 committed by GitHub
parent a0a355c3a7
commit 8e350ca4a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 24 deletions

View file

@ -154,39 +154,98 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings.Helper
continue;
}
if (windowsSetting.Name.StartsWith(query, StringComparison.CurrentCultureIgnoreCase))
if (!string.IsNullOrWhiteSpace(query))
{
result.Score = highScore--;
continue;
}
if (windowsSetting.Name.StartsWith(query, StringComparison.CurrentCultureIgnoreCase))
{
result.Score = highScore--;
continue;
}
// If query starts with second or next word of name, set score.
if (windowsSetting.Name.Contains($" {query}", StringComparison.CurrentCultureIgnoreCase))
{
result.Score = mediumScore--;
continue;
}
// If query starts with second or next word of name, set score.
if (windowsSetting.Name.Contains($" {query}", StringComparison.CurrentCultureIgnoreCase))
{
result.Score = mediumScore--;
continue;
}
if (windowsSetting.Areas.Any(x => x.StartsWith(query, StringComparison.CurrentCultureIgnoreCase)))
{
result.Score = lowScore--;
continue;
}
if (windowsSetting.Areas is null)
{
result.Score = lowScore--;
continue;
}
if (windowsSetting.AltNames is null)
{
result.Score = lowScore--;
continue;
}
if (windowsSetting.Areas.Any(x => x.StartsWith(query, StringComparison.CurrentCultureIgnoreCase)))
{
result.Score = lowScore--;
continue;
}
if (windowsSetting.AltNames.Any(x => x.StartsWith(query, StringComparison.CurrentCultureIgnoreCase)))
{
result.Score = mediumScore--;
continue;
if (windowsSetting.AltNames is null)
{
result.Score = lowScore--;
continue;
}
if (windowsSetting.AltNames.Any(x => x.StartsWith(query, StringComparison.CurrentCultureIgnoreCase)))
{
result.Score = mediumScore--;
continue;
}
}
result.Score = lowScore--;
}
}
/// <summary>
/// Checks if a setting <see cref="WindowsSetting"/> matches the search string <see cref="Query.Search"/> to filter settings by settings path.
/// This method is called from the <see cref="Predicate{T}"/> method in <see cref="Main.Query(Query)"/> if the search string <see cref="Query.Search"/> contains the character ">".
/// </summary>
/// <param name="found">The WindowsSetting's result that should be checked.</param>
/// <param name="queryString">The searchString entered by the user <see cref="Query.Search"/>s.</param>
internal static bool FilterBySettingsPath(in WindowsSetting found, in string queryString)
{
if (!queryString.Contains('>'))
{
return false;
}
// Init vars
var queryElements = queryString.Split('>');
List<string> settingsPath = new List<string>();
settingsPath.Add(found.Type);
if (!(found.Areas is null))
{
settingsPath.AddRange(found.Areas);
}
// Compare query and settings path
for (int i = 0; i < queryElements.Length; i++)
{
if (string.IsNullOrWhiteSpace(queryElements[i]))
{
// The queryElement is an WhiteSpace. Nothing to compare.
break;
}
if (i < settingsPath.Count)
{
if (!settingsPath[i].StartsWith(queryElements[i], StringComparison.CurrentCultureIgnoreCase))
{
return false;
}
}
else
{
// The user has entered more query parts than existing elements in settings path.
return false;
}
}
// Return "true" if <found> matches <queryString>.
return true;
}
}
}

View file

@ -110,6 +110,12 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings
bool Predicate(WindowsSetting found)
{
if (string.IsNullOrWhiteSpace(query.Search))
{
// If no search string is entered skip query comparison.
return true;
}
if (found.Name.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
{
return true;
@ -145,6 +151,12 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings
}
}
// Search by key char '>' for app name and settings path
if (query.Search.Contains('>'))
{
return ResultHelper.FilterBySettingsPath(found, query.Search);
}
return false;
}
}