Tig 10 bulan lalu
induk
melakukan
8dbdccd77f

+ 63 - 101
Terminal.Gui/Application/Application.Mouse.cs

@@ -1,16 +1,12 @@
 #nullable enable
-<<<<<<< Updated upstream
-=======
+using System.Diagnostics;
 using Microsoft.CodeAnalysis;
 using Microsoft.CodeAnalysis.VisualBasic.Syntax;
 
->>>>>>> Stashed changes
 namespace Terminal.Gui;
 
 public static partial class Application // Mouse handling
 {
-    #region Mouse handling
-
     /// <summary>Disable or enable the mouse. The mouse is enabled by default.</summary>
     [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
     public static bool IsMouseDisabled { get; set; }
@@ -145,22 +141,9 @@ public static partial class Application // Mouse handling
             return;
         }
 
-<<<<<<< Updated upstream
-        var view = View.FindDeepestView (Current, mouseEvent.Position);
-=======
-        Stack<View?> viewsUnderMouse = View.GetViewsUnderMouse (mouseEvent.Position);
-
-        View? deepestViewUnderMouse = viewsUnderMouse.TryPeek (out View? result) ? result : null;
+        List<View?> currentViewsUnderMouse = View.GetViewsUnderMouse (mouseEvent.Position);
 
-        if ((mouseEvent.Flags == MouseFlags.Button1Pressed
-             || mouseEvent.Flags == MouseFlags.Button2Pressed
-             || mouseEvent.Flags == MouseFlags.Button3Pressed
-             || mouseEvent.Flags == MouseFlags.Button4Pressed)
-            && Popover is { Visible: true } && !ApplicationNavigation.IsInHierarchy (Popover, deepestViewUnderMouse, includeAdornments: true))
-        {
-            Popover.Visible = false;
-        }
->>>>>>> Stashed changes
+        View? deepestViewUnderMouse = currentViewsUnderMouse.LastOrDefault ();
 
         if (deepestViewUnderMouse is { })
         {
@@ -223,36 +206,12 @@ public static partial class Application // Mouse handling
 
         // We can combine this into the switch expression to reduce cognitive complexity even more and likely
         // avoid one or two of these checks in the process, as well.
-<<<<<<< Updated upstream
-        WantContinuousButtonPressedView = view switch
-                                          {
-                                              { WantContinuousButtonPressed: true } => view,
-                                              _                                     => null
-                                          };
-
-        if (view is not Adornment
-         && (view is null || view == ApplicationOverlapped.OverlappedTop)
-         && Current is { Modal: false }
-         && ApplicationOverlapped.OverlappedTop != null
-         && mouseEvent.Flags is not MouseFlags.ReportMousePosition and not 0)
-        {
-            // This occurs when there are multiple overlapped "tops"
-            // E.g. "Mdi" - in the Background Worker Scenario
-            View? top = ApplicationOverlapped.FindDeepestTop (Top!, mouseEvent.Position);
-            view = View.FindDeepestView (top, mouseEvent.Position);
 
-            if (view is { } && view != ApplicationOverlapped.OverlappedTop && top != Current && top is { })
-            {
-                ApplicationOverlapped.MoveCurrent ((Toplevel)top);
-            }
-        }
-=======
         WantContinuousButtonPressedView = deepestViewUnderMouse switch
         {
             { WantContinuousButtonPressed: true } => deepestViewUnderMouse,
             _ => null
         };
->>>>>>> Stashed changes
 
         // May be null before the prior condition or the condition may set it as null.
         // So, the checking must be outside the prior condition.
@@ -289,13 +248,55 @@ public static partial class Application // Mouse handling
         }
         else
         {
+            Debug.Fail ("This should never happen");
             return;
         }
 
-        // Mouse Enter/Leave events
+        RaiseMouseEnterLeaveEvents (me.ScreenPosition, currentViewsUnderMouse, me);
+
+        WantContinuousButtonPressedView = deepestViewUnderMouse.WantContinuousButtonPressed ? deepestViewUnderMouse : null;
+
+        //Debug.WriteLine ($"OnMouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags}");
+        if (deepestViewUnderMouse.Id == "mouseDemo")
+        {
+
+        }
+
+        while (deepestViewUnderMouse.NewMouseEvent (me) is not true && MouseGrabView is not { })
+        {
+            if (deepestViewUnderMouse is Adornment adornmentView)
+            {
+                deepestViewUnderMouse = adornmentView.Parent!.SuperView;
+            }
+            else
+            {
+                deepestViewUnderMouse = deepestViewUnderMouse.SuperView;
+            }
+
+            if (deepestViewUnderMouse is null)
+            {
+                break;
+            }
+
+            Point boundsPoint = deepestViewUnderMouse.ScreenToViewport (mouseEvent.Position);
+
+            me = new ()
+            {
+                Position = boundsPoint,
+                Flags = mouseEvent.Flags,
+                ScreenPosition = mouseEvent.Position,
+                View = deepestViewUnderMouse
+            };
+        }
+
+        ApplicationOverlapped.BringOverlappedTopToFront ();
+    }
 
