From d2a6dac38453ac793d3659a13df80928b85b28f6 Mon Sep 17 00:00:00 2001 From: Corey Hayward Date: Sun, 28 Jul 2024 20:54:34 +0100 Subject: [PATCH] Allow configuration of TimeSpanParser to change how the default values are converted to time #3 --- ...ommunity.PowerToys.Run.Plugin.Timer.csproj | 2 +- Community.PowerToys.Run.Plugin.Timer/Main.cs | 43 +++++++++++++++++-- .../Settings.cs | 35 +++++++++++++++ .../TimerResultService.cs | 14 +++--- .../plugin.json | 2 +- 5 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 Community.PowerToys.Run.Plugin.Timer/Settings.cs diff --git a/Community.PowerToys.Run.Plugin.Timer/Community.PowerToys.Run.Plugin.Timer.csproj b/Community.PowerToys.Run.Plugin.Timer/Community.PowerToys.Run.Plugin.Timer.csproj index aae1a1f..4aeccbc 100644 --- a/Community.PowerToys.Run.Plugin.Timer/Community.PowerToys.Run.Plugin.Timer.csproj +++ b/Community.PowerToys.Run.Plugin.Timer/Community.PowerToys.Run.Plugin.Timer.csproj @@ -8,7 +8,7 @@ true Community.PowerToys.Run.Plugin.Timers Community.PowerToys.Run.Plugin.Timers - 0.2.0 + 0.3.0 false false x64;ARM64 diff --git a/Community.PowerToys.Run.Plugin.Timer/Main.cs b/Community.PowerToys.Run.Plugin.Timer/Main.cs index 077345c..f94991a 100644 --- a/Community.PowerToys.Run.Plugin.Timer/Main.cs +++ b/Community.PowerToys.Run.Plugin.Timer/Main.cs @@ -1,22 +1,46 @@ // Copyright (c) Corey Hayward. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using Microsoft.PowerToys.Settings.UI.Library; using Wox.Plugin; namespace Community.PowerToys.Run.Plugin.Timers; -public class Main : IPlugin, IContextMenu +public class Main : IPlugin, IContextMenu, ISettingProvider { private List _timers = []; private TimerResultService? _timerService; private PluginInitContext? _context; + private Settings _settings = new(); public string Name => "Timer"; - public string Description => "Timers."; + public string Description => "Set and manage timers."; public static string PluginID => "0cd68088246649a38205c7641df39db0"; + public IEnumerable AdditionalOptions => new List() + { + new PluginAdditionalOption() + { + Key = nameof(Settings.TimeSpanParserUncolonedDefault), + DisplayLabel = "Uncoloned Time Configuration", + DisplayDescription = "Sets how the first number of the timer should be treated, when not using colons.", + ComboBoxValue = (int)_settings.TimeSpanParserUncolonedDefault, + PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox, + ComboBoxItems = Settings.TimeSpanParserConfigurationOptions + }, + new PluginAdditionalOption() + { + Key = nameof(Settings.TimeSpanParserColonedDefault), + DisplayLabel = "Coloned Time Configuration", + DisplayDescription = "Sets how the first number of the timer should be treated, when using colons (i.e. 4:30).", + ComboBoxValue = (int)_settings.TimeSpanParserColonedDefault, + PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox, + ComboBoxItems = Settings.TimeSpanParserConfigurationOptions + }, + }; + public void Init(PluginInitContext context) { ArgumentNullException.ThrowIfNull(context); @@ -26,8 +50,21 @@ public void Init(PluginInitContext context) } public List Query(Query query) - => _timerService!.GetQueryResult(query); + => _timerService!.GetQueryResult(query, _settings); public List LoadContextMenus(Result selectedResult) => _timerService!.GetContextMenuResults(selectedResult); + + public System.Windows.Controls.Control CreateSettingPanel() + => throw new NotImplementedException(); + + public void UpdateSettings(PowerLauncherPluginSettings settings) + { + if (settings?.AdditionalOptions is null) + { + return; + } + + _settings.UpdateSettings(settings.AdditionalOptions); + } } diff --git a/Community.PowerToys.Run.Plugin.Timer/Settings.cs b/Community.PowerToys.Run.Plugin.Timer/Settings.cs new file mode 100644 index 0000000..f864a2e --- /dev/null +++ b/Community.PowerToys.Run.Plugin.Timer/Settings.cs @@ -0,0 +1,35 @@ +using Microsoft.PowerToys.Settings.UI.Library; +using TimeSpanParserUtil; + +namespace Community.PowerToys.Run.Plugin.Timers; + +public class Settings +{ + public static List> TimeSpanParserConfigurationOptions + = new List>() + { + new("None", ((int)Units.None).ToString()), + new("Milliseconds", ((int)Units.Milliseconds).ToString()), + new("Seconds", ((int)Units.Seconds).ToString()), + new("Minutes", ((int)Units.Minutes).ToString()), + new("Hours", ((int)Units.Hours).ToString()), + new("Days", ((int)Units.Days).ToString()), + new("Weeks", ((int)Units.Weeks).ToString()), + new("Months", ((int)Units.Months).ToString()), + }; + + public TimeSpanParserOptions TimeSpanParserOptions => new() + { + UncolonedDefault = TimeSpanParserUncolonedDefault, + ColonedDefault = TimeSpanParserColonedDefault, + }; + + public Units TimeSpanParserColonedDefault { get; private set; } = Units.Hours; + public Units TimeSpanParserUncolonedDefault { get; private set; } = Units.Minutes; + + public void UpdateSettings(IEnumerable settings) + { + TimeSpanParserUncolonedDefault = (Units)settings.Single(x => x.Key == nameof(Settings.TimeSpanParserUncolonedDefault)).ComboBoxValue; + TimeSpanParserColonedDefault = (Units)settings.Single(x => x.Key == nameof(Settings.TimeSpanParserColonedDefault)).ComboBoxValue; + } +} diff --git a/Community.PowerToys.Run.Plugin.Timer/TimerResultService.cs b/Community.PowerToys.Run.Plugin.Timer/TimerResultService.cs index 5a82e2b..7ef7f2f 100644 --- a/Community.PowerToys.Run.Plugin.Timer/TimerResultService.cs +++ b/Community.PowerToys.Run.Plugin.Timer/TimerResultService.cs @@ -1,12 +1,9 @@ - -using Humanizer; +using Humanizer; using ManagedCommon; using System.Timers; using System.Windows.Input; -using System.Xml.Linq; using TimeSpanParserUtil; using Wox.Plugin; -using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock; namespace Community.PowerToys.Run.Plugin.Timers; public class TimerResultService @@ -35,14 +32,14 @@ private void UpdateTheme(Theme theme) } } - public List GetQueryResult(Query query) + public List GetQueryResult(Query query, Settings settings) { if (string.IsNullOrWhiteSpace(query.Search)) { return GetRunningTimersResults(query.Search); } - return GetCreateTimerResult(query.Search); + return GetCreateTimerResult(query.Search, settings); } private List GetRunningTimersResults(string search) @@ -70,10 +67,11 @@ private List GetRunningTimersResults(string search) return timerResults; } - public List GetCreateTimerResult(string query) + public List GetCreateTimerResult(string query, Settings settings) { query = query.Trim(); - if (!TimeSpanParser.TryParse(query, out var timeSpan) || timeSpan <= TimeSpan.Zero) + + if (!TimeSpanParser.TryParse(query, settings.TimeSpanParserOptions, out var timeSpan) || timeSpan <= TimeSpan.Zero) { var parsingErrorResult = new Result() { diff --git a/Community.PowerToys.Run.Plugin.Timer/plugin.json b/Community.PowerToys.Run.Plugin.Timer/plugin.json index 7926800..a28efb0 100644 --- a/Community.PowerToys.Run.Plugin.Timer/plugin.json +++ b/Community.PowerToys.Run.Plugin.Timer/plugin.json @@ -4,7 +4,7 @@ "IsGlobal": false, "Name": "Timer", "Author": "Corey Hayward", - "Version": "0.2.0", + "Version": "0.3.0", "Language": "csharp", "Website": "https://github.com/CoreyHayward/PowerToys-Run-Timer", "IcoPathDark": "Images\\Timer.dark.png",