Class WindowRenderer
- Namespace
- SharpConsoleUI.Windows
- Assembly
- SharpConsoleUI.dll
Coordinates window rendering operations for the DOM-based layout system. Extracted from Window class as part of Phase 3.2 refactoring.
Responsibilities:
- DOM tree building and management
- Three-stage layout (Measure, Arrange, Paint)
- CharacterBuffer management
- Visible region clipping
- Hit testing
public class WindowRenderer
- Inheritance
-
WindowRenderer
- Inherited Members
Constructors
WindowRenderer(Window, ILogService?)
Initializes a new instance of the WindowRenderer class.
public WindowRenderer(Window window, ILogService? logService)
Parameters
windowWindowThe window instance this renderer serves
logServiceILogServiceOptional log service for diagnostic logging
Properties
Buffer
Gets the current character buffer for this window.
public CharacterBuffer? Buffer { get; }
Property Value
Remarks
CAUTION: Direct buffer manipulation should only be done via PostBufferPaint event to avoid race conditions. Reading is safe at any time.
MaxScrollOffset
Gets the maximum scroll offset.
public int MaxScrollOffset { get; }
Property Value
RootLayoutNode
Gets the root layout node of the DOM tree.
public LayoutNode? RootLayoutNode { get; }
Property Value
ScrollOffset
Gets or sets the scroll offset for the window content.
public int ScrollOffset { get; set; }
Property Value
ScrollableContentHeight
Gets the total height of scrollable content.
public int ScrollableContentHeight { get; }
Property Value
Methods
BufferToLines(Color, Color)
Converts the character buffer to a list of ANSI-formatted strings.
public List<string> BufferToLines(Color foregroundColor, Color backgroundColor)
Parameters
foregroundColorColorDefault foreground color
backgroundColorColorDefault background color
Returns
ConvertVisibleRegionsToClipRect(List<Rectangle>, int, int, int, int, bool)
Converts screen-space visible regions to a window-space clipping rectangle. This optimization prevents painting occluded areas that are covered by overlapping windows.
public LayoutRect ConvertVisibleRegionsToClipRect(List<Rectangle> visibleRegions, int windowLeft, int windowTop, int windowWidth, int windowHeight, bool showTitle)
Parameters
visibleRegionsList<Rectangle>Screen-space rectangles representing visible portions of the window.
windowLeftintWindow left position
windowTopintWindow top position
windowWidthintWindow width
windowHeightintWindow height
showTitleboolWhether the window shows a title bar
Returns
- LayoutRect
A window-space LayoutRect representing the bounding box of all visible regions, or empty rect if nothing is visible.
CreatePortal(IWindowControl, IWindowControl)
Creates a portal node for rendering external content within a control's bounds.
public LayoutNode? CreatePortal(IWindowControl ownerControl, IWindowControl portalContent)
Parameters
ownerControlIWindowControlThe control that owns the portal
portalContentIWindowControlThe content to render in the portal
Returns
- LayoutNode
The created portal node, or null if the owner control has no layout node
GetLayoutNode(IWindowControl)
Gets the layout node for a specific control.
public LayoutNode? GetLayoutNode(IWindowControl control)
Parameters
controlIWindowControlThe control to look up
Returns
- LayoutNode
The layout node for the control, or null if not found
HitTestDOM(int, int)
Performs hit testing to find the control at the specified position.
public IWindowControl? HitTestDOM(int x, int y)
Parameters
xintX coordinate relative to window content area.
yintY coordinate relative to window content area.
Returns
- IWindowControl
The control at the specified position, or null if none found.
InvalidateDOM()
Invalidates the entire DOM tree, forcing a complete rebuild on next render. Called when controls are added, removed, or the window structure changes.
public void InvalidateDOM()
InvalidateDOMLayout()
Invalidates the DOM layout, triggering a re-measure and re-arrange.
public void InvalidateDOMLayout()
PageDown()
Scrolls down by one page.
public void PageDown()
PageUp()
Scrolls up by one page.
public void PageUp()
PaintDOM(LayoutRect, Color)
Paints the DOM tree to the character buffer.
public void PaintDOM(LayoutRect clipRect, Color backgroundColor)
Parameters
clipRectLayoutRectThe clipping rectangle in window-space coordinates. Only content within this rect will be painted.
backgroundColorColorWindow background color
PerformDOMLayout(int, int)
Performs the measure and arrange passes on the DOM tree.
public void PerformDOMLayout(int contentWidth, int contentHeight)
Parameters
RebuildContentCacheDOM(IReadOnlyList<IWindowControl>, int, int, List<Rectangle>?, int, int, bool, Color, Color)
Rebuilds the content cache using DOM-based layout. This is the main entry point for the complete rendering pipeline.
public List<string> RebuildContentCacheDOM(IReadOnlyList<IWindowControl> controls, int availableWidth, int availableHeight, List<Rectangle>? visibleRegions, int windowLeft, int windowTop, bool showTitle, Color foregroundColor, Color backgroundColor)
Parameters
controlsIReadOnlyList<IWindowControl>The window's control list
availableWidthintAvailable content width
availableHeightintAvailable content height
visibleRegionsList<Rectangle>Optional screen-space visible regions for clipping optimization
windowLeftintWindow left position
windowTopintWindow top position
showTitleboolWhether the window shows a title bar
foregroundColorColorWindow foreground color
backgroundColorColorWindow background color
Returns
RebuildDOMTree(IReadOnlyList<IWindowControl>, int, int)
Rebuilds the complete DOM tree from the control list.
public void RebuildDOMTree(IReadOnlyList<IWindowControl> controls, int contentWidth, int contentHeight)
Parameters
controlsIReadOnlyList<IWindowControl>The window's control list
contentWidthintAvailable content width
contentHeightintAvailable content height
RemovePortal(IWindowControl, LayoutNode)
Removes a portal node from the DOM tree.
public void RemovePortal(IWindowControl ownerControl, LayoutNode portalNode)
Parameters
ownerControlIWindowControlThe control that owns the portal
portalNodeLayoutNodeThe portal node to remove
ScrollBy(int)
Scrolls by the specified delta.
public void ScrollBy(int delta)
Parameters
deltaint
ScrollToBottom()
Scrolls to the bottom of the content.
public void ScrollToBottom()
ScrollToTop()
Scrolls to the top of the content.
public void ScrollToTop()
Events
PostBufferPaint
Raised AFTER painting controls to the buffer but before converting to ANSI strings.
public event WindowRenderer.BufferPaintDelegate? PostBufferPaint
Event Type
Remarks
This event allows custom effects, transitions, filters, or compositor-style manipulations on the rendered buffer. The buffer can be safely modified here. Content painted here will appear ON TOP of controls.
Example use cases:
- Fade in/out transitions
- Blur effects for modal backgrounds
- Glow effects around focused controls
- Custom overlays and effects
PreBufferPaint
Raised BEFORE painting controls to the buffer.
public event WindowRenderer.BufferPaintDelegate? PreBufferPaint
Event Type
Remarks
This event allows painting backgrounds, game graphics, or other content that should appear BEHIND the controls. Controls will paint on top.
Example use cases:
- Game rendering (fractals, animations, sprites)
- Custom backgrounds
- Gradients or patterns behind UI