-        // Tell any views that are no longer under the mouse that the mouse has left and remove them from list
-        List<View?> viewsToLeave = ViewsUnderMouse.Where (v => v is { } && !viewsUnderMouse.Contains (v)).ToList ();
+    // TODO: Refactor MouseEnter/LeaveEvents to not take MouseEvent param.
+    internal static void RaiseMouseEnterLeaveEvents (Point screenPosition, List<View?> currentViewsUnderMouse, MouseEvent me)
+    {
+        // Tell any views that are no longer under the mouse that the mouse has left
+        List<View?> viewsToLeave = ViewsUnderMouse.Where (v => v is { } && !currentViewsUnderMouse.Contains (v)).ToList ();
         foreach (View? view in viewsToLeave)
         {
             if (view is null)
@@ -305,89 +306,50 @@ public static partial class Application // Mouse handling
 
             if (view is Adornment adornmentView)
             {
-                Point frameLoc = adornmentView.ScreenToFrame (mouseEvent.Position);
+                Point frameLoc = adornmentView.ScreenToFrame (screenPosition);
                 if (adornmentView.Parent is { } && !adornmentView.Contains (frameLoc))
                 {
-                    ViewsUnderMouse.Remove (view);
                     view.NewMouseLeaveEvent (me);
                 }
             }
             else
             {
-                Point viewportLocation = view.ScreenToViewport (mouseEvent.Position);
-                if (!view.Contains (viewportLocation))
+                Point superViewLoc = view.SuperView?.ScreenToViewport (screenPosition) ?? screenPosition;
+                if (!view.Contains (superViewLoc))
                 {
-                    ViewsUnderMouse.Remove (view);
                     view.NewMouseLeaveEvent (me);
                 }
-
             }
         }
 
-        // Tell any views that are now under the mouse (viewsUnderMouse) that the mouse has entered and add them to the list
-        foreach (View? view in viewsUnderMouse)
+        ViewsUnderMouse.Clear ();
+
+        // Tell any views that are now under the mouse that the mouse has entered and add them to the list
+        foreach (View? view in currentViewsUnderMouse)
         {
             if (view is null)
             {
                 continue;
             }
 
-            Point viewportLocation = view.ScreenToViewport (mouseEvent.Position);
+            ViewsUnderMouse.Add (view);
 
             if (view is Adornment adornmentView)
             {
-
-                if (adornmentView.Parent is { } && !adornmentView.Contains (viewportLocation))
+                Point frameLoc = view.ScreenToFrame (me.ScreenPosition);
+                if (adornmentView.Parent is { } && !adornmentView.Contains (frameLoc))
                 {
-                    ViewsUnderMouse.Add (view);
                     view.NewMouseEnterEvent (me);
                 }
             }
-            else if (view.Contains (viewportLocation))
-            {
-                ViewsUnderMouse.Add (view);
-                view.NewMouseEnterEvent (me);
-            }
-        }
-
-
-        WantContinuousButtonPressedView = deepestViewUnderMouse.WantContinuousButtonPressed ? deepestViewUnderMouse : null;
-
-        //Debug.WriteLine ($"OnMouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags}");
-        if (deepestViewUnderMouse.Id == "mouseDemo")
-        {
-
-        }
-
-        while (deepestViewUnderMouse.NewMouseEvent (me) is not true && MouseGrabView is not { })
-        {
-            if (deepestViewUnderMouse is Adornment adornmentView)
-            {
-                deepestViewUnderMouse = adornmentView.Parent!.SuperView;
-            }
             else
             {
-                deepestViewUnderMouse = deepestViewUnderMouse.SuperView;
-            }
-
-            if (deepestViewUnderMouse is null)
-            {
-                break;
+                Point superViewLoc = view.SuperView?.ScreenToViewport (me.ScreenPosition) ?? me.ScreenPosition;
+                if (view.Contains (superViewLoc))
+                {
+                    view.NewMouseEnterEvent (me);
+                }
             }
-
-            Point boundsPoint = deepestViewUnderMouse.ScreenToViewport (mouseEvent.Position);
-
-            me = new ()
-            {
-                Position = boundsPoint,
-                Flags = mouseEvent.Flags,
-                ScreenPosition = mouseEvent.Position,
-                View = deepestViewUnderMouse
-            };
         }
-
-        ApplicationOverlapped.BringOverlappedTopToFront ();
     }
-
-    #endregion Mouse handling
 }

+ 2 - 80
Terminal.Gui/View/View.Layout.cs

@@ -27,87 +27,9 @@ public partial class View // Layout APIs
     /// </returns>
 
     // CONCURRENCY: This method is not thread-safe. Undefined behavior and likely program crashes are exposed by unsynchronized access to InternalSubviews.
-    internal static View? FindDeepestView (View? start, in Point location)
+    internal static View? FindDeepestView (in Point location)
     {
-<<<<<<< Updated upstream
-=======
-        return GetViewsUnderMouse (location).TryPeek (out View? result) ? result : null;
-    }
-
-    internal static Stack<View?> GetViewsUnderMouse (in Point location)
-    {
-        Stack<View> viewsUnderMouse = new ();
-
-        View? start = Application.Top;
-
-        if (Application.Popover?.Visible == true)
-        {
-            start = Application.Popover;
-        }
-
->>>>>>> Stashed changes
-        Point currentLocation = location;
-
-        while (start is { Visible: true } && start.Contains (currentLocation))
-        {
-            Adornment? found = null;
-
-            if (start.Margin.Contains (currentLocation))
-            {
-                found = start.Margin;
-            }
-            else if (start.Border.Contains (currentLocation))
-            {
-                found = start.Border;
-            }
-            else if (start.Padding.Contains (currentLocation))
-            {
-                found = start.Padding;
-            }
-
-            Point viewportOffset = start.GetViewportOffsetFromFrame ();
-
-            if (found is { })
-            {
-                //viewsUnderMouse.Push (found);
-
-                start = found;
-                viewportOffset = found.Parent?.Frame.Location ?? Point.Empty;
-            }
-
-            int startOffsetX = currentLocation.X - (start.Frame.X + viewportOffset.X);
-            int startOffsetY = currentLocation.Y - (start.Frame.Y + viewportOffset.Y);
-
-            View? subview = null;
-
-            for (int i = start.InternalSubviews.Count - 1; i >= 0; i--)
-            {
-                if (start.InternalSubviews [i].Visible
-                    && start.InternalSubviews [i].Contains (new (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y)))
-                {
-                    subview = start.InternalSubviews [i];
-                    currentLocation.X = startOffsetX + start.Viewport.X;
-                    currentLocation.Y = startOffsetY + start.Viewport.Y;
-
-                    // start is the deepest subview under the mouse; stop searching the subviews
-                    viewsUnderMouse.Push (start);
-                    break;
-                }
-            }
-
-            if (subview is null)
-            {
-                // No subview was found that's under the mouse, so we're done
-                viewsUnderMouse.Push (start);
-                return viewsUnderMouse;
-            }
-
-            // We found a subview of start that's under the mouse, continue...
-            start = subview;
-            //viewsUnderMouse.Push (subview);
-        }
-
-        return viewsUnderMouse;
+        return GetViewsUnderMouse (location).LastOrDefault ();
     }
 
     // BUGBUG: This method interferes with Dialog/MessageBox default min/max size.

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

@@ -570,4 +570,78 @@ public partial class View // Mouse APIs
 
         return false;
     }
