浏览代码

Fixed Visible bug and addd unit tests

Tig Kindel 1 年之前
父节点
当前提交
cecfb71ee1
共有 3 个文件被更改,包括 109 次插入19 次删除
  1. 7 0
      Terminal.Gui/Application.cs
  2. 11 4
      Terminal.Gui/View/Layout/ViewLayout.cs
  3. 91 15
      UnitTests/View/FindDeepestViewTests.cs

+ 7 - 0
Terminal.Gui/Application.cs

@@ -1376,6 +1376,13 @@ public static partial class Application
 
 
         var view = View.FindDeepestView (Current, a.MouseEvent.X, a.MouseEvent.Y);
         var view = View.FindDeepestView (Current, a.MouseEvent.X, a.MouseEvent.Y);
 
 
+        // TODO: Remove this temporary filter:
+        if (view is Adornment adornment)
+        {
+            view = adornment.Parent;
+        }
+
+
         if (view is { WantContinuousButtonPressed: true })
         if (view is { WantContinuousButtonPressed: true })
         {
         {
             WantContinuousButtonPressedView = view;
             WantContinuousButtonPressedView = view;

+ 11 - 4
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -545,20 +545,27 @@ public partial class View
         }
         }
     }
     }
 
 
-    #nullable enable
+#nullable enable
     /// <summary>Finds which view that belong to the <paramref name="start"/> superview at the provided location.</summary>
     /// <summary>Finds which view that belong to the <paramref name="start"/> superview at the provided location.</summary>
     /// <param name="start">The superview where to look for.</param>
     /// <param name="start">The superview where to look for.</param>
     /// <param name="x">The column location in the superview.</param>
     /// <param name="x">The column location in the superview.</param>
     /// <param name="y">The row location in the superview.</param>
     /// <param name="y">The row location in the superview.</param>
+    /// <param name="findAdornments">TODO: Remove this after unit tests are fixed</param>
     /// <returns>
     /// <returns>
     ///     The view that was found at the <paramref name="x"/> and <paramref name="y"/> coordinates.
     ///     The view that was found at the <paramref name="x"/> and <paramref name="y"/> coordinates.
     ///     <see langword="null"/> if no view was found.
     ///     <see langword="null"/> if no view was found.
     /// </returns>
     /// </returns>
+
     // CONCURRENCY: This method is not thread-safe.
     // CONCURRENCY: This method is not thread-safe.
     // Undefined behavior and likely program crashes are exposed by unsynchronized access to InternalSubviews.
     // Undefined behavior and likely program crashes are exposed by unsynchronized access to InternalSubviews.
-    public static View? FindDeepestView (View? start, int x, int y)
+    public static View? FindDeepestView (View? start, int x, int y, bool findAdornments = false)
     {
     {
-        if (start is null || !start.Frame.Contains (x, y))
+        if (start is null || !start.Visible)
+        {
+            return null;
+        }
+
+        if (!start.Frame.Contains (x, y))
         {
         {
             return null;
             return null;
         }
         }
@@ -582,7 +589,7 @@ public partial class View
         }
         }
         return start;
         return start;
     }
     }
-    #nullable restore
+#nullable restore
 
 
     /// <summary>Gets the <see cref="Frame"/> with a screen-relative location.</summary>
     /// <summary>Gets the <see cref="Frame"/> with a screen-relative location.</summary>
     /// <returns>The location and size of the view in screen-relative coordinates.</returns>
     /// <returns>The location and size of the view in screen-relative coordinates.</returns>

+ 91 - 15
UnitTests/View/FindDeepestViewTests.cs

@@ -16,14 +16,14 @@ public class FindDeepestViewTests (ITestOutputHelper output)
     [InlineData (2, 2)]
     [InlineData (2, 2)]
     public void Returns_Start_If_No_SubViews (int testX, int testY)
     public void Returns_Start_If_No_SubViews (int testX, int testY)
     {
     {
-        var view = new View ()
+        var start = new View ()
         {
         {
             Width = 10, Height = 10,
             Width = 10, Height = 10,
         };
         };
 
 
-        Assert.Same (view, View.FindDeepestView (view, testX, testY));
+        Assert.Same (start, View.FindDeepestView (start, testX, testY));
     }
     }
-
+    
     // Test that FindDeepestView returns null if the start view has no subviews and coords are outside the view
     // Test that FindDeepestView returns null if the start view has no subviews and coords are outside the view
     [Theory]
     [Theory]
     [InlineData (0, 0)]
     [InlineData (0, 0)]
@@ -31,13 +31,29 @@ public class FindDeepestViewTests (ITestOutputHelper output)
     [InlineData (20, 20)]
     [InlineData (20, 20)]
     public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
     public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
     {
     {
-        var view = new View ()
+        var start = new View ()
+        {
+            X = 1, Y = 2,
+            Width = 10, Height = 10,
+        };
+
+        Assert.Null(View.FindDeepestView (start, testX, testY));
+    }
+
+    [Theory]
+    [InlineData (0, 0)]
+    [InlineData (2, 1)]
+    [InlineData (20, 20)]
+    public void Returns_Null_If_Start_Not_Visible (int testX, int testY)
+    {
+        var start = new View ()
         {
         {
             X = 1, Y = 2,
             X = 1, Y = 2,
             Width = 10, Height = 10,
             Width = 10, Height = 10,
+            Visible = false,
         };
         };
 
 
-        Assert.Null(View.FindDeepestView (view, testX, testY));
+        Assert.Null (View.FindDeepestView (start, testX, testY));
     }
     }
 
 
     // Test that FindDeepestView returns the correct view if the start view has subviews
     // Test that FindDeepestView returns the correct view if the start view has subviews
