From 7af8b930bef407c6b126ff18fe69da3c0560b3d6 Mon Sep 17 00:00:00 2001 From: Tobias Sekan Date: Fri, 24 Jun 2022 12:00:46 +0000 Subject: [PATCH] [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 --- PowerToys.sln | 15 ++ src/.editorconfig | 10 +- ...rToys.Run.Plugin.TimeZone.UnitTests.csproj | 38 ++++ .../ResultHelperTest.cs | 55 ++++++ .../Classes/TimeZoneSettings.cs | 2 +- .../Helper/ResultHelper.cs | 187 ++++++++++++------ .../Main.cs | 3 + 7 files changed, 249 insertions(+), 61 deletions(-) create mode 100644 src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests.csproj create mode 100644 src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests/ResultHelperTest.cs diff --git a/PowerToys.sln b/PowerToys.sln index 7435ce9cce..1bdda455f5 100644 --- a/PowerToys.sln +++ b/PowerToys.sln @@ -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 diff --git a/src/.editorconfig b/src/.editorconfig index b88e05361b..d0aa049c64 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -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 \ No newline at end of file diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests.csproj b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests.csproj new file mode 100644 index 0000000000..1dba77e0b9 --- /dev/null +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests.csproj @@ -0,0 +1,38 @@ + + + + net6.0-windows + false + Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + GlobalSuppressions.cs + + + StyleCop.json + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests/ResultHelperTest.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests/ResultHelperTest.cs new file mode 100644 index 0000000000..2b199cb6ef --- /dev/null +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests/ResultHelperTest.cs @@ -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)); + } + } + } +} diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Classes/TimeZoneSettings.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Classes/TimeZoneSettings.cs index 48c9595c15..8502d165b7 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Classes/TimeZoneSettings.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Classes/TimeZoneSettings.cs @@ -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; diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Helper/ResultHelper.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Helper/ResultHelper.cs index 71f657a0fd..5a7208b689 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Helper/ResultHelper.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Helper/ResultHelper.cs @@ -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 /// The path to the icon that is used for each result. /// The current time in UTC for the . /// A list with . - private static IEnumerable GetResults(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings options, in Query query, in string iconPath, in DateTime dateTime) + internal static IEnumerable GetResults(in TimeZoneProperties timeZoneProperties, in TimeZoneSettings options, in Query query, in string iconPath, in DateTime dateTime) { var results = new Collection(); - 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 /// The current time in UTC. /// indicate that the result is for a time zone that use a daylight saving time. /// The current local time in a time zone. - 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 /// The current time in UTC. /// indicate that the result is for a time zone that use a daylight saving time. /// A title for a time zone. - 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 /// The time zone that contain all information. /// Additional options to limit the results. /// A tool-tip with countries that use the standard time. - 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 /// The time zone that contain all information. /// Additional options to limit the type of the names. /// A tool-tip with countries that use the daylight saving time. - 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 /// The time zone that contain all information. /// Additional options to limit the type of the names. /// A tool-tip with countries. - 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 /// The query that should match. /// The maximum length of the result. /// All know names of the given time zone. - 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(); @@ -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(); + + 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 } /// - /// Return all standard time names shortcuts of the given time zone. + /// Return all standard time name shortcuts of the given time zone. /// /// The time zone that contain a hand of names. - /// All standard time names shortcuts of the given time zone. - private static StringBuilder GetStandardShortcuts(in TimeZoneProperties timeZoneProperties) + /// All standard time name shortcuts of the given time zone. + 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 } /// - /// Return all know daylight time names shortcuts of the given time zone. + /// Return all know daylight time name shortcuts of the given time zone. /// /// The time zone that contain a hand of names. - /// All know daylight time names shortcuts of the given time zone. - private static StringBuilder GetDaylightShortcuts(in TimeZoneProperties timeZoneProperties) + /// All know daylight time name shortcuts of the given time zone. + 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 } /// - /// 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. /// /// The time zone that contain the countries. /// The that should match a country that use standard time. /// The maximum length of the result. /// All countries that use the standard time of the given time zone. - 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 countries; @@ -471,15 +483,36 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper { countries = timeZoneProperties.CountriesStandard; } + else if (MatchStandardTimeShortcuts(timeZoneProperties, query)) + { + var matches = new Collection(); + + 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 } /// - /// 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 /// /// The time zone that contain the countries. /// The that should match a country that use daylight time. /// The maximum length of the result. /// All countries that use the daylight saving time of the given time zone. - 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 countries; @@ -558,15 +591,36 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Helper { countries = timeZoneProperties.CountriesDaylight; } + else if (MatchDaylightTimeShortcuts(timeZoneProperties, query)) + { + var matches = new Collection(); + + 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 } /// - /// Return all countries of the given time zone. + /// Return all countries of the given time zone that match the given query. /// /// The time zone that contain the countries. /// The that should match a country that use standard or daylight time. /// The maximum length of the result. /// All countries of the given time zone. - 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 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(); + + 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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 /// The time zone to check. /// The query that should match. /// if the query match, otherwise . - 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; } } diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Main.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Main.cs index 97e38696fc..8e191b9084 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Main.cs @@ -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 { ///