# ui/uidialog (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uidialog/

Confirmation dialog. (UIDialog of the original ui/uidialog.js)
- Pops up a dimming blocker + a centered card + confirm / cancel buttons with a single show. Appearing / disappearing moves with PopupMotion (bounce scale up / shrink).
- Pressing the blocker counts as cancel. If on_cancel is not passed, the cancel button is hidden and it becomes an alert with only a wide confirm button.
- Place it at the top of the scene root (the last child). The parent's content_size is used as the blocker size. (960 x 640 if none)
- The original is a node that inherits WorldNode. The Rust version is a component attached to a node (host), and on attach, like the original constructor, it sets up the host
  (top-left pivot, inactive) and creates the blocker, card, message, and button nodes as children. UIDialog::create(node_tree, parent_id, options) takes the place of new + add_child.
- Usage:
  ```text
    let dialog_id = UIDialog::create(node_tree, root_id, UIDialogOptions::default());
    node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
        dialog.show(tree, "Really delete?", UIDialogShowOptions { on_confirm: Some(Box::new(|dialog, context| { ... })), on_cancel: Some(Box::new(|_, _| {})), ..UIDialogShowOptions::default() });
    });
  ```
- Differences from the original:
  - show changes nodes, so it takes the tree. Scene code calls it with node_tree.with_component_mut, and callbacks with context.get_node_tree_mut().
  - Confirm / cancel handlers are one-shot functions (FnOnce) that take the dialog and its host's context. dismiss takes the context to pass it to the handlers.
    Scene code without a context can call request_dismiss, which the original also has, to dismiss on the next tick.
  - The original calls set_active(false) on the PopupMotion closed notification. The Rust version turns it off in tick when what was showing becomes closed (in dismiss for an instant transition).
  - The original tick runs the transitioner after the children's tick. In the Rust version, components run before children. (no visible difference)
  - Transition times are the same as PopupMotion. (appear 0.32 seconds, disappear 0.22 seconds. Tween is not used)

## UIDialogOptions (struct) (Clone, Debug, PartialEq, Default)

Dialog settings. (original options = { width = 420, height = 240, confirm_text = "OK", cancel_text = "Cancel", panel_color?, accent_color?, text_color? })

Example: A small card and changed confirm button text

```rust
use vanilla::{NodeTree, UIDialog, UIDialogOptions, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let options = UIDialogOptions {
	width: 360.0,
	height: 200.0,
	confirm_text: String::from("OK"),
	..UIDialogOptions::default()
};
let dialog_id = UIDialog::create(&mut node_tree, root_id, options);
let panel_id = node_tree[dialog_id].get_component::<UIDialog>().expect("dialog").get_panel_node().expect("panel");
assert_eq!(node_tree[panel_id].get_content_size(), Vector2::create(360.0, 200.0));
```

- `pub width: f64` (field, since 0.3.2): Card width. (default 420)
  - returns f64
