Table of Contents

Class WindowBuilder

Namespace
SharpConsoleUI.Builders
Assembly
SharpConsoleUI.dll

Provides a fluent builder pattern for creating and configuring windows in the console window system. Use method chaining to configure window properties before calling Build() or BuildAndShow(bool).

public sealed class WindowBuilder
Inheritance
WindowBuilder
Inherited Members

Examples

var window = new WindowBuilder(windowSystem)
    .WithTitle("My Window")
    .WithSize(80, 25)
    .Centered()
    .Build();

Constructors

WindowBuilder(ConsoleWindowSystem)

Initializes a new instance of the WindowBuilder class.

public WindowBuilder(ConsoleWindowSystem windowSystem)

Parameters

windowSystem ConsoleWindowSystem

The console window system that will manage the created window.

Exceptions

ArgumentNullException

Thrown when windowSystem is null.

Methods

AddControl(IWindowControl)

Adds a control to the window's control collection.

public WindowBuilder AddControl(IWindowControl control)

Parameters

control IWindowControl

The control to add. Null values are ignored.

Returns

WindowBuilder

The current builder instance for method chaining.

AddControl<T>(Action<T>)

Adds a control to the window using a configuration action for inline setup.

public WindowBuilder AddControl<T>(Action<T> configure) where T : class, IWindowControl, new()

Parameters

configure Action<T>

An action to configure the newly created control.

Returns

WindowBuilder

The current builder instance for method chaining.

Type Parameters

T

The type of control to create. Must have a parameterless constructor.

AddControls(params IWindowControl[])

Adds multiple controls to the window's control collection.

public WindowBuilder AddControls(params IWindowControl[] controls)

Parameters

controls IWindowControl[]

The controls to add. Null values in the array are ignored.

Returns

WindowBuilder

The current builder instance for method chaining.

AsModal()

Makes the window modal, blocking input to other windows until closed. Equivalent to calling WithMode(WindowMode) with Modal.

public WindowBuilder AsModal()

Returns

WindowBuilder

The current builder instance for method chaining.

AtPosition(int, int)

Sets the window position without changing its size. If no size has been set, defaults to 80x25 characters.

public WindowBuilder AtPosition(int x, int y)

Parameters

x int

The X coordinate of the window's left edge.

y int

The Y coordinate of the window's top edge.

Returns

WindowBuilder

The current builder instance for method chaining.

Borderless()

Creates a borderless window (renders borders as invisible spaces).

public WindowBuilder Borderless()

Returns

WindowBuilder

The current builder instance for method chaining.

Build()

Builds and returns the window with all configured settings applied. The window is created but not added to the window system.

public Window Build()

Returns

Window

A new Window instance with the configured settings.

BuildAndShow(bool)

Builds the window with all configured settings and immediately adds it to the window system.

public Window BuildAndShow(bool activate = true)

Parameters

activate bool

True to activate (bring to front and focus) the window after creation; false to add it inactive. Defaults to true.

Returns

Window

The created and displayed Window instance.

Centered()

Centers the window on the screen based on the current desktop dimensions. Should be called after WithSize(int, int) to ensure correct centering.

public WindowBuilder Centered()

Returns

WindowBuilder

The current builder instance for method chaining.

Closable(bool)

Sets whether the window can be closed by the user (via close button or programmatically).

public WindowBuilder Closable(bool closable = true)

Parameters

closable bool

True to allow closing; false to prevent it. Defaults to true.

Returns

WindowBuilder

The current builder instance for method chaining.

HideCloseButton()

Hides the close button [X] from the title bar while still allowing programmatic closing. Use this when you want Escape key to close but don't want a visible close button.

public WindowBuilder HideCloseButton()

Returns

WindowBuilder

The current builder instance for method chaining.

HideTitle()

Hides the window title from the title bar. Only the border and window buttons will be displayed.

public WindowBuilder HideTitle()

Returns

WindowBuilder

The current builder instance for method chaining.

