MessageBox
A MessageBox is a modal dialog with a title, an icon, a rich text message and buttons. ShowAsync returns the button the user chose:
var result = await MessageBox.ShowAsync(this, "Delete the layout?", "Confirm",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == MessageBoxResult.Yes) DeleteLayout();
Build a box yourself for custom buttons:
var box = new MessageBox { Title = "File exists", Message = "<b>report.txt</b> exists.", Icon = MessageBoxIcon.Warning };
box.AddButton("Overwrite", MessageBoxResult.Yes);
box.DefaultButton = box.AddButton("Keep both", MessageBoxResult.No);
box.CancelResult = MessageBoxResult.Cancel;
var result = await box.ShowAsync(this);
A custom button has an id. A timeout closes the box when the user does not answer:
var box = new MessageBox { Message = "Join the match?", Timeout = 10, TimeoutResult = MessageBoxResult.No };
box.AddButton("Join", "join");
box.AddButton("Watch", "watch");
box.AddButton("No", MessageBoxResult.No);
if (await box.ShowAsync(this) == MessageBoxResult.Custom) Debug.Log(box.CustomResult); // "join" or "watch"
Ask before an action that deletes something. The Delete button shows in the danger color:
if (await MessageBox.ConfirmAsync(this, "Delete the save?", "<b>Slot 3</b> goes for good.", "Delete",
MessageBoxIcon.Warning, destructive: true))
DeleteSave();
| Member | Description |
|---|---|
MessageBox.ShowAsync(anchor, message, title, buttons, icon) |
Shows a box with a standard button set: OK, OKCancel, YesNo, YesNoCancel, RetryCancel or AbortRetryIgnore. |
ShowAsync(anchor) |
Shows the box over the workspace of anchor, or over its document. The task completes when the box closes. |
Title, Message |
The title (empty hides the title bar) and the rich text message. |
Icon |
None, Info, Warning, Error or Question. |
SetButtons(buttons) |
Replaces the buttons with a standard set and sets CancelResult. |
MessageBox.ConfirmAsync(anchor, title, message, action, icon, destructive) |
Asks before an action that loses or spends something. The box has an action button and a Cancel button. Cancel has the focus, so a quick second submit does not do the action, and Escape is Cancel. The task is true only for the action button. With destructive true, the action button has the danger color. |
Destructive (destructive) |
Shows the first button, the one that confirms, in the danger color. Use it for an action that deletes something. |
AddButton(text, result) |
Adds a button. The first button is the default. |
AddButton(text, id) |
Adds a custom button. It closes the box with MessageBoxResult.Custom, and CustomResult has its id. |
CustomResult |
The id of the custom button that closed the box, or null. A show sets it to null. |
Timeout (timeout) |
Seconds until the box closes with TimeoutResult. 0 (the default) waits for the user. While the time runs, the button of that result shows the seconds left, such as "No (15)". The button does not get narrower when the number gets shorter. |
TimeoutResult, SecondsLeft |
The result when the time ends (CancelResult when not set), and the whole seconds left. |
DefaultButton |
The button that has focus when the box shows. |
CancelResult |
The result of Escape: Cancel, No for YesNo, OK for OK, or Abort for AbortRetryIgnore. |
Close(result) |
Closes the box from code. |
IsOpen, Closed |
The state, and an event raised with the result. |
Buttons |
The standard button set. Setting it calls SetButtons. A box with no buttons gets this set when it shows. |
Modal |
True (the default): the overlay dims the workspace and takes every pointer press. False: the overlay is clear and lets the pointer through, and the user drags the dialog by its body. |
Glass |
The box is frosted glass: it blurs the windows behind it and has the glass tint of the theme (--tb-color-glass-dialog). False by default. |
GlassOverlay |
The overlay is frosted glass: it blurs the whole workspace behind the box. Use it alone, or with Glass for a frosted box too. Only a Modal box has an overlay to frost. False by default. |
DragOffset |
How far the user dragged the dialog. It resets each time the box shows. |
Sound |
An AudioClip that plays when the box shows, through ToolboxAudio.PlayClip. With no Sound, the box plays the notification sound of the panel's WorkspaceAudio, or error for the Error icon. See Sounds and window events. |
- A blocking box takes every pointer press. Keys stop at the box, and the workspace shortcuts do nothing while it is open.
- A non-modal box stops only Escape, which closes it. The workspace keeps its shortcuts and keyboard navigation. A press on a button does not start a drag. The dialog stays inside the workspace.
- Tab and the arrow keys move between the buttons. Enter or Space chooses the focused button.
- If other code removes the box, the task completes with
MessageBoxResult.None. - The box fades and scales in and out when the workspace has
AnimationsEnabled. The task completes whenCloseruns, before the exit animation ends. AShowAsyncduring the exit keeps the box. - While the box shows, it takes the classes of the nearest
tb-style-scope(AnchoredPopup.STYLE_SCOPE_CLASS) above its UXML place, or above the anchor. So a box in a styled part of the UI keeps that style. See Style families and style scopes. - A box declared in UXML stays hidden until
ShowAsync. It moves to the workspace while it shows, then goes back to its place:
<tb:MessageBox name="declaredBox" title="Declared box" message="Save the layout?"
icon="Question" buttons="YesNo" blocking="true" />
var result = await root.Q<MessageBox>("declaredBox").ShowAsync(this);
- UXML child buttons are not read. Use
buttons, orAddButtonin code. A blocking box does not drag. - When the box closes, the focus goes back to the control that had it before the show. See Custom overlays (OverlayElement).
Styling
The rules are in Assets/UIToolbox/Runtime/Resources/UIToolbox/Styles/Controls.uss.
| Class | Element |
|---|---|
tb-message-box |
The overlay. It uses --tb-color-overlay. Modifiers: --none, --info, --warning, --error, --question. |
tb-message-box__dialog |
The dialog panel. |
tb-message-box__title, __body, __icon, __message, __buttons |
Parts of the dialog. |
tb-message-box__button |
A button. tb-message-box__button--default marks the default button, which also has tb-button--primary. The first button of a Destructive box has tb-button--danger. See Buttons and built-in controls. |
tb-message-box--non-modal |
A box with Modal false. The overlay is transparent. |
tb-message-box--glass, tb-message-box--glass-overlay |
A box with Glass or GlassOverlay. |
tb-message-box--hidden |
Opacity 0 and the dialog at scale 0.92: the start of the enter animation and the end of the exit. |
tb-message-box--anim-in, --anim-out |
The transitions on the overlay and the dialog, with --tb-animation-duration-fast and the entrance or exit easing. |
tb-modal |
Also on the overlay, only while a blocking box is open. A workspace child with this class stops the workspace shortcuts. |