Skip to content

Commit

Permalink
[WinFormsHotkeyService] Simplified initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingpie committed Oct 30, 2024
1 parent c1d2271 commit 4c5caad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/20-Services/Wtq.Services.WinForms/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;
global using System.Windows.Forms;
global using System.Windows.Forms;
global using Wtq.Events;
global using Wtq.Utils;
global using Wtq.Utils.AsyncInit;
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
using Microsoft.Extensions.DependencyInjection;
using Wtq.Services.Win32;

namespace Wtq.Services.WinForms;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddWinFormsHotkeyService(this IServiceCollection services)
{
Guard.Against.Null(services);

return services
.AddHostedService<WinFormsHotkeyService>();
.AddSingleton<WinFormsHotkeyService>();
}

public static IServiceCollection AddWinFormsScreenInfoProvider(this IServiceCollection services)
{
Guard.Against.Null(services);

return services
.AddSingleton<IWtqScreenInfoProvider, WinFormsScreenInfoProvider>();
}

public static IServiceCollection AddWinFormsTrayIcon(this IServiceCollection services)
{
Guard.Against.Null(services);

return services
.AddHostedService<WinFormsTrayIconService>();
}
Expand Down
20 changes: 6 additions & 14 deletions src/20-Services/Wtq.Services.WinForms/WinFormsHotkeyService.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using Microsoft.Extensions.Hosting;
using Wtq.Events;
using Wtq.Services.WinForms.Native;
using Wtq.Utils;

namespace Wtq.Services.WinForms;

public class WinFormsHotkeyService : IHostedService
public class WinFormsHotkeyService : IAsyncInitializable
{
private readonly ILogger _log = Log.For<WinFormsHotkeyService>();
private readonly IWtqBus _bus;

public WinFormsHotkeyService(IWtqBus bus)
{
_bus = bus ?? throw new ArgumentNullException(nameof(bus));
Guard.Against.Null(bus);

_bus.OnEvent<WtqRegisterHotkeyEvent>(
bus.OnEvent<WtqHotkeyDefinedEvent>(
e =>
{
var mods = (KeyModifiers)e.Modifiers;
Expand All @@ -29,21 +25,17 @@ public WinFormsHotkeyService(IWtqBus bus)

HotkeyManager.HotkeyPressed += (s, a) =>
{
_bus.Publish(new WtqHotkeyPressedEvent()
bus.Publish(new WtqHotkeyPressedEvent()
{
Key = a.Key.ToWtqKeys(),
Modifiers = a.Modifiers.ToWtqKeyModifiers(),
});
};
}

public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
public Task InitializeAsync()
{
// Only here to make sure an instance of this class is created.
return Task.CompletedTask;
}
}

0 comments on commit 4c5caad

Please sign in to comment.