Skip to content

Commit

Permalink
Revert "Reapply "Reapply "Rewrote FindDeepestView to be clearer and m…
Browse files Browse the repository at this point in the history
…ore performant"""

This reverts commit d8043cd.
  • Loading branch information
tig committed Apr 10, 2024
1 parent d8043cd commit 4feff89
Showing 1 changed file with 34 additions and 44 deletions.
78 changes: 34 additions & 44 deletions Terminal.Gui/View/Layout/ViewLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,63 +605,53 @@ public LayoutStyle LayoutStyle
// 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, int x, int y)
{
while (start is { Visible: true } && start.Contains (x, y))
if (start is null || !start.Visible || !start.Contains (x, y))
{
Adornment? found = null;
return null;
}

if (start.Margin.Contains (x, y))
{
found = start.Margin;
}
else if (start.Border.Contains (x, y))
{
found = start.Border;
}
else if (start.Padding.Contains (x, y))
{
found = start.Padding;
}
Adornment? found = null;

Point viewportOffset = start.GetViewportOffsetFromFrame ();
if (start.Margin.Contains (x, y))
{
found = start.Margin;
}
else if (start.Border.Contains (x, y))
{
found = start.Border;
}
else if (start.Padding.Contains (x, y))
{
found = start.Padding;
}

if (found is { })
{
start = found;
viewportOffset = found.Parent.Frame.Location;
}
Point viewportOffset = start.GetViewportOffsetFromFrame ();

if (start.InternalSubviews is { Count: > 0 })
{
int startOffsetX = x - (start.Frame.X + viewportOffset.X);
int startOffsetY = y - (start.Frame.Y + viewportOffset.Y);
if (found is { })
{
start = found;
viewportOffset = found.Parent.Frame.Location;
}

for (int i = start.InternalSubviews.Count - 1; i >= 0; i--)
{
View nextStart = start.InternalSubviews [i];
if (start.InternalSubviews is { Count: > 0 })
{
int startOffsetX = x - (start.Frame.X + viewportOffset.X);
int startOffsetY = y - (start.Frame.Y + viewportOffset.Y);

if (nextStart.Visible && nextStart.Contains (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y))
{
start = nextStart;
x = startOffsetX + start.Viewport.X;
y = startOffsetY + start.Viewport.Y;
break;
}
for (int i = start.InternalSubviews.Count - 1; i >= 0; i--)
{
View nextStart = start.InternalSubviews [i];

if (i == 0)
{
return start; // If no visible subview is found, return the current start view
}
if (nextStart.Visible && nextStart.Contains (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y))
{
// TODO: Remove recursion
return FindDeepestView (nextStart, startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y) ?? nextStart;
}
}
else
{
return start;
}
}

return null;
return start;
}

#nullable restore

/// <summary>
Expand Down

0 comments on commit 4feff89

Please sign in to comment.