jsakamoto / Toolbelt.Blazor.HotKeys2

This is a class library that provides configuration-centric keyboard shortcuts for your Blazor apps.
https://jsakamoto.github.io/Toolbelt.Blazor.HotKeys2/
Mozilla Public License 2.0
87 stars 6 forks source link

HotKeysContext?.Remove() requires all 5 parameters to be specified #16

Closed MikeWilliams-UK closed 3 months ago

MikeWilliams-UK commented 4 months ago

I have found that if you wish to remove a HotKey using the .Remove() method for a HotKey which you supplied options such as ExcludeSelector and Description you have to specify ALL of them when you wish to remove the HotKeys

In my application I have added HotKeysContext as a CascadingValue in order to have a Top level definition which includes keys "?" and "Escape" to show/hide the cheat sheet then in the page's OnInitialized I add the desired extra Hotkeys for the page and in Dispose I remove them.

Please fix the implementation of Remove() to work as expected when the HotKey has options set.

See example below

protected override void OnInitialized()
{
    HotKeysCheatSheetVisible = false;

    HotKeysContext?.Add(ModCode.Ctrl, Code.I, IncrementCount, new()
            {
                    Exclude = Exclude.None,
                    ExcludeSelector = ".disabled-hotkeys *",
                    Description = "Increment the counter"
            });
    HotKeysContext?.Add(ModCode.Alt, Code.D, DecrementCount, new()
            {
                Exclude = Exclude.None,
                ExcludeSelector = ".disabled-hotkeys *",
                Description = "Decrement the counter"
            });

    Console.WriteLine($"OnInitialized() {HotKeysContext?.Keys.Count}");

    foreach (var hotKeyEntry in HotKeysContext?.Keys)
    {
        Console.WriteLine($"{hotKeyEntry} {hotKeyEntry.Description}");
    }
}

public void Dispose()
{
    // HotKeysContext?.Remove(ModCode.Ctrl, Code.I) does not remove the HotKey - Please fix
    HotKeysContext?.Remove(ModCode.Ctrl, Code.I, "Increment the counter", Exclude.None, ".disabled-hotkeys *");
    // HotKeysContext?.Remove(ModCode.Alt, Code.D) does not remove the HotKey - Please fix
    HotKeysContext?.Remove(ModCode.Alt, Code.D, "Decrement the counter", Exclude.None, ".disabled-hotkeys *");

    HotKeysCheatSheetVisible = false;
}
jsakamoto commented 4 months ago

@MikeWilliams-UK Thank you for your feedback! Honestly, I haven't been concerned well about the "Remove" API. I'll try to fix it.

jsakamoto commented 3 months ago

@MikeWilliams-UK

Today, I released a new version of the Blazor Hotkeys2.

Could you try it out?

MikeWilliams-UK commented 3 months ago

Thanks that update fixes the problem I saw