Table of Contents

Class WindowStateService

Namespace
SharpConsoleUI.Core
Assembly
SharpConsoleUI.dll

Centralized service for managing window system state. Provides a single source of truth for window collection, active window, and interaction state (drag/resize operations).

public class WindowStateService : IDisposable
Inheritance
WindowStateService
Implements
Inherited Members

Constructors

WindowStateService(ILogService?, Func<ConsoleWindowSystem>?, ModalStateService?, FocusStateService?, Renderer?, IConsoleDriver?)

Initializes a new instance of the WindowStateService class.

public WindowStateService(ILogService? logService = null, Func<ConsoleWindowSystem>? getWindowSystem = null, ModalStateService? modalStateService = null, FocusStateService? focusStateService = null, Renderer? renderer = null, IConsoleDriver? consoleDriver = null)

Parameters

logService ILogService

Optional log service for diagnostic logging.

getWindowSystem Func<ConsoleWindowSystem>

Optional function to get window system context (for lifecycle methods).

modalStateService ModalStateService

Optional modal state service (for lifecycle methods).

focusStateService FocusStateService

Optional focus state service (for lifecycle methods).

renderer Renderer

Optional renderer (for lifecycle methods).

consoleDriver IConsoleDriver

Optional console driver (for lifecycle methods).

Properties

ActiveWindow

Gets the currently active window.

public Window? ActiveWindow { get; }

Property Value

Window

CurrentDrag

Gets the current drag state (if dragging).

public DragState? CurrentDrag { get; }

Property Value

DragState

CurrentResize

Gets the current resize state (if resizing).

public ResizeState? CurrentResize { get; }

Property Value

ResizeState

CurrentState

Gets the current window system state.

public WindowSystemState CurrentState { get; }

Property Value

WindowSystemState

IsDragging

Gets a value indicating whether a drag operation is in progress.

public bool IsDragging { get; }

Property Value

bool

IsResizing

Gets a value indicating whether a resize operation is in progress.

public bool IsResizing { get; }

Property Value

bool

WindowCount

Gets the number of registered windows.

public int WindowCount { get; }

Property Value

int

Windows

Gets all registered windows.

public IReadOnlyDictionary<string, Window> Windows { get; }

Property Value

IReadOnlyDictionary<string, Window>

Methods

ActivateNextNonMinimizedWindow(Window)

Activates the next non-minimized window after a window is minimized.

public void ActivateNextNonMinimizedWindow(Window minimizedWindow)

Parameters

minimizedWindow Window

The window that was just minimized.

ActivateNextWindow()

Activates the next window in Z-order.

public void ActivateNextWindow()

ActivateOrCreate(string, Func<Window>)

Activates an existing window by name, or creates it using the factory if not found.

public Window ActivateOrCreate(string name, Func<Window> factory)

Parameters

name string

The window name to find/create

factory Func<Window>

Factory function to create the window if it doesn't exist

Returns

Window

The activated or newly created window

ActivateOrCreateByGuid(string, Func<Window>)

Activates an existing window by GUID, or creates it using the factory if not found.

public Window ActivateOrCreateByGuid(string guid, Func<Window> factory)

Parameters

guid string

The window GUID to find/create

factory Func<Window>

Factory function to create the window if it doesn't exist

Returns

Window

The activated or newly created window

ActivatePreviousWindow()

Activates the previous window in Z-order.

public void ActivatePreviousWindow()

ActivateWindow(Window?)

Activates a window, making it the active window.

public void ActivateWindow(Window? window)

Parameters

window Window

The window to activate, or null to deactivate all windows.

ActivateWindowByIndex(int)

Activates a window by its index (1-based, for Alt+1-9).

public void ActivateWindowByIndex(int index)

Parameters

index int

The 1-based index of the window to activate.

AddWindow(Window, bool)

Adds a window to the window system and optionally activates it.

public Window AddWindow(Window window, bool activateWindow = true)

Parameters

window Window

The window to add.

activateWindow bool

Whether to activate the window after adding. Defaults to true.

Returns

Window

The added window.

AnyWindowDirty()

Checks if any window is dirty (needs re-rendering).

public bool AnyWindowDirty()

Returns

bool

True if any window needs re-rendering; otherwise, false.

BringToFront(Window)

Brings a window to the front (highest Z-index) and activates it. This matches typical windowing system behavior where bringing a window to front makes it active.

public void BringToFront(Window window)

Parameters

window Window

The window to bring to front.

ClearHistory()

Clears the state history.

public void ClearHistory()

CloseModalWindow(Window?)

Closes a modal window and optionally activates its parent window.

public void CloseModalWindow(Window? modalWindow)

Parameters

modalWindow Window

The modal window to close. If null or not a modal window, the method returns without action.

CloseWindow(Window?, bool, bool)

Closes a window and removes it from the window system.

public bool CloseWindow(Window? window, bool activateParent = true, bool force = false)

Parameters

window Window

The window to close. If null or not in the system, returns false.

activateParent bool

Whether to activate the parent window after closing. Defaults to true.

force bool

If true, forces the window to close even if IsClosable is false or OnClosing cancels.

Returns

bool

True if the window was closed successfully; false otherwise.

CycleActiveWindow()

Cycles to the next active window (Ctrl+T handler).

public void CycleActiveWindow()

DeactivateCurrentWindow()

Deactivates the current active window (e.g., when clicking on empty desktop).

public void DeactivateCurrentWindow()

Dispose()

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

public void Dispose()

EndDrag()

Ends the current drag operation.

public void EndDrag()

EndResize()