@@ -52,7 +68,7 @@ public class FindDeepestViewTests (ITestOutputHelper output)
     [InlineData (5, 6, true)]
     [InlineData (5, 6, true)]
     public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound)
     public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound)
     {
     {
-        var view = new View ()
+        var start = new View ()
         {
         {
             Width = 10, Height = 10,
             Width = 10, Height = 10,
         };
         };
@@ -62,13 +78,73 @@ public class FindDeepestViewTests (ITestOutputHelper output)
             X = 1, Y = 2,
             X = 1, Y = 2,
             Width = 5, Height = 5,
             Width = 5, Height = 5,
         };
         };
-        view.Add (subview);
+        start.Add (subview);
 
 
-        var found = View.FindDeepestView (view, testX, testY);
+        var found = View.FindDeepestView (start, testX, testY);
 
 
         Assert.Equal (expectedSubViewFound, found == subview);
         Assert.Equal (expectedSubViewFound, found == subview);
     }
     }
 
 
+    [Theory]
+    [InlineData (0, 0, false)]
+    [InlineData (1, 1, false)]
+    [InlineData (9, 9, false)]
+    [InlineData (10, 10, false)]
+    [InlineData (6, 7, false)]
+    [InlineData (1, 2, false)]
+    [InlineData (5, 6, false)]
+    public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expectedSubViewFound)
+    {
+        var start = new View ()
+        {
+            Width = 10, Height = 10,
+        };
+
+        var subview = new View ()
+        {
+            X = 1, Y = 2,
+            Width = 5, Height = 5,
+            Visible = false
+        };
+        start.Add (subview);
+
+        var found = View.FindDeepestView (start, testX, testY);
+
+        Assert.Equal (expectedSubViewFound, found == subview);
+    }
+
+
+    [Theory]
+    [InlineData (0, 0, false)]
+    [InlineData (1, 1, false)]
+    [InlineData (9, 9, false)]
+    [InlineData (10, 10, false)]
+    [InlineData (6, 7, false)]
+    [InlineData (1, 2, false)]
+    [InlineData (5, 6, false)]
+    public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int testY, bool expectedSubViewFound)
+    {
+        var start = new View ()
+        {
+            Width = 10, Height = 10,
+            Visible = false
+        };
+
+        var subview = new View ()
+        {
+            X = 1, Y = 2,
+            Width = 5, Height = 5,
+        };
+        start.Add (subview);
+        subview.Visible = true;
+        Assert.True (subview.Visible);
+        Assert.False (start.Visible);
+        var found = View.FindDeepestView (start, testX, testY);
+
+        Assert.Equal (expectedSubViewFound, found == subview);
+    }
+
+
     // Test that FindDeepestView works if the start view has positive Adornments
     // Test that FindDeepestView works if the start view has positive Adornments
     [Theory]
     [Theory]
     [InlineData (0, 0, false)]
     [InlineData (0, 0, false)]
@@ -84,20 +160,20 @@ public class FindDeepestViewTests (ITestOutputHelper output)
     [InlineData (6, 7, true)]
     [InlineData (6, 7, true)]
     public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
     public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
     {
     {
-        var view = new View ()
+        var start = new View ()
         {
         {
             Width = 10, Height = 10,
             Width = 10, Height = 10,
         };
         };
-        view.Margin.Thickness = new Thickness (1);
+        start.Margin.Thickness = new Thickness (1);
 
 
         var subview = new View ()
         var subview = new View ()
         {
         {
             X = 1, Y = 2,
             X = 1, Y = 2,
             Width = 5, Height = 5,
             Width = 5, Height = 5,
         };
         };
-        view.Add (subview);
+        start.Add (subview);
 
 
-        var found = View.FindDeepestView (view, testX, testY);
+        var found = View.FindDeepestView (start, testX, testY);
 
 
         Assert.Equal (expectedSubViewFound, found == subview);
         Assert.Equal (expectedSubViewFound, found == subview);
     }
     }
@@ -117,7 +193,7 @@ public class FindDeepestViewTests (ITestOutputHelper output)
     [InlineData (2, 3, true)]
     [InlineData (2, 3, true)]
     public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
     public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
     {
     {
-        var view = new View ()
+        var start = new View ()
         {
         {
             Width = 10, Height = 10,
             Width = 10, Height = 10,
         };
         };
@@ -128,9 +204,9 @@ public class FindDeepestViewTests (ITestOutputHelper output)
             Width = 5, Height = 5,
             Width = 5, Height = 5,
         };
         };
         subview.Margin.Thickness = new Thickness (1);
         subview.Margin.Thickness = new Thickness (1);
-        view.Add (subview);
+        start.Add (subview);
 
 
-        var found = View.FindDeepestView (view, testX, testY);
+        var found = View.FindDeepestView (start, testX, testY);
 
 
         Assert.Equal (expectedSubViewFound, found == subview);
         Assert.Equal (expectedSubViewFound, found == subview);
     }
     }