Skip to content

Commit

Permalink
Merge branch 'v2_3269_Bounds-ContentArea' of tig:tig/Terminal.Gui int…
Browse files Browse the repository at this point in the history
…o v2_3269_Bounds-ContentArea
  • Loading branch information
tig committed Mar 17, 2024
2 parents f9332fd + 0cde0e1 commit 1549d0d
Show file tree
Hide file tree
Showing 98 changed files with 1,299 additions and 1,175 deletions.
20 changes: 10 additions & 10 deletions Terminal.Gui/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ internal static void InternalInit (
Current = Top;

// Ensure Top's layout is up to date.
Current.SetRelativeLayout (Driver.Bounds);
Current.SetRelativeLayout (Driver.Viewport);

SupportedCultures = GetSupportedCultures ();
_mainThreadId = Thread.CurrentThread.ManagedThreadId;
Expand Down Expand Up @@ -487,7 +487,7 @@ public static RunState Begin (Toplevel Toplevel)
}

//if (Toplevel.LayoutStyle == LayoutStyle.Computed) {
Toplevel.SetRelativeLayout (Driver.Bounds);
Toplevel.SetRelativeLayout (Driver.Viewport);

//}

Expand Down Expand Up @@ -1365,7 +1365,7 @@ private static void OnUnGrabbedMouse (View view)
/// <remarks>
/// <para>
/// Use this event to receive mouse events in screen coordinates. Use <see cref="MouseEvent"/> to
/// receive mouse events relative to a <see cref="View"/>'s bounds.
/// receive mouse events relative to a <see cref="View.Viewport"/>.
/// </para>
/// <para>The <see cref="MouseEvent.View"/> will contain the <see cref="View"/> that contains the mouse coordinates.</para>
/// </remarks>
Expand Down Expand Up @@ -1408,7 +1408,7 @@ internal static void OnMouseEvent (MouseEventEventArgs a)
if (MouseGrabView is { })
{
// 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.
// The coordinates are relative to the Viewport of the view that grabbed the mouse.
Point frameLoc = MouseGrabView.ScreenToFrame (a.MouseEvent.X, a.MouseEvent.Y);

var viewRelativeMouseEvent = new MouseEvent
Expand All @@ -1420,9 +1420,9 @@ internal static void OnMouseEvent (MouseEventEventArgs a)
View = view
};

