[PTRun][TimeZone]Fix empty subtiles and missing results (#18632)

* Fix for #16627, Partial fixers for #16695 and #17349

* remove none related  PR changes

* more PR related fixes
This commit is contained in:
Tobias Sekan 2022-06-24 12:00:46 +00:00 committed by GitHub
parent 6f5429ec49
commit 7af8b930be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 249 additions and 61 deletions

View File

@ -404,6 +404,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerToys.Settings", "src\s
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PowerRenameUI", "src\modules\powerrename\PowerRenameUILib\PowerRenameUI.vcxproj", "{27718999-C175-450A-861C-89F911E16A88}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests", "src\modules\launcher\Plugins\Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests\Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests.csproj", "{C5D46169-5334-48C3-8C28-644C72832E54}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.PowerToys.Run.Plugin.OneNote", "src\modules\launcher\Plugins\Microsoft.PowerToys.Run.Plugin.OneNote\Microsoft.PowerToys.Run.Plugin.OneNote.csproj", "{5A1DB2F0-0715-4B3B-98E6-79BC41540045}"
EndProject
Global
@ -1563,6 +1565,18 @@ Global
{27718999-C175-450A-861C-89F911E16A88}.Release|x64.Build.0 = Release|x64
{27718999-C175-450A-861C-89F911E16A88}.Release|x86.ActiveCfg = Release|x64
{27718999-C175-450A-861C-89F911E16A88}.Release|x86.Build.0 = Release|x64
{C5D46169-5334-48C3-8C28-644C72832E54}.Debug|ARM64.ActiveCfg = Debug|ARM64
{C5D46169-5334-48C3-8C28-644C72832E54}.Debug|ARM64.Build.0 = Debug|ARM64
{C5D46169-5334-48C3-8C28-644C72832E54}.Debug|x64.ActiveCfg = Debug|x64
{C5D46169-5334-48C3-8C28-644C72832E54}.Debug|x64.Build.0 = Debug|x64
{C5D46169-5334-48C3-8C28-644C72832E54}.Debug|x86.ActiveCfg = Debug|x64
{C5D46169-5334-48C3-8C28-644C72832E54}.Debug|x86.Build.0 = Debug|x64
{C5D46169-5334-48C3-8C28-644C72832E54}.Release|ARM64.ActiveCfg = Release|ARM64
{C5D46169-5334-48C3-8C28-644C72832E54}.Release|ARM64.Build.0 = Release|ARM64
{C5D46169-5334-48C3-8C28-644C72832E54}.Release|x64.ActiveCfg = Release|x64
{C5D46169-5334-48C3-8C28-644C72832E54}.Release|x64.Build.0 = Release|x64
{C5D46169-5334-48C3-8C28-644C72832E54}.Release|x86.ActiveCfg = Release|x64
{C5D46169-5334-48C3-8C28-644C72832E54}.Release|x86.Build.0 = Release|x64
{5A1DB2F0-0715-4B3B-98E6-79BC41540045}.Debug|ARM64.ActiveCfg = Debug|ARM64
{5A1DB2F0-0715-4B3B-98E6-79BC41540045}.Debug|ARM64.Build.0 = Debug|ARM64
{5A1DB2F0-0715-4B3B-98E6-79BC41540045}.Debug|x64.ActiveCfg = Debug|x64
@ -1708,6 +1722,7 @@ Global
{8FE5A5EE-1B59-401C-9FB3-B04ECD3E29C1} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
{020A7474-3601-4160-A159-D7B70B77B15F} = {C3081D9A-1586-441A-B5F4-ED815B3719C1}
{27718999-C175-450A-861C-89F911E16A88} = {89E20BCE-EB9C-46C8-8B50-E01A82E6FDC3}
{C5D46169-5334-48C3-8C28-644C72832E54} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
{5A1DB2F0-0715-4B3B-98E6-79BC41540045} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution

View File

@ -1,10 +1,10 @@
[*.cs]
[*.cs]
# SA1201: Elements should appear in the correct order
dotnet_diagnostic.SA1201.severity = none
# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = suggestion
# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = suggestion
# CA1051: Do not declare visible instance fields
dotnet_code_quality.ca1051.exclude_structs = true
dotnet_code_quality.ca1051.exclude_structs = true

View File

@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.PowerToys.Run.Plugin.TimeZone\Microsoft.PowerToys.Run.Plugin.TimeZone.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\..\codeAnalysis\GlobalSuppressions.cs">
<Link>GlobalSuppressions.cs</Link>
</Compile>
<AdditionalFiles Include="..\..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Linq;
using Microsoft.PowerToys.Run.Plugin.TimeZone.Classes;
using Microsoft.PowerToys.Run.Plugin.TimeZone.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NLog;
using Wox.Plugin;
namespace Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests
{
[TestClass]
public class ResultHelperTest
{
private TimeZoneList _timeZoneList;
private TimeZoneSettings _timeZoneSettings;
private string _actionKeyword;
[TestInitialize]
public void SetUp()
{
_actionKeyword = "&";
_timeZoneList = JsonHelper.ReadAllPossibleTimeZones();
_timeZoneSettings = new TimeZoneSettings
{
ShowTimeNames = true,
ShowTimeZoneNames = true,
};
}
[DataTestMethod]
[DataRow("&MEZ", 1)]
[DataRow("&GMT", 1)]
[DataRow("&Germany", 1)] // https://github.com/microsoft/PowerToys/issues/17349
[DataRow("&AWST", 1)] // https://github.com/microsoft/PowerToys/issues/16695
[DataRow("&AEDT", 1)] // https://github.com/microsoft/PowerToys/issues/16695
[DataRow("&AEST", 1)] // https://github.com/microsoft/PowerToys/issues/16695
public void GetResultsTest(string search, int expectedResultCount)
{
var query = new Query(search, _actionKeyword);
var results = ResultHelper.GetResults(_timeZoneList.TimeZones, _timeZoneSettings, query, string.Empty);
Assert.AreEqual(results.Count(), expectedResultCount);
foreach (var result in results)
{
Assert.AreEqual(!result.Title.Contains("UTC"), _timeZoneSettings.ShowTimeZoneNames);
Assert.IsFalse(string.IsNullOrWhiteSpace(result.SubTitle));
}
}
}
}

View File

@ -86,7 +86,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Classes
{
var option = settings.AdditionalOptions.FirstOrDefault(x => x.Key == name);
// As a fallback if a setting isn't available, we use the value defined in the method GetAdditionalOptions()
// As a fall-back if a setting isn't available, we use the value defined in the method GetAdditionalOptions()
var settingsValue = option?.Value
?? GetAdditionalOptions().FirstOrDefault(x => x.Key == name)?.Value
?? default;

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
@ -63,34 +62,27 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="iconPath">The path to the icon that is used for each result.</param>
/// <param name="dateTime">The current time in UTC for the <see cref="Result"/>.</param>
/// <returns>A list with <see cref="Result"/>.</returns>
private static IEnumerable<Result> GetResults(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings options, in Query query, in string iconPath, in DateTime dateTime)
internal static IEnumerable<Result> GetResults(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings options, in Query query, in string iconPath, in DateTime dateTime)
{
var results = new Collection<Result>();
var standardTitle = GetTitle(timeZoneProperties, options, query, dateTime, false);
var daylightTitle = GetTitle(timeZoneProperties, options, query, dateTime, true);
var standardTitleResult = GetTitle(timeZoneProperties, options, query, dateTime, false);
var daylightTitleResult = GetTitle(timeZoneProperties, options, query, dateTime, true);
if (standardTitle.Equals(daylightTitle))
if (standardTitleResult.Equals(daylightTitleResult))
{
results.Add(new Result
{
ContextData = GetTimeInTimeZone(timeZoneProperties, dateTime, false),
IcoPath = iconPath,
Title = standardTitle.ToString(),
Title = standardTitleResult.ToString(),
SubTitle = GetAllCountries(timeZoneProperties, query, maxLength: 100).ToString(),
ToolTipData = new ToolTipData(standardTitle.ToString(), GetAllToolTip(timeZoneProperties, options).ToString()),
ToolTipData = new ToolTipData(standardTitleResult.ToString(), GetAllToolTip(timeZoneProperties, options).ToString()),
});
return results;
}
if (!MatchTimeZoneShortcut(timeZoneProperties, query)
&& !MatchTimeZoneNames(timeZoneProperties, query)
&& !MatchOffset(timeZoneProperties, query))
{
return results;
}
if (MatchStandardTimeShortcuts(timeZoneProperties, query)
|| MatchStandardTimeNames(timeZoneProperties, query)
|| MatchStandardCountries(timeZoneProperties, query))
@ -106,8 +98,8 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
ContextData = GetTimeInTimeZone(timeZoneProperties, dateTime, false),
IcoPath = iconPath,
SubTitle = GetStandardCountries(timeZoneProperties, query, maxLength: 100).ToString(),
Title = standardTitle.ToString(),
ToolTipData = new ToolTipData(standardTitle.ToString(), GetStandardToolTip(timeZoneProperties, options).ToString()),
Title = standardTitleResult.ToString(),
ToolTipData = new ToolTipData(standardTitleResult.ToString(), GetStandardToolTip(timeZoneProperties, options).ToString()),
});
}
@ -126,8 +118,8 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
ContextData = GetTimeInTimeZone(timeZoneProperties, dateTime, true),
IcoPath = iconPath,
SubTitle = GetDaylightCountries(timeZoneProperties, query, maxLength: 100).ToString(),
Title = daylightTitle.ToString(),
ToolTipData = new ToolTipData(daylightTitle.ToString(), GetDaylightToolTip(timeZoneProperties, options).ToString()),
Title = daylightTitleResult.ToString(),
ToolTipData = new ToolTipData(daylightTitleResult.ToString(), GetDaylightToolTip(timeZoneProperties, options).ToString()),
});
}
@ -141,7 +133,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="dateTime">The current time in UTC.</param>
/// <param name="daylightSavingTime">indicate that the result is for a time zone that use a daylight saving time.</param>
/// <returns>The current local time in a time zone.</returns>
private static DateTime GetTimeInTimeZone(in TimeZoneProperties timeZoneProperties, in DateTime dateTime, in bool daylightSavingTime)
internal static DateTime GetTimeInTimeZone(in TimeZoneProperties timeZoneProperties, in DateTime dateTime, in bool daylightSavingTime)
{
foreach (var timeZoneInfo in TimeZoneInfo.GetSystemTimeZones())
{
@ -166,7 +158,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="dateTime">The current time in UTC.</param>
/// <param name="daylightSavingTime">indicate that the result is for a time zone that use a daylight saving time.</param>
/// <returns>A title for a time zone.</returns>
private static StringBuilder GetTitle(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings, in Query query, in DateTime dateTime, in bool daylightSavingTime)
internal static StringBuilder GetTitle(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings, in Query query, in DateTime dateTime, in bool daylightSavingTime)
{
var stringBuilder = new StringBuilder();
@ -188,7 +180,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone that contain all information.</param>
/// <param name="timeZoneSettings">Additional options to limit the results.</param>
/// <returns>A tool-tip with countries that use the standard time.</returns>
private static StringBuilder GetStandardToolTip(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings)
internal static StringBuilder GetStandardToolTip(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings)
{
var countries = GetStandardCountries(timeZoneProperties, null, maxLength: int.MaxValue);
var names = GetNames(timeZoneProperties, timeZoneSettings, null, maxLength: int.MaxValue);
@ -223,7 +215,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone that contain all information.</param>
/// <param name="timeZoneSettings">Additional options to limit the type of the names.</param>
/// <returns>A tool-tip with countries that use the daylight saving time.</returns>
private static StringBuilder GetDaylightToolTip(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings)
internal static StringBuilder GetDaylightToolTip(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings)
{
var dstCountries = GetDaylightCountries(timeZoneProperties, null, maxLength: int.MaxValue);
var names = GetNames(timeZoneProperties, timeZoneSettings, null, maxLength: int.MaxValue);
@ -258,7 +250,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone that contain all information.</param>
/// <param name="timeZoneSettings">Additional options to limit the type of the names.</param>
/// <returns>A tool-tip with countries.</returns>
private static StringBuilder GetAllToolTip(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings)
internal static StringBuilder GetAllToolTip(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings)
{
var countries = GetStandardCountries(timeZoneProperties, null, maxLength: int.MaxValue);
var dstCountries = GetDaylightCountries(timeZoneProperties, null, maxLength: int.MaxValue);
@ -307,7 +299,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="query">The query that should match.</param>
/// <param name="maxLength">The maximum length of the result.</param>
/// <returns>All know names of the given time zone.</returns>
private static StringBuilder GetNames(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings, Query? query, in int maxLength)
internal static StringBuilder GetNames(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings timeZoneSettings, Query? query, in int maxLength)
{
var allNames = new List<string>();
@ -337,6 +329,25 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
{
names = allNames;
}
else if (MatchStandardCountries(timeZoneProperties, query) || MatchDaylightCountries(timeZoneProperties, query))
{
names = allNames;
}
else if (MatchStandardTimeShortcuts(timeZoneProperties, query) || MatchDaylightTimeShortcuts(timeZoneProperties, query))
{
var matches = new Collection<string>();
foreach (var name in allNames)
{
var matchAll = query.Search.All(x => name.Contains(x, StringComparison.CurrentCultureIgnoreCase));
if (matchAll)
{
matches.Add(name);
}
}
names = matches;
}
else
{
names = allNames.Where(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
@ -387,6 +398,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
}
else
{
// only when we don't have found any names so we
stringBuilder.Append("UTC");
var totalMinutes = timeZoneProperties.OffsetAsTimeSpan.TotalMinutes;
@ -409,11 +421,11 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
}
/// <summary>
/// Return all standard time names shortcuts of the given time zone.
/// Return all standard time name shortcuts of the given time zone.
/// </summary>
/// <param name="timeZoneProperties">The time zone that contain a hand of names.</param>
/// <returns>All standard time names shortcuts of the given time zone.</returns>
private static StringBuilder GetStandardShortcuts(in TimeZoneProperties timeZoneProperties)
/// <returns>All standard time name shortcuts of the given time zone.</returns>
internal static StringBuilder GetStandardShortcuts(in TimeZoneProperties timeZoneProperties)
{
var stringBuilder = new StringBuilder();
var lastEntry = timeZoneProperties.ShortcutsStandard.LastOrDefault();
@ -433,11 +445,11 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
}
/// <summary>
/// Return all know daylight time names shortcuts of the given time zone.
/// Return all know daylight time name shortcuts of the given time zone.
/// </summary>
/// <param name="timeZoneProperties">The time zone that contain a hand of names.</param>
/// <returns>All know daylight time names shortcuts of the given time zone.</returns>
private static StringBuilder GetDaylightShortcuts(in TimeZoneProperties timeZoneProperties)
/// <returns>All know daylight time name shortcuts of the given time zone.</returns>
internal static StringBuilder GetDaylightShortcuts(in TimeZoneProperties timeZoneProperties)
{
var stringBuilder = new StringBuilder();
var lastEntry = timeZoneProperties.ShortcutsDaylight.LastOrDefault();
@ -457,13 +469,13 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
}
/// <summary>
/// Return all countries that use the standard time of the given time zone.
/// Return all countries that use the standard time of the given time zone that match the given query.
/// </summary>
/// <param name="timeZoneProperties">The time zone that contain the countries.</param>
/// <param name="query">The <see cref="Query"/> that should match a country that use standard time.</param>
/// <param name="maxLength">The maximum length of the result.</param>
/// <returns>All countries that use the standard time of the given time zone.</returns>
private static StringBuilder GetStandardCountries(in TimeZoneProperties timeZoneProperties, Query? query, in int maxLength)
internal static StringBuilder GetStandardCountries(in TimeZoneProperties timeZoneProperties, Query? query, in int maxLength)
{
IEnumerable<string> countries;
@ -471,15 +483,36 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
{
countries = timeZoneProperties.CountriesStandard;
}
else if (MatchStandardTimeShortcuts(timeZoneProperties, query))
{
var matches = new Collection<string>();
foreach (var name in timeZoneProperties.CountriesStandard)
{
var matchAll = query.Search.All(x => name.Contains(x, StringComparison.CurrentCultureIgnoreCase));
if (matchAll)
{
matches.Add(name);
}
}
countries = matches;
}
else
{
countries = timeZoneProperties.CountriesStandard.Where(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
}
// When the search query don't match a country, show all countries
if (countries is null || !countries.Any())
{
countries = timeZoneProperties.CountriesStandard;
}
var stringBuilder = new StringBuilder();
var lastEntry = countries.LastOrDefault();
foreach (var country in countries)
foreach (var country in countries.Distinct())
{
stringBuilder.Append(country);
@ -544,13 +577,13 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
}
/// <summary>
/// Return all countries that use the daylight saving time of the given time zone.
/// Return all countries that use the daylight saving time of the given time zone that match the given query
/// </summary>
/// <param name="timeZoneProperties">The time zone that contain the countries.</param>
/// <param name="query">The <see cref="Query"/> that should match a country that use daylight time.</param>
/// <param name="maxLength">The maximum length of the result.</param>
/// <returns>All countries that use the daylight saving time of the given time zone.</returns>
private static StringBuilder GetDaylightCountries(in TimeZoneProperties timeZoneProperties, Query? query, in int maxLength)
internal static StringBuilder GetDaylightCountries(in TimeZoneProperties timeZoneProperties, Query? query, in int maxLength)
{
IEnumerable<string> countries;
@ -558,15 +591,36 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
{
countries = timeZoneProperties.CountriesDaylight;
}
else if (MatchDaylightTimeShortcuts(timeZoneProperties, query))
{
var matches = new Collection<string>();
foreach (var name in timeZoneProperties.CountriesDaylight)
{
var matchAll = query.Search.All(x => name.Contains(x, StringComparison.CurrentCultureIgnoreCase));
if (matchAll)
{
matches.Add(name);
}
}
countries = matches;
}
else
{
countries = timeZoneProperties.CountriesDaylight.Where(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
}
// When the search query don't match a country, show all countries
if (countries is null || !countries.Any())
{
countries = timeZoneProperties.CountriesDaylight;
}
var stringBuilder = new StringBuilder();
var lastEntry = countries.LastOrDefault();
foreach (var country in countries)
foreach (var country in countries.Distinct())
{
stringBuilder.Append(country);
@ -631,19 +685,35 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
}
/// <summary>
/// Return all countries of the given time zone.
/// Return all countries of the given time zone that match the given query.
/// </summary>
/// <param name="timeZoneProperties">The time zone that contain the countries.</param>
/// <param name="query">The <see cref="Query"/> that should match a country that use standard or daylight time.</param>
/// <param name="maxLength">The maximum length of the result.</param>
/// <returns>All countries of the given time zone.</returns>
private static StringBuilder GetAllCountries(in TimeZoneProperties timeZoneProperties, Query? query, in int maxLength)
internal static StringBuilder GetAllCountries(in TimeZoneProperties timeZoneProperties, Query? query, in int maxLength)
{
IEnumerable<string> countries;
if (query is null || string.IsNullOrWhiteSpace(query.Search))
{
countries = timeZoneProperties.CountriesDaylight.Concat(timeZoneProperties.CountriesStandard);
countries = timeZoneProperties.CountriesDaylight
.Concat(timeZoneProperties.CountriesStandard);
}
else if (MatchDaylightTimeShortcuts(timeZoneProperties, query) || MatchStandardTimeShortcuts(timeZoneProperties, query))
{
var matches = new Collection<string>();
foreach (var name in timeZoneProperties.CountriesDaylight.Concat(timeZoneProperties.CountriesStandard))
{
var matchAll = query.Search.All(x => name.Contains(x, StringComparison.CurrentCultureIgnoreCase));
if (matchAll)
{
matches.Add(name);
}
}
countries = matches;
}
else
{
@ -651,10 +721,17 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
.Concat(timeZoneProperties.CountriesStandard.Where(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase)));
}
// When the search query don't match a country, show all countries
if (countries is null || !countries.Any())
{
countries = timeZoneProperties.CountriesDaylight
.Concat(timeZoneProperties.CountriesStandard);
}
var stringBuilder = new StringBuilder();
var lastEntry = countries.LastOrDefault();
foreach (var country in countries)
foreach (var country in countries.Distinct())
{
stringBuilder.Append(country);
@ -724,7 +801,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchTimeZoneShortcut(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchTimeZoneShortcut(in TimeZoneProperties timeZoneProperties, Query query)
{
var result = timeZoneProperties.Shortcut.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase);
return result;
@ -736,7 +813,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchTimeZoneNames(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchTimeZoneNames(in TimeZoneProperties timeZoneProperties, Query query)
{
var result = timeZoneProperties.Name.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase)
|| timeZoneProperties.MilitaryName.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase);
@ -750,7 +827,7 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchOffset(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchOffset(in TimeZoneProperties timeZoneProperties, Query query)
{
// allow search for "-xx:xx"
if (timeZoneProperties.Offset.StartsWith('-') && query.Search.StartsWith('-'))
@ -779,9 +856,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchStandardTimeNames(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchStandardTimeNames(in TimeZoneProperties timeZoneProperties, Query query)
{
var result = timeZoneProperties.TimeNamesDaylight?.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase)) == true;
var result = timeZoneProperties.TimeNamesDaylight.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
return result;
}
@ -791,9 +868,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchDaylightTimeNames(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchDaylightTimeNames(in TimeZoneProperties timeZoneProperties, Query query)
{
var result = timeZoneProperties.TimeNamesDaylight?.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase)) == true;
var result = timeZoneProperties.TimeNamesDaylight.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
return result;
}
@ -803,9 +880,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchStandardCountries(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchStandardCountries(in TimeZoneProperties timeZoneProperties, Query query)
{
var result = timeZoneProperties.CountriesStandard?.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase)) == true;
var result = timeZoneProperties.CountriesStandard.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
return result;
}
@ -815,9 +892,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchDaylightCountries(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchDaylightCountries(in TimeZoneProperties timeZoneProperties, Query query)
{
var result = timeZoneProperties.CountriesDaylight?.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase)) == true;
var result = timeZoneProperties.CountriesDaylight.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
return result;
}
@ -827,9 +904,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchStandardTimeShortcuts(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchStandardTimeShortcuts(in TimeZoneProperties timeZoneProperties, Query query)
{
var result = timeZoneProperties.ShortcutsStandard?.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase)) == true;
var result = timeZoneProperties.ShortcutsStandard.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
return result;
}
@ -839,9 +916,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper
/// <param name="timeZoneProperties">The time zone to check.</param>
/// <param name="query">The query that should match.</param>
/// <returns><see langword="true"/>if the query match, otherwise <see langword="false"/>.</returns>
private static bool MatchDaylightTimeShortcuts(in TimeZoneProperties timeZoneProperties, Query query)
internal static bool MatchDaylightTimeShortcuts(in TimeZoneProperties timeZoneProperties, Query query)
{
var result = timeZoneProperties.ShortcutsDaylight?.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase)) == true;
var result = timeZoneProperties.ShortcutsDaylight.Any(x => x.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase));
return result;
}
}

View File

@ -6,6 +6,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using ManagedCommon;
using Microsoft.PowerToys.Run.Plugin.TimeZone.Classes;
@ -14,6 +15,8 @@ using Microsoft.PowerToys.Run.Plugin.TimeZone.Properties;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Plugin;
[assembly: InternalsVisibleTo("Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests")]
namespace Microsoft.PowerToys.Run.Plugin.TimeZone
{
/// <summary>