Browse Source

Merge branch 'v2_develop' into v2_3273_FOUND_finddeepestview

Tig 1 year ago
parent
commit
919f8e5991
3 changed files with 63 additions and 0 deletions
  1. 5 0
      Terminal.Gui/View/ViewMouse.cs
  2. 37 0
      UnitTests/View/MouseTests.cs
  3. 21 0
      UnitTests/Views/LabelTests.cs

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

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

+ 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 ()
     {