Class ConfirmDialog
A two-choice confirmation dialog, and the imperative helper that shows one and awaits the answer.
Inheritance
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class ConfirmDialog : AlertDialog, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, IDismissInvocator
Remarks
ConfirmDialog is the ergonomic layer over AlertDialog and Modal: instead of wiring a primary action, a cancel action and a modal by hand every time you need a yes/no answer, you call Show(VisualElement, string, string, string, string, bool) and await a Task<TResult> of bool.
The task completes true when the user takes the confirm action and false for
every other outcome: the cancel action, the escape key, or a click outside the modal. It
resolves exactly once, so a dismiss that follows a confirm cannot flip the answer.
Concurrent calls queue. Only one confirmation is on screen at a time and the next one is shown as soon as the current one closes, so a loop over several items does not stack a pile of modals on top of each other.
ConfirmDelete(VisualElement, string, int) is the preset for the common destructive case. It frames the dialog with the Destructive semantic, labels the confirm action "Delete" and adds the "This action cannot be undone." description.
The element itself is a normal AlertDialog subclass, so it can also be declared in UXML and driven manually through Confirm() and Cancel().
Examples
Awaiting a confirmation before running a destructive action.
async void OnDeleteClicked()
{
if (await ConfirmDialog.ConfirmDelete(rootElement, selection.name))
DeleteSelection();
}
A custom confirmation with its own labels.
var confirmed = await ConfirmDialog.Show(
rootElement,
title: "Discard changes?",
description: "Your edits to this scene will be lost.",
confirmLabel: "Discard",
cancelLabel: "Keep editing",
destructive: true);
Declaring the dialog in UXML and showing it yourself.
<UXML>
<appui:ConfirmDialog
title="Delete asset?"
description="This action cannot be undone."
confirm-label="Delete"
cancel-label="Cancel"
destructive="true" />
</UXML>
Constructors
ConfirmDialog()
Default constructor.
Declaration
public ConfirmDialog()
Fields
confirmUssClassName
The main styling class of the ConfirmDialog. This is the class that is used to style the element in addition to the ussClassName class.
Declaration
public const string confirmUssClassName = "appui-confirm-dialog"
Field Value
| Type | Description |
|---|---|
| string |
defaultCancelLabel
The default label of the cancel action.
Declaration
public const string defaultCancelLabel = "Cancel"
Field Value
| Type | Description |
|---|---|
| string |
defaultConfirmLabel
The default label of the confirm action.
Declaration
public const string defaultConfirmLabel = "Confirm"
Field Value
| Type | Description |
|---|---|
| string |
deleteDescription
The description used by ConfirmDelete(VisualElement, string, int).
Declaration
public const string deleteDescription = "This action cannot be undone."
Field Value
| Type | Description |
|---|---|
| string |
destructiveUssClassName
The styling class applied when destructive is true.
Declaration
public const string destructiveUssClassName = "appui-confirm-dialog--destructive"
Field Value
| Type | Description |
|---|---|
| string |
Properties
cancelLabel
The label of the cancel action. Defaults to defaultCancelLabel.
Declaration
[Tooltip("The label of the cancel action.")]
[CreateProperty]
[UxmlAttribute]
public string cancelLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
confirmLabel
The label of the confirm action. Defaults to defaultConfirmLabel.
Declaration
[Tooltip("The label of the confirm action.")]
[CreateProperty]
[UxmlAttribute]
[Header("Confirm Dialog")]
public string confirmLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
confirmed
The task that completes with the user's answer: true when the confirm action is taken,
false for a cancel, an escape key press or an outside click. It resolves exactly once.
Declaration
public Task<bool> confirmed { get; }
Property Value
| Type | Description |
|---|---|
| Task<bool> |
destructive
Frames the confirmation as destructive: the dialog takes the Destructive semantic and the confirm action becomes a Destructive button.
Declaration
[Tooltip("Frames the confirmation as destructive.")]
[CreateProperty]
[UxmlAttribute]
public bool destructive { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
BuildDelete(string, int)
Build an unshown ConfirmDialog configured as a delete confirmation.
Declaration
public static ConfirmDialog BuildDelete(string what, int count = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| string | what | Names the thing being deleted — an object's name, or a plural noun such as "objects" when deleting several. |
| int | count | How many things are being deleted. Above one the title reads "Delete N <what>?". |
Returns
| Type | Description |
|---|---|
| ConfirmDialog | The configured dialog. |
Cancel()
Resolve confirmed with false. Called by the cancel action and by any
dismissal; safe to call more than once, later calls are ignored.
Declaration
public void Cancel()
Confirm()
Resolve confirmed with true. Called by the confirm action; safe to call
more than once, later calls are ignored.
Declaration
public void Confirm()
ConfirmDelete(VisualElement, string, int)
Show a delete confirmation and await the user's answer. The destructive preset of Show(VisualElement, string, string, string, string, bool).
Declaration
public static Task<bool> ConfirmDelete(VisualElement referenceView, string what, int count = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| VisualElement | referenceView | Any VisualElement currently in the UI panel that should host the modal. |
| string | what | Names the thing being deleted. |
| int | count | How many things are being deleted. |
Returns
| Type | Description |
|---|---|
| Task<bool> | A task that completes |
Show(VisualElement, string, string, string, string, bool)
Show a confirmation dialog and await the user's answer.
Declaration
public static Task<bool> Show(VisualElement referenceView, string title, string description = null, string confirmLabel = null, string cancelLabel = null, bool destructive = false)
Parameters
| Type | Name | Description |
|---|---|---|
| VisualElement | referenceView | Any VisualElement currently in the UI panel that should host the modal. |
| string | title | The dialog title. |
| string | description | An optional supporting line under the title. |
| string | confirmLabel | The confirm action's label. Defaults to "Confirm". |
| string | cancelLabel | The cancel action's label. Defaults to "Cancel". |
| bool | destructive | Frames the confirmation as destructive. |
Returns
| Type | Description |
|---|---|
| Task<bool> | A task that completes |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | If |
Show(VisualElement, ConfirmDialog)
Show an already configured ConfirmDialog and await the user's answer.
Declaration
public static Task<bool> Show(VisualElement referenceView, ConfirmDialog dialog)
Parameters
| Type | Name | Description |
|---|---|---|
| VisualElement | referenceView | Any VisualElement currently in the UI panel that should host the modal. |
| ConfirmDialog | dialog | The dialog to show. |
Returns
| Type | Description |
|---|---|
| Task<bool> | A task that completes |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | If |