Singleton UI state for a confirmation dialog. Stores title, body, labels, open state and a confirm callback, exposes Show/Confirm/Cancel methods and optional sound action hooks.
namespace Sunless.Libraries.UI;
using System;
using Sandbox;
// Singleton confirm-dialog state. Host `ConfirmPromptOverlay.razor` once in
// your top-level HUD and call `ConfirmPrompt.Show(...)` from anywhere.
//
// Sound hooks are optional Actions — set them at startup if you want feedback,
// leave them null for a silent prompt.
public static class ConfirmPrompt
{
public static string Title { get; private set; }
public static string Body { get; private set; }
public static string ConfirmLabel { get; private set; }
public static string CancelLabel { get; private set; } = "#common.cancel";
public static bool Open { get; private set; }
public static int Version { get; private set; }
static Action _onConfirm;
// Optional sound hooks. Hosting project assigns these once at boot:
// ConfirmPrompt.OnOpenSound = () => StudioSounds.PromptOpen();
// ConfirmPrompt.OnConfirmSound = () => StudioSounds.PromptConfirm();
// ConfirmPrompt.OnCancelSound = () => StudioSounds.PromptCancel();
public static Action OnOpenSound { get; set; }
public static Action OnConfirmSound { get; set; }
public static Action OnCancelSound { get; set; }
public static void Show( string title, string body, string confirmLabel, Action onConfirm, string cancelLabel = null )
{
Title = title ?? "";
Body = body ?? "";
ConfirmLabel = string.IsNullOrWhiteSpace( confirmLabel ) ? "#common.confirm" : confirmLabel;
CancelLabel = string.IsNullOrWhiteSpace( cancelLabel ) ? "#common.cancel" : cancelLabel;
_onConfirm = onConfirm;
Open = true;
Version++;
try { OnOpenSound?.Invoke(); } catch { }
}
public static void Confirm()
{
var cb = _onConfirm;
Open = false;
_onConfirm = null;
Version++;
try { OnConfirmSound?.Invoke(); } catch { }
try { cb?.Invoke(); }
catch ( Exception e ) { Log.Warning( $"[ConfirmPrompt] confirm threw: {e.Message}" ); }
}
public static void Cancel()
{
if ( !Open ) return;
Open = false;
_onConfirm = null;
Version++;
try { OnCancelSound?.Invoke(); } catch { }
}
}