# ui/uitoast (vanilla-engine 0.4.0)

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

Toast. (UIToast of the original ui/uitoast.js)
- With show(message), a pill-shaped notification rises from below (0.24 seconds), stays a while (default 1.8 seconds), then exits upward and disappears (0.2 seconds).
- Calling it several times queues them and shows them one at a time.
- Place it at the top of the scene root (the last child). If the horizontal position is not set, it stands at the parent's horizontal center. (480 if the parent size is unknown)
- The original is a node that inherits WorldNode, and the node itself is the pill. The Rust version is a component attached to a node (host), and on attach, like the original constructor, it
  sets up the host (center pivot, size) and attaches Paint (rounded pill) and UILabel to the host. UIToast::create(node_tree, parent_id, options) takes the place of new + add_child.
- Usage:
  ```text
    let toast_id = UIToast::create(node_tree, root_id, UIToastOptions::default());
    node_tree[toast_id].get_component_mut::<UIToast>().unwrap().show("Saved.");
  ```
- Differences from the original:
  - show only puts it in the queue, and the next tick takes it out and shows it. (the first step also happens on the same tick, so the timing is the same as the original)
    So while the original calls set_active(false) when hiding, the Rust version needs tick to run, so it hides with set_visible(false).
    The UI document writes and reads a hidden toast as active false, as in the original. (UIDocumentNode in uidocument.rs)
  - The appear / stay / exit times and curves are counted inside here exactly as in the original. (Tween is not used)
  - The horizontal position rest_x is `Option<f64>`. (original null = parent center) The options object is the UIToastOptions struct.

## UIToastOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

Toast settings. (original options = { width = 400, height = 48, rest_y = 560, rest_x = null, hold_seconds = 1.8, font_size = 15, background_color?, text_color? })

Example: A narrow toast that stays long near the top

```rust
use vanilla::{NodeTree, UIToast, UIToastOptions, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let options = UIToastOptions {
	width: 320.0,
	rest_y: 80.0,
	hold_seconds: 3.0,
	..UIToastOptions::default()
};
let toast_id = UIToast::create(&mut node_tree, root_id, options);
assert_eq!(node_tree[toast_id].get_content_size(), Vector2::create(320.0, 48.0));
assert!(!node_tree[toast_id].is_visible());
```

- `pub width: f64` (field, since 0.3.2): Pill width. (default 400)
  - returns f64
- `pub height: f64` (field, since 0.3.2): Pill height. (default 48, the rounding is half of it)
  - returns f64
- `pub rest_y: f64` (field, since 0.3.2): Vertical coordinate of the center of the resting position.
  - returns f64
- `pub rest_x: Option<f64>` (field, since 0.3.2): Horizontal coordinate of the center of the resting position. (None for the parent's horizontal center)
  - returns Option<f64>
- `pub hold_seconds: f64` (field, since 0.3.2): Stay time. (seconds)
  - returns f64
- `pub font_size: f64` (field, since 0.3.2): Text size. (default 15)
  - returns f64
- `pub background_color: Color` (field, since 0.3.2): Pill color.
  - returns Color
- `pub text_color: Color` (field, since 0.3.2): Text color.
  - returns Color

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

Toast.

Example: Show a saved notification

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, UILabel, UIToast, UIToastOptions, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
node_tree[root_id].set_content_size(Vector2::create(800.0, 600.0));
let toast_id = UIToast::create(&mut node_tree, root_id, UIToastOptions::default());
node_tree[toast_id].get_component_mut::<UIToast>().expect("toast").show("Saved.");
node_tree.tick(&engine, 0.24);
assert!(node_tree[toast_id].is_visible());
assert_eq!(node_tree[toast_id].get_component::<UILabel>().expect("label").get_text(), "Saved.");
assert_eq!(node_tree[toast_id].get_local_position(), Vector2::create(400.0, 560.0));
```

- `pub fn new() -> UIToast` (constructor, since 0.3.2): Constructor. (default settings)
  - returns UIToast: Toast before being attached. (UIToastOptions::default)
- `pub fn create_with_options(options: UIToastOptions) -> UIToast` (static, since 0.3.2): Creates with settings. (attach to a node with node_tree.add_component)
  - `options` UIToastOptions: Toast settings.
  - returns UIToast: Toast before being attached.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UIToastOptions) -> NodeId` (static, since 0.3.2): Creates the node. (creates and attaches a "UIToast" node under parent_id. original new UIToast(options) + add_child)
  - `node_tree` &mut NodeTree: Node tree.
  - `parent_id` NodeId: Parent node. (usually the scene root, stands at its horizontal center if the horizontal position is not set)
  - `options` UIToastOptions: Toast settings.
  - returns NodeId: The created toast node. (hidden because it has not been shown yet)
- `pub fn show(&mut self, message_text: &str)` (method, since 0.3.2): Shows a toast. (puts it in the queue. waits its turn if one is showing)
  - `message_text` &str: Text to show. (shown from the next tick)

  Example: Calling it twice shows the next one after the first disappears

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeTree, UILabel, UIToast, UIToastOptions};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let toast_id = UIToast::create(&mut node_tree, root_id, UIToastOptions::default());
  if let Some(toast) = node_tree[toast_id].get_component_mut::<UIToast>() {
  	toast.show("First");
  	toast.show("Second");
  }
  node_tree.tick(&engine, 0.24);
  node_tree.tick(&engine, 1.8);
  node_tree.tick(&engine, 0.2);
  assert_eq!(node_tree[toast_id].get_component::<UILabel>().expect("label").get_text(), "Second");
  ```

- `pub fn set_rest_y(&mut self, rest_y: f64)` (method, since 0.3.2): Sets the resting position (vertical coordinate of the center).
  - `rest_y` f64: Vertical coordinate of the center of the resting position. (parent local coordinates, rises from 28 below when appearing, from the next position calculation)
- `pub fn set_rest_x(&mut self, rest_x: Option<f64>)` (method, since 0.3.2): Sets the resting position (horizontal coordinate of the center). (None for the parent's horizontal center)
  - `rest_x` Option<f64>: Horizontal coordinate of the center of the resting position. (parent local coordinates, None for the parent's horizontal center, 480 if the parent size is unknown)
- `pub fn set_hold_seconds(&mut self, hold_seconds: f64)` (method, since 0.3.2): Sets the stay time. (seconds)
  - `hold_seconds` f64: Stay time after fully rising. (seconds, default 1.8)
- `pub fn is_showing(&self) -> bool` (method, since 0.3.2): Whether it is showing. (true even if messages remain in the queue)
  - returns bool: true if appearing, staying, exiting, or there are messages waiting in the queue.
