# ui/toastmotion (vanilla-engine 0.4.0)

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

Motion for a notification appearing and disappearing. (ToastMotion, ToastPhase, readToastSeconds, attachToastMotion of vanilla.js ui/toastmotion.js)
- Attach it to a notification node and show it with show. It slides in from the outside (upper notifications from above, lower notifications from below), is visible, grows slightly and returns,
  and when the stay time passes it floats up and disappears.
- If another text arrives while it is up, the earlier one moves out quickly and the new text comes in; if the same text arrives again, only the time is refilled and it bounces once.
- The node's position moves only around the placed position as the reference. (if placed again in between, that position becomes the new reference)
- Differences from the original: show only records the request and moves the node on the next tick. (scene code calls it without a node context)
  The text is put into the node when the function given to show starts to appear. (same as the original show(text, seconds, applyText)) While hidden, the node is not turned off but only made invisible. (so tick keeps running)
- UIToast creates stacked pill notifications by itself, and this one moves a notification node made by the game.

## ToastApplyText (type)

A function that puts text and color into the node. (applyText of the original show; called once when it starts to appear; components like a UILabel on the same node are found from the context's node)

```rust
pub type ToastApplyText = Box<dyn FnOnce(&mut ComponentContext)>
```

## ToastMotion (struct) (Component)

Toast motion. (attached to a toast node made by the game, handles the movement that appears on show, stays, and disappears)

Example: Showing a notification node made by the game

```rust
use vanilla::{attach_toast_motion, ComponentContext, Engine, EngineConfiguration, NodeTree, Text, ToastMotion, ToastPhase, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let toast_id = node_tree.create_child(root_id, "Toast");
node_tree[toast_id].set_local_position(Vector2::create(640.0, 80.0));
node_tree[toast_id].set_content_size(Vector2::create(320.0, 48.0));
node_tree[toast_id].add_component(Text::create_with_text("", 24.0));
node_tree[toast_id].set_visible(false);
let toast_motion = attach_toast_motion(&mut node_tree, toast_id, -1.0).expect("toast motion");
toast_motion.show("Saved", 1.0, |context: &mut ComponentContext| {
	if let Some(text_component) = context.get_node_mut().get_component_mut::<Text>() {
		text_component.set_text("Saved");
	}
});
assert!(toast_motion.is_showing());

node_tree.tick(&engine, 0.016);
assert!(node_tree[toast_id].is_visible());
assert_eq!(node_tree[toast_id].get_component::<Text>().expect("text").get_text(), "Saved");
let toast_motion = node_tree[toast_id].get_component::<ToastMotion>().expect("toast motion");
assert_eq!(toast_motion.get_phase(), ToastPhase::Enter);

for _frame_index in 0..120 {
	node_tree.tick(&engine, 1.0 / 60.0);
}
assert!(!node_tree[toast_id].is_visible());
assert_eq!(node_tree[toast_id].get_local_position(), Vector2::create(640.0, 80.0));
```

- `pub fn new() -> ToastMotion` (constructor, since 0.4.0): Creates. (the original new ToastMotion(); it comes in from above, and the entering side is changed with set_enter_side)
  - returns ToastMotion: New hidden motion. (use attach_toast_motion to attach it to a node)
- `pub fn show(&mut self, text: &str, seconds: f64, apply_text: impl FnOnce(&mut ComponentContext) + 'static)` (method, since 0.4.0): Requests showing. (shown on the next tick, any other text showing quickly gets out of the way first)
  - `text` &str: Text to show. (if it is the same as the text showing and not disappearing, only refills the stay time and gives a little bounce)
  - `seconds` f64: Stay time. (seconds, excluding the appear and disappear times, read_toast_seconds can fit it to the text length)
  - `apply_text` impl FnOnce(&mut ComponentContext) + 'static: A function that puts text and color into the node. (receives the context and is called once when it starts to appear; the original applyText)
- `pub fn hide_now(&mut self)` (method, since 0.4.0): Requests immediate hiding. (when the stage changes)
- `pub fn is_showing(&self) -> bool` (method, since 0.4.0): Whether it is showing. (appearing, staying, disappearing, or a show request pending)
  - returns bool: true if not in the hidden phase or a show request is pending.
- `pub fn get_phase(&self) -> ToastPhase` (method, since 0.4.0): Returns the phase.
  - returns ToastPhase: Current phase.
- `pub fn set_enter_side(&mut self, enter_side: f64)` (method, since 0.4.0): Sets the entry side.
  - `enter_side` f64: Entry side. (-1 from the top, 1 from the bottom)

## Functions and constants

- `pub fn read_toast_seconds(text: &str, base_seconds: f64) -> f64` (method, since 0.4.0): Toast stay time. (for long text, adds 0.5 seconds per 20 characters, up to 4 seconds)
  - `text` &str: Toast text. (characters are counted by char)
  - `base_seconds` f64: Base stay time. (seconds)
  - returns f64: Stay time. (seconds, adds 0.5 seconds for every 20 characters beyond 20, rounded up, and never exceeds 4 seconds. 4 even if base_seconds is longer than 4 seconds)

  Example: Stay seconds by text length

  ```rust
  use vanilla::read_toast_seconds;
  
  assert_eq!(read_toast_seconds("Saved", 2.0), 2.0);
  assert_eq!(read_toast_seconds(&"a".repeat(45), 2.0), 3.0);
  assert_eq!(read_toast_seconds(&"a".repeat(400), 2.0), 4.0);
  ```

- `pub fn attach_toast_motion(node_tree: &mut NodeTree, node_id: NodeId, enter_side: f64) -> Option<&mut ToastMotion>` (method, since 0.4.0): Attaches a motion to a toast node. (sets the entry side and returns the attached ToastMotion, original attachToastMotion)
  - `node_tree` &mut NodeTree: Node tree.
  - `node_id` NodeId: Notification node.
  - `enter_side` f64: Entry side. (-1 from the top, 1 from the bottom)
  - returns Option<&mut ToastMotion>: The attached ToastMotion. None if there is no node.

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

Notification phases.

- `Hidden`: Hidden. (the node is not turned off, only made invisible)
- `Enter`: Entering. (slides in while appearing, grows slightly and settles)
- `Stay`: Staying. (counts down the stay seconds)
- `Exit`: Exiting. (rises while fading out)
