Table of Contents

Class ToastService

Namespace
SharpConsoleUI.Core
Assembly
SharpConsoleUI.dll

Manages transient toast notifications: showing, auto-dismissing, and stacking.

public class ToastService : INotifyPropertyChanged, IDisposable
Inheritance
ToastService
Implements
Inherited Members
Extension Methods

Remarks

The service is observable: it raises INotifyPropertyChanged for derived properties (HasToasts, ActiveCount, DefaultPosition), exposes an ObservableCollection<T> of active toasts, and fires ToastShown/ToastDismissed/AllToastsDismissed/StateChanged. Portal creation/removal and slot reflow are protected virtual seams so a derived type can wire the toasts into the live window system without coupling the core to rendering.

Constructors

ToastService(ConsoleWindowSystem, ILogService?, IToastScheduler?)

Initializes a new instance of the ToastService class.

public ToastService(ConsoleWindowSystem windowSystem, ILogService? logService = null, IToastScheduler? scheduler = null)

Parameters

windowSystem ConsoleWindowSystem

The owning window system, used to marshal callbacks to the UI thread.

logService ILogService

Optional log service for diagnostics.

scheduler IToastScheduler

Optional auto-dismiss scheduler; defaults to a TaskToastScheduler.

Properties

ActiveCount

Gets the number of currently active toasts.

public int ActiveCount { get; }

Property Value

int

ActiveToasts

Gets the observable collection of currently active toasts.

public ObservableCollection<ToastInfo> ActiveToasts { get; }

Property Value

ObservableCollection<ToastInfo>

CurrentState

Gets an immutable snapshot of the current toast state.

public ToastState CurrentState { get; }

Property Value

ToastState

DefaultPosition

Gets or sets the default position used for toasts that do not specify one.

public ToastPosition DefaultPosition { get; set; }

Property Value

ToastPosition

HasToasts

Gets a value indicating whether any toasts are currently active.

public bool HasToasts { get; }

Property Value

bool

Methods

CreatePortalFor(ToastContent, ToastInfo)

Creates the portal/window handle for a toast. Override to wire into the live system.

protected virtual object CreatePortalFor(ToastContent content, ToastInfo info)

Parameters

content ToastContent

The toast content control.

info ToastInfo

The toast descriptor.

Returns

object

An opaque handle later passed to RemovePortalFor(object).

Dismiss(string)

Dismisses the toast with the given identifier, if present.

public bool Dismiss(string id)

Parameters

id string

The identifier of the toast to dismiss.

Returns

bool

true if a toast was dismissed; false if no matching toast existed.

DismissAll()

Dismisses all active toasts at once.

public void DismissAll()

Dispose()

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

public void Dispose()

ReflowSlots()

Recomputes stacking slots after the active set changes. Called under the lock.

protected virtual void ReflowSlots()

RemovePortalFor(object)

Removes a previously-created portal/window handle.

protected virtual void RemovePortalFor(object handle)

Parameters

handle object

The handle returned by CreatePortalFor(ToastContent, ToastInfo).

Show(string, NotificationSeverity)

Shows a toast with the given message and severity using default options.

public string Show(string message, NotificationSeverity severity)

Parameters

message string

The message text to display.

severity NotificationSeverity

The severity level determining the visual style.

Returns

string

The unique identifier of the new toast.

Show(string, NotificationSeverity, ToastOptions)

Shows a toast with the given message, severity, and explicit options.

public string Show(string message, NotificationSeverity severity, ToastOptions options)

Parameters

message string

The message text to display.

severity NotificationSeverity

The severity level determining the visual style.

options ToastOptions

Per-toast options overriding service defaults.

Returns

string

The unique identifier of the new toast.

Events

AllToastsDismissed

Raised when all toasts are dismissed at once via DismissAll().

public event EventHandler? AllToastsDismissed

Event Type

EventHandler

PropertyChanged

Occurs when a property value changes.

public event PropertyChangedEventHandler? PropertyChanged

Event Type

PropertyChangedEventHandler

StateChanged

Raised whenever the toast subsystem state changes.

public event EventHandler<ToastState>? StateChanged

Event Type

EventHandler<ToastState>

ToastDismissed

Raised when an individual toast is dismissed.

public event EventHandler<ToastEventArgs>? ToastDismissed

Event Type

EventHandler<ToastEventArgs>

ToastShown

Raised when a new toast is shown.

public event EventHandler<ToastEventArgs>? ToastShown

Event Type

EventHandler<ToastEventArgs>