Browse Source

Merge pull request #3311 from tig/v2_3310-OnMouseClick-CanSetFocus

Tig 1 year ago
parent
commit
e4e3ffeafd

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

@@ -146,6 +146,11 @@ public partial class View
             return true;
         }
 
+        if (!HasFocus && CanFocus)
+        {
+            SetFocus ();
+        }
+
         return args.Handled;
     }
 }

+ 1 - 1
Terminal.Gui/Views/Label.cs

@@ -30,7 +30,7 @@ public class Label : View
 
     private void Label_MouseClick (object sender, MouseEventEventArgs e)
     {
-        e.Handled = InvokeCommand (Command.Accept) == true;
+        e.Handled = InvokeCommand (Command.HotKey) == true;
     }
 
     private void Label_TitleChanged (object sender, StateEventArgs<string> e)

+ 37 - 0
UnitTests/View/MouseTests.cs

@@ -0,0 +1,37 @@
+using Xunit.Abstractions;
+
+namespace Terminal.Gui.ViewTests;
+
+public class MouseTests (ITestOutputHelper output)
+{
+    [Theory]
+    [InlineData (false, false, false)]
+    [InlineData (true, false, true)]
+    [InlineData (true, true, true)]
+    public void MouseClick_SetsFocus_If_CanFocus (bool canFocus, bool setFocus, bool expectedHasFocus)
+    {
+        var superView = new View { CanFocus = true, Height = 1, Width = 15 };
+        var focusedView = new View { CanFocus = true, Width = 1, Height = 1 };
+        var testView = new View { CanFocus = canFocus, X = 4, Width = 4, Height = 1 };
+        superView.Add (focusedView, testView);
+        superView.BeginInit ();
+        superView.EndInit ();
+
+        focusedView.SetFocus ();
+
+        Assert.True (superView.HasFocus);
+        Assert.True (focusedView.HasFocus);
+        Assert.False (testView.HasFocus);
+
+        if (setFocus)
+        {
+            testView.SetFocus ();
+        }
+
+        testView.OnMouseEvent (new() { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked });
+        Assert.True (superView.HasFocus);
+        Assert.Equal (expectedHasFocus, testView.HasFocus);
+    }
+
+    // TODO: Add more tests that ensure the above test works with positive adornments
+}

+ 21 - 0
UnitTests/Views/LabelTests.cs

@@ -51,6 +51,27 @@ public class LabelTests
         Assert.True (nextSubview.HasFocus);
     }
 
+
+    [Fact]
+    public void MouseClick_SetsFocus_OnNextSubview ()
+    {
+        var superView = new View () { CanFocus = true, Height = 1, Width = 15};
+        var focusedView = new View () { CanFocus = true, Width = 1, Height = 1 };
+        var label = new Label () { X = 2, Title = "_x" };
+        var nextSubview = new View () { CanFocus = true, X = 4, Width = 4, Height = 1 };
+        superView.Add (focusedView, label, nextSubview);
+        superView.BeginInit ();
+        superView.EndInit ();
+
+        Assert.False (focusedView.HasFocus);
+        Assert.False (label.HasFocus);
+        Assert.False (nextSubview.HasFocus);
+
+        label.OnMouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked });
+        Assert.False (label.HasFocus);
+        Assert.True (nextSubview.HasFocus);
+    }
+
     [Fact]
     public void HotKey_Command_Does_Not_Accept ()
     {