- `pub height: f64` (field, since 0.3.2): Card height. (default 240, the top edge of the buttons is 76 above the card's bottom)
  - returns f64
- `pub confirm_text: String` (field, since 0.3.2): Confirm button text. (default "OK")
  - returns String
- `pub cancel_text: String` (field, since 0.3.2): Cancel button text. (default "Cancel")
  - returns String
- `pub panel_color: Color` (field, since 0.3.2): Card background color.
  - returns Color
- `pub accent_color: Color` (field, since 0.3.2): Confirm button color.
  - returns Color
- `pub text_color: Color` (field, since 0.3.2): Color of the message and button text.
  - returns Color

## UIDialogHandler (type)

Confirm / cancel handler. (takes the dialog and its host's context. called only once)

```rust
pub type UIDialogHandler = Box<dyn FnOnce(&mut UIDialog, &mut ComponentContext)>
```

## UIDialogShowOptions (struct) (Default)

Open settings. (options of the original show = { on_confirm?, on_cancel?, confirm_text?, cancel_text? })

Example: An alert opened without a cancel handler hides the cancel button

```rust
use vanilla::{NodeTree, UIDialog, UIDialogOptions, UIDialogShowOptions};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let dialog_id = UIDialog::create(&mut node_tree, root_id, UIDialogOptions::default());
let show_options = UIDialogShowOptions {
	confirm_text: Some(String::from("OK")),
	..UIDialogShowOptions::default()
};
node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
	dialog.show(tree, "Saved.", show_options);
});
let cancel_id = node_tree[dialog_id].get_component::<UIDialog>().expect("dialog").get_cancel_node().expect("cancel");
assert!(!node_tree[cancel_id].is_active());
```

- `pub on_confirm: Option<UIDialogHandler>` (field, since 0.3.2): Handler called when closed by confirm. (None only closes)
  - returns Option<UIDialogHandler>
- `pub on_cancel: Option<UIDialogHandler>` (field, since 0.3.2): If none, it becomes an alert with the cancel button hidden.
  - returns Option<UIDialogHandler>
- `pub confirm_text: Option<String>` (field, since 0.3.2): New text for the confirm button. (None keeps the current text)
  - returns Option<String>
- `pub cancel_text: Option<String>` (field, since 0.3.2): New text for the cancel button. (None keeps the current text)
  - returns Option<String>

## UIDialog (struct) (Default, Component, UIDocumentNode)

Confirmation dialog.

Example: Ask, then close with confirm once fully open

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, UIDialog, UIDialogOptions, UIDialogShowOptions};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let dialog_id = UIDialog::create(&mut node_tree, root_id, UIDialogOptions::default());
let is_deleted = Rc::new(Cell::new(false));
let is_deleted_handler = is_deleted.clone();
let show_options = UIDialogShowOptions {
	on_confirm: Some(Box::new(move |_dialog: &mut UIDialog, _context: &mut ComponentContext| {
		is_deleted_handler.set(true);
	})),
	on_cancel: Some(Box::new(|_dialog: &mut UIDialog, _context: &mut ComponentContext| {})),
	..UIDialogShowOptions::default()
};
node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
	dialog.show(tree, "Delete this item?", show_options);
});
node_tree.tick(&engine, 0.4);
node_tree[dialog_id].get_component_mut::<UIDialog>().expect("dialog").request_dismiss(true);
node_tree.tick(&engine, 0.016);
assert!(is_deleted.get());
node_tree.tick(&engine, 0.3);
assert!(!node_tree[dialog_id].is_active());
```

- `pub fn new() -> UIDialog` (constructor, since 0.3.2): Constructor. (default settings)
  - returns UIDialog: Dialog before being attached. (UIDialogOptions::default)
- `pub fn create_with_options(options: UIDialogOptions) -> UIDialog` (static, since 0.3.2): Creates with settings. (attach to a node with node_tree.add_component)
  - `options` UIDialogOptions: Dialog settings.
  - returns UIDialog: Dialog before being attached.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UIDialogOptions) -> NodeId` (static, since 0.3.2): Creates the node. (creates and attaches a "UIDialog" node under parent_id. original new UIDialog(options) + add_child)
  - `node_tree` &mut NodeTree: Node tree.
  - `parent_id` NodeId: Parent node. (usually the scene root, its content_size is used as the blocker size)
  - `options` UIDialogOptions: Dialog settings.
  - returns NodeId: The created host node. (turned off because it has not been opened yet)
