Tig 7 mēneši atpakaļ
vecāks
revīzija
7d4f4e4735

+ 9 - 8
Terminal.Gui/Input/Keyboard/KeyBindings.cs

@@ -346,16 +346,17 @@ public class KeyBindings
     ///     </para>
     /// </remarks>
     /// <param name="key">The key bound to the command to be replaced.</param>
-    /// <param name="commands">The set of commands to replace the old ones with.</param>
-    public void ReplaceCommands (Key key, params Command [] commands)
+    /// <param name="newCommands">The set of commands to replace the old ones with.</param>
+    public void ReplaceCommands (Key key, params Command [] newCommands)
     {
         if (TryGet (key, out KeyBinding binding))
         {
-            binding.Commands = commands;
+            Remove (key);
+            Add (key, binding.Scope, newCommands);
         }
         else
         {
-            Add (key, commands);
+            Add (key, newCommands);
         }
     }
 
@@ -418,10 +419,10 @@ public class KeyBindings
     {
         if (!key.IsValid)
         {
-        //if (BoundView is null)
-        //{
-        //    throw new InvalidOperationException ("KeyBindings must be bound to a View to use this method.");
-        //}
+            //if (BoundView is null)
+            //{
+            //    throw new InvalidOperationException ("KeyBindings must be bound to a View to use this method.");
+            //}
 
             binding = new (Array.Empty<Command> (), KeyBindingScope.Disabled, null);
             return false;

+ 4 - 1
Terminal.Gui/View/View.Command.cs

@@ -102,7 +102,10 @@ public partial class View // Command APIs
                 }
             }
 
-            return SuperView?.InvokeCommand<KeyBinding> (Command.Accept, new ([Command.Accept], 0, null, this)) == true;
+            if (SuperView is { })
+            {
+                return SuperView?.InvokeCommand<KeyBinding> (Command.Accept, new ([Command.Accept], 0, null, this)) is true;
+            }
         }
 
         return Accepting is null ? null : args.Cancel;

+ 5 - 0
Terminal.Gui/View/View.Mouse.cs

@@ -13,7 +13,12 @@ public partial class View // Mouse APIs
     {
         MouseBindings = new ();
 
+        // TODO: Should the default really work with any button or just button1?
         MouseBindings.Add (MouseFlags.Button1Clicked, Command.Select);
+        MouseBindings.Add (MouseFlags.Button2Clicked, Command.Select);
+        MouseBindings.Add (MouseFlags.Button3Clicked, Command.Select);
+        MouseBindings.Add (MouseFlags.Button4Clicked, Command.Select);
+        MouseBindings.Add (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Select);
     }
 
 

+ 7 - 1
Terminal.Gui/Views/CharMap/CharMap.cs

@@ -59,7 +59,8 @@ public class CharMap : View, IDesignable
         KeyBindings.Add (ContextMenu.DefaultKey, Command.Context);
 
         MouseBindings.Add (MouseFlags.Button1DoubleClicked, Command.Accept);
-        MouseBindings.Add (MouseFlags.Button3Clicked, Command.Context);
+        MouseBindings.ReplaceCommands(MouseFlags.Button3Clicked, Command.Context);
+        MouseBindings.ReplaceCommands (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Context);
         MouseBindings.Add (MouseFlags.WheeledDown, Command.ScrollDown);
         MouseBindings.Add (MouseFlags.WheeledUp, Command.ScrollUp);
         MouseBindings.Add (MouseFlags.WheeledLeft, Command.ScrollLeft);
