Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3460. xxxtoxxx consistency #3461

Merged
merged 13 commits into from
May 11, 2024
12 changes: 6 additions & 6 deletions Terminal.Gui/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
return;
}

var view = View.FindDeepestView (Current, mouseEvent.X, mouseEvent.Y);
var view = View.FindDeepestView (Current, new (mouseEvent.X, mouseEvent.Y));
tig marked this conversation as resolved.
Show resolved Hide resolved

if (view is { })
{
Expand All @@ -1574,7 +1574,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
{
// If the mouse is grabbed, send the event to the view that grabbed it.
// The coordinates are relative to the Bounds of the view that grabbed the mouse.
Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.X, mouseEvent.Y);
Point frameLoc = MouseGrabView.ScreenToViewport (new (mouseEvent.X, mouseEvent.Y));

var viewRelativeMouseEvent = new MouseEvent
{
Expand Down Expand Up @@ -1619,7 +1619,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
// This occurs when there are multiple overlapped "tops"
// E.g. "Mdi" - in the Background Worker Scenario
View? top = FindDeepestTop (Top, mouseEvent.X, mouseEvent.Y);
view = View.FindDeepestView (top, mouseEvent.X, mouseEvent.Y);
view = View.FindDeepestView (top, new (mouseEvent.X, mouseEvent.Y));

if (view is { } && view != OverlappedTop && top != Current)
{
Expand All @@ -1637,7 +1637,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)

if (view is Adornment adornment)
{
Point frameLoc = adornment.ScreenToFrame (mouseEvent.X, mouseEvent.Y);
Point frameLoc = adornment.ScreenToFrame (new (mouseEvent.X, mouseEvent.Y));

me = new ()
{
Expand All @@ -1650,7 +1650,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
}
else if (view.ViewportToScreen (Rectangle.Empty with { Size = view.Viewport.Size }).Contains (mouseEvent.X, mouseEvent.Y))
{
Point viewportLocation = view.ScreenToViewport (mouseEvent.X, mouseEvent.Y);
Point viewportLocation = view.ScreenToViewport (new (mouseEvent.X, mouseEvent.Y));

me = new ()
{
Expand Down Expand Up @@ -1709,7 +1709,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
break;
}

Point boundsPoint = view.ScreenToViewport (mouseEvent.X, mouseEvent.Y);
Point boundsPoint = view.ScreenToViewport (new (mouseEvent.X, mouseEvent.Y));

me = new ()
{
Expand Down
7 changes: 3 additions & 4 deletions Terminal.Gui/Drawing/Thickness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,13 @@ public int Vertical
/// the rectangle described by <see cref="GetInside(Rectangle)"/>.
/// </summary>
/// <param name="outside">Describes the location and size of the rectangle that contains the thickness.</param>
/// <param name="x">The x coord to check.</param>
/// <param name="y">The y coord to check.</param>
/// <param name="location">The coordinate to check.</param>
/// <returns><see langword="true"/> if the specified coordinate is within the thickness; <see langword="false"/> otherwise.</returns>
public bool Contains (Rectangle outside, int x, int y)
public bool Contains (in Rectangle outside, in Point location)
{
Rectangle inside = GetInside (outside);

return outside.Contains (x, y) && !inside.Contains (x, y);
return outside.Contains (location) && !inside.Contains (location);
}

/// <summary>
Expand Down
14 changes: 8 additions & 6 deletions Terminal.Gui/View/Adornment/Adornment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ public override Rectangle FrameToScreen ()
}

/// <inheritdoc/>
public override Point ScreenToFrame (int x, int y) { return Parent.ScreenToFrame (x - Frame.X, y - Frame.Y); }
public override Point ScreenToFrame (in Point location)
{
return Parent.ScreenToFrame (new (location.X - Frame.X, location.Y - Frame.Y));
}

/// <summary>Does nothing for Adornment</summary>
/// <returns></returns>
Expand Down Expand Up @@ -206,12 +209,11 @@ public override bool SuperViewRendersLineCanvas
/// Indicates whether the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness.
/// </summary>
/// <remarks>
/// The <paramref name="x"/> and <paramref name="x"/> are relative to the PARENT's SuperView.
/// The <paramref name="location"/> is relative to the PARENT's SuperView.
/// </remarks>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="location"></param>
/// <returns><see langword="true"/> if the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness. </returns>
public override bool Contains (int x, int y)
public override bool Contains (in Point location)
{
if (Parent is null)
{
Expand All @@ -221,7 +223,7 @@ public override bool Contains (int x, int y)
Rectangle frame = Frame;
frame.Offset (Parent.Frame.Location);

return Thickness.Contains (frame, x, y);
return Thickness.Contains (frame, location);
}

/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/View/Adornment/Border.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ protected internal override bool OnMouseEvent (MouseEvent mouseEvent)

// Only start grabbing if the user clicks in the Thickness area
// Adornment.Contains takes Parent SuperView=relative coords.
if (Contains (mouseEvent.X + Parent.Frame.X + Frame.X, mouseEvent.Y + Parent.Frame.Y + Frame.Y))
if (Contains (new (mouseEvent.X + Parent.Frame.X + Frame.X, mouseEvent.Y + Parent.Frame.Y + Frame.Y)))
tig marked this conversation as resolved.
Show resolved Hide resolved
{
// Set the start grab point to the Frame coords
_startGrabPoint = new (mouseEvent.X + Frame.X, mouseEvent.Y + Frame.Y);
Expand Down Expand Up @@ -301,7 +301,7 @@ protected internal override bool OnMouseEvent (MouseEvent mouseEvent)

_dragPosition = new Point (mouseEvent.X, mouseEvent.Y);

Point parentLoc = Parent.SuperView?.ScreenToViewport (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y) ?? mouseEvent.ScreenPosition;
Point parentLoc = Parent.SuperView?.ScreenToViewport (new (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y)) ?? mouseEvent.ScreenPosition;

GetLocationEnsuringFullVisibility (
Parent,
Expand Down
65 changes: 30 additions & 35 deletions Terminal.Gui/View/Layout/ViewLayout.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Microsoft.CodeAnalysis;

namespace Terminal.Gui;

Expand Down Expand Up @@ -88,15 +89,13 @@ public Rectangle Frame
}
}

private void SetFrame (Rectangle frame)
private void SetFrame (in Rectangle frame)
{
var oldViewport = Rectangle.Empty;
Size? oldContentSize = null;

if (IsInitialized)
{
oldViewport = Viewport;
oldContentSize = ContentSize;
}

// This is the only place where _frame should be set directly. Use Frame = or SetFrame instead.
Expand Down Expand Up @@ -147,25 +146,22 @@ public virtual Rectangle FrameToScreen ()
/// View's <see cref="SuperView"/>'s <see cref="Viewport"/>.
/// </summary>
/// <returns>The coordinate relative to the <see cref="SuperView"/>'s <see cref="Viewport"/>.</returns>
/// <param name="x">Screen-relative column.</param>
/// <param name="y">Screen-relative row.</param>
public virtual Point ScreenToFrame (int x, int y)
/// <param name="location">Screen-relative coordinate.</param>
public virtual Point ScreenToFrame (in Point location)
{
if (SuperView is null)
{
return new (x - Frame.X, y - Frame.Y);
return new (location.X - Frame.X, location.Y - Frame.Y);
tig marked this conversation as resolved.
Show resolved Hide resolved
}

Point superViewViewportOffset = SuperView.GetViewportOffsetFromFrame ();
superViewViewportOffset.X -= SuperView.Viewport.X;
superViewViewportOffset.Y -= SuperView.Viewport.Y;
superViewViewportOffset.Offset(-SuperView.Viewport.X, -SuperView.Viewport.Y);

x -= superViewViewportOffset.X;
y -= superViewViewportOffset.Y;
Point frame = location;
frame.Offset(-superViewViewportOffset.X, -superViewViewportOffset.Y);

Point frame = SuperView.ScreenToFrame (x, y);
frame.X -= Frame.X;
frame.Y -= Frame.Y;
frame = SuperView.ScreenToFrame (frame);
frame.Offset (-Frame.X, -Frame.Y);

return frame;
}
Expand All @@ -180,7 +176,7 @@ public virtual Point ScreenToFrame (int x, int y)
/// </para>
/// <para>
/// If set to a relative value (e.g. <see cref="Pos.Center"/>) the value is indeterminate until the view has been
/// initialized ( <see cref="IsInitialized"/> is true) and <see cref="SetRelativeLayout(Size)"/> has been
/// initialized ( <see cref="IsInitialized"/> is true) and <see cref="SetRelativeLayout"/> has been
/// called.
/// </para>
/// <para>
Expand Down Expand Up @@ -219,7 +215,7 @@ public Pos X
/// </para>
/// <para>
/// If set to a relative value (e.g. <see cref="Pos.Center"/>) the value is indeterminate until the view has been
/// initialized ( <see cref="IsInitialized"/> is true) and <see cref="SetRelativeLayout(Size)"/> has been
/// initialized ( <see cref="IsInitialized"/> is true) and <see cref="SetRelativeLayout"/> has been
/// called.
/// </para>
/// <para>
Expand Down Expand Up @@ -258,7 +254,7 @@ public Pos Y
/// </para>
/// <para>
/// If set to a relative value (e.g. <see cref="Dim.Fill(int)"/>) the value is indeterminate until the view has
/// been initialized ( <see cref="IsInitialized"/> is true) and <see cref="SetRelativeLayout(Size)"/> has been
/// been initialized ( <see cref="IsInitialized"/> is true) and <see cref="SetRelativeLayout"/> has been
/// called.
/// </para>
/// <para>
Expand Down Expand Up @@ -304,7 +300,7 @@ public Dim Height
/// </para>
/// <para>
/// If set to a relative value (e.g. <see cref="Dim.Fill(int)"/>) the value is indeterminate until the view has
/// been initialized ( <see cref="IsInitialized"/> is true) and <see cref="SetRelativeLayout(Size)"/> has been
/// been initialized ( <see cref="IsInitialized"/> is true) and <see cref="SetRelativeLayout"/> has been
/// called.
/// </para>
/// <para>
Expand Down Expand Up @@ -397,10 +393,9 @@ public LayoutStyle LayoutStyle
/// <summary>
/// Indicates whether the specified SuperView-relative coordinates are within the View's <see cref="Frame"/>.
/// </summary>
/// <param name="x">SuperView-relative X coordinate.</param>
/// <param name="y">SuperView-relative Y coordinate.</param>
/// <param name="location">SuperView-relative coordinate</param>
/// <returns><see langword="true"/> if the specified SuperView-relative coordinates are within the View.</returns>
public virtual bool Contains (int x, int y) { return Frame.Contains (x, y); }
public virtual bool Contains (in Point location) { return Frame.Contains (location); }

#nullable enable
/// <summary>Finds the first Subview of <paramref name="start"/> that is visible at the provided location.</summary>
Expand All @@ -410,29 +405,29 @@ public LayoutStyle LayoutStyle
/// </para>
/// </remarks>
/// <param name="start">The view to scope the search by.</param>
/// <param name="x"><paramref name="start"/>.SuperView-relative X coordinate.</param>
/// <param name="y"><paramref name="start"/>.SuperView-relative Y coordinate.</param>
/// <param name="location"><paramref name="start"/>.SuperView-relative coordinate.</param>
/// <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="location"/> coordinate.
/// <see langword="null"/> if no view was found.
/// </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, int x, int y)
internal static View? FindDeepestView (View? start, in Point location)
{
while (start is { Visible: true } && start.Contains (x, y))
Point currentLocation = location;
while (start is { Visible: true } && start.Contains (currentLocation))
{
Adornment? found = null;

if (start.Margin.Contains (x, y))
if (start.Margin.Contains (currentLocation))
{
found = start.Margin;
}
else if (start.Border.Contains (x, y))
else if (start.Border.Contains (currentLocation))
{
found = start.Border;
}
else if (start.Padding.Contains (x, y))
else if (start.Padding.Contains (currentLocation))
{
found = start.Padding;
}
Expand All @@ -445,19 +440,19 @@ public LayoutStyle LayoutStyle
viewportOffset = found.Parent.Frame.Location;
}

int startOffsetX = x - (start.Frame.X + viewportOffset.X);
int startOffsetY = y - (start.Frame.Y + viewportOffset.Y);
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 (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y))
&& start.InternalSubviews [i].Contains (new (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y)))
{
subview = start.InternalSubviews [i];
x = startOffsetX + start.Viewport.X;
y = startOffsetY + start.Viewport.Y;
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;
Expand Down Expand Up @@ -736,7 +731,7 @@ private void LayoutSubview (View v, Size contentSize)
/// <remarks>
/// <para>
/// Determines the relative bounds of the <see cref="View"/> and its <see cref="Frame"/>s, and then calls
/// <see cref="SetRelativeLayout(Size)"/> to update the view.
/// <see cref="SetRelativeLayout"/> to update the view.
/// </para>
/// </remarks>
internal void OnResizeNeeded ()
Expand Down
13 changes: 6 additions & 7 deletions Terminal.Gui/View/ViewContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public Point ContentToScreen (in Point location)
public Point ScreenToContent (in Point location)
{
Point viewportOffset = GetViewportOffsetFromFrame ();
Point screen = ScreenToFrame (location.X, location.Y);
Point screen = ScreenToFrame (location);
screen.Offset (Viewport.X - viewportOffset.X, Viewport.Y - viewportOffset.Y);

return screen;
Expand Down Expand Up @@ -446,15 +446,14 @@ public Rectangle ViewportToScreen (in Rectangle location)
/// <remarks>
/// Viewport-relative means relative to the top-left corner of the inner rectangle of the <see cref="Padding"/>.
/// </remarks>
/// <param name="x">Column relative to the left side of the Viewport.</param>
/// <param name="y">Row relative to the top of the Viewport</param>
public Point ScreenToViewport (int x, int y)
/// <param name="location">Screen-Relative Coordinate.</param>
public Point ScreenToViewport (in Point location)
{
Point viewportOffset = GetViewportOffsetFromFrame ();
Point screen = ScreenToFrame (x, y);
screen.Offset (-viewportOffset.X, -viewportOffset.Y);
Point frame = ScreenToFrame (location);
frame.Offset (-viewportOffset.X, -viewportOffset.Y);

return screen;
return frame;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ private void Application_RootMouseEvent (object sender, MouseEvent a)

View view = a.View ?? this;

Point boundsPoint = view.ScreenToViewport (a.X, a.Y);
Point boundsPoint = view.ScreenToViewport (new (a.X, a.Y));
var me = new MouseEvent
{
X = boundsPoint.X,
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Menu/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ internal bool HandleGrabView (MouseEvent me, View current)

if (me.Y > -1)
{
Point frameLoc = v.ScreenToFrame (me.X, me.Y);
Point frameLoc = v.ScreenToFrame (new (me.X, me.Y));

nme = new ()
{
Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/TileView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public override void OnDrawContent (Rectangle viewport)
bool isRoot = _splitterLines.Contains (line);

Rectangle screen = line.ViewportToScreen (Rectangle.Empty);
Point origin = ScreenToFrame (screen.X, screen.Y);
Point origin = ScreenToFrame (screen.Location);
int length = line.Orientation == Orientation.Horizontal ? line.Frame.Width : line.Frame.Height;

if (!isRoot)
Expand Down Expand Up @@ -841,7 +841,7 @@ public TileTitleToRender (TileView parent, Tile tile, int depth)
public Point GetLocalCoordinateForTitle (TileView intoCoordinateSpace)
{
Rectangle screen = Tile.ContentView.ViewportToScreen (Rectangle.Empty);
return intoCoordinateSpace.ScreenToFrame (screen.X, screen.Y - 1);
return intoCoordinateSpace.ScreenToFrame (new (screen.X, screen.Y - 1));
}

internal string GetTrimmedTitle ()
Expand Down
6 changes: 3 additions & 3 deletions UnitTests/Drawing/ThicknessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ public void TestContains_Uniform1 (int x, int y, int width, int height, int poin
{
var rect = new Rectangle (x, y, width, height);
var thickness = new Thickness (1, 1, 1, 1); // Uniform thickness for simplicity
bool result = thickness.Contains (rect, pointX, pointY);
bool result = thickness.Contains (rect, new (pointX, pointY));
Assert.Equal (expected, result);
}

Expand Down Expand Up @@ -702,7 +702,7 @@ public void TestContains_Uniform2 (int x, int y, int width, int height, int poin
{
var rect = new Rectangle (x, y, width, height);
var thickness = new Thickness (2, 2, 2, 2); // Uniform thickness for simplicity
bool result = thickness.Contains (rect, pointX, pointY);
bool result = thickness.Contains (rect, new (pointX, pointY));
Assert.Equal (expected, result);
}

Expand Down Expand Up @@ -737,7 +737,7 @@ bool expected
{
var rect = new Rectangle (x, y, width, height);
var thickness = new Thickness (0, 0, 0, 0); // Uniform thickness for simplicity
bool result = thickness.Contains (rect, pointX, pointY);
bool result = thickness.Contains (rect, new (pointX, pointY));
Assert.Equal (expected, result);
}

Expand Down