if (MouseGrabView.Bounds.Contains (viewRelativeMouseEvent.X, viewRelativeMouseEvent.Y) is false)
if (MouseGrabView.Viewport.Contains (viewRelativeMouseEvent.X, viewRelativeMouseEvent.Y) is false)
{
// The mouse has moved outside the bounds of the view that
// The mouse has moved outside the Viewport of the view that
// grabbed the mouse, so we tell the view that last got
// OnMouseEnter the mouse is leaving
// BUGBUG: That sentence makes no sense. Either I'm missing something or this logic is flawed.
Expand Down Expand Up @@ -1476,14 +1476,14 @@ internal static void OnMouseEvent (MouseEventEventArgs a)
View = view
};
}
else if (view.BoundsToScreen (view.Bounds).Contains (a.MouseEvent.X, a.MouseEvent.Y))
else if (view.ViewportToScreen (view.Viewport).Contains (a.MouseEvent.X, a.MouseEvent.Y))
{
Point boundsPoint = view.ScreenToBounds (a.MouseEvent.X, a.MouseEvent.Y);
Point viewportLocation = view.ScreenToViewport (a.MouseEvent.X, a.MouseEvent.Y);

me = new MouseEvent
{
X = boundsPoint.X,
Y = boundsPoint.Y,
X = viewportLocation.X,
Y = viewportLocation.Y,
Flags = a.MouseEvent.Flags,
ScreenPosition = new (a.MouseEvent.X, a.MouseEvent.Y),
View = view
Expand Down
6 changes: 3 additions & 3 deletions Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public abstract class ConsoleDriver
internal bool [] _dirtyLines;

/// <summary>Gets the dimensions of the terminal.</summary>
public Rectangle Bounds => new (0, 0, Cols, Rows);
public Rectangle Viewport => new (0, 0, Cols, Rows);

/// <summary>
/// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
/// to.
/// </summary>
/// <value>The rectangle describing the bounds of <see cref="Clip"/>.</value>
/// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
public Rectangle Clip { get; set; }

/// <summary>Get the operating system clipboard.</summary>
Expand Down Expand Up @@ -372,7 +372,7 @@ public void FillRect (Rectangle rect, Rune rune = default)
/// <param name="col">The column.</param>
/// <param name="row">The row.</param>
/// <returns>
/// <see langword="false"/> if the coordinate is outside of the screen bounds or outside of <see cref="Clip"/>.
/// <see langword="false"/> if the coordinate is outside the screen bounds or outside of <see cref="Clip"/>.
/// <see langword="true"/> otherwise.
/// </returns>
public bool IsValidLocation (int col, int row) { return col >= 0 && row >= 0 && col < Cols && row < Rows && Clip.Contains (col, row); }
Expand Down
48 changes: 24 additions & 24 deletions Terminal.Gui/Drawing/LineCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class LineCanvas : IDisposable
// TODO: Add other resolvers
};

private Rectangle _cachedBounds;
private Rectangle _cachedViewport;

/// <summary>Creates a new instance.</summary>
public LineCanvas ()
Expand All @@ -67,37 +67,37 @@ public LineCanvas ()
/// Gets the rectangle that describes the bounds of the canvas. Location is the coordinates of the line that is
/// furthest left/top and Size is defined by the line that extends the furthest right/bottom.
/// </summary>
public Rectangle Bounds
public Rectangle Viewport
{
get
{
if (_cachedBounds.IsEmpty)
if (_cachedViewport.IsEmpty)
{
if (_lines.Count == 0)
{
return _cachedBounds;
return _cachedViewport;
}

Rectangle bounds = _lines [0].Bounds;
Rectangle viewport = _lines [0].Viewport;

for (var i = 1; i < _lines.Count; i++)
{
bounds = Rectangle.Union (bounds, _lines [i].Bounds);
viewport = Rectangle.Union (viewport, _lines [i].Viewport);
}

if (bounds is {Width: 0} or {Height: 0})
if (viewport is {Width: 0} or {Height: 0})
{
bounds = bounds with
viewport = viewport with
{
Width = Math.Clamp (bounds.Width, 1, short.MaxValue),
Height = Math.Clamp (bounds.Height, 1, short.MaxValue)
Width = Math.Clamp (viewport.Width, 1, short.MaxValue),
Height = Math.Clamp (viewport.Height, 1, short.MaxValue)
};
}

_cachedBounds = bounds;
_cachedViewport = viewport;
}

return _cachedBounds;
return _cachedViewport;
}
}

Expand Down Expand Up @@ -134,30 +134,30 @@ public void AddLine (
Attribute? attribute = default
)
{
_cachedBounds = Rectangle.Empty;
_cachedViewport = Rectangle.Empty;
_lines.Add (new StraightLine (start, length, orientation, style, attribute));
}

/// <summary>Adds a new line to the canvas</summary>
/// <param name="line"></param>
public void AddLine (StraightLine line)
{
_cachedBounds = Rectangle.Empty;
_cachedViewport = Rectangle.Empty;
_lines.Add (line);
}

/// <summary>Clears all lines from the LineCanvas.</summary>
public void Clear ()
{
_cachedBounds = Rectangle.Empty;
_cachedViewport = Rectangle.Empty;
_lines.Clear ();
}

/// <summary>
/// Clears any cached states from the canvas Call this method if you make changes to lines that have already been
/// added.
/// </summary>
public void ClearCache () { _cachedBounds = Rectangle.Empty; }
public void ClearCache () { _cachedViewport = Rectangle.Empty; }

/// <summary>
/// Evaluates the lines that have been added to the canvas and returns a map containing the glyphs and their
Expand All @@ -170,9 +170,9 @@ public Dictionary<Point, Cell> GetCellMap ()
Dictionary<Point, Cell> map = new ();

// walk through each pixel of the bitmap
for (int y = Bounds.Y; y < Bounds.Y + Bounds.Height; y++)
for (int y = Viewport.Y; y < Viewport.Y + Viewport.Height; y++)
{
for (int x = Bounds.X; x < Bounds.X + Bounds.Width; x++)
for (int x = Viewport.X; x < Viewport.X + Viewport.Width; x++)
{
IntersectionDefinition? [] intersects = _lines
.Select (l => l.Intersects (x, y))
Expand Down Expand Up @@ -232,7 +232,7 @@ public Dictionary<Point, Rune> GetMap (Rectangle inArea)
/// intersection symbols.
/// </summary>
/// <returns>A map of all the points within the canvas.</returns>
public Dictionary<Point, Rune> GetMap () { return GetMap (Bounds); }
public Dictionary<Point, Rune> GetMap () { return GetMap (Viewport); }

/// <summary>Merges one line canvas into this one.</summary>
/// <param name="lineCanvas"></param>
Expand Down Expand Up @@ -260,13 +260,13 @@ public StraightLine RemoveLastLine ()

/// <summary>
/// Returns the contents of the line canvas rendered to a string. The string will include all columns and rows,
/// even if <see cref="Bounds"/> has negative coordinates. For example, if the canvas contains a single line that
/// even if <see cref="Viewport"/> has negative coordinates. For example, if the canvas contains a single line that
/// starts at (-1,-1) with a length of 2, the rendered string will have a length of 2.
/// </summary>
/// <returns>The canvas rendered to a string.</returns>
public override string ToString ()
{
if (Bounds.IsEmpty)
if (Viewport.IsEmpty)
{
return string.Empty;
}
Expand All @@ -275,13 +275,13 @@ public override string ToString ()
Dictionary<Point, Rune> runeMap = GetMap ();

// Create the rune canvas
Rune [,] canvas = new Rune [Bounds.Height, Bounds.Width];
Rune [,] canvas = new Rune [Viewport.Height, Viewport.Width];

// Copy the rune map to the canvas, adjusting for any negative coordinates
foreach (KeyValuePair<Point, Rune> kvp in runeMap)
{
int x = kvp.Key.X - Bounds.X;
int y = kvp.Key.Y - Bounds.Y;
int x = kvp.Key.X - Viewport.X;
int y = kvp.Key.Y - Viewport.Y;
canvas [y, x] = kvp.Value;
}

Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Drawing/StraightLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public StraightLine (
/// Gets the rectangle that describes the bounds of the canvas. Location is the coordinates of the line that is
/// furthest left/top and Size is defined by the line that extends the furthest right/bottom.
/// </summary>
// PERF: Probably better to store the rectangle rather than make a new one on every single access to Bounds.
internal Rectangle Bounds
// PERF: Probably better to store the rectangle rather than make a new one on every single access to Viewport.
internal Rectangle Viewport
{
get
{
Expand Down
6 changes: 3 additions & 3 deletions Terminal.Gui/Input/Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ public class MouseEvent
/// <summary>The View at the location for the mouse event.</summary>
public View View { get; set; }

/// <summary>The X position of the mouse in <see cref="View.Bounds"/>-relative coordinates.</summary>
/// <summary>The X position of the mouse in <see cref="Gui.View.Viewport"/>-relative coordinates.</summary>
public int X { get; set; }

/// <summary>The Y position of the mouse in <see cref="View.Bounds"/>-relative coordinates.</summary>
/// <summary>The Y position of the mouse in <see cref="Gui.View.Viewport"/>-relative coordinates.</summary>
public int Y { get; set; }

/// <summary>
Expand All @@ -133,7 +133,7 @@ public class MouseEvent
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="X"/> and <see cref="Y"/> properties are always <see cref="View.Bounds"/>-relative. When the mouse is grabbed by a view,
/// The <see cref="X"/> and <see cref="Y"/> properties are always <see cref="Gui.View.Viewport"/>-relative. When the mouse is grabbed by a view,
/// <see cref="ScreenPosition"/> provides the mouse position screen-relative coordinates, enabling the grabbed view to know how much the
/// mouse has moved.
/// </para>
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Text/Autocomplete/AppendAutocomplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public override void RenderOverlay (Point renderAt)
Suggestion suggestion = Suggestions.ElementAt (SelectedIdx);
string fragment = suggestion.Replacement.Substring (suggestion.Remove);

int spaceAvailable = textField.Bounds.Width - textField.Text.GetColumns ();
int spaceAvailable = textField.Viewport.Width - textField.Text.GetColumns ();
int spaceRequired = fragment.EnumerateRunes ().Sum (c => c.GetColumns ());

if (spaceAvailable < spaceRequired)
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Text/Autocomplete/PopupAutocomplete.PopUp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Popup (PopupAutocomplete autoComplete)

protected internal override bool OnMouseEvent (MouseEvent mouseEvent) { return _autoComplete.OnMouseEvent (mouseEvent); }

public override void OnDrawContent (Rectangle contentArea)
public override void OnDrawContent (Rectangle viewport)
{
if (!_autoComplete.LastPopupPos.HasValue)
{
Expand Down
22 changes: 11 additions & 11 deletions Terminal.Gui/Text/Autocomplete/PopupAutocomplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ public override void RenderOverlay (Point renderAt)
if (PopupInsideContainer)
{
// don't overspill vertically
height = Math.Min (HostControl.Bounds.Height - renderAt.Y, MaxHeight);
height = Math.Min (HostControl.Viewport.Height - renderAt.Y, MaxHeight);

// There is no space below, lets see if can popup on top
if (height < Suggestions.Count && HostControl.Bounds.Height - renderAt.Y >= height)
if (height < Suggestions.Count && HostControl.Viewport.Height - renderAt.Y >= height)
{
// Verifies that the upper limit available is greater than the lower limit
if (renderAt.Y > HostControl.Bounds.Height - renderAt.Y)
if (renderAt.Y > HostControl.Viewport.Height - renderAt.Y)
{
renderAt.Y = Math.Max (renderAt.Y - Math.Min (Suggestions.Count + 1, MaxHeight + 1), 0);
height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), LastPopupPos.Value.Y - 1);
Expand All @@ -287,13 +287,13 @@ public override void RenderOverlay (Point renderAt)
else
{
// don't overspill vertically
height = Math.Min (Math.Min (top.Bounds.Height - HostControl.Frame.Bottom, MaxHeight), Suggestions.Count);
height = Math.Min (Math.Min (top.Viewport.Height - HostControl.Frame.Bottom, MaxHeight), Suggestions.Count);

// There is no space below, lets see if can popup on top
if (height < Suggestions.Count && HostControl.Frame.Y - top.Frame.Y >= height)
{
// Verifies that the upper limit available is greater than the lower limit
if (HostControl.Frame.Y > top.Bounds.Height - HostControl.Frame.Y)
if (HostControl.Frame.Y > top.Viewport.Height - HostControl.Frame.Y)
{
renderAt.Y = Math.Max (HostControl.Frame.Y - Math.Min (Suggestions.Count, MaxHeight), 0);
height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), HostControl.Frame.Y);
Expand Down Expand Up @@ -323,34 +323,34 @@ public override void RenderOverlay (Point renderAt)
if (PopupInsideContainer)
{
// don't overspill horizontally, let's see if can be displayed on the left
if (width > HostControl.Bounds.Width - renderAt.X)
if (width > HostControl.Viewport.Width - renderAt.X)
{
// Verifies that the left limit available is greater than the right limit
if (renderAt.X > HostControl.Bounds.Width - renderAt.X)
if (renderAt.X > HostControl.Viewport.Width - renderAt.X)
{
renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
width = Math.Min (width, LastPopupPos.Value.X);
}
else
{
width = Math.Min (width, HostControl.Bounds.Width - renderAt.X);
width = Math.Min (width, HostControl.Viewport.Width - renderAt.X);
}
}
}
else
{
// don't overspill horizontally, let's see if can be displayed on the left
if (width > top.Bounds.Width - (renderAt.X + HostControl.Frame.X))
if (width > top.Viewport.Width - (renderAt.X + HostControl.Frame.X))
{
// Verifies that the left limit available is greater than the right limit
if (renderAt.X + HostControl.Frame.X > top.Bounds.Width - (renderAt.X + HostControl.Frame.X))
if (renderAt.X + HostControl.Frame.X > top.Viewport.Width - (renderAt.X + HostControl.Frame.X))
{
renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
width = Math.Min (width, LastPopupPos.Value.X);
}
else
{
width = Math.Min (width, top.Bounds.Width - renderAt.X);
width = Math.Min (width, top.Viewport.Width - renderAt.X);
}
}
}
Expand Down
Loading

0 comments on commit 1549d0d

Please sign in to comment.