@@ -561,6 +562,11 @@ public class CharMap : View, IDesignable
     [RequiresDynamicCode ("AOT")]
     private void ShowDetails ()
     {
+        if (!Application.Initialized)
+        {
+            // Some unit tests invoke Accept without Init
+            return;
+        }
         UcdApiClient? client = new ();
         var decResponse = string.Empty;
         var getCodePointError = string.Empty;

+ 2 - 0
Terminal.Gui/Views/CheckBox.cs

@@ -36,6 +36,8 @@ public class CheckBox : View
         // Accept (Enter key) - Raise Accept event - DO NOT advance state
         AddCommand (Command.Accept, RaiseAccepting);
 
+        MouseBindings.Add (MouseFlags.Button1DoubleClicked, Command.Accept);
+
         TitleChanged += Checkbox_TitleChanged;
 
         HighlightStyle = DefaultHighlightStyle;

+ 10 - 0
UnitTests/Input/Keyboard/KeyBindingTests.cs

@@ -0,0 +1,10 @@
+using Terminal.Gui.EnumExtensions;
+using Xunit.Abstractions;
+
+namespace Terminal.Gui.InputTests;
+
+public class KeyBindingTests ()
+{
+    // TODO: Add tests for KeyBinding
+
+}

+ 19 - 7
UnitTests/Input/KeyBindingTests.cs → UnitTests/Input/Keyboard/KeyBindingsTests.cs

@@ -1,13 +1,11 @@
 using Terminal.Gui.EnumExtensions;
 using Xunit.Abstractions;
+using static Unix.Terminal.Delegates;
 
 namespace Terminal.Gui.InputTests;
 
-public class KeyBindingTests
+public class KeyBindingsTests ()
 {
-    public KeyBindingTests (ITestOutputHelper output) { _output = output; }
-    private readonly ITestOutputHelper _output;
-
     [Fact]
     public void Add_Invalid_Key_Throws ()
     {
@@ -72,7 +70,7 @@ public class KeyBindingTests
     }
 
     // Add should not allow duplicates
-        [Fact]
+    [Fact]
     public void Add_With_Throws_If_Exists ()
     {
         var keyBindings = new KeyBindings (new View ());
@@ -282,7 +280,7 @@ public class KeyBindingTests
     [InlineData (KeyBindingScope.Application)]
     public void Scope_Add_Adds (KeyBindingScope scope)
     {
-        var keyBindings = new KeyBindings (scope.FastHasFlags(KeyBindingScope.Application) ? null : new ());
+        var keyBindings = new KeyBindings (scope.FastHasFlags (KeyBindingScope.Application) ? null : new ());
         Command [] commands = { Command.Right, Command.Left };
 
         var key = new Key (Key.A);
@@ -356,7 +354,7 @@ public class KeyBindingTests
         keyBindings.Add (Key.Q.WithCtrl, KeyBindingScope.Application, Command.HotKey);
         var key = new Key (Key.Q.WithCtrl);
         bool result = keyBindings.TryGet (key, out KeyBinding _);
-        Assert.True (result);;
+        Assert.True (result); ;
 
         result = keyBindings.Bindings.TryGetValue (key, out KeyBinding _);
         Assert.True (result);
@@ -379,4 +377,18 @@ public class KeyBindingTests
         Assert.True (result);
         Assert.Contains (Command.HotKey, bindings.Commands);
     }
+
+    [Fact]
+    public void ReplaceCommands_Replaces ()
+    {
+        var keyBindings = new KeyBindings ();
+        keyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept);
+
+        keyBindings.ReplaceCommands (Key.A, Command.Refresh);
+
+        bool result = keyBindings.TryGet (Key.A, out KeyBinding bindings);
+        Assert.True (result);
+        Assert.Contains (Command.Refresh, bindings.Commands);
+
+    }
 }

+ 0 - 0
UnitTests/Input/KeyTests.cs → UnitTests/Input/Keyboard/KeyTests.cs


+ 13 - 4
UnitTests/Views/CheckBoxTests.cs

@@ -252,7 +252,7 @@ public class CheckBoxTests (ITestOutputHelper output)
 
     [Fact]
     [SetupFakeDriver]
-    public void Mouse_Click ()
+    public void Mouse_Click_Selects ()
     {
         var checkBox = new CheckBox { Text = "_Checkbox" };
         Assert.True (checkBox.CanFocus);
@@ -296,7 +296,7 @@ public class CheckBoxTests (ITestOutputHelper output)
 
     [Fact]
     [SetupFakeDriver]
-    public void Mouse_DoubleClick ()
+    public void Mouse_DoubleClick_Accepts ()
     {
         var checkBox = new CheckBox { Text = "_Checkbox" };
         Assert.True (checkBox.CanFocus);
@@ -308,7 +308,11 @@ public class CheckBoxTests (ITestOutputHelper output)
         checkBox.Selecting += (s, e) => selectCount++;
 
         int acceptCount = 0;
-        checkBox.Accepting += (s, e) => acceptCount++;
+        checkBox.Accepting += (s, e) =>
+                              {
+                                  acceptCount++;
+                                  e.Cancel = true;
+                              };
 
         checkBox.HasFocus = true;
         Assert.True (checkBox.HasFocus);
@@ -319,9 +323,14 @@ public class CheckBoxTests (ITestOutputHelper output)
 
         Assert.True (checkBox.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1DoubleClicked }));
 
+        Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
+        Assert.Equal (0, checkedStateChangingCount);
+        Assert.Equal (0, selectCount);
+        Assert.Equal (1, acceptCount);
+
     }
 
-#endregion Mouse Tests
+    #endregion Mouse Tests
 
     [Fact]
     [AutoInitShutdown]

+ 2 - 2
UnitTests/Views/TimeFieldTests.cs

@@ -147,8 +147,8 @@ public class TimeFieldTests
         Assert.True (tf.NewKeyDownEvent (Key.End));
         Assert.Equal (8, tf.CursorPosition);
         Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
-        Assert.Equal (9, tf.CursorPosition);
-        Assert.Equal (tf.SelectedLength, tf.Text.Length);
+        Assert.Equal (1, tf.CursorPosition);
+        Assert.Equal (9, tf.Text.Length);
         Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
         Assert.Equal (8, tf.CursorPosition);
         Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));