< Summary

Information
Class: Pomodoro.Web.Services.KeyboardShortcutService
Assembly: Pomodoro.Web
File(s): /home/runner/work/Pomodoro/Pomodoro/src/Pomodoro.Web/Services/KeyboardShortcutService.cs
Line coverage
100%
Covered lines: 58
Uncovered lines: 0
Coverable lines: 58
Total lines: 91
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RegisterShortcut(...)100%22100%
UnregisterShortcut(...)100%11100%
GetRegisteredShortcuts()100%11100%
InitializeAsync()100%11100%
HandleShortcut(...)100%22100%
DisposeAsync()100%22100%

File(s)

/home/runner/work/Pomodoro/Pomodoro/src/Pomodoro.Web/Services/KeyboardShortcutService.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2using Microsoft.JSInterop;
 3
 4namespace Pomodoro.Web.Services;
 5
 6/// <summary>
 7/// Service for managing keyboard shortcuts in the application
 8/// </summary>
 9public class KeyboardShortcutService : IKeyboardShortcutService, IAsyncDisposable
 10{
 11    private readonly IJSRuntime _jsRuntime;
 12    private readonly ILogger<KeyboardShortcutService> _logger;
 2013    private readonly Dictionary<string, Action> _shortcuts = new();
 2014    private readonly Dictionary<string, string> _descriptions = new();
 15    private DotNetObjectReference<KeyboardShortcutService>? _dotNetRef;
 16
 2017    public KeyboardShortcutService(IJSRuntime jsRuntime, ILogger<KeyboardShortcutService> logger)
 2018    {
 2019        _jsRuntime = jsRuntime;
 2020        _logger = logger;
 2021    }
 22
 23    public void RegisterShortcut(string key, Action action, string? description = null)
 1324    {
 1325        var normalizedKey = key.ToLowerInvariant();
 1326        _shortcuts[normalizedKey] = action;
 1327        if (description != null)
 728        {
 729            _descriptions[normalizedKey] = description;
 730        }
 1331        _logger.LogDebug("Registered keyboard shortcut: {Key}", normalizedKey);
 1332    }
 33
 34    public void UnregisterShortcut(string key)
 435    {
 436        var normalizedKey = key.ToLowerInvariant();
 437        _shortcuts.Remove(normalizedKey);
 438        _descriptions.Remove(normalizedKey);
 439        _logger.LogDebug("Unregistered keyboard shortcut: {Key}", normalizedKey);
 440    }
 41
 42    public Dictionary<string, string> GetRegisteredShortcuts()
 643    {
 644        return new Dictionary<string, string>(_descriptions);
 645    }
 46
 47    public async Task InitializeAsync()
 648    {
 649        _dotNetRef = DotNetObjectReference.Create(this);
 650        await _jsRuntime.InvokeVoidAsync("keyboardShortcuts.initialize", _dotNetRef);
 651        _logger.LogInformation("Keyboard shortcut service initialized");
 652    }
 53
 54    /// <summary>
 55    /// Called from JavaScript when a keyboard shortcut is triggered
 56    /// </summary>
 57    [JSInvokable]
 58    public void HandleShortcut(string key)
 859    {
 860        var normalizedKey = key.ToLowerInvariant();
 861        if (_shortcuts.TryGetValue(normalizedKey, out var action))
 562        {
 563            _logger.LogDebug("Keyboard shortcut triggered: {Key}", normalizedKey);
 64            try
 565            {
 566                action();
 467            }
 168            catch (Exception ex)
 169            {
 170                _logger.LogError(ex, "Error executing keyboard shortcut action for key: {Key}", normalizedKey);
 171            }
 572        }
 873    }
 74
 75    public async ValueTask DisposeAsync()
 576    {
 577        if (_dotNetRef != null)
 378        {
 79            try
 380            {
 381                await _jsRuntime.InvokeVoidAsync("keyboardShortcuts.dispose");
 282                _dotNetRef.Dispose();
 283                _dotNetRef = null;
 284            }
 185            catch (Exception ex)
 186            {
 187                _logger.LogError(ex, "Error disposing keyboard shortcut service");
 188            }
 389        }
 590    }
 91}