// 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.Collections.Generic; using System.Linq; using Microsoft.PowerToys.Run.Plugin.TimeZone.Properties; using Microsoft.PowerToys.Settings.UI.Library; namespace Microsoft.PowerToys.Run.Plugin.TimeZone.Classes { /// /// Additional settings for the time zone plugin. /// internal sealed class TimeZoneSettings { /// /// Gets or sets a value indicating whether the time zone name of a time zone is shown in the results. /// internal bool ShowTimeZoneNames { get; set; } /// /// Gets or sets a value indicating whether the time name of a time zone is shown in the results. /// internal bool ShowTimeNames { get; set; } /// /// Gets or sets a value indicating whether the military name of a time zone is shown in the results. /// internal bool ShowMilitaryTimeZoneNames { get; set; } /// /// Return a list with all settings. Additional /// /// A list with all settings. internal static List GetAdditionalOptions() { var optionList = new List { new PluginAdditionalOption { Key = "ShowTimeZoneNames", DisplayLabel = Resources.ShowTimeZoneNames, Value = true, }, new PluginAdditionalOption { Key = "ShowTimeNames", DisplayLabel = Resources.ShowTimeNames, Value = true, }, new PluginAdditionalOption { Key = "ShowMilitaryTimeZoneNames", DisplayLabel = Resources.ShowMilitaryTimeZoneNames, Value = false, }, }; return optionList; } /// /// Update this settings. /// /// The settings for all power launcher plugin. internal void UpdateSettings(PowerLauncherPluginSettings settings) { if (settings is null || settings.AdditionalOptions is null) { return; } ShowTimeZoneNames = GetSettingOrDefault(settings, "ShowTimeZoneNames"); ShowTimeNames = GetSettingOrDefault(settings, "ShowTimeNames"); ShowMilitaryTimeZoneNames = GetSettingOrDefault(settings, "ShowMilitaryTimeZoneNames"); } /// /// Return one setting of the given settings list with the given name. /// /// The object that contain all settings. /// The name of the setting. /// A settings value. private static bool GetSettingOrDefault(PowerLauncherPluginSettings settings, string name) { var option = settings.AdditionalOptions.FirstOrDefault(x => x.Key == name); // 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; return settingsValue; } } }