Table of Contents

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

window Window

The window instance this renderer serves

logService ILogService

Optional log service for diagnostic logging

Properties

Buffer

Gets the current character buffer for this window.

public CharacterBuffer? Buffer { get; }

Property Value

CharacterBuffer

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

int

RootLayoutNode

Gets the root layout node of the DOM tree.

public LayoutNode? RootLayoutNode { get; }

Property Value

LayoutNode

ScrollOffset

Gets or sets the scroll offset for the window content.

public int ScrollOffset { get; set; }

Property Value

int

ScrollableContentHeight

Gets the total height of scrollable content.

public int ScrollableContentHeight { get; }

Property Value

int

Methods

BufferToLines(Color, Color)

Converts the character buffer to a list of ANSI-formatted strings.

public List<string> BufferToLines(Color foregroundColor, Color backgroundColor)

Parameters

foregroundColor Color

Default foreground color

backgroundColor Color

Default background color

Returns

List<string>

List of ANSI-formatted strings

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

visibleRegions List<Rectangle>

Screen-space rectangles representing visible portions of the window.

windowLeft int

Window left position

windowTop int

Window top position

windowWidth int

Window width

windowHeight int

Window height

showTitle bool

Whether 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

ownerControl IWindowControl

The control that owns the portal

portalContent IWindowControl

The 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

control IWindowControl

The 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

x int

X coordinate relative to window content area.

y int

Y 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

clipRect LayoutRect

The clipping rectangle in window-space coordinates. Only content within this rect will be painted.

backgroundColor Color

Window background color

PerformDOMLayout(int, int)

Performs the measure and arrange passes on the DOM tree.

public void PerformDOMLayout(int contentWidth, int contentHeight)

Parameters

contentWidth int
contentHeight int

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

controls IReadOnlyList<IWindowControl>

The window's control list

availableWidth int

Available content width

availableHeight int

Available content height

visibleRegions List<Rectangle>

Optional screen-space visible regions for clipping optimization

windowLeft int

Window left position

windowTop int

Window top position

showTitle bool

Whether the window shows a title bar

foregroundColor Color

Window foreground color

backgroundColor Color

Window background color

Returns

List<string>

List of rendered lines as ANSI strings

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

controls IReadOnlyList<IWindowControl>

The window's control list

contentWidth int

Available content width

contentHeight int

Available content height

RemovePortal(IWindowControl, LayoutNode)

Removes a portal node from the DOM tree.

public void RemovePortal(IWindowControl ownerControl, LayoutNode portalNode)

Parameters

ownerControl IWindowControl

The control that owns the portal

portalNode LayoutNode

The portal node to remove

ScrollBy(int)

Scrolls by the specified delta.

public void ScrollBy(int delta)

Parameters

delta int

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

WindowRenderer.BufferPaintDelegate

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

WindowRenderer.BufferPaintDelegate

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