+
+    /// <summary>
+    ///    INTERNAL: Gets the Views that are under the mouse at <paramref name="location"/>, including Adornments.
+    /// </summary>
+    /// <param name="location"></param>
+    /// <returns></returns>
+    internal static List<View?> GetViewsUnderMouse (in Point location)
+    {
+        List<View> viewsUnderMouse = new ();
+
+        View? start = Application.Current ?? Application.Top;
+
+        Point currentLocation = location;
+
+        while (start is { Visible: true } && start.Contains (currentLocation))
+        {
+            viewsUnderMouse.Add (start);
+
+            Adornment? found = null;
+
+            if (start.Margin.Contains (currentLocation))
+            {
+                found = start.Margin;
+            }
+            else if (start.Border.Contains (currentLocation))
+            {
+                found = start.Border;
+            }
+            else if (start.Padding.Contains (currentLocation))
+            {
+                found = start.Padding;
+            }
+
+            Point viewportOffset = start.GetViewportOffsetFromFrame ();
+
+            if (found is { })
+            {
+                start = found;
+                viewsUnderMouse.Add (start);
+                viewportOffset = found.Parent?.Frame.Location ?? Point.Empty;
+            }
+
+            int startOffsetX = currentLocation.X - (start.Frame.X + viewportOffset.X);
+            int startOffsetY = currentLocation.Y - (start.Frame.Y + viewportOffset.Y);
+
+            View? subview = null;
+
+            for (int i = start.InternalSubviews.Count - 1; i >= 0; i--)
+            {
+                if (start.InternalSubviews [i].Visible
+                    && start.InternalSubviews [i].Contains (new (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y)))
+                {
+                    subview = start.InternalSubviews [i];
+                    currentLocation.X = startOffsetX + start.Viewport.X;
+                    currentLocation.Y = startOffsetY + start.Viewport.Y;
+
+                    // start is the deepest subview under the mouse; stop searching the subviews
+                    break;
+                }
+            }
+
+            if (subview is null)
+            {
+                // No subview was found that's under the mouse, so we're done
+                return viewsUnderMouse;
+            }
+
+            // We found a subview of start that's under the mouse, continue...
+            start = subview;
+        }
+
+        return viewsUnderMouse;
+    }
+
 }

+ 76 - 36
UICatalog/Scenarios/Mouse.cs

@@ -43,7 +43,10 @@ public class Mouse : Scenario
 
         for (var i = 0; i < filterSlider.Options.Count; i++)
         {
-            filterSlider.SetOption (i);
+            if (filterSlider.Options [i].Data != MouseFlags.ReportMousePosition)
+            {
+                filterSlider.SetOption (i);
+            }
         }
 
         win.Add (filterSlider);
@@ -93,17 +96,51 @@ public class Mouse : Scenario
 
         win.Add (cbHighlightOnPress);
 
-        var demo = new MouseDemo
+        var demo = new MouseEventDemoView
         {
             X = Pos.Right (filterSlider),
             Y = Pos.Bottom (cbHighlightOnPress),
-            Width = 20,
-            Height = 3,
-            Text = "Enter/Leave Demo",
-            TextAlignment = Alignment.Center,
-            VerticalTextAlignment = Alignment.Center,
-            ColorScheme = Colors.ColorSchemes ["Dialog"]
+            Width = Dim.Fill (),
+            Height = 15,
+            Title = "Enter/Leave Demo",
         };
+
+        demo.Padding.Initialized += DemoPaddingOnInitialized;
+
+        void DemoPaddingOnInitialized (object o, EventArgs eventArgs)
+        {
+            demo.Padding.Add (
+                              new MouseEventDemoView ()
+                              {
+                                  X = 0,
+                                  Y = 0,
+                                  Width = Dim.Fill (),
+                                  Height = Dim.Func (() => demo.Padding.Thickness.Top),
+                                  Title = "inPadding"
+                              });
+            demo.Padding.Thickness = demo.Padding.Thickness with { Top = 5 };
+        }
+
+        demo.Add (
+                  new MouseEventDemoView ()
+                  {
+                      X = 0,
+                      Y = 0,
+                      Width = Dim.Percent(30),
+                      Height = Dim.Fill(),
+                      Title = "sub1",
+                  });
+
+        demo.Add (
+                  new MouseEventDemoView ()
+                  {
+                      X = Pos.AnchorEnd(),
+                      Y = 0,
+                      Width = Dim.Percent (30),
+                      Height = Dim.Fill (),
+                      Title = "sub2",
+                  });
+
         win.Add (demo);
 
         var label = new Label
@@ -187,45 +224,48 @@ public class Mouse : Scenario
         Application.Shutdown ();
     }
 
-    public class MouseDemo : Shortcut
+    public class MouseEventDemoView : View
     {
-        private bool _button1PressedOnEnter;
-
-        public MouseDemo ()
+        public MouseEventDemoView ()
         {
             CanFocus = true;
-            Id = "mouseDemo";
-            Title = "Hi";
-            Key = Key.A.WithAlt;
-            HelpText = "Help!";
-            WantMousePositionReports = true;
+            Id = "mouseEventDemoView";
 
-            MouseEvent += (s, e) =>
-                          {
-                              if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
-                              {
-                                  if (!_button1PressedOnEnter)
-                                  {
-                                      ColorScheme = Colors.ColorSchemes ["Toplevel"];
-                                  }
-                              }
+            Padding.Thickness = new Thickness (1, 1, 1, 1);
 
-                              if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Released))
-                              {
-                                  ColorScheme = Colors.ColorSchemes ["Dialog"];
-                                  _button1PressedOnEnter = false;
-                              }
-                          };
+            Initialized += OnInitialized;
+
+            void OnInitialized (object sender, EventArgs e)
+            {
+                TextAlignment = Alignment.Center;
+                VerticalTextAlignment = Alignment.Center;
+
+                Padding.ColorScheme = new ColorScheme (new Attribute (Color.Black));
+
+                Padding.MouseEnter += PaddingOnMouseEnter;
+                Padding.MouseLeave += PaddingOnMouseLeave;
+
+                void PaddingOnMouseEnter (object o, MouseEventEventArgs mouseEventEventArgs)
+                {
+                    Padding.ColorScheme = Colors.ColorSchemes ["Error"];
+                }
+
+                void PaddingOnMouseLeave (object o, MouseEventEventArgs mouseEventEventArgs)
+                {
+                    Padding.ColorScheme = Colors.ColorSchemes ["Dialog"];
+                }
+
+                Border.Thickness = new Thickness (1);
+                Border.LineStyle = LineStyle.Rounded;
+            }
 
             MouseLeave += (s, e) =>
                           {
-                              ColorScheme = Colors.ColorSchemes ["Menu"];
-                              _button1PressedOnEnter = false;
+                              Text = "Leave";
                           };
             MouseEnter += (s, e) =>
                           {
-                              ColorScheme = Colors.ColorSchemes ["Error"];
-                              _button1PressedOnEnter = e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed);
+                              Text = "Enter";
                           };
         }
     }