Ends the current resize operation.

public void EndResize()

FindWindowByName(string)

Finds a window by its Name property.

public Window? FindWindowByName(string name)

Parameters

name string

The window name to search for.

Returns

Window

The window if found; otherwise, null.

FlashWindow(Window?, int, int, Color?)

Flashes a window to draw user attention by briefly changing its background color.

public void FlashWindow(Window? window, int flashCount = 1, int flashDuration = 150, Color? flashBackgroundColor = null)

Parameters

window Window

The window to flash. If null, the method returns without action.

flashCount int

The number of times to flash. Defaults to 1.

flashDuration int

The duration of each flash in milliseconds. Defaults to 150.

flashBackgroundColor Color?

The background color to use for flashing. If null, uses the theme's ModalFlashColor.

GetDebugInfo()

Gets a debug string representation of current state.

public string GetDebugInfo()

Returns

string

A formatted string containing the current state information.

GetHistory()

Gets recent state history for debugging.

public IReadOnlyList<WindowSystemState> GetHistory()

Returns

IReadOnlyList<WindowSystemState>

A read-only list of recent window system states.

GetMaxZIndex()

Gets the maximum Z-index among all windows.

public int GetMaxZIndex()

Returns

int

The maximum Z-index, or 0 if no windows exist.

GetVisibleWindows()

Gets only visible windows (excludes minimized).

public IReadOnlyList<Window> GetVisibleWindows()

Returns

IReadOnlyList<Window>

A read-only list of visible windows ordered by Z-index.

GetWindow(string)

Gets a window by its GUID.

public Window? GetWindow(string guid)

Parameters

guid string

The GUID of the window to find.

Returns

Window

The window if found; otherwise, null.

GetWindowsByZOrder()

Gets all windows ordered by Z-index (back to front).

public IReadOnlyList<Window> GetWindowsByZOrder()

Returns

IReadOnlyList<Window>

A read-only list of windows ordered by Z-index.

MaximizeWindow(Window)

Maximizes a window.

public void MaximizeWindow(Window window)

Parameters

window Window

The window to maximize.

MinimizeWindow(Window)

Minimizes a window.

public void MinimizeWindow(Window window)

Parameters

window Window

The window to minimize.

RegisterWindow(Window, bool)

Registers a new window with the system.

public void RegisterWindow(Window window, bool activate = true)

Parameters

window Window

The window to register.

activate bool

Whether to activate the window after registration.

Exceptions

ArgumentNullException

Thrown when window is null.

RestoreWindow(Window)

Restores a window to normal state.

public void RestoreWindow(Window window)

Parameters

window Window

The window to restore.

SendToBack(Window)

Sends a window to the back (lowest Z-index). If the window was active, activates the next available window.

public void SendToBack(Window window)

Parameters

window Window

The window to send to back.

SetActiveWindow(Window)

Sets the specified window as the active window, handling modal window logic and focus.

public void SetActiveWindow(Window window)

Parameters

window Window

The window to activate. If null, the method returns without action.

SetRenderer(Renderer)

Sets the renderer for this service. Used for screen redraws during window close.

public void SetRenderer(Renderer renderer)

Parameters

renderer Renderer

StartDrag(Window, Point)

Starts a drag operation.

public void StartDrag(Window window, Point mousePos)

Parameters

window Window

The window being dragged.

mousePos Point

The initial mouse position.

StartResize(Window, ResizeDirection, Point)

Starts a resize operation.

public void StartResize(Window window, ResizeDirection direction, Point mousePos)

Parameters

window Window

The window being resized.

direction ResizeDirection

The resize direction.

mousePos Point

The initial mouse position.

TryActivate(string)

Activates an existing window by name if found, otherwise returns null.

public Window? TryActivate(string name)

Parameters

name string

The window name to find

Returns

Window

The activated window, or null if not found

TryActivateByGuid(string)

Activates an existing window by GUID if found, otherwise returns null.

public Window? TryActivateByGuid(string guid)

Parameters

guid string

The window GUID to find

Returns

Window

The activated window, or null if not found

UnregisterWindow(Window)

Unregisters a window from the system.

public void UnregisterWindow(Window window)

Parameters

window Window

The window to unregister.

WindowExists(string)

Checks if a window with the given name exists.

public bool WindowExists(string name)

Parameters

name string

The window name to check.

Returns

bool

True if a window with the given name exists; otherwise, false.

Events

InteractionChanged

Occurs when interaction state (drag/resize) changes.

public event EventHandler<InteractionStateChangedEventArgs>? InteractionChanged

Event Type

EventHandler<InteractionStateChangedEventArgs>

StateChanged

Occurs when any aspect of window system state changes.

public event EventHandler<WindowSystemStateChangedEventArgs>? StateChanged

Event Type

EventHandler<WindowSystemStateChangedEventArgs>

WindowActivated

Occurs when the active window changes.

public event EventHandler<WindowActivatedEventArgs>? WindowActivated

Event Type

EventHandler<WindowActivatedEventArgs>

WindowClosed

Occurs when a window is closed/unregistered.

public event EventHandler<WindowEventArgs>? WindowClosed

Event Type

EventHandler<WindowEventArgs>

WindowCreated

Occurs when a window is created/registered.

public event EventHandler<WindowEventArgs>? WindowCreated

Event Type

EventHandler<WindowEventArgs>

WindowStateChanged

Occurs when a window's state (min/max/restore) changes.

public event EventHandler<WindowStateEventArgs>? WindowStateChanged

Event Type

EventHandler<WindowStateEventArgs>