Skip to content

Remove all keyboard shortcuts #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/Blazor.Diagrams.Core/Behaviors/KeyboardShortcutsBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,54 @@
using Blazor.Diagrams.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Blazor.Diagrams.Core.Behaviors;

public class KeyboardShortcutsBehavior : Behavior
{
private readonly Dictionary<string, Func<Diagram, ValueTask>> _shortcuts;
public record KeyboardShortcut(string Key, bool Ctrl, bool Shift, bool Alt, Func<Diagram, ValueTask> Action);

private readonly Dictionary<string, KeyboardShortcut> _shortcuts;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to use a record, wouldn't it be better now to just have a list/set of the shortcuts and change the equality check to test for Key+Ctrl+Shift+Alt ?


public KeyboardShortcutsBehavior(Diagram diagram) : base(diagram)
{
_shortcuts = new Dictionary<string, Func<Diagram, ValueTask>>();
_shortcuts = new Dictionary<string, KeyboardShortcut>();
SetShortcut("Delete", false, false, false, KeyboardShortcutsDefaults.DeleteSelection);
SetShortcut("g", true, false, true, KeyboardShortcutsDefaults.Grouping);

Diagram.KeyDown += OnDiagramKeyDown;
}

public KeyboardShortcut[] GetShortcuts()
{
return _shortcuts.Values.ToArray();
}

public void SetShortcut(string key, bool ctrl, bool shift, bool alt, Func<Diagram, ValueTask> action)
{
var k = KeysUtils.GetStringRepresentation(ctrl, shift, alt, key);
_shortcuts[k] = action;
_shortcuts[k] = new KeyboardShortcut(key, ctrl, shift, alt, action);
}

public bool RemoveShortcut(string key, bool ctrl, bool shift, bool alt)
{
var k = KeysUtils.GetStringRepresentation(ctrl, shift, alt, key);
return _shortcuts.Remove(k);
}

public void RemoveAllShortcuts()
{
_shortcuts.Clear();
}

private async void OnDiagramKeyDown(KeyboardEventArgs e)
{
var k = KeysUtils.GetStringRepresentation(e.CtrlKey, e.ShiftKey, e.AltKey, e.Key);
if (_shortcuts.TryGetValue(k, out var action))
if (_shortcuts.TryGetValue(k, out var shortcut))
{
await action(Diagram);
await shortcut.Action(Diagram);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public void Behavior_ShouldExecuteAction_WhenCombinationIsPressed(string key, bo
executed.Should().BeTrue();
}

[Fact]
public void RemoveAll_ShouldRemoveAllShortcuts()
{
var diagram = new TestDiagram();
var ksb = diagram.GetBehavior<KeyboardShortcutsBehavior>()!;

ksb.GetShortcuts().Length.Should().Be(2);

ksb.RemoveAllShortcuts();

ksb.GetShortcuts().Length.Should().Be(0);
}

[Fact]
public void Behavior_ShouldDoNothing_WhenRemoved()
{
Expand Down