// -----------------------------------------------------------------------------
// Glass.Message — fluent builder for dialogs that go beyond a plain message box.
// Every method returns the same builder instance so calls can be chained, and
// Show()/ShowEx() hand the assembled configuration off to GlassMessage.
//
// File : GlassBuilder.cs
// Developer ::> Gehan Fernando
// -----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Glass;
///
/// Chainable builder for composing a dialog from optional parts — title, icon,
/// buttons, animation, input field, checkbox, progress bar, detail panel, and so
/// on. Obtain one from .
///
public sealed class GlassBuilder
{
// The message is the one required value, captured at construction; everything
// else mirrors GlassDialogConfig and starts at the same sensible defaults.
private readonly string _message;
private string _title = string.Empty;
private MessageBoxIcon _icon = MessageBoxIcon.None;
private Bitmap _customIcon;
private MessageBoxButtons _buttons = MessageBoxButtons.OK;
private MessageBoxDefaultButton _defaultButton = MessageBoxDefaultButton.Button1;
private GlassTheme _theme;
private IWin32Window _owner;
private string[] _customLabels;
private GlassAnimation _animation = GlassAnimation.Fade;
private int _autoCloseMs;
private string _checkBoxLabel;
private bool _checkBoxDefault;
private GlassInputMode _inputMode = GlassInputMode.None;
private string _inputPlaceholder;
private string[] _inputDropdownItems;
private string _inputDefault;
private bool _inputShowCapsLockHint = true;
private string _detailText;
private bool _showProgress;
private int _progressValue = -1;
private int _progressMax = 100;
private GlassProgressActivity _progressActivity = GlassProgressActivity.None;
private bool _rightToLeft;
// Nullable so "never called" stays distinct from an explicit true/false and
// can fall back to the global GlassMessage.UseRoundedCorners setting.
private bool? _roundedCorners;
// Same nullable pattern for the system-sound override (falls back to
// GlassMessage.PlaySystemSounds when never set).
private bool? _playSound;
internal GlassBuilder(string message) => _message = message ?? string.Empty;
// --- Simple single-value setters -----------------------------------------
/// Sets the title-bar caption.
public GlassBuilder Title(string title) { _title = title ?? string.Empty; return this; }
/// Uses one of the standard system icons.
public GlassBuilder Icon(MessageBoxIcon i) { _icon = i; _customIcon = null; return this; }
/// Uses a custom bitmap as the dialog icon (e.g. a product logo).
public GlassBuilder Icon(Bitmap bitmap) { _customIcon = bitmap; return this; }
/// Selects which button is focused (and is the auto-close target) by default.
public GlassBuilder Default(MessageBoxDefaultButton d) { _defaultButton = d; return this; }
/// Overrides the theme for this dialog only.
public GlassBuilder Theme(GlassTheme t) { _theme = t; return this; }
/// Sets the owner window so the dialog centres on and stays above it.
public GlassBuilder Owner(IWin32Window o) { _owner = o; return this; }
/// Chooses the open/close animation.
public GlassBuilder Animation(GlassAnimation a) { _animation = a; return this; }
/// Uses a standard button set (OK, OKCancel, YesNo, …).
public GlassBuilder Buttons(MessageBoxButtons buttons)
{
_buttons = buttons;
_customLabels = null;
return this;
}
///
/// Supplies custom button captions. At most three labels are rendered (mapped to
/// the Yes/No/Cancel positions). The count picks the layout: 1 → OK, 2 →
/// OK/Cancel, 3 → Yes/No/Cancel. Labels beyond the third are silently ignored.
///
public GlassBuilder Buttons(params string[] labels)
{
if (labels == null || labels.Length == 0)
{
return this;
}
_customLabels = labels;
_buttons = labels.Length switch
{
1 => MessageBoxButtons.OK,
2 => MessageBoxButtons.OKCancel,
_ => MessageBoxButtons.YesNoCancel,
};
return this;
}
/// Enables (or disables) Windows 11 rounded corners for this dialog.
public GlassBuilder RoundedCorners(bool enable = true)
{
_roundedCorners = enable;
return this;
}
/// Plays (or suppresses) the Windows system sound matching the icon when the dialog opens.
public GlassBuilder Sound(bool enable = true)
{
_playSound = enable;
return this;
}
/// Auto-confirms the default button after the given delay (clamped to ≥ 0).
public GlassBuilder AutoClose(int milliseconds) { _autoCloseMs = Math.Max(0, milliseconds); return this; }
/// Adds a checkbox (e.g. "Don't show this again") below the message.
public GlassBuilder CheckBox(string label, bool defaultChecked = false)
{
_checkBoxLabel = label;
_checkBoxDefault = defaultChecked;
return this;
}
/// Adds a single-line text input.
public GlassBuilder InputText(string placeholder = "", string defaultValue = "")
{
_inputMode = GlassInputMode.Text;
_inputPlaceholder = placeholder;
_inputDefault = defaultValue;
return this;
}
///
/// Adds a masked password input with a reveal-eye toggle. When
/// is true (the default), a small
/// "Caps Lock is on" badge appears beneath the field while Caps Lock is active
/// and the field is focused. Set it to false to suppress the badge.
///
public GlassBuilder InputPassword(string placeholder = "", bool showCapsLockHint = true)
{
_inputMode = GlassInputMode.Password;
_inputPlaceholder = placeholder;
_inputShowCapsLockHint = showCapsLockHint;
return this;
}
/// Adds a multi-line text input.
public GlassBuilder InputMultiline(string placeholder = "", string defaultValue = "")
{
_inputMode = GlassInputMode.Multiline;
_inputPlaceholder = placeholder;
_inputDefault = defaultValue;
return this;
}
/// Adds a drop-down list. The optional is pre-selected.
public GlassBuilder InputDropdown(IEnumerable items, string defaultItem = null)
{
_inputMode = GlassInputMode.Dropdown;
_inputDropdownItems = items != null ? [.. items] : [];
_inputDefault = defaultItem;
return this;
}
/// Adds an expandable "Show details" panel (handy for stack traces and diagnostics).
public GlassBuilder Detail(string detail) { _detailText = detail; return this; }
/// Adds an indeterminate (marquee) progress bar.
public GlassBuilder ProgressIndeterminate()
{
_showProgress = true;
_progressValue = -1;
return this;
}
/// Adds a determinate progress bar at of .
public GlassBuilder Progress(int value, int max = 100)
{
_showProgress = true;
_progressValue = value;
_progressMax = max;
return this;
}
///
/// Selects the directional flow animation painted over the progress bar so it
/// matches the operation in progress — e.g. ,
/// , or .
/// Layers on top of either a determinate () or
/// indeterminate () bar; the value is unaffected.
///
public GlassBuilder ProgressActivity(GlassProgressActivity activity)
{
_showProgress = true;
_progressActivity = activity;
return this;
}
/// Mirrors the layout for right-to-left languages.
public GlassBuilder RightToLeft(bool enable = true) { _rightToLeft = enable; return this; }
/// Shows the dialog modally and returns just the button that was pressed.
public DialogResult Show() => GlassMessage.CoreEx(_owner, BuildConfig()).Button;
/// Shows the dialog modally and returns the full (button + checkbox + input).
public GlassResult ShowEx() => GlassMessage.CoreEx(_owner, BuildConfig());
///
/// Shows the dialog without blocking the UI thread and returns just the button
/// that was pressed. The optional closes the
/// dialog (yielding ) if cancelled while open.
///
public async Task ShowAsync(CancellationToken cancellationToken = default)
=> (await GlassMessage.CoreExAsync(_owner, BuildConfig(), cancellationToken).ConfigureAwait(true)).Button;
///
/// Shows the dialog without blocking the UI thread and returns the full
/// (button + checkbox + input) once it closes.
///
public Task ShowExAsync(CancellationToken cancellationToken = default)
=> GlassMessage.CoreExAsync(_owner, BuildConfig(), cancellationToken);
///
/// Shows a non-blocking progress dialog and returns a
/// to update and close it while work runs.
/// Use with for a determinate bar (the controller
/// updates the value) or for a marquee.
///
public GlassProgressController ShowProgress()
{
if (!_showProgress)
{
// Default to a determinate bar starting at 0 so the controller has
// something to drive even if the caller forgot to call Progress().
_showProgress = true;
_progressValue = 0;
}
return GlassMessage.CoreProgress(_owner, BuildConfig());
}
// Snapshots the accumulated fields into the config the dialog consumes.
private GlassDialogConfig BuildConfig() => new()
{
Message = _message,
Title = _title,
Icon = _icon,
CustomIcon = _customIcon,
Buttons = _buttons,
DefaultButton = _defaultButton,
Theme = _theme ?? GlassMessage.DefaultTheme ?? GlassTheme.Default,
CustomLabels = _customLabels,
Animation = _animation,
AutoCloseMs = _autoCloseMs,
CheckBoxLabel = _checkBoxLabel,
CheckBoxDefault = _checkBoxDefault,
InputMode = _inputMode,
InputPlaceholder = _inputPlaceholder,
InputDropdownItems = _inputDropdownItems,
InputDefault = _inputDefault,
InputShowCapsLockHint = _inputShowCapsLockHint,
DetailText = _detailText,
ShowProgress = _showProgress,
ProgressValue = _progressValue,
ProgressMax = _progressMax,
ProgressActivity = _progressActivity,
RightToLeft = _rightToLeft,
UseRoundedCorners = _roundedCorners,
PlaySound = _playSound,
};
}