HideTitleButtons()

Hides all title buttons (close, minimize, maximize) for a clean window appearance. Useful for dialogs or panels that should not have window controls.

public WindowBuilder HideTitleButtons()

Returns

WindowBuilder

The current builder instance for method chaining.

Maximizable(bool)

Sets whether the window shows a maximize button and can be maximized.

public WindowBuilder Maximizable(bool maximizable = true)

Parameters

maximizable bool

True to show the maximize button; false to hide it. Defaults to true.

Returns

WindowBuilder

The current builder instance for method chaining.

Maximized()

Makes the window start in maximized state, filling the available desktop area. Equivalent to calling WithState(WindowState) with Maximized.

public WindowBuilder Maximized()

Returns

WindowBuilder

The current builder instance for method chaining.

Minimizable(bool)

Sets whether the window shows a minimize button and can be minimized.

public WindowBuilder Minimizable(bool minimizable = true)

Parameters

minimizable bool

True to show the minimize button; false to hide it. Defaults to true.

Returns

WindowBuilder

The current builder instance for method chaining.

Movable(bool)

Sets whether the window can be moved by dragging the title bar.

public WindowBuilder Movable(bool movable = true)

Parameters

movable bool

True to allow moving; false to prevent it. Defaults to true.

Returns

WindowBuilder

The current builder instance for method chaining.

OnActivated(EventHandler)

Subscribes a handler to the window's Activated event, which is raised when the window becomes the active window.

public WindowBuilder OnActivated(EventHandler handler)

Parameters

handler EventHandler

The event handler to invoke when the window is activated.

Returns

WindowBuilder

The current builder instance for method chaining.

OnClosed(EventHandler)

Subscribes a handler to the window's OnClosed event, which is raised after the window has been closed.

public WindowBuilder OnClosed(EventHandler handler)

Parameters

handler EventHandler

The event handler to invoke when the window is closed.

Returns

WindowBuilder

The current builder instance for method chaining.

OnClosing(EventHandler<ClosingEventArgs>)

Subscribes a handler to the window's OnClosing event, which is raised when the window is about to close and can be cancelled.

public WindowBuilder OnClosing(EventHandler<ClosingEventArgs> handler)

Parameters

handler EventHandler<ClosingEventArgs>

The event handler to invoke when the window is closing.

Returns

WindowBuilder

The current builder instance for method chaining.

OnDeactivated(EventHandler)

Subscribes a handler to the window's Deactivated event, which is raised when the window loses focus to another window.

public WindowBuilder OnDeactivated(EventHandler handler)

Parameters

handler EventHandler

The event handler to invoke when the window is deactivated.

Returns

WindowBuilder

The current builder instance for method chaining.

OnKeyPressed(EventHandler<KeyPressedEventArgs>)

Subscribes a handler to the window's KeyPressed event, which is raised when a key is pressed while the window has focus.

public WindowBuilder OnKeyPressed(EventHandler<KeyPressedEventArgs> handler)

Parameters

handler EventHandler<KeyPressedEventArgs>

The event handler to invoke when a key is pressed.

Returns

WindowBuilder

The current builder instance for method chaining.

OnResize(EventHandler)

Subscribes a handler to the window's OnResize event, which is raised when the window size changes.

public WindowBuilder OnResize(EventHandler handler)

Parameters

handler EventHandler

The event handler to invoke when the window is resized.

Returns

WindowBuilder

The current builder instance for method chaining.

OnShown(EventHandler)

Subscribes a handler to the window's OnShown event, which is raised when the window is first displayed.

public WindowBuilder OnShown(EventHandler handler)

Parameters

handler EventHandler

The event handler to invoke when the window is shown.

Returns

WindowBuilder

The current builder instance for method chaining.

OnStateChanged(EventHandler<WindowStateChangedEventArgs>)

Subscribes a handler to the window's StateChanged event, which is raised when the window state changes (normal, minimized, maximized).

