namespace Khaemhat.Web.Shell; /// The weight of a toast. public enum ToastKind { /// Information. Info = 0, /// Done. Success = 1, /// Attention. Warning = 2, /// Failure. Error = 3, } /// A short message in the bottom right corner (SPEC §10.7 KhToast). /// The identifier. /// The weight. /// The title. /// The text, or . public sealed record Toast(Guid Id, ToastKind Kind, string Title, string? Text); /// Shows toasts; KhToastHost in the layout renders them and removes them after a few seconds. public sealed class ToastService { private readonly List _toasts = []; /// Raised when toasts appear or go. public event Action? Changed; /// The toasts on screen. public IReadOnlyList Toasts => _toasts; /// Shows a toast. /// The weight. /// The title. /// The text. /// The toast. public Toast Show(ToastKind kind, string title, string? text = null) { var toast = new Toast(Guid.NewGuid(), kind, title, text); _toasts.Add(toast); if (_toasts.Count > 5) { _toasts.RemoveAt(0); } Changed?.Invoke(); return toast; } /// Removes a toast. /// The toast. public void Dismiss(Guid id) { if (_toasts.RemoveAll(t => t.Id == id) > 0) { Changed?.Invoke(); } } } /// A question that needs a yes or no (SPEC §10.7 KhConfirm). /// The title. /// The question. /// The text of the confirming button. /// The text of the cancelling button. /// Whether confirming destroys something (red button). public sealed record ConfirmRequest(string Title, string Text, string ConfirmText, string CancelText, bool Danger = false); /// Asks confirmations; KhDialogHost in the layout shows them one at a time. public sealed class DialogService { private TaskCompletionSource? _pending; /// Raised when a question appears or is answered. public event Action? Changed; /// The question on screen, or . public ConfirmRequest? Current { get; private set; } /// Asks a question and waits for the answer. /// The question. /// when the user confirms. public Task ConfirmAsync(ConfirmRequest request) { ArgumentNullException.ThrowIfNull(request); _pending?.TrySetResult(false); _pending = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); Current = request; Changed?.Invoke(); return _pending.Task; } /// Called by the dialog with the answer. /// The answer. public void Answer(bool confirmed) { var pending = _pending; _pending = null; Current = null; Changed?.Invoke(); pending?.TrySetResult(confirmed); } } /// A side panel opened from code (SPEC §10.7 DrawerService). /// The title. /// The icon. /// The content. /// The buttons, or . /// A wider panel. public sealed record DrawerRequest(string Title, string? Icon, Microsoft.AspNetCore.Components.RenderFragment Content, Microsoft.AspNetCore.Components.RenderFragment? Footer = null, bool Wide = false); /// /// Opens one side panel at a time from code; KhDrawerHost in the layout shows it. A page that owns its panel /// can also write <KhDrawer Open="..."> directly. /// public sealed class DrawerService { private TaskCompletionSource? _closed; /// Raised when the panel opens, closes or must be drawn again. public event Action? Changed; /// The panel shown, or . public DrawerRequest? Current { get; private set; } /// Shows a panel, closing the previous one. /// The panel. /// A task that ends when the panel is closed. public Task ShowAsync(DrawerRequest request) { ArgumentNullException.ThrowIfNull(request); _closed?.TrySetResult(); _closed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); Current = request; Changed?.Invoke(); return _closed.Task; } /// Closes the panel. public void Close() { var closed = _closed; _closed = null; Current = null; Changed?.Invoke(); closed?.TrySetResult(); } /// Draws the panel again after its data changed. public void Refresh() => Changed?.Invoke(); }