+ 3 - 3
UnitTests/Application/ApplicationTests.cs

@@ -317,7 +317,7 @@ public class ApplicationTests
             Assert.False (Application._forceFakeConsole);
             Assert.Equal (-1, Application.MainThreadId);
             Assert.Empty (Application.TopLevels);
-            Assert.Null (Application.MouseEnteredView);
+            Assert.Empty (Application.ViewsUnderMouse);
 
             // Keyboard
             Assert.Empty (Application.GetViewKeyBindings ());
@@ -347,7 +347,7 @@ public class ApplicationTests
         Application.MainThreadId = 1;
 
         //Application._topLevels = new List<Toplevel> ();
-        Application.MouseEnteredView = new ();
+        Application.ViewsUnderMouse.Clear ();
 
         //Application.SupportedCultures = new List<CultureInfo> ();
         Application.Force16Colors = true;
@@ -361,7 +361,7 @@ public class ApplicationTests
 
         //ApplicationOverlapped.OverlappedChildren = new List<View> ();
         //ApplicationOverlapped.OverlappedTop = 
-        Application.MouseEnteredView = new ();
+        Application.ViewsUnderMouse.Clear ();
 
         //Application.WantContinuousButtonPressedView = new View ();
 

+ 12 - 9
UnitTests/View/Adornment/AdornmentSubViewTests.cs

@@ -14,12 +14,12 @@ public class AdornmentSubViewTests (ITestOutputHelper output)
     [InlineData (2, 1, true)]
     public void Adornment_WithSubView_FindDeepestView_Finds (int viewMargin, int subViewMargin, bool expectedFound)
     {
-        var view = new View ()
+        Application.Top = new Toplevel()
         {
             Width = 10,
             Height = 10
         };
-        view.Margin.Thickness = new Thickness (viewMargin);
+        Application.Top.Margin.Thickness = new Thickness (viewMargin);
 
         var subView = new View ()
         {
@@ -29,24 +29,25 @@ public class AdornmentSubViewTests (ITestOutputHelper output)
             Height = 5
         };
         subView.Margin.Thickness = new Thickness (subViewMargin);
-        view.Margin.Add (subView);
+        Application.Top.Margin.Add (subView);
 
-        var foundView = View.FindDeepestView (view, new (0, 0));
+        var foundView = View.FindDeepestView (new (0, 0));
 
         bool found = foundView == subView || foundView == subView.Margin;
         Assert.Equal (expectedFound, found);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     [Fact]
     public void Adornment_WithNonVisibleSubView_FindDeepestView_Finds_Adornment ()
     {
-        var view = new View ()
+        Application.Top = new Toplevel ()
         {
             Width = 10,
             Height = 10
-
         };
-        view.Padding.Thickness = new Thickness (1);
+        Application.Top.Padding.Thickness = new Thickness (1);
 
         var subView = new View ()
         {
@@ -56,9 +57,11 @@ public class AdornmentSubViewTests (ITestOutputHelper output)
             Height = 1,
             Visible = false
         };
-        view.Padding.Add (subView);
+        Application.Top.Padding.Add (subView);
 
-        Assert.Equal (view.Padding, View.FindDeepestView (view, new (0, 0)));
+        Assert.Equal (Application.Top.Padding, View.FindDeepestView (new (0, 0)));
+        Application.Top?.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     [Fact]

+ 81 - 53
UnitTests/View/FindDeepestViewTests.cs

@@ -111,12 +111,14 @@ public class FindDeepestViewTests ()
     [InlineData (2, 2)]
     public void Returns_Start_If_No_SubViews (int testX, int testY)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
 
-        Assert.Same (start, View.FindDeepestView (start, new (testX, testY)));
+        Assert.Same (Application.Top, View.FindDeepestView (new (testX, testY)));
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     // Test that FindDeepestView returns null if the start view has no subviews and coords are outside the view
@@ -126,13 +128,15 @@ public class FindDeepestViewTests ()
     [InlineData (20, 20)]
     public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             X = 1, Y = 2,
             Width = 10, Height = 10,
         };
 
-        Assert.Null (View.FindDeepestView (start, new (testX, testY)));
+        Assert.Null (View.FindDeepestView (new (testX, testY)));
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     [Theory]
@@ -141,14 +145,16 @@ public class FindDeepestViewTests ()
     [InlineData (20, 20)]
     public void Returns_Null_If_Start_Not_Visible (int testX, int testY)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             X = 1, Y = 2,
             Width = 10, Height = 10,
             Visible = false,
         };
 
-        Assert.Null (View.FindDeepestView (start, new (testX, testY)));
+        Assert.Null (View.FindDeepestView (new (testX, testY)));
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     // Test that FindDeepestView returns the correct view if the start view has subviews
@@ -163,7 +169,7 @@ public class FindDeepestViewTests ()
     [InlineData (5, 6, true)]
     public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
@@ -173,11 +179,13 @@ public class FindDeepestViewTests ()
             X = 1, Y = 2,
             Width = 5, Height = 5,
         };
-        start.Add (subview);
+        Application.Top.Add (subview);
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == subview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     [Theory]
@@ -190,7 +198,7 @@ public class FindDeepestViewTests ()
     [InlineData (5, 6, false)]
     public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
@@ -201,11 +209,13 @@ public class FindDeepestViewTests ()
             Width = 5, Height = 5,
             Visible = false
         };
-        start.Add (subview);
+        Application.Top.Add (subview);
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == subview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
 
@@ -219,7 +229,7 @@ public class FindDeepestViewTests ()
     [InlineData (5, 6, false)]
     public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
             Visible = false
@@ -230,13 +240,15 @@ public class FindDeepestViewTests ()
             X = 1, Y = 2,
             Width = 5, Height = 5,
         };
