Scrolling
Terminal.Gui provides a rich system for how View users can scroll content with the keyboard and/or mouse.
See Also
Lexicon & Taxonomy
| Term | Meaning |
|---|---|
| Content Area | The total area of content that can be scrolled, defined by @Terminal.Gui.View.GetContentSize. When larger than the Viewport, scrolling is enabled. |
| Scroll | The act of causing content to move either horizontally or vertically within the @Terminal.Gui.View.Viewport. Also referred to as "Content Scrolling". |
| ScrollBar | Indicates the size of scrollable content and controls the position of the visible content, either vertically or horizontally. At each end, a @Terminal.Gui.Button is provided, one to scroll up or left and one to scroll down or right. Between the buttons is a @Terminal.Gui.ScrollSlider that can be dragged to control the position of the visible content. The ScrollSlider is sized to show the proportion of the scrollable content to the size of the @Terminal.Gui.View.Viewport. |
| ScrollSlider | A visual indicator that shows the proportion of the scrollable content to the size of the @Terminal.Gui.View.Viewport and allows the user to use the mouse to scroll. |
| Viewport | The scrollable "viewport" into the View's Content Area (which is defined by the return value of @Terminal.Gui.View.GetContentSize). See Layout Deep Dive for more details. |
| ViewportSettings | Configuration flags that adjust the behavior of scrolling, including whether negative coordinates are allowed and how clipping is applied. |
Overview
The ability to scroll content is built into View. The Viewport represents the scrollable "viewport" into the View's Content Area (which is defined by the return value of GetContentSize() ).
By default, View, includes no bindings for the typical directional keyboard and mouse input and cause the Content Area.
Terminal.Gui also provides the ability show a visual scroll bar that responds to mouse input. This ability is not enabled by default given how precious TUI screen real estate is.
Scrolling with the mouse and keyboard are enabled by:
- Making the
Viewportsize smaller than the size returned byGetContentSize(). - Creating key bindings for the appropriate directional keys, and calling
ScrollHorizontal()(System.Int32) /ScrollVertical()(System.Int32) as needed. - Subscribing to
MouseEventand calling callingScrollHorizontal()(System.Int32) /ScrollVertical()(System.Int32) as needed. - Enabling the ScrollBars built into View by setting the ViewportSettingsFlags.HasVerticalScrollBar or ViewportSettingsFlags.HasHorizontalScrollBar flags on the
ViewportSettingsproperty. Alternatively, theScrollBar.VisibilityModeproperty can be set to control scrollbar visibility manually.
While ScrollBar can be used in a standalone manner to provide proportional scrolling, it is typically enabled automatically via the HorizontalScrollBar and VerticalScrollBar properties.
ScrollBar Visibility
The ScrollBar.VisibilityMode property controls how a ScrollBar manages its Visible state. The ScrollBarVisibilityMode enum provides these options:
Manual(default) - The scrollbar does not manage its own visibility. The developer controlsVisibledirectly to show or hide the scrollbar.Auto- The scrollbar is automatically shown when the scrollable content size exceeds the visible content size, and hidden otherwise.Always- The scrollbar is always visible regardless of content size.None- The scrollbar is always hidden regardless of content size or ViewportSettingsFlags.
Enabling Built-in Scrollbars
The recommended way to enable the built-in scrollbars (VerticalScrollBar and HorizontalScrollBar) is to use the ViewportSettingsFlags.HasVerticalScrollBar and ViewportSettingsFlags.HasHorizontalScrollBar flags:
// Enable vertical scrollbar with automatic visibility
view.ViewportSettings |= ViewportSettingsFlags.HasVerticalScrollBar;
// Enable both scrollbars
view.ViewportSettings |= ViewportSettingsFlags.HasScrollBars;
// Disable horizontal scrollbar
view.ViewportSettings &= ~ViewportSettingsFlags.HasHorizontalScrollBar;
Setting these flags automatically:
- Creates the scrollbar (they are lazy-loaded)
- Sets the scrollbar's
VisibilityModetoAuto - Makes the scrollbar visible when content exceeds viewport size
Alternatively, you can manually control scrollbar visibility:
// Manual control
view.VerticalScrollBar.Visible = true;
view.VerticalScrollBar.VisibilityMode = ScrollBarVisibilityMode.Always;
Examples
These UI Catalog Scenarios illustrate Terminal.Gui scrolling:
- Scrolling - Demonstrates the ScrollBar objects built into-View.
- ScrollBar Demo - Demonstrates using ScrollBar view in a standalone manner.
- ViewportSettings - Demonstrates the various ViewportSettingsFlags (see below) in an interactive manner. Used by the development team to visually verify that convoluted View layout and arrangement scenarios scroll properly.
- Character Map - Demonstrates a sophisticated scrolling use-case. The entire set of Unicode code-points can be scrolled and searched. From a scrolling perspective, this Scenario illustrates how to manually configure Viewport, Content Area, and Viewport Settings to enable horizontal and vertical headers (as might appear in a spreadsheet), full keyboard and mouse support, and more.
- ListView and HexEdit - The source code to these built-in Views are good references for how to support scrolling and ScrollBars in a re-usable View sub-class.
ViewportSettings
The ViewportSettings property (of type ViewportSettingsFlags) controls the behavior of scrolling.
Negative Location Flags - Allow scrolling before the content origin (0,0):
AllowNegativeX- If set,Viewport.Xcan be set to negative coordinates enabling scrolling beyond the left of the content area.AllowNegativeY- If set,Viewport.Ycan be set to negative coordinates enabling scrolling beyond the top of the content area.AllowNegativeLocation- Combines both X and Y.
Greater Than Content Flags - Allow scrolling past the last row/column:
AllowXGreaterThanContentWidth- If set,Viewport.Xcan be set to values greater than or equal to the content width, enabling scrolling beyond the right of the Content Area. When not set,Viewport.Xis constrained so the last column remains visible.AllowYGreaterThanContentHeight- If set,Viewport.Ycan be set to values greater than or equal to the content height, enabling scrolling beyond the bottom of the Content Area. When not set,Viewport.Yis constrained so the last row remains visible.AllowLocationGreaterThanContentSize- Combines both X and Y.
Blank Space Flags - Allow blank space to appear when scrolling:
AllowXPlusWidthGreaterThanContentWidth- If set,Viewport.X + Viewport.Widthcan exceedGetContentSize().Width, allowing blank space on the right when scrolling.AllowYPlusHeightGreaterThanContentHeight- If set,Viewport.Y + Viewport.Heightcan exceedGetContentSize().Height, allowing blank space at the bottom when scrolling.AllowLocationPlusSizeGreaterThanContentSize- Combines both X and Y.
Conditional Negative Flags - Allow negative scrolling only when viewport is larger than content:
AllowNegativeXWhenWidthGreaterThanContentWidth- Useful for centering content smaller than the view.AllowNegativeYWhenHeightGreaterThanContentHeight- Useful for centering content smaller than the view.AllowNegativeLocationWhenSizeGreaterThanContentSize- Combines both X and Y.
Drawing Flags - Control clipping and clearing behavior:
ClipContentOnly- By default, clipping is applied toViewport. Setting this flag will cause clipping to be applied to the visible content area.ClearContentOnly- If set,ClearViewport()will clear only the portion of the content area that is visible within the Viewport. This is useful for views that have a content area larger than the Viewport and want the area outside the content to be visually distinct.ClipContentOnlymust be set for this to work.Transparent- The view does not clear its background when drawing.TransparentMouse- Mouse events pass through areas not occupied by SubViews.
ScrollBar Flags - Enable built-in scrollbars:
HasVerticalScrollBar- If set, the built-inVerticalScrollBaris enabled with ScrollBarVisibilityMode.Auto behavior. Clearing this flag disables the scrollbar.HasHorizontalScrollBar- If set, the built-inHorizontalScrollBaris enabled with ScrollBarVisibilityMode.Auto behavior. Clearing this flag disables the scrollbar.HasScrollBars- Combines both vertical and horizontal scrollbar flags.