Table of Contents

Class FocusStateService

Namespace
SharpConsoleUI.Core
Assembly
SharpConsoleUI.dll

Centralized service for managing focus state across the window system. Provides a single source of truth for which window and control has focus.

public class FocusStateService : IDisposable
Inheritance
FocusStateService
Implements
Inherited Members

Constructors

FocusStateService(ILogService?)

Initializes a new instance of the FocusStateService class.

public FocusStateService(ILogService? logService = null)

Parameters

logService ILogService

Optional log service for diagnostic logging.

Properties

CurrentState

Gets the current focus state.

public FocusState CurrentState { get; }

Property Value

FocusState

FocusStackDepth

Gets the depth of the focus stack.

public int FocusStackDepth { get; }

Property Value

int

FocusedControl

Gets the currently focused control.

public IInteractiveControl? FocusedControl { get; }

Property Value

IInteractiveControl

FocusedWindow

Gets the currently focused window.

public Window? FocusedWindow { get; }

Property Value

Window

HasFocus

Gets a value indicating whether any control has focus.

public bool HasFocus { get; }

Property Value

bool

Methods

ClearControlFocus(FocusChangeReason)

Clears control focus within the current window but keeps window focus.

public void ClearControlFocus(FocusChangeReason reason = FocusChangeReason.Programmatic)

Parameters

reason FocusChangeReason

The reason for clearing control focus.

ClearFocus(FocusChangeReason)

Clears all focus (no window or control has focus).

public void ClearFocus(FocusChangeReason reason = FocusChangeReason.Programmatic)

Parameters

reason FocusChangeReason

The reason for clearing focus.

ClearFocus(Window, FocusChangeReason)

Clears focus for a specific window (only if that window currently has focus).

public void ClearFocus(Window window, FocusChangeReason reason = FocusChangeReason.Programmatic)

Parameters

window Window

The window to clear focus for.

reason FocusChangeReason

The reason for clearing focus.

ClearHistory()

Clears the state history.

public void ClearHistory()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetDebugInfo()

Gets a debug string representation of current focus state.

public string GetDebugInfo()

Returns

string

A formatted string containing the current focus state information.

GetHistory()

Gets recent focus state history for debugging.

public IReadOnlyList<FocusState> GetHistory()

Returns

IReadOnlyList<FocusState>

A read-only list of recent focus states.

HasControlFocus(IInteractiveControl)

Checks if a specific control currently has focus.

public bool HasControlFocus(IInteractiveControl control)

Parameters

control IInteractiveControl

The control to check.

Returns

bool

True if the control has focus; otherwise, false.

HasWindowFocus(Window)

Checks if a specific window currently has focus.

public bool HasWindowFocus(Window window)

Parameters

window Window

The window to check.

Returns

bool

True if the window has focus; otherwise, false.

PopFocus(FocusChangeReason)

Pops focus from the stack and restores it.

public void PopFocus(FocusChangeReason reason = FocusChangeReason.Programmatic)

Parameters

reason FocusChangeReason

The reason for the focus change.

PushFocus(Window, IInteractiveControl?, FocusChangeReason)

Pushes the current focus state onto the stack and sets new focus.

public void PushFocus(Window window, IInteractiveControl? control, FocusChangeReason reason = FocusChangeReason.Programmatic)

Parameters

window Window

The window to focus.

control IInteractiveControl

The control to focus (optional).

reason FocusChangeReason

The reason for the focus change.

SetFocus(Window, IInteractiveControl?, FocusChangeReason)

Sets focus to a specific control within a window.

public void SetFocus(Window window, IInteractiveControl? control, FocusChangeReason reason = FocusChangeReason.Programmatic)

Parameters

window Window

The window containing the control.

control IInteractiveControl

The control to focus (null to clear control focus).

reason FocusChangeReason

The reason for the focus change.

Exceptions

ArgumentNullException

Thrown when window is null.

SetWindowFocus(Window, FocusChangeReason)

Sets focus to a window without specifying a control.

public void SetWindowFocus(Window window, FocusChangeReason reason = FocusChangeReason.Programmatic)

Parameters

window Window

The window to focus.

reason FocusChangeReason

The reason for the focus change.

Exceptions

ArgumentNullException

Thrown when window is null.

Events

ControlBlurred

Occurs when a control loses focus.

public event EventHandler<ControlFocusEventArgs>? ControlBlurred

Event Type

EventHandler<ControlFocusEventArgs>

ControlFocused

Occurs when a control gains focus.

public event EventHandler<ControlFocusEventArgs>? ControlFocused

Event Type

EventHandler<ControlFocusEventArgs>

StateChanged

Occurs when any aspect of focus state changes.

public event EventHandler<FocusStateChangedEventArgs>? StateChanged

Event Type

EventHandler<FocusStateChangedEventArgs>

WindowFocusChanged

Occurs when the focused window changes.

public event EventHandler<FocusStateChangedEventArgs>? WindowFocusChanged

Event Type

EventHandler<FocusStateChangedEventArgs>