Files
PowerToys/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeDate/Components/TimeDateSettings.cs
Heiko 34e4e7e5bd [PT Run] TimeDate plugin (#16662)
* create plugin

* Update plugin code

* fix deps

* last changes

* unix

* new results and small changes

* Update settings name

* make spellcheck happy

* new time/date formats

* add comment

* code cleanup, installer, signing pipeline

* fix unix result

* UnitTests

* spell fix

* Update tests, Timestamp query feature

* new formats

* last changes

* last changes

* unit tests and fixes

* cjhanges and fixes

* fix installer

* fix settings class init

* context menu

* fix tests

* add settings tests

* update/fix DateTimeResult tests

* small improvements

* update pipeline

* enable analyzer

* fixes and improvements

* spell fix

* dev docs

* doc fixes

* spell fix

* last changes

* changes and fixes

* fixes and test updates

* improvements

* last changes

* try to fix tests

* remove obsolete code

* add info to test log

* fix search

* tag fix

* tests

* change tests

* update dev docs

* fix spelling

* fix culture for ui strings

* improvements based on feedback

* improve global search

* improve text

* docs improvement

* add settings note

* fix and update tests

* fix spelling
2022-03-17 19:33:05 +00:00

155 lines
6.4 KiB
C#

// 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 System.Runtime.CompilerServices;
using Microsoft.PowerToys.Run.Plugin.TimeDate.Properties;
using Microsoft.PowerToys.Settings.UI.Library;
[assembly: InternalsVisibleTo("Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests")]
namespace Microsoft.PowerToys.Run.Plugin.TimeDate.Components
{
/// <summary>
/// Additional settings for the WindowWalker plugin.
/// </summary>
/// <remarks>Some code parts reused from TimeZone plugin.</remarks>
internal sealed class TimeDateSettings
{
/// <summary>
/// Are the class properties initialized with default values
/// </summary>
private readonly bool _initialized;
/// <summary>
/// An instance of the class <see cref="TimeDateSettings"></see>
/// </summary>
private static TimeDateSettings instance;
/// <summary>
/// Gets a value indicating whether to show only the time and date in global results or not
/// </summary>
internal bool OnlyDateTimeNowGlobal { get; private set; }
/// <summary>
/// Gets a value indicating whether to show the time with seconds or not
/// </summary>
internal bool TimeWithSeconds { get; private set; }
/// <summary>
/// Gets a value indicating whether the date with the weekday or not
/// </summary>
internal bool DateWithWeekday { get; private set; }
/// <summary>
/// Gets a value indicating whether to hide the number input error message on global results
/// </summary>
internal bool HideNumberMessageOnGlobalQuery { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="TimeDateSettings"/> class.
/// Private constructor to make sure there is never more than one instance of this class
/// </summary>
private TimeDateSettings()
{
// Init class properties with default values
UpdateSettings(null);
_initialized = true;
}
/// <summary>
/// Gets an instance property of this class that makes sure that the first instance gets created
/// and that all the requests end up at that one instance.
/// The benefit of this is that we don't need additional variables/parameters
/// to communicate the settings between plugin's classes/methods.
/// We can simply access this one instance, whenever we need the actual settings.
/// </summary>
internal static TimeDateSettings Instance
{
get
{
if (instance == null)
{
instance = new TimeDateSettings();
}
return instance;
}
}
/// <summary>
/// Return a list with all additional plugin options.
/// </summary>
/// <returns>A list with all additional plugin options.</returns>
internal static List<PluginAdditionalOption> GetAdditionalOptions()
{
var optionList = new List<PluginAdditionalOption>
{
new PluginAdditionalOption()
{
// ToDo: When description property is implemented (#15853), move the note in brackets to description.
Key = nameof(OnlyDateTimeNowGlobal),
DisplayLabel = Resources.Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal,
Value = true,
},
new PluginAdditionalOption()
{
// ToDo: When description property is implemented (#15853), move the note in brackets to description.
Key = nameof(TimeWithSeconds),
DisplayLabel = Resources.Microsoft_plugin_timedate_SettingTimeWithSeconds,
Value = false,
},
new PluginAdditionalOption()
{
// ToDo: When description property is implemented (#15853), move the note in brackets to description.
Key = nameof(DateWithWeekday),
DisplayLabel = Resources.Microsoft_plugin_timedate_SettingDateWithWeekday,
Value = false,
},
new PluginAdditionalOption()
{
Key = nameof(HideNumberMessageOnGlobalQuery),
DisplayLabel = Resources.Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery,
Value = false,
},
};
return optionList;
}
/// <summary>
/// Update this settings.
/// </summary>
/// <param name="settings">The settings for all power launcher plugins.</param>
internal void UpdateSettings(PowerLauncherPluginSettings settings)
{
if ((settings is null || settings.AdditionalOptions is null) & _initialized)
{
return;
}
OnlyDateTimeNowGlobal = GetSettingOrDefault(settings, nameof(OnlyDateTimeNowGlobal));
TimeWithSeconds = GetSettingOrDefault(settings, nameof(TimeWithSeconds));
DateWithWeekday = GetSettingOrDefault(settings, nameof(DateWithWeekday));
HideNumberMessageOnGlobalQuery = GetSettingOrDefault(settings, nameof(HideNumberMessageOnGlobalQuery));
}
/// <summary>
/// Return one <see cref="bool"/> setting of the given settings list with the given name.
/// </summary>
/// <param name="settings">The object that contain all settings.</param>
/// <param name="name">The name of the setting.</param>
/// <returns>A settings value.</returns>
private static bool GetSettingOrDefault(PowerLauncherPluginSettings settings, string name)
{
var option = settings?.AdditionalOptions?.FirstOrDefault(x => x.Key == name);
// If a setting isn't available, we use the value defined in the method GetAdditionalOptions() as fallback.
// We can use First() instead of FirstOrDefault() because the values must exist. Otherwise, we made a mistake when defining the settings.
return option?.Value ?? GetAdditionalOptions().First(x => x.Key == name).Value;
}
}
}