Layout
Terminal.Gui provides a rich system for how View objects are laid out relative to each other. The layout system also defines how coordinates are specified.
See View Deep Dive and Arrangement Deep Dive for more.
Lexicon & Taxonomy
Coordinates
Screen-Relative - Describes the dimensions and characteristics of the underlying terminal. Currently Terminal.Gui only supports applications that run "full-screen", meaning they fill the entire terminal when running. As the user resizes their terminal, the Screen changes size and the application will be resized to fit. Screen-Relative means an origin (
0, 0
) at the top-left corner of the terminal. ConsoleDriver s operate exclusively on Screen-Relative coordinates.Application-Relative - The dimensions and characteristics of the application. Because only full-screen apps are currently supported, Application is effectively the same as
Screen
from a layout perspective. Application-Relative currently means an origin (0, 0
) at the top-left corner of the terminal. Top is aView
with a top-left corner fixed at the Application.Relative coordinate of (0, 0
) and is the size ofScreen
.Frame-Relative - The Frame property of a
View
is a rectangle that describes the current location and size of the view relative to theSuperview
's content area. Frame-Relative means a coordinate is relative to the top-left corner of the View in question. FrameToScreen() and @Terminal.Gui.View.ScreenToFrame are helper methods for translating a Frame-Relative coordinate to a Screen-Relative coordinate and vice-versa.Content-Relative - A rectangle, with an origin of (
0, 0
) and size (defined by @Terminal.Gui.View.GetContentSize) where the View's content exists. Content-Relative means a coordinate is relative to the top-left corner of the content, which is always (0,0
). @Terminal.Gui.View.ContentToScreen and @Terminal.Gui.View.ScreenToContent are helper methods for translating a Content-Relative coordinate to a Screen-Relative coordinate and vice-versa.Viewport-Relative - A Content-Relative rectangle representing the subset of the View's content that is visible to the user: Viewport.
If GetContentSize() is larger than the Viewport, scrolling is enabled.
Viewport-Relative means a coordinate that is bound by (
0,0
) and the size of the inner-rectangle of the View'sPadding
. The View drawing primitives (e.g.View.Move
) take Viewport-Relative coordinates;Move (0, 0)
means theCell
in the top-left corner of the inner rectangle ofPadding
.View.ViewportToScreen ()
andView.ScreenToViewport ()
are helper methods for translating a Viewport-Relative coordinate to a Screen-Relative coordinate and vice-versa. To convert a Viewport-Relative coordinate to a Content-Relative coordinate, simply subtractViewport.X
and/orViewport.Y
from the Content-Relative coordinate. To convert a Viewport-Relative coordinate to a Frame-Relative coordinate, subtract the point returned by GetViewportOffsetFromFrame().
View Composition
*@Terminal.Gui.Thickness* - A
record struct
describing a rectangle where each of the four sides can have a width. Valid width values are >= 0. The inner area of a Thickness is the sum of the widths of the four sides minus the size of the rectangle.*Frame - The
Rectangle
that defines the location and size of the View including all of the margin, border, padding, and content area. The coordinates are relative to the SuperView of the View (or, in the case ofApplication.Top
,ConsoleDriver.Row == 0; ConsoleDriver.Col == 0
). The Frame's location and size are controlled by the.X
,.Y
,.Height
, and.Width
properties of the View.Adornments - The
Thickness
es that separate theFrame
from theContentArea
. There are three Adornments,Margin
,Padding
, andBorder
. Adornments are not part of the View's content and are not clipped by the View'sClipArea
. Examples of Adornments:*Margin - The
Adornment
that separates a View from other SubViews of the same SuperView. The Margin is not part of the View's content and is not clipped by the View'sClipArea
. By defaultMargin
is{0,0,0,0}
.Enabling ShadowStyle will change the
Thickness
of theMargin
to include the shadow.Margin
can be used instead of (or with)Dim.Pos
to position a View relative to another View.Eg.
view.X = Pos.Right (otherView) + 1; view.Y = Pos.Bottom (otherView) + 1;
is equivalent to
otherView.Margin.Thickness = new Thickness (0, 0, 1, 1); view.X = Pos.Right (otherView); view.Y = Pos.Bottom (otherView);
*Border - The
Adornment
where a visual border (drawn using line-drawing glyphs) and the Title are drawn, and where the user can interact with the mouse/keyboard to adjust the Views' Arrangement.The Border expands inward; in other words if
Border.Thickness.Top == 2
the border & title will take up the first row and the second row will be filled with spaces. The Border is not part of the View's content and is not clipped by the View'sClip
.*Padding - The
Adornment
that offsets theContentArea
from theBorder
.Padding
is{0, 0, 0, 0}
by default. Padding is not part of the View's content and is not clipped by the View'sClip
.When, enabled, scroll bars reside within
Padding
.
Arrangement Modes
See Arrangement Deep Dive for more.
Tile, Tiled, Tiling - Refer to a form of View are visually arranged such that they abut each other and do not overlap. In a Tiled view arrangement, Z-ordering only comes into play when a developer intentionally causes views to be aligned such that they overlap. Borders that are drawn between the SubViews can optionally support resizing the SubViews (negating the need for
TileView
).Overlap, Overlapped, Overlapping - Refers to a form Layout where SubViews of a View are visually arranged such that their Frames overlap. In Overlap view arrangements there is a Z-axis (Z-order) in addition to the X and Y dimension. The Z-order indicates which Views are shown above other views.
The Frame
The Frame property of a View
is a rectangle that describes the current location and size of the view relative to the Superview
's content area. The Frame
has a Location
and Size
. The Location
describes the top-left corner of the view relative to the SuperView
's content area. The Size
describes the width and height of the view. The Frame
is used to determine where the view is drawn on the screen and is used to calculate the Viewport and content size.
The Content Area
The content area is the area where the view's content is drawn. Content can be any combination of the Text property, Subviews
, and other content drawn by the View. The GetContentSize() method gets the size of the content area of the view. Content Area refers to the rectangle with a location of 0,0
with the size returned by GetContentSize().
The Content Area size tracks the size of the Viewport by default. If the content size is set via @Terminal.Gui.View.SetContentSize, the content area is the provided size. If the content size is larger than the Viewport, scrolling is enabled.
The Viewport
The Viewport (@Terminal.Gui.View.Viewport) is a rectangle describing the portion of the Content Area that is currently visible to the user. It is a "portal" into the content. The Viewport.Location
is relative to the top-left corner of the inner rectangle of View.Padding
. If Viewport.Size
is the same as View.GetContentSize()
, Viewport.Location
will be 0,0
.
To enable scrolling call View.SetContentSize()
and then set Viewport.Location
to positive values. Making Viewport.Location
positive moves the Viewport down and to the right in the content.
The View.ViewportSettings
property controls how the Viewport is constrained. By default, the ViewportSettings
is set to ViewportSettings.None
. To enable the viewport to be moved up-and-to-the-left of the content, use ViewportSettings.AllowNegativeX
and or ViewportSettings.AllowNegativeY
.
The default ViewportSettings
also constrains the Viewport to the size of the content, ensuring the right-most column or bottom-most row of the content will always be visible (in v1 the equivalent concept was ScrollBarView.AlwaysKeepContentInViewport
). To allow the Viewport to be smaller than the content, set ViewportSettings.AllowXGreaterThanContentWidth
and/or ViewportSettings.AllowXGreaterThanContentHeight
.
*@Terminal.Gui.View.GetContentSize()* - The content area is the area where the view's content is drawn. Content can be any combination of the Text property,
Subviews
, and other content drawn by the View. The GetContentSize() method gets the size of the content area of the view. Content Area refers to the rectangle with a location of0,0
with the size returned by GetContentSize(). The Layout Deep Dive has more details on the Content Area.*Viewport A rectangle describing the portion of the Content Area that is currently visible to the user. It is a "portal" into the content. The
Viewport.Location
is relative to the top-left corner of the inner rectangle ofView.Padding
. IfViewport.Size
is the same asView.GetContentSize()
,Viewport.Location
will be0,0
.
Layout
Terminal.Gui provides a rich system for how views are laid out relative to each other. The position of a view is set by setting the X
and Y
properties, which are of time Pos. The size is set via Width
and Height
, which are of type Dim.
var label1 = new Label () { X = 1, Y = 2, Width = 3, Height = 4, Title = "Absolute")
var label2 = new Label () {
Title = "Computed",
X = Pos.Right (otherView),
Y = Pos.Center (),
Width = Dim.Fill (),
Height = Dim.Percent (50)
};
The Frame
property is a rectangle that provides the current location and size of the view relative to the View's Superview
's Content area.
The Pos
Type
The Pos is the type of View.X
and View.Y
and supports the following sub-types:
- Absolute position, by passing an integer - Absolute(int).
- Percentage of the parent's view size - Percent(int)
- Anchored from the end of the dimension - AnchorEnd(int)
- Centered, using @Terminal.Gui.Pos.Center()
- The Left(View), Right(View), Top(View), and Bottom(View) tracks the position of another view.
- Aligned (left, right, center, etc...) with other views - Align(Alignment, AlignmentModes, int).
- An arbitrary function - @Terminal.Gui.Pos.FuncTerminal.Gui.Pos.Func(System.Func{System.Int32})
All Pos
coordinates are relative to the SuperView's content area.
Pos
values can be combined using addition or subtraction:
// Set the X coordinate to 10 characters left from the center
view.X = Pos.Center () - 10;
view.Y = Pos.Percent (20);
anotherView.X = AnchorEnd (10);
anotherView.Width = 9;
myView.X = Pos.X (view);
myView.Y = Pos.Bottom (anotherView) + 5;
The Dim
Type
The Dim is the type of View.Width
and View.Height
and supports the following sub-types:
- Automatic size based on the View's content (either Subviews or Text) - Auto(DimAutoStyle, Dim?, Dim?) - See Dim.Auto Deep Dive.
- Absolute size, by passing an integer - Absolute(int).
- Percentage of the SuperView's Content Area - @Terminal.Gui.Dim.Percent(System.Int32).
- Fill to the end of the SuperView's Content Area - Fill().
- Reference the Width or Height of another view - Width(View?), Height(View?).
- An arbitrary function - Func(Func<int>).
All Dim
dimensions are relative to the SuperView's content area.
Like, Pos
, objects of type Dim
can be combined using addition or subtraction, like this:
// Set the Width to be 10 characters less than filling
// the remaining portion of the screen
view.Width = Dim.Fill () - 10;
view.Height = Dim.Percent(20) - 1;
anotherView.Height = Dim.Height (view) + 1;