KeyBinding -> record struct

This commit is contained in:
Tig
2024-06-08 08:46:26 -06:00
parent 695b8cf293
commit c2dcd28a15
3 changed files with 29 additions and 14 deletions

View File

@@ -1,10 +1,11 @@
// These classes use a key binding system based on the design implemented in Scintilla.Net which is an
#nullable enable
// These classes use a key binding system based on the design implemented in Scintilla.Net which is an
// MIT licensed open source project https://github.com/jacobslusser/ScintillaNET/blob/master/src/ScintillaNET/Command.cs
namespace Terminal.Gui;
/// <summary>Provides a collection of <see cref="Command"/> objects that are scoped to <see cref="KeyBindingScope"/>.</summary>
public class KeyBinding
public record struct KeyBinding
{
/// <summary>Initializes a new instance.</summary>
/// <param name="commands"></param>

View File

@@ -1,4 +1,4 @@
using static System.Formats.Asn1.AsnWriter;
#nullable enable
namespace Terminal.Gui;
@@ -36,7 +36,7 @@ public class KeyBindings
else
{
Bindings.Add (key, binding);
if (binding.Scope.HasFlag (KeyBindingScope.Application))
if (binding.Scope.FastHasFlags (KeyBindingScope.Application))
{
Application.AddKeyBinding (key, BoundView);
}
@@ -81,7 +81,7 @@ public class KeyBindings
else
{
Add (key, new KeyBinding (commands, scope));
if (scope.HasFlag (KeyBindingScope.Application))
if (scope.FastHasFlags (KeyBindingScope.Application))
{
Application.AddKeyBinding (key, BoundView);
}
@@ -145,13 +145,27 @@ public class KeyBindings
/// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
/// <param name="key"></param>
/// <returns></returns>
public KeyBinding Get (Key key) { return TryGet (key, out KeyBinding binding) ? binding : null; }
public KeyBinding Get (Key key)
{
if (TryGet (key, out KeyBinding binding))
{
return binding;
}
throw new InvalidOperationException ($"Key {key} is not bound.");
}
/// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
/// <param name="key"></param>
/// <param name="scope"></param>
/// <returns></returns>
public KeyBinding Get (Key key, KeyBindingScope scope) { return TryGet (key, scope, out KeyBinding binding) ? binding : null; }
public KeyBinding Get (Key key, KeyBindingScope scope)
{
if (TryGet (key, scope, out KeyBinding binding))
{
return binding;
}
throw new InvalidOperationException ($"Key {key}/{scope} is not bound.");
}
/// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="key"/> if it exists.</summary>
/// <param name="key">The key to check.</param>

View File

@@ -261,14 +261,14 @@ public class KeyBindingTests
binding = keyBindings.Get (key, scope);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
}
// negative test
binding = keyBindings.Get (key, 0);
Assert.Null (binding);
Command [] resultCommands = keyBindings.GetCommands (key);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
[Fact]
public void Get_Binding_Not_Found_Throws ()
{
var keyBindings = new KeyBindings ();
Assert.Throws<InvalidOperationException> (() => keyBindings.Get (Key.A));
Assert.Throws<InvalidOperationException> (() => keyBindings.Get (Key.B, KeyBindingScope.Application));
}
[Theory]