public WindowBuilder OnStateChanged(EventHandler<Window.WindowStateChangedEventArgs> handler)

Parameters

handler EventHandler<Window.WindowStateChangedEventArgs>

The event handler to invoke when the window state changes.

Returns

WindowBuilder

The current builder instance for method chaining.

Resizable(bool)

Sets whether the window can be resized by the user.

public WindowBuilder Resizable(bool resizable = true)

Parameters

resizable bool

True to allow resizing; false to prevent it. Defaults to true.

Returns

WindowBuilder

The current builder instance for method chaining.

WithActiveBorderColor(Color)

Sets the border foreground color when the window is active.

public WindowBuilder WithActiveBorderColor(Color color)

Parameters

color Color

The color to use for the active border.

Returns

WindowBuilder

The current builder instance for method chaining.

WithAlwaysOnTop(bool)

Configures the window to always render on top of normal windows. AlwaysOnTop windows are rendered after all normal windows regardless of ZIndex.

public WindowBuilder WithAlwaysOnTop(bool alwaysOnTop = true)

Parameters

alwaysOnTop bool

True to make the window always on top; false otherwise. Default is true.

Returns

WindowBuilder

The current builder instance for method chaining.

WithAsyncWindowThread(WindowThreadDelegateAsync)

Sets an asynchronous window thread method that runs in the background while the window is open.

public WindowBuilder WithAsyncWindowThread(Window.WindowThreadDelegateAsync asyncThreadMethod)

Parameters

asyncThreadMethod Window.WindowThreadDelegateAsync

The async delegate to execute as the window's background thread.

Returns

WindowBuilder

The current builder instance for method chaining.

WithBackgroundColor(Color)

Sets the window background color.

public WindowBuilder WithBackgroundColor(Color color)

Parameters

color Color

The background color for the window content area.

Returns

WindowBuilder

The current builder instance for method chaining.

WithBorderColor(Color)

Sets the border foreground color for both active and inactive states.

public WindowBuilder WithBorderColor(Color color)

Parameters

color Color

The color to use for the border.

Returns

WindowBuilder

The current builder instance for method chaining.

WithBorderStyle(BorderStyle)

Sets the border style for the window.

public WindowBuilder WithBorderStyle(BorderStyle borderStyle)

Parameters

borderStyle BorderStyle

The border style to use.

Returns

WindowBuilder

The current builder instance for method chaining.

WithBounds(WindowBounds)

Sets the window bounds using a WindowBounds instance.

public WindowBuilder WithBounds(WindowBounds bounds)

Parameters

bounds WindowBounds

The bounds defining position and size of the window.

Returns

WindowBuilder

The current builder instance for method chaining.

WithBounds(int, int, int, int)

Sets the window bounds including position and size.

public WindowBuilder WithBounds(int x, int y, int width, int height)

Parameters

x int

The X coordinate of the window's left edge.

y int

The Y coordinate of the window's top edge.

width int

The width of the window in characters.

height int

The height of the window in characters.

Returns

WindowBuilder

The current builder instance for method chaining.

WithColors(Color, Color)

Sets both the background and foreground colors for the window.

public WindowBuilder WithColors(Color backgroundColor, Color foregroundColor)

Parameters

backgroundColor Color

The background color for the window content area.

foregroundColor Color

The foreground color for text in the window content area.

Returns

WindowBuilder

The current builder instance for method chaining.

WithDOMLayout(bool)

Enables DOM-based layout for this window. DOM layout is now always enabled and is the only rendering path.

[Obsolete("DOM layout is now always enabled. This method will be removed.")]
public WindowBuilder WithDOMLayout(bool enabled = true)

Parameters

enabled bool

This parameter is ignored. DOM layout is always enabled.

Returns

WindowBuilder

The current builder instance for method chaining.

WithForegroundColor(Color)

Sets the window foreground color used for text rendering.

public WindowBuilder WithForegroundColor(Color color)

Parameters

color Color

The foreground color for the window content area.

Returns

WindowBuilder

