Class ScrollablePanelControl
- Namespace
- SharpConsoleUI.Controls
- Assembly
- SharpConsoleUI.dll
A scrollable panel control that can host child controls with automatic scrolling support. Supports vertical and horizontal scrolling, mouse wheel, and visual scrollbars.
public class ScrollablePanelControl : BaseControl, IDOMPaintable, INotifyPropertyChanged, IInteractiveControl, IFocusableControl, IMouseAwareControl, IWindowControl, IDisposable, IContainer, IContainerControl, IControlHost, IScrollableContainer, IFocusScope, ILogicalCursorProvider, IColorRoleableControl
- Inheritance
-
ScrollablePanelControl
- Implements
- Inherited Members
- Extension Methods
Constructors
ScrollablePanelControl()
Initializes a new instance of the ScrollablePanelControl class.
public ScrollablePanelControl()
Properties
ArrowKeyScrolling
Gets or sets whether arrow / page / home-end keys scroll the viewport.
When false, those keys are left unhandled by ProcessKey(ConsoleKeyInfo) so they
can bubble to an owning control that wants them for its own navigation (e.g. the
NavigationView nav pane, which moves the selected item on arrows and
relies on focus-driven auto-scroll to keep it visible). Mouse-wheel scrolling and the
scrollbar are unaffected. Defaults to true.
public bool ArrowKeyScrolling { get; set; }
Property Value
AutoScroll
Gets or sets whether to automatically scroll to bottom when content is added. When enabled, scrolls to bottom on AddControl if currently at/near bottom. Disables automatically when user scrolls up, re-enables when user scrolls to bottom.
public bool AutoScroll { get; set; }
Property Value
BackgroundColor
Gets or sets the background color for the container and its child controls.
public Color BackgroundColor { get; set; }
Property Value
BorderColor
Gets or sets the border color. When null, uses the foreground color.
public Color? BorderColor { get; set; }
Property Value
BorderStyle
Gets or sets the border style for the panel. Default is None (no border).
public BorderStyle BorderStyle { get; set; }
Property Value
CanFocusWithMouse
Whether this control can receive focus via mouse clicks
public bool CanFocusWithMouse { get; }
Property Value
CanReceiveFocus
ScrollablePanel is focusable when it has anything to interact with: either scrollable content or focusable children. The panel acts as an opaque focus container — it owns its children's focus lifecycle entirely.
public bool CanReceiveFocus { get; }
Property Value
CanScrollDown
Gets whether the content can be scrolled downward (more content exists below the viewport).
public bool CanScrollDown { get; }
Property Value
CanScrollLeft
Gets whether the content can be scrolled left (horizontal offset is greater than zero).
public bool CanScrollLeft { get; }
Property Value
CanScrollRight
Gets whether the content can be scrolled right (more content exists beyond the visible content width). The visible width excludes the vertical scrollbar columns, so the last columns of content remain reachable even when a vertical scrollbar is present.
public bool CanScrollRight { get; }
Property Value
CanScrollUp
Gets whether the content can be scrolled upward (vertical offset is greater than zero).
public bool CanScrollUp { get; }
Property Value
Children
Gets the collection of child controls.
public IReadOnlyList<IWindowControl> Children { get; }
Property Value
ColorRole
The semantic color role. Default = no role (normal resolution).
public ColorRole ColorRole { get; set; }
Property Value
ColorRoleMode
Optional ThemeMode override for role-colour derivation. When non-null, the role's dark/light seed colours are resolved as if the theme were in this mode, regardless of the theme's own Mode. When null (the default), the active theme's mode is used.
public ThemeMode? ColorRoleMode { get; set; }
Property Value
ContentHeight
public int? ContentHeight { get; }
Property Value
- int?
ContentWidth
Gets the minimum width needed to display the control's content, including margins. Returns null if width cannot be determined. This is calculated based on content (text length, child controls, etc.) and represents the natural/intrinsic size.
public override int? ContentWidth { get; }
Property Value
- int?
EnableMouseWheel
Gets or sets whether mouse wheel scrolling is enabled.
public bool EnableMouseWheel { get; set; }
Property Value
ForceReceiveFocus
When true, forces CanReceiveFocus to return true regardless of whether the panel has scrollable content or focusable children. Use this when the panel must be focusable as a scroll/container target even when its children are non-interactive (e.g., the NavigationView nav pane).
public bool ForceReceiveFocus { get; set; }
Property Value
ForegroundColor
Gets or sets the foreground (text) color for the container and its child controls.
public Color ForegroundColor { get; set; }
Property Value
GetConsoleWindowSystem
Gets the console window system instance, or null if not attached to a window system.
public ConsoleWindowSystem? GetConsoleWindowSystem { get; }
Property Value
HasFocus
public bool HasFocus { get; }
Property Value
HasHorizontalScrollbar
Gets whether a horizontal scrollbar is currently shown: scrollbars are enabled, the horizontal scroll mode is Scroll, and content overflows the (vertical-scrollbar-reduced) viewport width.
public bool HasHorizontalScrollbar { get; }
Property Value
HasVerticalScrollbar
Gets whether a vertical scrollbar is currently shown: scrollbars are enabled, the vertical scroll mode is Scroll, and content overflows the (horizontal- scrollbar-reduced) viewport height.
public bool HasVerticalScrollbar { get; }
Property Value
Header
Gets or sets the header text displayed in the top border.
public string? Header { get; set; }
Property Value
HeaderAlignment
Gets or sets the alignment of the header text.
public TextJustification HeaderAlignment { get; set; }
Property Value
Height
Gets or sets the explicit height of the control, or null for automatic sizing.
public override int? Height { get; set; }
Property Value
- int?
HorizontalScrollMode
Gets or sets the horizontal scroll mode.
public ScrollMode HorizontalScrollMode { get; set; }
Property Value
HorizontalScrollOffset
Gets the current horizontal scroll offset in characters.
public int HorizontalScrollOffset { get; }
Property Value
IsEnabled
Gets or sets whether this control is enabled and can receive input.
public bool IsEnabled { get; set; }
Property Value
Outline
When true and a role is set, renders outline style (role color on text + border, surface fill).
public bool Outline { get; set; }
Property Value
Padding
Gets or sets the padding inside the border.
public Padding Padding { get; set; }
Property Value
SavedFocus
Saved focus position. FocusManager sets this before exiting the scope (if scope opts in). GetInitialFocus should return this when set, then clear it.
public IFocusableControl? SavedFocus { get; set; }
Property Value
ScrollbarColor
Gets or sets the scrollbar track color. When null (the default), the track uses the built-in focus-aware default (lighter when focused, darker when not).
public Color? ScrollbarColor { get; set; }
Property Value
ScrollbarOverlay
When true, the vertical scrollbar is painted on the panel's border instead of
reserving an interior column, freeing that space for content. Only takes effect when the
panel has a border; a borderless panel falls back to the normal reserved-column scrollbar.
Default false. Orthogonal to ScrollbarPosition (overlays whichever side).
public bool ScrollbarOverlay { get; set; }
Property Value
ScrollbarPosition
Gets or sets the position of the scrollbar.
public ScrollbarPosition ScrollbarPosition { get; set; }
Property Value
ScrollbarThumbColor
Gets or sets the scrollbar thumb (handle and arrow) color. When null (the default), the thumb uses the built-in focus-aware default (cyan when focused, grey when not).
public Color? ScrollbarThumbColor { get; set; }
Property Value
ShowScrollbar
Gets or sets whether to show the scrollbar.
public bool ShowScrollbar { get; set; }
Property Value
TotalContentHeight
Gets the total height of the scrollable content area in lines. Unlike ContentHeight (which returns the control's own height), this returns the height of the inner content that may extend beyond the viewport.
public int TotalContentHeight { get; }
Property Value
TotalContentWidth
Gets the total width of the scrollable content area in characters. Unlike ContentWidth (which returns the control's own width), this returns the width of the inner content that may extend beyond the viewport.
public int TotalContentWidth { get; }
Property Value
VerticalScrollMode
Gets or sets the vertical scroll mode.
public ScrollMode VerticalScrollMode { get; set; }
Property Value
VerticalScrollOffset
Gets the current vertical scroll offset in lines.
public int VerticalScrollOffset { get; }
Property Value
ViewportHeight
Gets the height of the visible viewport area in lines.
public int ViewportHeight { get; }
Property Value
ViewportWidth
Gets the width of the visible viewport area in characters.
public int ViewportWidth { get; }
Property Value
Visible
Gets or sets whether this control is visible.
public override bool Visible { get; set; }
Property Value
WantsMouseEvents
Whether this control wants to receive mouse events
public bool WantsMouseEvents { get; }
Property Value
Methods
AddControl(IWindowControl)
Adds a child control to the panel. This method is not thread-safe and must be called from the UI thread. For multi-threaded scenarios, queue additions and process them during paint.
public void AddControl(IWindowControl control)
Parameters
controlIWindowControl
ClearContents()
Removes all child controls from the panel.
public void ClearContents()
Create()
Creates a new builder for configuring a ScrollablePanelControl
public static ScrollablePanelBuilder Create()
Returns
- ScrollablePanelBuilder
A new builder instance
GetChildren()
Gets the children of this container for Tab navigation traversal. Required by IContainerControl interface.
public IReadOnlyList<IWindowControl> GetChildren()
Returns
GetInitialFocus(bool)
Returns the first child to focus when Tab enters this scope. backward=true means Shift+Tab entered from the right — return last child.
public IFocusableControl? GetInitialFocus(bool backward)
Parameters
backwardbool
Returns
GetLogicalContentSize()
Gets the logical size of the control's content without rendering.
public override Size GetLogicalContentSize()
Returns
- Size
The size representing the content's natural dimensions.
GetLogicalCursorPosition()
Gets the logical cursor position within the control's content coordinate system. This should be the raw position without any visual adjustments for margins, scrolling, etc.
public Point? GetLogicalCursorPosition()
Returns
- Point?
Logical cursor position or null if no cursor.
GetNextFocus(IFocusableControl, bool)
Returns the next child to focus after Tab from 'current'. Returns null when Tab should exit this scope.
public IFocusableControl? GetNextFocus(IFocusableControl current, bool backward)
Parameters
currentIFocusableControlbackwardbool
Returns
GetVisibleHeightForControl(IWindowControl)
Gets the actual visible height for a control within the container viewport. Returns null if the control is not found or visibility cannot be determined.
public int? GetVisibleHeightForControl(IWindowControl control)
Parameters
controlIWindowControlThe control to check
Returns
- int?
The number of visible lines, or null if unknown
InsertControl(int, IWindowControl)
Inserts a child control at the specified index in the panel. This method is not thread-safe and must be called from the UI thread.
public void InsertControl(int index, IWindowControl control)
Parameters
indexintThe zero-based index at which to insert the control.
controlIWindowControlThe control to insert.
Invalidate(Invalidation, IWindowControl?)
Marks this container as needing the specified work on the next frame. The request propagates up the container chain and folds into the owning window's frame-intent accumulator.
public void Invalidate(Invalidation work, IWindowControl? callerControl = null)
Parameters
workInvalidationThe kind of work requested: Repaint (appearance-only, Measure skipped) or Relayout (full layout).
callerControlIWindowControlThe control that triggered the invalidation, if any (cycle guard).
MeasureDOM(LayoutConstraints)
Measures the control's desired size given the available constraints.
public override LayoutSize MeasureDOM(LayoutConstraints constraints)
Parameters
constraintsLayoutConstraintsThe layout constraints (min/max width/height).
Returns
- LayoutSize
The desired size of the control.
OnDisposing()
Called during Dispose() before Container is set to null.
Override to perform control-specific cleanup (null events, close portals, clear data, etc.).
protected override void OnDisposing()
PaintDOM(CharacterBuffer, LayoutRect, LayoutRect, Color, Color)
Paints the control's content directly to a CharacterBuffer.
public override void PaintDOM(CharacterBuffer buffer, LayoutRect bounds, LayoutRect clipRect, Color defaultFg, Color defaultBg)
Parameters
bufferCharacterBufferThe buffer to paint to.
boundsLayoutRectThe absolute bounds where the control should paint.
clipRectLayoutRectThe clipping rectangle (visible area).
defaultFgColordefaultBgColor
ProcessKey(ConsoleKeyInfo)
Processes a keyboard input event.
public bool ProcessKey(ConsoleKeyInfo key)
Parameters
keyConsoleKeyInfoThe key information for the pressed key.
Returns
- bool
True if the key was handled by this control; otherwise, false.
ProcessMouseEvent(MouseEventArgs)
Processes a mouse event for this control
public bool ProcessMouseEvent(MouseEventArgs args)
Parameters
argsMouseEventArgsMouse event arguments with control-relative coordinates
Returns
- bool
True if the event was handled and should not propagate further
RemoveControl(IWindowControl)
Removes a child control from the panel. This method is not thread-safe and must be called from the UI thread.
public void RemoveControl(IWindowControl control)
Parameters
controlIWindowControl
ScrollChildIntoView(IWindowControl)
Automatically scrolls to bring a child control into view when it receives focus. This is called by the focus system when a child within this panel gains focus.
public void ScrollChildIntoView(IWindowControl child)
Parameters
childIWindowControl
Remarks
Horizontal scrolling is not applied for children (children typically fit the width).
ScrollChildRegionIntoView(IWindowControl, int, int)
Scrolls so that a sub-region of child is visible. The region is given in the
child's own content coordinates: childRelativeTop rows from the child's top,
spanning regionHeight rows. Mirrors ScrollChildIntoView(IWindowControl)'s clamp,
applied to the region instead of the whole child — used to bring a focused element's row into view
when the child itself does not scroll.
public void ScrollChildRegionIntoView(IWindowControl child, int childRelativeTop, int regionHeight)
Parameters
childIWindowControlThe (direct) child whose sub-region should be made visible.
childRelativeTopintRow offset of the region from the child's top edge. A negative value is clamped to 0.
regionHeightintHeight of the region in rows. A value less than 1 is treated as 1.
ScrollHorizontalBy(int)
Scrolls the content horizontally by the specified number of characters. Positive values scroll right, negative values scroll left. The offset is clamped to valid bounds automatically.
public void ScrollHorizontalBy(int chars)
Parameters
charsintNumber of characters to scroll (positive = right, negative = left).
Remarks
No-op when HorizontalScrollMode is None, consistent with ScrollToPosition(int, int).
ScrollToBottom()
Scrolls to the bottom of the content.
public void ScrollToBottom()
Remarks
This is a one-shot scroll — it does not enable AutoScroll. When called
before the panel has been laid out (e.g. immediately after AddWindow), the scroll
is deferred to the first paint. When called right after adding content, the content
height is recomputed on demand so the new content is included in the target.
ScrollToPosition(int, int)
Scrolls to a specific position.
public void ScrollToPosition(int vertical, int horizontal = 0)
Parameters
ScrollToTop()
Scrolls to the top of the content.
public void ScrollToTop()
ScrollVerticalBy(int)
Scrolls the content vertically by the specified number of lines. Positive values scroll down, negative values scroll up. The offset is clamped to valid bounds automatically.
public void ScrollVerticalBy(int lines)
Parameters
linesintNumber of lines to scroll (positive = down, negative = up).
SetLogicalCursorPosition(Point)
Sets the logical cursor position within the control's content coordinate system.
public void SetLogicalCursorPosition(Point position)
Parameters
positionPoint
Events
MouseClick
Event fired when the control is clicked.
public event EventHandler<MouseEventArgs>? MouseClick
Event Type
MouseDoubleClick
Event fired when the control is double-clicked.
public event EventHandler<MouseEventArgs>? MouseDoubleClick
Event Type
MouseEnter
Event fired when the mouse enters the control area.
public event EventHandler<MouseEventArgs>? MouseEnter
Event Type
MouseLeave
Event fired when the mouse leaves the control area.
public event EventHandler<MouseEventArgs>? MouseLeave
Event Type
MouseMove
Event fired when the mouse moves over the control.
public event EventHandler<MouseEventArgs>? MouseMove
Event Type
MouseRightClick
Event fired when the control is right-clicked.
public event EventHandler<MouseEventArgs>? MouseRightClick
Event Type
Scrolled
Event fired when the panel is scrolled.
public event EventHandler<ScrollEventArgs>? Scrolled