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
logServiceILogServiceOptional log service for diagnostic logging.
getWindowSystemFunc<ConsoleWindowSystem>Optional function to get window system context (for lifecycle methods).
modalStateServiceModalStateServiceOptional modal state service (for lifecycle methods).
focusStateServiceFocusStateServiceOptional focus state service (for lifecycle methods).
rendererRendererOptional renderer (for lifecycle methods).
consoleDriverIConsoleDriverOptional console driver (for lifecycle methods).
Properties
ActiveWindow
Gets the currently active window.
public Window? ActiveWindow { get; }
Property Value
CurrentDrag
Gets the current drag state (if dragging).
public DragState? CurrentDrag { get; }
Property Value
CurrentResize
Gets the current resize state (if resizing).
public ResizeState? CurrentResize { get; }
Property Value
CurrentState
Gets the current window system state.
public WindowSystemState CurrentState { get; }
Property Value
IsDragging
Gets a value indicating whether a drag operation is in progress.
public bool IsDragging { get; }
Property Value
IsResizing
Gets a value indicating whether a resize operation is in progress.
public bool IsResizing { get; }
Property Value
WindowCount
Gets the number of registered windows.
public int WindowCount { get; }
Property Value
Windows
Gets all registered windows.
public IReadOnlyDictionary<string, Window> Windows { get; }
Property Value
Methods
ActivateNextNonMinimizedWindow(Window)
Activates the next non-minimized window after a window is minimized.
public void ActivateNextNonMinimizedWindow(Window minimizedWindow)
Parameters
minimizedWindowWindowThe 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
namestringThe window name to find/create
factoryFunc<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
guidstringThe window GUID to find/create
factoryFunc<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
windowWindowThe 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
indexintThe 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
windowWindowThe window to add.
activateWindowboolWhether 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
windowWindowThe 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
modalWindowWindowThe 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
windowWindowThe window to close. If null or not in the system, returns false.
activateParentboolWhether to activate the parent window after closing. Defaults to true.
forceboolIf 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
namestringThe 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
windowWindowThe window to flash. If null, the method returns without action.
flashCountintThe number of times to flash. Defaults to 1.
flashDurationintThe duration of each flash in milliseconds. Defaults to 150.
flashBackgroundColorColor?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
guidstringThe 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
windowWindowThe window to maximize.
MinimizeWindow(Window)
Minimizes a window.
public void MinimizeWindow(Window window)
Parameters
windowWindowThe window to minimize.
RegisterWindow(Window, bool)
Registers a new window with the system.
public void RegisterWindow(Window window, bool activate = true)
Parameters
windowWindowThe window to register.
activateboolWhether to activate the window after registration.
Exceptions
- ArgumentNullException
Thrown when
windowis null.
RestoreWindow(Window)
Restores a window to normal state.
public void RestoreWindow(Window window)
Parameters
windowWindowThe 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
windowWindowThe 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
windowWindowThe 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
rendererRenderer
StartDrag(Window, Point)
Starts a drag operation.
public void StartDrag(Window window, Point mousePos)
Parameters
StartResize(Window, ResizeDirection, Point)
Starts a resize operation.
public void StartResize(Window window, ResizeDirection direction, Point mousePos)
Parameters
windowWindowThe window being resized.
directionResizeDirectionThe resize direction.
mousePosPointThe initial mouse position.
TryActivate(string)
Activates an existing window by name if found, otherwise returns null.
public Window? TryActivate(string name)
Parameters
namestringThe 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
guidstringThe 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
windowWindowThe window to unregister.
WindowExists(string)
Checks if a window with the given name exists.
public bool WindowExists(string name)
Parameters
namestringThe 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
StateChanged
Occurs when any aspect of window system state changes.
public event EventHandler<WindowSystemStateChangedEventArgs>? StateChanged
Event Type
WindowActivated
Occurs when the active window changes.
public event EventHandler<WindowActivatedEventArgs>? WindowActivated
Event Type
WindowClosed
Occurs when a window is closed/unregistered.
public event EventHandler<WindowEventArgs>? WindowClosed
Event Type
WindowCreated
Occurs when a window is created/registered.
public event EventHandler<WindowEventArgs>? WindowCreated
Event Type
WindowStateChanged
Occurs when a window's state (min/max/restore) changes.
public event EventHandler<WindowStateEventArgs>? WindowStateChanged