# ui/uiconfirmwindow (vanilla-engine 0.4.0)

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

Confirm window. (vanilla.js ui/uiconfirmwindow.js, UIConfirmWindow)
- A title, a question text, and two buttons, cancel and confirm. It is a UIWindow, so it is opened with UIWindowLayer.
- The question text wraps at the panel width, and the panel height is set to fit the number of lines. If the text is long and exceeds the area height, only the text area scrolls and the two buttons are always visible.
- The buttons are cancel on the left and confirm (highlighted) on the right. For right-to-left languages (set_right_to_left), the order is reversed.
- Pressing confirm closes it after the confirm notification. Closing by cancel, outside press, close_top_window, or close_all_windows gives the cancel notification.
- Usage:
  ```text
    let window_id = UIConfirmWindow::create(node_tree, layer_id, "quit", UIConfirmWindowOptions { title: String::from("Leave"), message: String::from("Really leave?"), ..UIConfirmWindowOptions::default() });
    if let Some(confirm_window) = node_tree[window_id].get_component_mut::<UIWindow>().and_then(|window| window.get_behavior_mut::<UIConfirmWindow>()) {
        confirm_window.set_confirm_event(Some(Box::new(move || { leave_requested.set(true); })));
    }
    UIWindowLayer::open_window_in_tree(node_tree, layer_id, window_id);
  ```
- Differences from the original:
  - The original inherits UIWindow. The Rust version is a value implementing UIWindow's override points (UIWindowBehavior). (taken out with UIWindow::get_behavior_mut)
  - The number of text lines is measured right at layout as in the original. Since there is no Graphic there, it measures with the registered fonts. (wrap_lines_without_graphic of the Text that UILabel holds)
    Only when it cannot measure because there are no fonts at all (no font files on desktop), it lays out as one line, and once the text is drawn and wraps, it lays out again on the next tick.
  - The title and question text are UILabels as in the original. For right-to-left languages, as in the original, it sets the Rtl direction on both texts (Inherit otherwise) and reverses the button order.
    set_right_to_left does not take the window node, so the text direction is applied at the next layout. To apply the text direction right away as in the original, call set_right_to_left_in_tree. (the button order is at the next layout in the original too)
  - The notification is an argumentless `Box<dyn FnMut()>`.

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

Confirm window settings. (original options = { title = "", message = "", cancelText = "Cancel", confirmText = "OK" })

Example: Changing only the title and question text, with default button texts

```rust
use vanilla::UIConfirmWindowOptions;

let options = UIConfirmWindowOptions {
	title: String::from("Delete"),
	message: String::from("Delete this save file?"),
	..UIConfirmWindowOptions::default()
};
assert_eq!(options.cancel_text, UIConfirmWindowOptions::default().cancel_text);
assert!(UIConfirmWindowOptions::default().title.is_empty());
```

- `pub title: String` (field, since 0.4.0): If empty, the title line is removed.
  - returns String
- `pub message: String` (field, since 0.4.0): Question text. (wraps at the panel width, default empty string)
  - returns String
- `pub cancel_text: String` (field, since 0.4.0): Cancel button text. (default "Cancel")
  - returns String
- `pub confirm_text: String` (field, since 0.4.0): Confirm button text. (default "OK")
  - returns String

## UIConfirmWindow (struct) (UIWindowBehavior)

Confirm window.

