Browse Source

KeyBinding -> record struct

Tig 1 year ago
parent
commit
c2dcd28a15

+ 3 - 2
Terminal.Gui/Input/KeyBinding.cs

@@ -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
 // MIT licensed open source project https://github.com/jacobslusser/ScintillaNET/blob/master/src/ScintillaNET/Command.cs
 
 
 namespace Terminal.Gui;
 namespace Terminal.Gui;
 
 
 /// <summary>Provides a collection of <see cref="Command"/> objects that are scoped to <see cref="KeyBindingScope"/>.</summary>
 /// <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>
     /// <summary>Initializes a new instance.</summary>
     /// <param name="commands"></param>
     /// <param name="commands"></param>

+ 19 - 5
Terminal.Gui/Input/KeyBindings.cs

@@ -1,4 +1,4 @@
-using static System.Formats.Asn1.AsnWriter;
+#nullable enable
 
 
 namespace Terminal.Gui;
 namespace Terminal.Gui;
 
 
@@ -36,7 +36,7 @@ public class KeyBindings
         else
         else
         {
         {
             Bindings.Add (key, binding);
             Bindings.Add (key, binding);
-            if (binding.Scope.HasFlag (KeyBindingScope.Application))
+            if (binding.Scope.FastHasFlags (KeyBindingScope.Application))
             {
             {
                 Application.AddKeyBinding (key, BoundView);
                 Application.AddKeyBinding (key, BoundView);
             }
             }
@@ -81,7 +81,7 @@ public class KeyBindings
         else
         else
         {
         {
             Add (key, new KeyBinding (commands, scope));
             Add (key, new KeyBinding (commands, scope));
-            if (scope.HasFlag (KeyBindingScope.Application))
+            if (scope.FastHasFlags (KeyBindingScope.Application))
             {
             {
                 Application.AddKeyBinding (key, BoundView);
                 Application.AddKeyBinding (key, BoundView);
             }
             }
@@ -145,13 +145,27 @@ public class KeyBindings
     /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
     /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
     /// <param name="key"></param>
     /// <param name="key"></param>
     /// <returns></returns>
     /// <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>
     /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
     /// <param name="key"></param>
     /// <param name="key"></param>
     /// <param name="scope"></param>
     /// <param name="scope"></param>
     /// <returns></returns>
     /// <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>
     /// <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>
     /// <param name="key">The key to check.</param>

+ 7 - 7
UnitTests/Input/KeyBindingTests.cs

@@ -261,14 +261,14 @@ public class KeyBindingTests
         binding = keyBindings.Get (key, scope);
         binding = keyBindings.Get (key, scope);
         Assert.Contains (Command.Right, binding.Commands);
         Assert.Contains (Command.Right, binding.Commands);
         Assert.Contains (Command.Left, 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]
     [Theory]