# core/modaldialog (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/modaldialog/

Modal question dialog. (in place of the browser window.alert, window.confirm, window.prompt that games called, the same as the original core/modaldialog.js)
- The Rust version has the engine functions Engine::alert, Engine::confirm, Engine::prompt, the original has engine.alert, confirm, prompt. The DevTools Local Storage tab also asks for keys and values, and whether to delete, with this dialog.
- In both engines, on the web and the desktop alike, the engine draws on the canvas a question dialog like the Chrome tab modal dialog (JavaScriptTabModalDialogViewViews). (no browser dialog is shown)
  - Position: horizontal center, top edge inside the canvas (window). (Chrome overlaps the bottom of the toolbar by 3px at the horizontal center of the tab content area. The canvas has no toolbar, so it is the top edge)
  - Title: "This page says" (the title Chrome uses for pages without an origin (file:, data:)) There is no close X.
  - alert has only OK, confirm and prompt have OK and Cancel. The default button is OK. Button order is like Chrome: Windows `[OK][Cancel]`, macOS and Linux `[Cancel][OK]`.
  - The prompt input field gets the default value and receives focus with everything selected. Enter confirms, Escape cancels.
  - The text (title, OK, Cancel) is chosen by the system language standing in for the browser UI language. (core/browseruitext.rs)
  - While it is open, the scene and async tasks are not updated like the original (drawing keeps the frozen screen), and only the dialog receives input. When it closes, pressed keys and buttons are cleared.
  - The input field receives IME and edit keys from the text input queue (core/textinput.rs). On the web a hidden DOM input receives the text like the original. Pressing and dragging sets the cursor and selection.
  - Several calls in one frame are shown one after another.
- The Rust version returns a ModalDialogHandle, which finishes when answered. (is_finished, the original returns a Promise)
  Attach a function to call afterwards with set_callback. Inside engine tasks, await the Promise of Engine::alert_async, confirm_async, prompt_async.
  (async tasks also pause while it is open)

## ModalDialogHandle (struct) (Clone)

Question dialog handle. (clones refer to the same dialog)
- Returned by Engine::alert, confirm, prompt. Finishes when answered (is_finished), and the answer is read with get_result, is_confirmed, get_prompt_text.

Example: Show a confirm dialog and wait for the answer

```rust
use vanilla::{Engine, EngineConfiguration, ModalDialogResult};

let engine = Engine::new(EngineConfiguration::new());
let modal_dialog_handle = engine.confirm("Delete the save data?");
assert!(!modal_dialog_handle.is_finished());
assert_eq!(modal_dialog_handle.get_result(), None);
assert!(!modal_dialog_handle.is_confirmed());
modal_dialog_handle.set_callback(|result: &ModalDialogResult| {
	if let ModalDialogResult::Confirmed(_) = result {
		println!("deleted");
	}
});
```

Example: Ask for a name in a scene

```rust
use vanilla::Engine;

fn ask_player_name(engine: &Engine) {
	let modal_dialog_handle = engine.prompt("Name?", "Player");
	let callback_handle = modal_dialog_handle.clone();
	modal_dialog_handle.set_callback(move |_result| {
		match callback_handle.get_prompt_text() {
			Some(player_name) => {
				println!("{}", player_name);
			}
			None => {
				println!("cancelled");
			}
		}
	});
}
```

- `pub fn is_finished(&self) -> bool` (method, since 0.4.0): Whether answered.
  - returns bool: true if answered with OK or Cancel.
- `pub fn get_result(&self) -> Option<ModalDialogResult>` (method, since 0.4.0): Answer. (None if not yet)
  - returns Option<ModalDialogResult>: A copy of the answer. (None if not answered yet)
- `pub fn is_confirmed(&self) -> bool` (method, since 0.4.0): Whether confirmed. (the value the original confirm returns, false if not yet or cancelled)
  - returns bool: true if answered with OK. (false if not yet or cancelled)
- `pub fn get_prompt_text(&self) -> Option<String>` (method, since 0.4.0): The entered text. (the value the original prompt returns, None on cancel like the original null. Also None if not yet)
  - returns Option<String>: Input text when confirmed. (empty text if alert or confirm was confirmed, None if not yet or cancelled)
- `pub fn set_callback(&self, callback: impl FnOnce(&ModalDialogResult) + 'static)` (method, since 0.4.0): Sets the function to call when finished. (calls it right away if already finished)
  - Called only once. Setting it again replaces the previous function. (while not finished yet)
  - `callback` impl FnOnce(&ModalDialogResult) + 'static: Function that receives the answer. (once, when finished)

## ModalDialogKind (enum) (Clone, Copy, Debug, PartialEq, Eq)

Question dialog kind. (alert, confirm, prompt of the original ModalDialogKind)

- `Alert`: Alert dialog. (in place of window.alert, only OK)
- `Confirm`: Confirm dialog. (in place of window.confirm, OK and Cancel)
- `Prompt`: Prompt dialog. (in place of window.prompt, an input field with OK and Cancel)

## ModalDialogResult (enum) (Clone, Debug, PartialEq, Eq)

Answer of a dialog.

- `Confirmed` = `Confirmed(String)`: OK. (the entered text for prompt, empty text for alert and confirm)
- `Cancelled`: Cancel. (null for the original prompt, false for confirm)