The current builder instance for method chaining.

WithInactiveBorderColor(Color)

Sets the border foreground color when the window is inactive.

public WindowBuilder WithInactiveBorderColor(Color color)

Parameters

color Color

The color to use for the inactive border.

Returns

WindowBuilder

The current builder instance for method chaining.

WithMaximumSize(int, int)

Sets the maximum size constraints for the window when resizing.

public WindowBuilder WithMaximumSize(int maxWidth, int maxHeight)

Parameters

maxWidth int

The maximum width in characters.

maxHeight int

The maximum height in characters.

Returns

WindowBuilder

The current builder instance for method chaining.

WithMinimumSize(int, int)

Sets the minimum size constraints for the window when resizing.

public WindowBuilder WithMinimumSize(int minWidth, int minHeight)

Parameters

minWidth int

The minimum width in characters.

minHeight int

The minimum height in characters.

Returns

WindowBuilder

The current builder instance for method chaining.

WithMode(WindowMode)

Sets the window mode (normal or modal).

public WindowBuilder WithMode(WindowMode mode)

Parameters

mode WindowMode

The window mode to apply.

Returns

WindowBuilder

The current builder instance for method chaining.

WithName(string)

Sets the window name for singleton window patterns. Windows can be found and activated by name.

public WindowBuilder WithName(string name)

Parameters

name string

The unique window name used for identification.

Returns

WindowBuilder

The current builder instance for method chaining.

WithParent(Window)

Sets the parent window for establishing a parent-child relationship. Child windows are typically positioned relative to their parent.

public WindowBuilder WithParent(Window parent)

Parameters

parent Window

The parent window instance.

Returns

WindowBuilder

The current builder instance for method chaining.

WithPosition(int, int)

Sets the window position without changing its size. Alias for AtPosition. If no size has been set, defaults to 80x25 characters.

public WindowBuilder WithPosition(int x, int y)

Parameters

x int

The X coordinate of the window's left edge.

y int

The Y coordinate of the window's top edge.

Returns

WindowBuilder

The current builder instance for method chaining.

WithShowCloseButton(bool)

Sets whether the close button [X] is shown in the title bar. This only affects visual display - use Closable() to prevent closing entirely.

public WindowBuilder WithShowCloseButton(bool show)

Parameters

show bool

True to show the close button; false to hide it.

Returns

WindowBuilder

The current builder instance for method chaining.

WithShowTitle(bool)

Sets whether the window title is shown in the title bar.

public WindowBuilder WithShowTitle(bool show)

Parameters

show bool

True to show the title, false to hide it.

Returns

WindowBuilder

The current builder instance for method chaining.

WithSize(int, int)

Sets the window size without changing its position. If no position has been set, defaults to position (0, 0).

public WindowBuilder WithSize(int width, int height)

Parameters

width int

The width of the window in characters.

height int

The height of the window in characters.

Returns

WindowBuilder

The current builder instance for method chaining.

WithState(WindowState)

Sets the initial window state (normal, minimized, or maximized).

public WindowBuilder WithState(WindowState state)

Parameters

state WindowState

The initial window state.

Returns

WindowBuilder

The current builder instance for method chaining.

WithTemplate(WindowTemplate)

Applies a preconfigured window template to set multiple properties at once. Templates encapsulate common window configurations for reuse.

public WindowBuilder WithTemplate(WindowTemplate template)

Parameters

template WindowTemplate

The window template to apply.

Returns

WindowBuilder

The current builder instance for method chaining.

WithTheme(ITheme)

Applies a theme to set the window's background and foreground colors.

public WindowBuilder WithTheme(ITheme theme)

Parameters

theme ITheme

The theme to apply. If null, no changes are made.

Returns

WindowBuilder

The current builder instance for method chaining.

WithTitle(string)

Sets the window title displayed in the title bar.

public WindowBuilder WithTitle(string title)

Parameters

title string

The title text to display.

Returns

WindowBuilder

The current builder instance for method chaining.