Skip to content

Commit dbb0a8b

Browse files
committed
Add Polyfill package and polyfill additional things manually
1 parent c5bb8e6 commit dbb0a8b

7 files changed

Lines changed: 61 additions & 50 deletions

File tree

src/Directory.Packages.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0" />
1010
</ItemGroup>
1111

12+
<ItemGroup>
13+
<PackageVersion Include="Polyfill" Version="9.0.3" />
14+
</ItemGroup>
15+
1216
<ItemGroup>
1317
<GlobalPackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.14.15" />
1418
<GlobalPackageReference Include="Roslynator.Analyzers" Version="4.14.1" />

src/TinyHotKey/ITinyHotKey.cs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,3 @@ public interface ITinyHotKey
1717
/// <returns>Registration status, check IsRegistered to see if the registration was successful, dispose when done to remove the hotkey detection.</returns>
1818
ITinyHotKeyRegistration RegisterHotKey(Modifier modifiers, Key key, Func<Task> callback);
1919
}
20-
21-
public static class TinyHotKeyExtensions
22-
{
23-
extension(ITinyHotKey tinyHotKey)
24-
{
25-
/// <summary>
26-
/// <para>Register a callback to be invoke when a keyboard combination is detected.</para>
27-
/// <para>
28-
/// Since only one application at a time may listen to a certain keyboard combination on Windows
29-
/// this method will throw an error if the registration was not a success.
30-
/// </para>
31-
/// <para>Multiple registrations for the same keyboard combination with the same instance is ok.</para>
32-
/// </summary>
33-
/// <param name="modifiers">Modifier keys, eg. Modifier.Ctrl | Modifier.Alt or Modifier.None</param>
34-
/// <param name="key">Key to detect, eg. Key.C or Key.MediaPlayPause</param>
35-
/// <param name="callback">Callback to be invoked when the keyboard combination is detected</param>
36-
/// <returns>Registration status, should always be successful with this overload, dispose when done to remove the hotkey detection.</returns>
37-
/// <exception cref="InvalidOperationException">Thrown when the hotkey combination could not be registered.</exception>
38-
public ITinyHotKeyRegistration RegisterHotKeyOrThrow(Modifier modifiers, Key key, Func<Task> callback)
39-
{
40-
#if NET
41-
ArgumentNullException.ThrowIfNull(tinyHotKey);
42-
#else
43-
if (tinyHotKey is null)
44-
{
45-
throw new ArgumentNullException(nameof(tinyHotKey));
46-
}
47-
#endif
48-
49-
var registration = tinyHotKey.RegisterHotKey(modifiers, key, callback);
50-
51-
if (!registration.IsRegistered)
52-
{
53-
throw new InvalidOperationException($"Couldn't register hotkey {modifiers} {key}");
54-
}
55-
56-
return registration;
57-
}
58-
}
59-
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#if !NET
2+
using System.Runtime.CompilerServices;
3+
4+
namespace System;
5+
6+
internal static class ArgumentNullExceptionExtensions
7+
{
8+
extension(ArgumentNullException)
9+
{
10+
public static void ThrowIfNull(object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
11+
{
12+
if (argument is null)
13+
{
14+
throw new ArgumentNullException(paramName);
15+
}
16+
}
17+
}
18+
}
19+
#endif

src/TinyHotKey/TinyHotKey.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
1717
</ItemGroup>
1818

19+
<ItemGroup Condition=" '$(TargetFramework)' != 'net10.0' ">
20+
<PackageReference Include="Polyfill" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers" />
21+
</ItemGroup>
22+
1923
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace TinyHotKey;
2+
3+
public static class TinyHotKeyExtensions
4+
{
5+
extension(ITinyHotKey tinyHotKey)
6+
{
7+
/// <summary>
8+
/// <para>Register a callback to be invoke when a keyboard combination is detected.</para>
9+
/// <para>
10+
/// Since only one application at a time may listen to a certain keyboard combination on Windows
11+
/// this method will throw an error if the registration was not a success.
12+
/// </para>
13+
/// <para>Multiple registrations for the same keyboard combination with the same instance is ok.</para>
14+
/// </summary>
15+
/// <param name="modifiers">Modifier keys, eg. Modifier.Ctrl | Modifier.Alt or Modifier.None</param>
16+
/// <param name="key">Key to detect, eg. Key.C or Key.MediaPlayPause</param>
17+
/// <param name="callback">Callback to be invoked when the keyboard combination is detected</param>
18+
/// <returns>Registration status, should always be successful with this overload, dispose when done to remove the hotkey detection.</returns>
19+
/// <exception cref="InvalidOperationException">Thrown when the hotkey combination could not be registered.</exception>
20+
public ITinyHotKeyRegistration RegisterHotKeyOrThrow(Modifier modifiers, Key key, Func<Task> callback)
21+
{
22+
ArgumentNullException.ThrowIfNull(tinyHotKey);
23+
24+
var registration = tinyHotKey.RegisterHotKey(modifiers, key, callback);
25+
26+
if (!registration.IsRegistered)
27+
{
28+
throw new InvalidOperationException($"Couldn't register hotkey {modifiers} {key}");
29+
}
30+
31+
return registration;
32+
}
33+
}
34+
}

src/TinyHotKey/TinyHotKeyInstance.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ public TinyHotKeyInstance()
1717

1818
public TinyHotKeyInstance(ILoggerFactory? loggerFactory)
1919
{
20-
#if NET
2120
if (!OperatingSystem.IsWindows())
2221
{
2322
throw new PlatformNotSupportedException("Operating system not supported");
2423
}
25-
#endif
2624

2725
platformInstance = new TinyHotKeyWindows(loggerFactory?.CreateLogger("TinyHotKey"));
2826
}

src/TinyHotKey/TinyHotKeyWindows.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
using System.Diagnostics.CodeAnalysis;
44
#endif
55
using System.Runtime.InteropServices;
6-
#if NET
76
using System.Runtime.Versioning;
8-
#endif
97
using Microsoft.Extensions.Logging;
108

119
namespace TinyHotKey;
@@ -17,19 +15,13 @@ namespace TinyHotKey;
1715
/// which only works thanks to a hidden Window and a custom message loop on a
1816
/// dedicated thread.
1917
/// </summary>
20-
#if NET
2118
[SupportedOSPlatform("windows")]
22-
#endif
2319
internal sealed partial class TinyHotKeyWindows : ITinyHotKey, IDisposable
2420
{
2521
private readonly string className = Guid.NewGuid().ToString("n");
2622
private readonly AutoResetEvent messageLoopDone = new(false);
2723
private readonly ILogger? logger;
28-
#if NET9_0_OR_GREATER
2924
private readonly Lock registrationLock = new();
30-
#else
31-
private readonly object registrationLock = new();
32-
#endif
3325
private readonly List<TinyHotKeyRegistration> registrations = [];
3426
private readonly WndProc wndProcDelegate;
3527

0 commit comments

Comments
 (0)