-        start.Add (subview);
+        Application.Top.Add (subview);
         subview.Visible = true;
         Assert.True (subview.Visible);
-        Assert.False (start.Visible);
-        var found = View.FindDeepestView (start, new (testX, testY));
+        Assert.False (Application.Top.Visible);
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == subview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     // Test that FindDeepestView works if the start view has positive Adornments
@@ -253,22 +265,24 @@ public class FindDeepestViewTests ()
     [InlineData (6, 7, true)]
     public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
-        start.Margin.Thickness = new Thickness (1);
+        Application.Top.Margin.Thickness = new Thickness (1);
 
         var subview = new View ()
         {
             X = 1, Y = 2,
             Width = 5, Height = 5,
         };
-        start.Add (subview);
+        Application.Top.Add (subview);
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == subview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     // Test that FindDeepestView works if the start view has offset Viewport location
@@ -283,23 +297,25 @@ public class FindDeepestViewTests ()
     [InlineData (-1, 0, 0, false)]
     public void Returns_Correct_If_Start_Has_Offset_Viewport (int offset, int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
             ViewportSettings = ViewportSettings.AllowNegativeLocation
         };
-        start.Viewport = new (offset, offset, 10, 10);
+        Application.Top.Viewport = new (offset, offset, 10, 10);
 
         var subview = new View ()
         {
             X = 1, Y = 1,
             Width = 2, Height = 2,
         };
-        start.Add (subview);
+        Application.Top.Add (subview);
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == subview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     [Theory]
@@ -314,24 +330,26 @@ public class FindDeepestViewTests ()
     [InlineData (6, 7, false)]
     public void Returns_Correct_If_Start_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
-        start.Padding.Thickness = new Thickness (1);
+        Application.Top.Padding.Thickness = new Thickness (1);
 
         var subview = new View ()
         {
             X = Pos.AnchorEnd(1), Y = Pos.AnchorEnd(1),
             Width = 1, Height = 1,
         };
-        start.Padding.Add (subview);
-        start.BeginInit();
-        start.EndInit();
+        Application.Top.Padding.Add (subview);
+        Application.Top.BeginInit();
+        Application.Top.EndInit();
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == subview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
 
@@ -345,26 +363,28 @@ public class FindDeepestViewTests ()
     [InlineData (2, 2, typeof (Padding))]
     [InlineData (7, 7, typeof (Padding))]
 
-    [InlineData (5, 5, typeof (View))]
+    [InlineData (5, 5, typeof (Toplevel))]
     public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
-        start.Margin.Thickness = new Thickness (1);
-        start.Border.Thickness = new Thickness (1);
-        start.Padding.Thickness = new Thickness (1);
+        Application.Top.Margin.Thickness = new Thickness (1);
+        Application.Top.Border.Thickness = new Thickness (1);
+        Application.Top.Padding.Thickness = new Thickness (1);
 
         var subview = new View ()
         {
             X = 1, Y = 1,
             Width = 1, Height = 1,
         };
-        start.Add (subview);
+        Application.Top.Add (subview);
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
         Assert.Equal (expectedAdornmentType, found!.GetType ());
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     // Test that FindDeepestView works if the subview has positive Adornments
@@ -381,7 +401,7 @@ public class FindDeepestViewTests ()
     [InlineData (2, 3, true)]
     public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
@@ -392,11 +412,13 @@ public class FindDeepestViewTests ()
             Width = 5, Height = 5,
         };
         subview.Margin.Thickness = new Thickness (1);
-        start.Add (subview);
+        Application.Top.Add (subview);
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == subview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     [Theory]
@@ -412,7 +434,7 @@ public class FindDeepestViewTests ()
     [InlineData (5, 5, true)]
     public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
@@ -435,13 +457,15 @@ public class FindDeepestViewTests ()
             Height = 1,
         };
         subview.Padding.Add (paddingSubview);
-        start.Add (subview);
-        start.BeginInit();
-        start.EndInit();
+        Application.Top.Add (subview);
+        Application.Top.BeginInit();
+        Application.Top.EndInit();
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == paddingSubview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     [Theory]
@@ -457,7 +481,7 @@ public class FindDeepestViewTests ()
     [InlineData (5, 5, true)]
     public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10,
         };
@@ -484,13 +508,15 @@ public class FindDeepestViewTests ()
             Height = 1,
         };
         subview.Padding.Add (paddingSubview);
-        start.Add (subview);
-        start.BeginInit ();
-        start.EndInit ();
+        Application.Top.Add (subview);
+        Application.Top.BeginInit ();
+        Application.Top.EndInit ();
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
 
         Assert.Equal (expectedSubViewFound, found == paddingSubview);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     // Test that FindDeepestView works with nested subviews
@@ -506,7 +532,7 @@ public class FindDeepestViewTests ()
     [InlineData (5, 5, 2)]
     public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound)
     {
-        var start = new View ()
+        Application.Top = new ()
         {
             Width = 10, Height = 10
         };
@@ -528,9 +554,11 @@ public class FindDeepestViewTests ()
             }
         }
 
-        start.Add (subviews [0]);
+        Application.Top.Add (subviews [0]);
 
-        var found = View.FindDeepestView (start, new (testX, testY));
+        var found = View.FindDeepestView (new (testX, testY));
         Assert.Equal (expectedSubViewFound, subviews.IndexOf (found!));
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 }

+ 10 - 7
UnitTests/View/Layout/Pos.CombineTests.cs

@@ -66,7 +66,7 @@ public class PosCombineTests (ITestOutputHelper output)
     [SetupFakeDriver]
     public void PosCombine_DimCombine_View_With_SubViews ()
     {
-        Toplevel top = new Toplevel () { Width = 80, Height = 25 };
+        Application.Top = new Toplevel () { Width = 80, Height = 25 };
         var win1 = new Window { Id = "win1", Width = 20, Height = 10 };
         var view1 = new View
         {
@@ -85,18 +85,21 @@ public class PosCombineTests (ITestOutputHelper output)
         view2.Add (view3);
         win2.Add (view2);
         win1.Add (view1, win2);
-        top.Add (win1);
-        top.BeginInit ();
-        top.EndInit ();
+        Application.Top.Add (win1);
+        Application.Top.BeginInit ();
+        Application.Top.EndInit ();
 
-        Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
+        Assert.Equal (new Rectangle (0, 0, 80, 25), Application.Top.Frame);
         Assert.Equal (new Rectangle (0, 0, 5, 1), view1.Frame);
         Assert.Equal (new Rectangle (0, 0, 20, 10), win1.Frame);
         Assert.Equal (new Rectangle (0, 2, 10, 3), win2.Frame);
         Assert.Equal (new Rectangle (0, 0, 8, 1), view2.Frame);
         Assert.Equal (new Rectangle (0, 0, 7, 1), view3.Frame);
-        var foundView = View.FindDeepestView (top, new (9, 4));
+        var foundView = View.FindDeepestView (new (9, 4));
         Assert.Equal (foundView, view2);
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
+
     }
 
     [Fact]
@@ -136,7 +139,7 @@ public class PosCombineTests (ITestOutputHelper output)
 
         Assert.Throws<InvalidOperationException> (() => Application.Run ());
         top.Dispose ();
-        Application.Shutdown ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
 }