- `pub fn show(&mut self, node_tree: &mut NodeTree, message_text: &str, options: UIDialogShowOptions)` (method, since 0.3.2): Opens the dialog. (without on_cancel, hides the cancel button and widens the confirm button to the center)
  - Does nothing before being attached to a tree. On open, turns on the host, fits the blocker to the parent size, and starts the appear movement.
  - `node_tree` &mut NodeTree: Node tree that holds the dialog. (context.get_node_tree_mut() in callbacks)
  - `message_text` &str: Text to show. (wraps at card width - 56)
  - `options` UIDialogShowOptions: Handlers and the button text to use this time. (handlers replace the previous ones)

  Example: Delete a node through the context in the confirm handler

  ```rust
  use vanilla::{ComponentContext, NodeId, NodeTree, UIDialog, UIDialogOptions, UIDialogShowOptions};
  
  fn ask_delete(node_tree: &mut NodeTree, dialog_id: NodeId, item_id: NodeId) {
  	let show_options = UIDialogShowOptions {
  		on_confirm: Some(Box::new(move |_dialog: &mut UIDialog, context: &mut ComponentContext| {
  			context.get_node_tree_mut().destroy_node(item_id);
  		})),
  		on_cancel: Some(Box::new(|_dialog: &mut UIDialog, _context: &mut ComponentContext| {})),
  		..UIDialogShowOptions::default()
  	};
  	node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
  		dialog.show(tree, "Delete this item?", show_options);
  	});
  }
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let item_id = node_tree.create_child(root_id, "Item");
  let dialog_id = UIDialog::create(&mut node_tree, root_id, UIDialogOptions::default());
  ask_delete(&mut node_tree, dialog_id, item_id);
  assert!(node_tree[dialog_id].get_component::<UIDialog>().expect("dialog").is_showing());
  ```

- `pub fn dismiss(&mut self, context: &mut ComponentContext, is_confirmed: bool)` (method, since 0.3.2): Closes. (only once fully open. calls the confirm / cancel handler depending on is_confirmed. the context is the dialog host's)
  - Starts the disappear movement, then calls the handler, and discards the handler of the side not called.
  - `context` &mut ComponentContext: Context of the dialog host node. (passed straight to the handler)
  - `is_confirmed` bool: true for confirm, false for cancel.

  Example: Close as cancel from another button

  ```rust
  use vanilla::{ComponentContext, NodeId, UIButton, UIDialog};
  
  fn bind_skip_button(button: &mut UIButton, dialog_id: NodeId) {
  	button.set_clicked_event(Box::new(move |_button: &mut UIButton, context: &mut ComponentContext| {
  		context.with_component_mut::<UIDialog, _>(dialog_id, |dialog, dialog_context| {
  			dialog.dismiss(dialog_context, false);
  		});
  	}));
  }
  ```

- `pub fn request_dismiss(&mut self, is_confirmed: bool)` (method, since 0.3.2): Closes on the next tick. (for scene code without a context)
  - `is_confirmed` bool: true for confirm, false for cancel. (passed to dismiss on the next tick, does not close if not fully open by then)
- `pub fn is_showing(&self) -> bool` (method, since 0.3.2): Whether it is showing. (true while appearing / open / disappearing)
  - returns bool: true unless in the closed state.
- `pub fn set_instant_enabled(&mut self, is_instant_enabled: bool)` (method, since 0.3.2): Sets instant transitions. (UI tone without effects)
  - `is_instant_enabled` bool: If true, opens and closes instantly without movement. (default false)
- `pub fn get_popup_motion(&self) -> &PopupMotion` (method, since 0.3.2): Returns the transitioner.
  - returns &PopupMotion: Appear and disappear movement. (appear 0.32 seconds, disappear 0.22 seconds)
- `pub fn get_overlay_node(&self) -> Option<NodeId>` (method, since 0.3.2): Blocker node handle.
  - returns Option<NodeId>: Dimming blocker node. (None before being attached to a tree)
- `pub fn get_panel_node(&self) -> Option<NodeId>` (method, since 0.3.2): Card node handle.
  - returns Option<NodeId>: Card node. (center pivot, None before being attached to a tree)
- `pub fn get_confirm_node(&self) -> Option<NodeId>` (method, since 0.3.2): Confirm button node handle.
  - returns Option<NodeId>: Confirm button node. (None before being attached to a tree)
- `pub fn get_cancel_node(&self) -> Option<NodeId>` (method, since 0.3.2): Cancel button node handle.
  - returns Option<NodeId>: Cancel button node. (None before being attached to a tree, turned off when opened as an alert)
