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
windowSystemConsoleWindowSystemThe owning window system, used to marshal callbacks to the UI thread.
logServiceILogServiceOptional log service for diagnostics.
schedulerIToastSchedulerOptional auto-dismiss scheduler; defaults to a TaskToastScheduler.
Properties
ActiveCount
Gets the number of currently active toasts.
public int ActiveCount { get; }
Property Value
ActiveToasts
Gets the observable collection of currently active toasts.
public ObservableCollection<ToastInfo> ActiveToasts { get; }
Property Value
CurrentState
Gets an immutable snapshot of the current toast state.
public ToastState CurrentState { get; }
Property Value
DefaultPosition
Gets or sets the default position used for toasts that do not specify one.
public ToastPosition DefaultPosition { get; set; }
Property Value
HasToasts
Gets a value indicating whether any toasts are currently active.
public bool HasToasts { get; }
Property Value
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
contentToastContentThe toast content control.
infoToastInfoThe 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
idstringThe identifier of the toast to dismiss.
Returns
- bool
trueif a toast was dismissed;falseif 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
handleobjectThe 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
messagestringThe message text to display.
severityNotificationSeverityThe 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
messagestringThe message text to display.
severityNotificationSeverityThe severity level determining the visual style.
optionsToastOptionsPer-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
PropertyChanged
Occurs when a property value changes.
public event PropertyChangedEventHandler? PropertyChanged
Event Type
StateChanged
Raised whenever the toast subsystem state changes.
public event EventHandler<ToastState>? StateChanged
Event Type
ToastDismissed
Raised when an individual toast is dismissed.
public event EventHandler<ToastEventArgs>? ToastDismissed
Event Type
ToastShown
Raised when a new toast is shown.
public event EventHandler<ToastEventArgs>? ToastShown