Example: Setting a confirm notification, opening, then confirming

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{NodeTree, UIConfirmWindow, UIConfirmWindowOptions, UIWindow, UIWindowLayer};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let layer_id = UIWindowLayer::create(&mut node_tree, root_id);
let options = UIConfirmWindowOptions {
	title: String::from("Quit"),
	message: String::from("Leave the game?"),
	..UIConfirmWindowOptions::default()
};
let window_id = UIConfirmWindow::create(&mut node_tree, layer_id, "quit", options);
let leave_requested = Rc::new(Cell::new(false));
let leave_requested_event = leave_requested.clone();
if let Some(confirm_window) = node_tree[window_id].get_component_mut::<UIWindow>().and_then(|window| window.get_behavior_mut::<UIConfirmWindow>()) {
	confirm_window.set_confirm_event(Some(Box::new(move || {
		leave_requested_event.set(true);
	})));
}
UIWindowLayer::open_window_in_tree(&mut node_tree, layer_id, window_id);
UIConfirmWindow::confirm_in_tree(&mut node_tree, window_id);
assert!(leave_requested.get());
assert!(!node_tree[layer_id].get_component::<UIWindowLayer>().expect("layer").has_open_window());
```

- `pub fn create_with_options(options: UIConfirmWindowOptions) -> UIConfirmWindow` (static, since 0.4.0): Constructor. (passed to UIWindow::create, usually UIConfirmWindow::create)
  - `options` UIConfirmWindowOptions: Confirm window settings.
  - returns UIConfirmWindow: Confirm window before being attached to a window node. (right-to-left language off, no notifications)
- `pub fn create(node_tree: &mut NodeTree, layer_id: NodeId, name: &str, options: UIConfirmWindowOptions) -> NodeId` (static, since 0.4.0): Creates a confirm window node. (under the layer_id window layer node, opening is UIWindowLayer::open_window)
  - `node_tree` &mut NodeTree: Node tree.
  - `layer_id` NodeId: Window layer node.
  - `name` &str: Window node name. (the title, text and button nodes are this name followed by Title, MessageScroll, Message, Cancel, Confirm)
  - `options` UIConfirmWindowOptions: Confirm window settings.
  - returns NodeId: The created window node. (UIWindow attached, off since it is not opened yet)
- `pub fn confirm_in_tree(node_tree: &mut NodeTree, window_id: NodeId)` (static, since 0.4.0): Confirms. (calls the confirm notification and closes, does nothing if not open. Called by the confirm button)
  - `node_tree` &mut NodeTree: Node tree.
  - `window_id` NodeId: Confirm window node. (does nothing if it is not a confirm window)
- `pub fn refresh_if_open_in_tree(node_tree: &mut NodeTree, window_id: NodeId)` (static, since 0.4.0): Lays out again if open. (original refreshIfOpen, after changing text or order)
  - `node_tree` &mut NodeTree: Node tree.
  - `window_id` NodeId: Confirm window node. (does nothing if not open)
- `pub fn set_title_in_tree(node_tree: &mut NodeTree, window_id: NodeId, title: &str)` (static, since 0.4.0): Changes the title. (lays out again if open, original setTitle)
  - `node_tree` &mut NodeTree: Node tree.
  - `window_id` NodeId: Confirm window node.
  - `title` &str: New title. (if an empty string, the title line is removed)
- `pub fn set_message_in_tree(node_tree: &mut NodeTree, window_id: NodeId, message: &str)` (static, since 0.4.0): Changes the question text. (lays out again if open, original setMessage)
  - `node_tree` &mut NodeTree: Node tree.
  - `window_id` NodeId: Confirm window node.
  - `message` &str: New question text.

  Example: Changing the text after opening sets the panel height again

  ```rust
  use vanilla::{NodeTree, UIConfirmWindow, UIConfirmWindowOptions, UILabel, UIWindow, UIWindowLayer};
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let layer_id = UIWindowLayer::create(&mut node_tree, root_id);
  let window_id = UIConfirmWindow::create(&mut node_tree, layer_id, "save", UIConfirmWindowOptions::default());
  UIWindowLayer::open_window_in_tree(&mut node_tree, layer_id, window_id);
  UIConfirmWindow::set_message_in_tree(&mut node_tree, window_id, "Overwrite the save file?");
  let window = node_tree[window_id].get_component::<UIWindow>().expect("window");
  let message_id = window.get_behavior::<UIConfirmWindow>().expect("confirm").get_message_node().expect("message");
  assert_eq!(node_tree[message_id].get_component::<UILabel>().expect("label").get_text(), "Overwrite the save file?");
  ```

- `pub fn set_right_to_left_in_tree(node_tree: &mut NodeTree, window_id: NodeId, is_right_to_left: bool)` (static, since 0.4.0): Sets whether it is a right-to-left language and applies the text direction right away. (original setRightToLeft, the button order from the next layout)
  - `node_tree` &mut NodeTree: Node tree.
  - `window_id` NodeId: Confirm window node.
  - `is_right_to_left` bool: true for a right-to-left language. (the title and question text are Rtl, otherwise Inherit)
- `pub fn set_button_texts(&mut self, node_tree: &mut NodeTree, cancel_text: &str, confirm_text: &str)` (method, since 0.4.0): Sets the button texts. (original setButtonTexts)
  - If not attached yet, it only records them in the settings and uses them when creating the buttons.
  - `node_tree` &mut NodeTree: Node tree holding the window.
  - `cancel_text` &str: Cancel button text.
  - `confirm_text` &str: Confirm button text.
- `pub fn set_confirm_event(&mut self, confirm_event: Option<UIWindowEvent>)` (method, since 0.4.0): Sets the confirm notification.
  - `confirm_event` Option<UIWindowEvent>: Notification called before closing when the confirm button is pressed (or confirm_in_tree). (None turns it off)
- `pub fn set_cancel_event(&mut self, cancel_event: Option<UIWindowEvent>)` (method, since 0.4.0): Sets the cancel notification. (cancel button, pressing outside, when the window layer closes it)
  - `cancel_event` Option<UIWindowEvent>: Notification called when closed by a path other than confirm. (None turns it off)

  Example: The cancel notification arrives even when the window layer closes all windows

  ```rust
  use std::cell::Cell;
  use std::rc::Rc;
  use vanilla::{NodeTree, UIConfirmWindow, UIConfirmWindowOptions, UIWindow, UIWindowLayer};
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let layer_id = UIWindowLayer::create(&mut node_tree, root_id);
  let window_id = UIConfirmWindow::create(&mut node_tree, layer_id, "retry", UIConfirmWindowOptions::default());
  let cancel_count = Rc::new(Cell::new(0));
  let cancel_counter = cancel_count.clone();
  if let Some(confirm_window) = node_tree[window_id].get_component_mut::<UIWindow>().and_then(|window| window.get_behavior_mut::<UIConfirmWindow>()) {
  	confirm_window.set_cancel_event(Some(Box::new(move || {
  		cancel_counter.set(cancel_counter.get() + 1);
  	})));
  }
  UIWindowLayer::open_window_in_tree(&mut node_tree, layer_id, window_id);
  node_tree.with_component_mut::<UIWindowLayer, _>(layer_id, |window_layer, tree| {
  	window_layer.close_all_windows(tree);
  });
  assert_eq!(cancel_count.get(), 1);
  ```

- `pub fn set_right_to_left(&mut self, is_right_to_left: bool)` (method, since 0.4.0): Sets whether it is a right-to-left language. (if true, the title and question text are in the Rtl direction, cancel on the right, confirm on the left, from the next layout)
  - `is_right_to_left` bool: true for a right-to-left language.
- `pub fn is_right_to_left(&self) -> bool` (method, since 0.4.0): Whether the language is right-to-left.
  - returns bool: true for a right-to-left language. (default false)
- `pub fn set_title(&mut self, node_tree: &mut NodeTree, title: &str)` (method, since 0.4.0): Changes the title. (if an empty string, the title line is removed, if open it is laid out again with UIWindow::refresh_layout)
  - `node_tree` &mut NodeTree: Node tree holding the window.
  - `title` &str: New title.
- `pub fn set_message(&mut self, node_tree: &mut NodeTree, message: &str)` (method, since 0.4.0): Changes the question text. (if open, it is laid out again with UIWindow::refresh_layout)
  - `node_tree` &mut NodeTree: Node tree holding the window.
  - `message` &str: New question text.
- `pub fn get_title_node(&self) -> Option<NodeId>` (method, since 0.4.0): Title node. (UILabel)
  - returns Option<NodeId>: Title node. (None before the window node is created, off if the title is empty)
- `pub fn get_message_node(&self) -> Option<NodeId>` (method, since 0.4.0): Question text node. (UILabel)
  - returns Option<NodeId>: Question text node. (None before the window node is created)
- `pub fn get_message_scroll_node(&self) -> Option<NodeId>` (method, since 0.4.0): Scroll area node of the question text. (UIScrollView)
  - returns Option<NodeId>: Scroll area node. (None before the window node is created)
- `pub fn get_cancel_button_node(&self) -> Option<NodeId>` (method, since 0.4.0): Cancel button node. (Paint, UIButton, UILabel)
  - returns Option<NodeId>: Cancel button node. (None before the window node is created)
- `pub fn get_confirm_button_node(&self) -> Option<NodeId>` (method, since 0.4.0): Confirm button node. (Paint, UIButton, UILabel)
  - returns Option<NodeId>: Confirm button node. (None before the window node is created)
