| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Microsoft.JSInterop; |
| | | 3 | | |
| | | 4 | | namespace Pomodoro.Web.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Service for managing keyboard shortcuts in the application |
| | | 8 | | /// </summary> |
| | | 9 | | public class KeyboardShortcutService : IKeyboardShortcutService, IAsyncDisposable |
| | | 10 | | { |
| | | 11 | | private readonly IJSRuntime _jsRuntime; |
| | | 12 | | private readonly ILogger<KeyboardShortcutService> _logger; |
| | 20 | 13 | | private readonly Dictionary<string, Action> _shortcuts = new(); |
| | 20 | 14 | | private readonly Dictionary<string, string> _descriptions = new(); |
| | | 15 | | private DotNetObjectReference<KeyboardShortcutService>? _dotNetRef; |
| | | 16 | | |
| | 20 | 17 | | public KeyboardShortcutService(IJSRuntime jsRuntime, ILogger<KeyboardShortcutService> logger) |
| | 20 | 18 | | { |
| | 20 | 19 | | _jsRuntime = jsRuntime; |
| | 20 | 20 | | _logger = logger; |
| | 20 | 21 | | } |
| | | 22 | | |
| | | 23 | | public void RegisterShortcut(string key, Action action, string? description = null) |
| | 13 | 24 | | { |
| | 13 | 25 | | var normalizedKey = key.ToLowerInvariant(); |
| | 13 | 26 | | _shortcuts[normalizedKey] = action; |
| | 13 | 27 | | if (description != null) |
| | 7 | 28 | | { |
| | 7 | 29 | | _descriptions[normalizedKey] = description; |
| | 7 | 30 | | } |
| | 13 | 31 | | _logger.LogDebug("Registered keyboard shortcut: {Key}", normalizedKey); |
| | 13 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void UnregisterShortcut(string key) |
| | 4 | 35 | | { |
| | 4 | 36 | | var normalizedKey = key.ToLowerInvariant(); |
| | 4 | 37 | | _shortcuts.Remove(normalizedKey); |
| | 4 | 38 | | _descriptions.Remove(normalizedKey); |
| | 4 | 39 | | _logger.LogDebug("Unregistered keyboard shortcut: {Key}", normalizedKey); |
| | 4 | 40 | | } |
| | | 41 | | |
| | | 42 | | public Dictionary<string, string> GetRegisteredShortcuts() |
| | 6 | 43 | | { |
| | 6 | 44 | | return new Dictionary<string, string>(_descriptions); |
| | 6 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task InitializeAsync() |
| | 6 | 48 | | { |
| | 6 | 49 | | _dotNetRef = DotNetObjectReference.Create(this); |
| | 6 | 50 | | await _jsRuntime.InvokeVoidAsync("keyboardShortcuts.initialize", _dotNetRef); |
| | 6 | 51 | | _logger.LogInformation("Keyboard shortcut service initialized"); |
| | 6 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Called from JavaScript when a keyboard shortcut is triggered |
| | | 56 | | /// </summary> |
| | | 57 | | [JSInvokable] |
| | | 58 | | public void HandleShortcut(string key) |
| | 8 | 59 | | { |
| | 8 | 60 | | var normalizedKey = key.ToLowerInvariant(); |
| | 8 | 61 | | if (_shortcuts.TryGetValue(normalizedKey, out var action)) |
| | 5 | 62 | | { |
| | 5 | 63 | | _logger.LogDebug("Keyboard shortcut triggered: {Key}", normalizedKey); |
| | | 64 | | try |
| | 5 | 65 | | { |
| | 5 | 66 | | action(); |
| | 4 | 67 | | } |
| | 1 | 68 | | catch (Exception ex) |
| | 1 | 69 | | { |
| | 1 | 70 | | _logger.LogError(ex, "Error executing keyboard shortcut action for key: {Key}", normalizedKey); |
| | 1 | 71 | | } |
| | 5 | 72 | | } |
| | 8 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async ValueTask DisposeAsync() |
| | 5 | 76 | | { |
| | 5 | 77 | | if (_dotNetRef != null) |
| | 3 | 78 | | { |
| | | 79 | | try |
| | 3 | 80 | | { |
| | 3 | 81 | | await _jsRuntime.InvokeVoidAsync("keyboardShortcuts.dispose"); |
| | 2 | 82 | | _dotNetRef.Dispose(); |
| | 2 | 83 | | _dotNetRef = null; |
| | 2 | 84 | | } |
| | 1 | 85 | | catch (Exception ex) |
| | 1 | 86 | | { |
| | 1 | 87 | | _logger.LogError(ex, "Error disposing keyboard shortcut service"); |
| | 1 | 88 | | } |
| | 3 | 89 | | } |
| | 5 | 90 | | } |
| | | 91 | | } |