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
logServiceILogServiceOptional log service for diagnostic logging.
Properties
CurrentState
Gets the current focus state.
public FocusState CurrentState { get; }
Property Value
FocusStackDepth
Gets the depth of the focus stack.
public int FocusStackDepth { get; }
Property Value
FocusedControl
Gets the currently focused control.
public IInteractiveControl? FocusedControl { get; }
Property Value
FocusedWindow
Gets the currently focused window.
public Window? FocusedWindow { get; }
Property Value
HasFocus
Gets a value indicating whether any control has focus.
public bool HasFocus { get; }
Property Value
Methods
ClearControlFocus(FocusChangeReason)
Clears control focus within the current window but keeps window focus.
public void ClearControlFocus(FocusChangeReason reason = FocusChangeReason.Programmatic)
Parameters
reasonFocusChangeReasonThe reason for clearing control focus.
ClearFocus(FocusChangeReason)
Clears all focus (no window or control has focus).
public void ClearFocus(FocusChangeReason reason = FocusChangeReason.Programmatic)
Parameters
reasonFocusChangeReasonThe 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
windowWindowThe window to clear focus for.
reasonFocusChangeReasonThe 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
controlIInteractiveControlThe 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
windowWindowThe 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
reasonFocusChangeReasonThe 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
windowWindowThe window to focus.
controlIInteractiveControlThe control to focus (optional).
reasonFocusChangeReasonThe 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
windowWindowThe window containing the control.
controlIInteractiveControlThe control to focus (null to clear control focus).
reasonFocusChangeReasonThe reason for the focus change.
Exceptions
- ArgumentNullException
Thrown when
windowis null.
SetWindowFocus(Window, FocusChangeReason)
Sets focus to a window without specifying a control.
public void SetWindowFocus(Window window, FocusChangeReason reason = FocusChangeReason.Programmatic)
Parameters
windowWindowThe window to focus.
reasonFocusChangeReasonThe reason for the focus change.
Exceptions
- ArgumentNullException
Thrown when
windowis null.
Events
ControlBlurred
Occurs when a control loses focus.
public event EventHandler<ControlFocusEventArgs>? ControlBlurred
Event Type
ControlFocused
Occurs when a control gains focus.
public event EventHandler<ControlFocusEventArgs>? ControlFocused
Event Type
StateChanged
Occurs when any aspect of focus state changes.
public event EventHandler<FocusStateChangedEventArgs>? StateChanged
Event Type
WindowFocusChanged
Occurs when the focused window changes.
public event EventHandler<FocusStateChangedEventArgs>? WindowFocusChanged