mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
KeyBinding -> record struct
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user