Skip to content

Commit d2a6dac

Browse files
committed
Allow configuration of TimeSpanParser to change how the default values are converted to time #3
1 parent c2864ab commit d2a6dac

File tree

5 files changed

+83
-13
lines changed

5 files changed

+83
-13
lines changed

Community.PowerToys.Run.Plugin.Timer/Community.PowerToys.Run.Plugin.Timer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<UseWindowsForms>true</UseWindowsForms>
99
<RootNamespace>Community.PowerToys.Run.Plugin.Timers</RootNamespace>
1010
<AssemblyName>Community.PowerToys.Run.Plugin.Timers</AssemblyName>
11-
<Version>0.2.0</Version>
11+
<Version>0.3.0</Version>
1212
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
1313
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
1414
<Platforms>x64;ARM64</Platforms>
Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
11
// Copyright (c) Corey Hayward. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using Microsoft.PowerToys.Settings.UI.Library;
45
using Wox.Plugin;
56

67
namespace Community.PowerToys.Run.Plugin.Timers;
78

8-
public class Main : IPlugin, IContextMenu
9+
public class Main : IPlugin, IContextMenu, ISettingProvider
910
{
1011
private List<TimerPlus> _timers = [];
1112
private TimerResultService? _timerService;
1213
private PluginInitContext? _context;
14+
private Settings _settings = new();
1315

1416
public string Name => "Timer";
1517

16-
public string Description => "Timers.";
18+
public string Description => "Set and manage timers.";
1719

1820
public static string PluginID => "0cd68088246649a38205c7641df39db0";
1921

22+
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
23+
{
24+
new PluginAdditionalOption()
25+
{
26+
Key = nameof(Settings.TimeSpanParserUncolonedDefault),
27+
DisplayLabel = "Uncoloned Time Configuration",
28+
DisplayDescription = "Sets how the first number of the timer should be treated, when not using colons.",
29+
ComboBoxValue = (int)_settings.TimeSpanParserUncolonedDefault,
30+
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox,
31+
ComboBoxItems = Settings.TimeSpanParserConfigurationOptions
32+
},
33+
new PluginAdditionalOption()
34+
{
35+
Key = nameof(Settings.TimeSpanParserColonedDefault),
36+
DisplayLabel = "Coloned Time Configuration",
37+
DisplayDescription = "Sets how the first number of the timer should be treated, when using colons (i.e. 4:30).",
38+
ComboBoxValue = (int)_settings.TimeSpanParserColonedDefault,
39+
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox,
40+
ComboBoxItems = Settings.TimeSpanParserConfigurationOptions
41+
},
42+
};
43+
2044
public void Init(PluginInitContext context)
2145
{
2246
ArgumentNullException.ThrowIfNull(context);
@@ -26,8 +50,21 @@ public void Init(PluginInitContext context)
2650
}
2751

2852
public List<Result> Query(Query query)
29-
=> _timerService!.GetQueryResult(query);
53+
=> _timerService!.GetQueryResult(query, _settings);
3054

3155
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
3256
=> _timerService!.GetContextMenuResults(selectedResult);
57+
58+
public System.Windows.Controls.Control CreateSettingPanel()
59+
=> throw new NotImplementedException();
60+
61+
public void UpdateSettings(PowerLauncherPluginSettings settings)
62+
{
63+
if (settings?.AdditionalOptions is null)
64+
{
65+
return;
66+
}
67+
68+
_settings.UpdateSettings(settings.AdditionalOptions);
69+
}
3370
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.PowerToys.Settings.UI.Library;
2+
using TimeSpanParserUtil;
3+
4+
namespace Community.PowerToys.Run.Plugin.Timers;
5+
6+
public class Settings
7+
{
8+
public static List<KeyValuePair<string, string>> TimeSpanParserConfigurationOptions
9+
= new List<KeyValuePair<string, string>>()
10+
{
11+
new("None", ((int)Units.None).ToString()),
12+
new("Milliseconds", ((int)Units.Milliseconds).ToString()),
13+
new("Seconds", ((int)Units.Seconds).ToString()),
14+
new("Minutes", ((int)Units.Minutes).ToString()),
15+
new("Hours", ((int)Units.Hours).ToString()),
16+
new("Days", ((int)Units.Days).ToString()),
17+
new("Weeks", ((int)Units.Weeks).ToString()),
18+
new("Months", ((int)Units.Months).ToString()),
19+
};
20+
21+
public TimeSpanParserOptions TimeSpanParserOptions => new()
22+
{
23+
UncolonedDefault = TimeSpanParserUncolonedDefault,
24+
ColonedDefault = TimeSpanParserColonedDefault,
25+
};
26+
27+
public Units TimeSpanParserColonedDefault { get; private set; } = Units.Hours;
28+
public Units TimeSpanParserUncolonedDefault { get; private set; } = Units.Minutes;
29+
30+
public void UpdateSettings(IEnumerable<PluginAdditionalOption> settings)
31+
{
32+
TimeSpanParserUncolonedDefault = (Units)settings.Single(x => x.Key == nameof(Settings.TimeSpanParserUncolonedDefault)).ComboBoxValue;
33+
TimeSpanParserColonedDefault = (Units)settings.Single(x => x.Key == nameof(Settings.TimeSpanParserColonedDefault)).ComboBoxValue;
34+
}
35+
}

Community.PowerToys.Run.Plugin.Timer/TimerResultService.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-

2-
using Humanizer;
1+
using Humanizer;
32
using ManagedCommon;
43
using System.Timers;
54
using System.Windows.Input;
6-
using System.Xml.Linq;
75
using TimeSpanParserUtil;
86
using Wox.Plugin;
9-
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
107

118
namespace Community.PowerToys.Run.Plugin.Timers;
129
public class TimerResultService
@@ -35,14 +32,14 @@ private void UpdateTheme(Theme theme)
3532
}
3633
}
3734

38-
public List<Result> GetQueryResult(Query query)
35+
public List<Result> GetQueryResult(Query query, Settings settings)
3936
{
4037
if (string.IsNullOrWhiteSpace(query.Search))
4138
{
4239
return GetRunningTimersResults(query.Search);
4340
}
4441

45-
return GetCreateTimerResult(query.Search);
42+
return GetCreateTimerResult(query.Search, settings);
4643
}
4744

4845
private List<Result> GetRunningTimersResults(string search)
@@ -70,10 +67,11 @@ private List<Result> GetRunningTimersResults(string search)
7067
return timerResults;
7168
}
7269

73-
public List<Result> GetCreateTimerResult(string query)
70+
public List<Result> GetCreateTimerResult(string query, Settings settings)
7471
{
7572
query = query.Trim();
76-
if (!TimeSpanParser.TryParse(query, out var timeSpan) || timeSpan <= TimeSpan.Zero)
73+
74+
if (!TimeSpanParser.TryParse(query, settings.TimeSpanParserOptions, out var timeSpan) || timeSpan <= TimeSpan.Zero)
7775
{
7876
var parsingErrorResult = new Result()
7977
{

Community.PowerToys.Run.Plugin.Timer/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"IsGlobal": false,
55
"Name": "Timer",
66
"Author": "Corey Hayward",
7-
"Version": "0.2.0",
7+
"Version": "0.3.0",
88
"Language": "csharp",
99
"Website": "https://github.com/CoreyHayward/PowerToys-Run-Timer",
1010
"IcoPathDark": "Images\\Timer.dark.png",

0 commit comments

Comments
 (0)