+ 72 - 71
UnitTests/View/Layout/ToScreenTests.cs

@@ -1,4 +1,5 @@
 using Xunit.Abstractions;
+using static System.Net.Mime.MediaTypeNames;
 
 namespace Terminal.Gui.LayoutTests;
 
@@ -946,8 +947,8 @@ public class ToScreenTests (ITestOutputHelper output)
     [AutoInitShutdown]
     public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
     {
-        Toplevel top = new ();
-        top.BorderStyle = LineStyle.Single;
+        Application.Top = new ();
+        Application.Top.BorderStyle = LineStyle.Single;
 
         var view = new View
         {
@@ -957,18 +958,17 @@ public class ToScreenTests (ITestOutputHelper output)
             Height = 1,
             Text = "0123456789"
         };
-        top.Add (view);
+        Application.Top.Add (view);
 
-        Application.Begin (top);
+        Application.Begin (Application.Top);
 
-        Assert.Equal (Application.Current, top);
         Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
-        Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
-        Assert.Equal (new (0, 0, 80, 25), top.Frame);
+        Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
+        Assert.Equal (new (0, 0, 80, 25), Application.Top.Frame);
 
         ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
-        Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
-        Assert.Equal (new (0, 0, 20, 10), top.Frame);
+        Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
+        Assert.Equal (new (0, 0, 20, 10), Application.Top.Frame);
 
         _ = TestHelpers.AssertDriverContentsWithFrameAre (
                                                           @"
@@ -987,64 +987,64 @@ public class ToScreenTests (ITestOutputHelper output)
                                                          );
 
         // top
-        Assert.Equal (Point.Empty, top.ScreenToFrame (new (0, 0)));
-        Point screen = top.Margin.ViewportToScreen (new Point (0, 0));
+        Assert.Equal (Point.Empty, Application.Top.ScreenToFrame (new (0, 0)));
+        Point screen = Application.Top.Margin.ViewportToScreen (new Point (0, 0));
         Assert.Equal (0, screen.X);
         Assert.Equal (0, screen.Y);
-        screen = top.Border.ViewportToScreen (new Point (0, 0));
+        screen = Application.Top.Border.ViewportToScreen (new Point (0, 0));
         Assert.Equal (0, screen.X);
         Assert.Equal (0, screen.Y);
-        screen = top.Padding.ViewportToScreen (new Point (0, 0));
+        screen = Application.Top.Padding.ViewportToScreen (new Point (0, 0));
         Assert.Equal (1, screen.X);
         Assert.Equal (1, screen.Y);
-        screen = top.ViewportToScreen (new Point (0, 0));
+        screen = Application.Top.ViewportToScreen (new Point (0, 0));
         Assert.Equal (1, screen.X);
         Assert.Equal (1, screen.Y);
-        screen = top.ViewportToScreen (new Point (-1, -1));
+        screen = Application.Top.ViewportToScreen (new Point (-1, -1));
         Assert.Equal (0, screen.X);
         Assert.Equal (0, screen.Y);
-        var found = View.FindDeepestView (top, new (0, 0));
-        Assert.Equal (top.Border, found);
+        var found = View.FindDeepestView (new (0, 0));
+        Assert.Equal (Application.Top.Border, found);
 
         Assert.Equal (0, found.Frame.X);
         Assert.Equal (0, found.Frame.Y);
-        Assert.Equal (new (3, 2), top.ScreenToFrame (new (3, 2)));
-        screen = top.ViewportToScreen (new Point (3, 2));
+        Assert.Equal (new (3, 2), Application.Top.ScreenToFrame (new (3, 2)));
+        screen = Application.Top.ViewportToScreen (new Point (3, 2));
         Assert.Equal (4, screen.X);
         Assert.Equal (3, screen.Y);
-        found = View.FindDeepestView (top, new (screen.X, screen.Y));
+        found = View.FindDeepestView (new (screen.X, screen.Y));
         Assert.Equal (view, found);
 
         //Assert.Equal (0, found.FrameToScreen ().X);
         //Assert.Equal (0, found.FrameToScreen ().Y);
-        found = View.FindDeepestView (top, new (3, 2));
-        Assert.Equal (top, found);
+        found = View.FindDeepestView (new (3, 2));
+        Assert.Equal (Application.Top, found);
 
         //Assert.Equal (3, found.FrameToScreen ().X);
         //Assert.Equal (2, found.FrameToScreen ().Y);
-        Assert.Equal (new (13, 2), top.ScreenToFrame (new (13, 2)));
-        screen = top.ViewportToScreen (new Point (12, 2));
+        Assert.Equal (new (13, 2), Application.Top.ScreenToFrame (new (13, 2)));
+        screen = Application.Top.ViewportToScreen (new Point (12, 2));
         Assert.Equal (13, screen.X);
         Assert.Equal (3, screen.Y);
-        found = View.FindDeepestView (top, new (screen.X, screen.Y));
+        found = View.FindDeepestView (new (screen.X, screen.Y));
         Assert.Equal (view, found);
 
         //Assert.Equal (9, found.FrameToScreen ().X);
         //Assert.Equal (0, found.FrameToScreen ().Y);
-        screen = top.ViewportToScreen (new Point (13, 2));
+        screen = Application.Top.ViewportToScreen (new Point (13, 2));
         Assert.Equal (14, screen.X);
         Assert.Equal (3, screen.Y);
-        found = View.FindDeepestView (top, new (13, 2));
-        Assert.Equal (top, found);
+        found = View.FindDeepestView (new (13, 2));
+        Assert.Equal (Application.Top, found);
 
         //Assert.Equal (13, found.FrameToScreen ().X);
         //Assert.Equal (2, found.FrameToScreen ().Y);
-        Assert.Equal (new (14, 3), top.ScreenToFrame (new (14, 3)));
-        screen = top.ViewportToScreen (new Point (14, 3));
+        Assert.Equal (new (14, 3), Application.Top.ScreenToFrame (new (14, 3)));
+        screen = Application.Top.ViewportToScreen (new Point (14, 3));
         Assert.Equal (15, screen.X);
         Assert.Equal (4, screen.Y);
-        found = View.FindDeepestView (top, new (14, 3));
-        Assert.Equal (top, found);
+        found = View.FindDeepestView (new (14, 3));
+        Assert.Equal (Application.Top, found);
 
         //Assert.Equal (14, found.FrameToScreen ().X);
         //Assert.Equal (3, found.FrameToScreen ().Y);
@@ -1066,37 +1066,39 @@ public class ToScreenTests (ITestOutputHelper output)
         screen = view.ViewportToScreen (new Point (-4, -3));
         Assert.Equal (0, screen.X);
         Assert.Equal (0, screen.Y);
-        found = View.FindDeepestView (top, new (0, 0));
-        Assert.Equal (top.Border, found);
+        found = View.FindDeepestView (new (0, 0));
+        Assert.Equal (Application.Top.Border, found);
 
         Assert.Equal (new (-1, -1), view.ScreenToFrame (new (3, 2)));
         screen = view.ViewportToScreen (new Point (0, 0));
         Assert.Equal (4, screen.X);
         Assert.Equal (3, screen.Y);
-        found = View.FindDeepestView (top, new (4, 3));
+        found = View.FindDeepestView (new (4, 3));
         Assert.Equal (view, found);
 
         Assert.Equal (new (9, -1), view.ScreenToFrame (new (13, 2)));
         screen = view.ViewportToScreen (new Point (10, 0));
         Assert.Equal (14, screen.X);
         Assert.Equal (3, screen.Y);
-        found = View.FindDeepestView (top, new (14, 3));
-        Assert.Equal (top, found);
+        found = View.FindDeepestView (new (14, 3));
+        Assert.Equal (Application.Top, found);
 
         Assert.Equal (new (10, 0), view.ScreenToFrame (new (14, 3)));
         screen = view.ViewportToScreen (new Point (11, 1));
         Assert.Equal (15, screen.X);
         Assert.Equal (4, screen.Y);
-        found = View.FindDeepestView (top, new (15, 4));
-        Assert.Equal (top, found);
-        top.Dispose ();
+        found = View.FindDeepestView (new (15, 4));
+        Assert.Equal (Application.Top, found);
+
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     [Fact]
     [AutoInitShutdown]
     public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
     {
-        var top = new Toplevel
+        Application.Top = new ()
         {
             X = 3,
             Y = 2,
@@ -1113,19 +1115,18 @@ public class ToScreenTests (ITestOutputHelper output)
             Height = 1,
             Text = "0123456789"
         };
-        top.Add (view);
+        Application.Top.Add (view);
 
-        Application.Begin (top);
+        Application.Begin (Application.Top);
 
-        Assert.Equal (Application.Current, top);
         Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
-        Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
-        Assert.Equal (new (3, 2, 20, 10), top.Frame);
+        Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
+        Assert.Equal (new (3, 2, 20, 10), Application.Top.Frame);
 
         ((FakeDriver)Application.Driver!).SetBufferSize (30, 20);
         Assert.Equal (new (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
-        Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
-        Assert.Equal (new (3, 2, 20, 10), top.Frame);
+        Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
+        Assert.Equal (new (3, 2, 20, 10), Application.Top.Frame);
 
         Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
                                                                         @"
@@ -1148,45 +1149,45 @@ public class ToScreenTests (ITestOutputHelper output)
         Assert.Equal (new (3, 2, 23, 10), frame);
 
         // top
-        Assert.Equal (new (-3, -2), top.ScreenToFrame (new (0, 0)));
-        Point screen = top.Margin.ViewportToScreen (new Point (-3, -2));
+        Assert.Equal (new (-3, -2), Application.Top.ScreenToFrame (new (0, 0)));
+        Point screen = Application.Top.Margin.ViewportToScreen (new Point (-3, -2));
         Assert.Equal (0, screen.X);
         Assert.Equal (0, screen.Y);
-        screen = top.Border.ViewportToScreen (new Point (-3, -2));
+        screen = Application.Top.Border.ViewportToScreen (new Point (-3, -2));
         Assert.Equal (0, screen.X);
         Assert.Equal (0, screen.Y);
-        screen = top.Padding.ViewportToScreen (new Point (-3, -2));
+        screen = Application.Top.Padding.ViewportToScreen (new Point (-3, -2));
         Assert.Equal (1, screen.X);
         Assert.Equal (1, screen.Y);
-        screen = top.ViewportToScreen (new Point (-3, -2));
+        screen = Application.Top.ViewportToScreen (new Point (-3, -2));
         Assert.Equal (1, screen.X);
         Assert.Equal (1, screen.Y);
-        screen = top.ViewportToScreen (new Point (-4, -3));
+        screen = Application.Top.ViewportToScreen (new Point (-4, -3));
         Assert.Equal (0, screen.X);
         Assert.Equal (0, screen.Y);
-        var found = View.FindDeepestView (top, new (-4, -3));
+        var found = View.FindDeepestView (new (-4, -3));
         Assert.Null (found);
-        Assert.Equal (Point.Empty, top.ScreenToFrame (new (3, 2)));
-        screen = top.ViewportToScreen (new Point (0, 0));
+        Assert.Equal (Point.Empty, Application.Top.ScreenToFrame (new (3, 2)));
+        screen = Application.Top.ViewportToScreen (new Point (0, 0));
         Assert.Equal (4, screen.X);
         Assert.Equal (3, screen.Y);
-        Assert.Equal (top.Border, View.FindDeepestView (top, new (3, 2)));
+        Assert.Equal (Application.Top.Border, View.FindDeepestView (new (3, 2)));
 
         //Assert.Equal (0, found.FrameToScreen ().X);
         //Assert.Equal (0, found.FrameToScreen ().Y);
-        Assert.Equal (new (10, 0), top.ScreenToFrame (new (13, 2)));
-        screen = top.ViewportToScreen (new Point (10, 0));
+        Assert.Equal (new (10, 0), Application.Top.ScreenToFrame (new (13, 2)));
+        screen = Application.Top.ViewportToScreen (new Point (10, 0));
         Assert.Equal (14, screen.X);
         Assert.Equal (3, screen.Y);
-        Assert.Equal (top.Border, View.FindDeepestView (top, new (13, 2)));
+        Assert.Equal (Application.Top.Border, View.FindDeepestView (new (13, 2)));
 
         //Assert.Equal (10, found.FrameToScreen ().X);
         //Assert.Equal (0, found.FrameToScreen ().Y);
-        Assert.Equal (new (11, 1), top.ScreenToFrame (new (14, 3)));
-        screen = top.ViewportToScreen (new Point (11, 1));
+        Assert.Equal (new (11, 1), Application.Top.ScreenToFrame (new (14, 3)));
+        screen = Application.Top.ViewportToScreen (new Point (11, 1));
         Assert.Equal (15, screen.X);
         Assert.Equal (4, screen.Y);
-        Assert.Equal (top, View.FindDeepestView (top, new (14, 3)));
+        Assert.Equal (Application.Top, View.FindDeepestView (new (14, 3)));
 
         // view
         Assert.Equal (new (-7, -5), view.ScreenToFrame (new (0, 0)));
@@ -1202,32 +1203,32 @@ public class ToScreenTests (ITestOutputHelper output)
         screen = view.ViewportToScreen (new Point (-6, -4));
         Assert.Equal (1, screen.X);
         Assert.Equal (1, screen.Y);
-        Assert.Null (View.FindDeepestView (top, new (1, 1)));
+        Assert.Null (View.FindDeepestView (new (1, 1)));
         Assert.Equal (new (-4, -3), view.ScreenToFrame (new (3, 2)));
         screen = view.ViewportToScreen (new Point (-3, -2));
         Assert.Equal (4, screen.X);
         Assert.Equal (3, screen.Y);
-        Assert.Equal (top, View.FindDeepestView (top, new (4, 3)));
+        Assert.Equal (Application.Top, View.FindDeepestView (new (4, 3)));
         Assert.Equal (new (-1, -1), view.ScreenToFrame (new (6, 4)));
         screen = view.ViewportToScreen (new Point (0, 0));
         Assert.Equal (7, screen.X);
         Assert.Equal (5, screen.Y);
-        Assert.Equal (view, View.FindDeepestView (top, new (7, 5)));
+        Assert.Equal (view, View.FindDeepestView (new (7, 5)));
         Assert.Equal (new (6, -1), view.ScreenToFrame (new (13, 4)));
         screen = view.ViewportToScreen (new Point (7, 0));
         Assert.Equal (14, screen.X);
         Assert.Equal (5, screen.Y);
-        Assert.Equal (view, View.FindDeepestView (top, new (14, 5)));
+        Assert.Equal (view, View.FindDeepestView (new (14, 5)));
         Assert.Equal (new (7, -2), view.ScreenToFrame (new (14, 3)));
         screen = view.ViewportToScreen (new Point (8, -1));
         Assert.Equal (15, screen.X);
         Assert.Equal (4, screen.Y);
-        Assert.Equal (top, View.FindDeepestView (top, new (15, 4)));
+        Assert.Equal (Application.Top, View.FindDeepestView (new (15, 4)));
         Assert.Equal (new (16, -2), view.ScreenToFrame (new (23, 3)));
         screen = view.ViewportToScreen (new Point (17, -1));
         Assert.Equal (24, screen.X);
         Assert.Equal (4, screen.Y);
-        Assert.Null (View.FindDeepestView (top, new (24, 4)));
-        top.Dispose ();
+        Assert.Null (View.FindDeepestView (new (24, 4)));
+        Application.Top.Dispose ();
     }
 }

+ 4 - 4
UnitTests/Views/ContextMenuTests.cs

@@ -1394,7 +1394,7 @@ public class ContextMenuTests (ITestOutputHelper output)
         Assert.True (tf1.HasFocus);
         Assert.False (tf2.HasFocus);
         Assert.Equal (4, win.Subviews.Count); // TF & TV add autocomplete popup's to their superviews.
-        Assert.Null (Application.MouseEnteredView);
+        Assert.Empty (Application.ViewsUnderMouse);
 
         // Right click on tf2 to open context menu
         Application.OnMouseEvent (new () { Position = new (1, 3), Flags = MouseFlags.Button3Clicked });
@@ -1404,7 +1404,7 @@ public class ContextMenuTests (ITestOutputHelper output)
         Assert.True (tf2.ContextMenu.MenuBar.IsMenuOpen);
         Assert.True (win.Focused is Menu);
         Assert.True (Application.MouseGrabView is MenuBar);
-        Assert.Equal (tf2, Application.MouseEnteredView);
+        Assert.Equal (tf2, Application.ViewsUnderMouse.LastOrDefault ());
 
         // Click on tf1 to focus it, which cause context menu being closed
         Application.OnMouseEvent (new () { Position = new (1, 1), Flags = MouseFlags.Button1Clicked });
@@ -1416,7 +1416,7 @@ public class ContextMenuTests (ITestOutputHelper output)
         Assert.NotNull (tf2.ContextMenu.MenuBar);
         Assert.Equal (win.Focused, tf1);
         Assert.Null (Application.MouseGrabView);
-        Assert.Equal (tf1, Application.MouseEnteredView);
+        Assert.Equal (tf1, Application.ViewsUnderMouse.LastOrDefault ());
 
         // Click on tf2 to focus it
         Application.OnMouseEvent (new () { Position = new (1, 3), Flags = MouseFlags.Button1Clicked });
@@ -1428,7 +1428,7 @@ public class ContextMenuTests (ITestOutputHelper output)
         Assert.NotNull (tf2.ContextMenu.MenuBar);
         Assert.Equal (win.Focused, tf2);
         Assert.Null (Application.MouseGrabView);
-        Assert.Equal (tf2, Application.MouseEnteredView);
+        Assert.Equal (tf2, Application.ViewsUnderMouse.LastOrDefault ());
 
         Application.End (rs);
         win.Dispose ();