# ui/uiwindowlayer (vanilla-engine 0.4.0)

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

Window layer. (vanilla.js ui/uiwindowlayer.js, UIWindowLayer)
- Stacks UIWindows over the area. Opening a window stacks it on top, and only the top window is visible. When the upper window closes, the window below becomes visible again.
- On close it leaves the list right away so presses and covering are released immediately, and after the close motion (PopupMotion) finishes the window node is detached from the layer. Closing a covered window detaches it right away.
  The detached window node is not removed, so it can be opened again with open_window. (same as the original) The scene removes windows it will not reuse with destroy_node.
- The top window gets the wheel first (UIWindowBehavior::handle_wheel), and if it does not take it, the scroll view at that spot does. (scroll_window_at, GameSceneBase passes wheel input pointing inside the window layer)
- This layer is enlarged by the UI scale (UIScale::read), and windows are told the area size and safe area widths divided by that much. (so text is readable even on small screens)
  With set_ui_scale_options(None), it does not enlarge.
- While even one window is open, play is suspended. It notifies when windows open and close (set_window_change_event, passes the number of open windows). The scene may check has_open_window and pause play.
- Usage: (placed as the top child of the area node, notified whenever the area size changes)
  ```text
    let layer_id = UIWindowLayer::create(node_tree, play_area_id);
    node_tree.with_component_mut::<UIWindowLayer, _>(layer_id, |layer, tree| {
        layer.set_view_size(tree, width, height, view_manager.get_css_pixel_scale());
        layer.set_view_bleed(tree, top, right, bottom, left);
    });
    let window_id = UIConfirmWindow::create(node_tree, layer_id, "quit", UIConfirmWindowOptions { title: ..., ..UIConfirmWindowOptions::default() });
    UIWindowLayer::open_window_in_tree(node_tree, layer_id, window_id);
  ```
- Differences from the original:
  - In the original it is a node inheriting WorldNode. The Rust version is a component attached to the layer node, and windows are child nodes of that node. Functions that change things take the tree.
  - The original reopens a detached window object if it is held. In the Rust version the detached node remains in the tree storage, so it is reopened with that handle.
    In the original a window held by no one is cleaned up by the garbage collector, but in the Rust version the scene removes it or it is removed together when the scene ends.
  - In the original the layer asks UIScale for the engine's CSS scale. In the Rust version set_view_size takes the CSS scale (ViewManager::get_css_pixel_scale).
  - The window notification is `Box<dyn FnMut(usize)>`. (number of open windows)

## UIWindowChangeEvent (type)

Notification when a window opens or closes. (number of open windows)

```rust
pub type UIWindowChangeEvent = Box<dyn FnMut(usize)>
```

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

Window layer.

Example: Placing a layer under the area node and opening a confirm window

```rust
use vanilla::{NodeTree, UIConfirmWindow, UIConfirmWindowOptions, UIWindowLayer, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let play_area_id = node_tree.create_child(root_id, "PlayArea");
node_tree[play_area_id].set_content_size(Vector2::create(800.0, 600.0));
let layer_id = UIWindowLayer::create(&mut node_tree, play_area_id);
node_tree.with_component_mut::<UIWindowLayer, _>(layer_id, |window_layer, tree| {
	window_layer.set_view_size(tree, 800.0, 600.0, 1.0);
	window_layer.set_view_bleed(tree, 40.0, 0.0, 30.0, 0.0);
});
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);
UIWindowLayer::open_window_in_tree(&mut node_tree, layer_id, window_id);
let window_layer = node_tree[layer_id].get_component::<UIWindowLayer>().expect("layer");
assert!(window_layer.has_open_window());
assert_eq!(window_layer.get_top_window(), Some(window_id));
assert_eq!(window_layer.find_window(&node_tree, "quit"), Some(window_id));
```

- `pub fn new() -> UIWindowLayer` (constructor, since 0.4.0): Constructor. (attach to a node with node_tree.add_component, usually UIWindowLayer::create)
  - returns UIWindowLayer: Layer before being attached. (no open windows, UI scale settings are UIScaleOptions::default)
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId) -> NodeId` (static, since 0.4.0): Creates a layer node. (creates and attaches a "UIWindowLayer" node under parent_id, original new UIWindowLayer() + add_child)
  - `node_tree` &mut NodeTree: Node tree.
  - `parent_id` NodeId: Area node. (the layer is used as its top child)
  - returns NodeId: The created layer node.
- `pub fn open_window_in_tree(node_tree: &mut NodeTree, layer_id: NodeId, window_id: NodeId)` (static, since 0.4.0): Opens a window. (for scene code, calls with_component_mut on your behalf)
  - `node_tree` &mut NodeTree: Node tree.
  - `layer_id` NodeId: Window layer node. (does nothing if there is no UIWindowLayer)
  - `window_id` NodeId: Window node to open. (the one returned by UIWindow::create or UIConfirmWindow::create)
- `pub fn set_window_change_event(&mut self, window_change_event: Option<UIWindowChangeEvent>)` (method, since 0.4.0): Sets the notification for when a window opens or closes.
  - `window_change_event` Option<UIWindowChangeEvent>: Notification. (receives the number of open windows, None turns it off)

  Example: Pausing play while a window is open

  ```rust
  use std::cell::Cell;
  use std::rc::Rc;
  use vanilla::{NodeTree, UIWindow, UIWindowDefaultBehavior, 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 is_paused = Rc::new(Cell::new(false));
  let is_paused_event = is_paused.clone();
  node_tree[layer_id].get_component_mut::<UIWindowLayer>().expect("layer").set_window_change_event(Some(Box::new(move |window_count| {
  	is_paused_event.set(window_count > 0);
  })));
  let window_id = UIWindow::create(&mut node_tree, layer_id, "pause", Box::new(UIWindowDefaultBehavior));
  UIWindowLayer::open_window_in_tree(&mut node_tree, layer_id, window_id);
  assert!(is_paused.get());
  UIWindow::close_in_tree(&mut node_tree, window_id);
  assert!(!is_paused.get());
  ```

- `pub fn set_ui_scale_options(&mut self, node_tree: &mut NodeTree, ui_scale_options: Option<UIScaleOptions>)` (method, since 0.4.0): Sets the UI scale. (None means no enlargement, default is UIScaleOptions::default)
  - `node_tree` &mut NodeTree: Node tree holding the layer. (lays out again with the last area size)
  - `ui_scale_options` Option<UIScaleOptions>: UI scale settings. (None means scale 1)
- `pub fn get_ui_scale(&self) -> f64` (method, since 0.4.0): Current UI scale.
  - returns f64: Scale the layer was enlarged by. (the value read at the last set_view_size, initially 1)
- `pub fn has_open_window(&self) -> bool` (method, since 0.4.0): Whether at least one window is open. (windows in the close motion are not counted)
  - returns bool: true if a window is open.
- `pub fn get_window_count(&self) -> usize` (method, since 0.4.0): Number of open windows.
  - returns usize: Number of windows. (excluding windows in their close motion)
- `pub fn get_top_window(&self) -> Option<NodeId>` (method, since 0.4.0): The top window. (None if none)
  - returns Option<NodeId>: Top window node. (None if no window is open)
- `pub fn find_window(&self, node_tree: &NodeTree, window_name: &str) -> Option<NodeId>` (method, since 0.4.0): The window with that name. (from the top among open ones, None if none)
  - `node_tree` &NodeTree: Node tree holding the layer.
  - `window_name` &str: Window node name to find. (the name of UIWindow::create)
  - returns Option<NodeId>: The topmost of the window nodes with that name. (None if not among the open windows)
- `pub fn set_view_size(&mut self, node_tree: &mut NodeTree, view_width: f64, view_height: f64, css_pixel_scale: f64)` (method, since 0.4.0): Notifies the area size. (reads the UI scale again and lays out all open windows again, css_pixel_scale is ViewManager::get_css_pixel_scale)
  - `node_tree` &mut NodeTree: Node tree holding the layer.
  - `view_width` f64: Area width. (parent units, 1 if less than 1)
  - `view_height` f64: Area height. (parent units, 1 if less than 1)
  - `css_pixel_scale` f64: CSS pixels of one view unit. (used by UIScale::read to determine the scale)

  Example: On a small screen the layer is enlarged and windows are told the divided size

  ```rust
  use vanilla::{NodeTree, 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);
  node_tree.with_component_mut::<UIWindowLayer, _>(layer_id, |window_layer, tree| {
  	window_layer.set_view_size(tree, 800.0, 600.0, 0.4);
  });
  let ui_scale = node_tree[layer_id].get_component::<UIWindowLayer>().expect("layer").get_ui_scale();
  assert!((ui_scale - 1.8).abs() < 0.0001);
  assert!((node_tree[layer_id].get_content_size().x - 800.0 / 1.8).abs() < 0.001);
  node_tree.with_component_mut::<UIWindowLayer, _>(layer_id, |window_layer, tree| {
  	window_layer.set_ui_scale_options(tree, None);
  });
  assert_eq!(node_tree[layer_id].get_component::<UIWindowLayer>().expect("layer").get_ui_scale(), 1.0);
  ```

- `pub fn set_view_bleed(&mut self, node_tree: &mut NodeTree, top: f64, right: f64, bottom: f64, left: f64)` (method, since 0.4.0): Notifies the extra coverage width beyond the area. (safe area, parent units)
  - Windows are told the widths divided by the UI scale.
  - `node_tree` &mut NodeTree: Node tree holding the layer.
  - `top` f64: Extra coverage width upward. (0 if less than 0)
  - `right` f64: Extra coverage width to the right. (0 if less than 0)
  - `bottom` f64: Extra coverage width downward. (0 if less than 0)
  - `left` f64: Extra coverage width to the left. (0 if less than 0)
- `pub fn open_window(&mut self, node_tree: &mut NodeTree, window_id: NodeId)` (method, since 0.4.0): Opens a window. (stacks it on top, brings it to the top if already open, attaches it if it is not a child of the layer node. Also reopens detached windows)
  - Does nothing if the layer is not attached to the tree yet or the window node is not in the tree.
  - `node_tree` &mut NodeTree: Node tree holding the layer.
  - `window_id` NodeId: Window node to open. (the node with UIWindow attached)
- `pub fn close_window(&mut self, node_tree: &mut NodeTree, window_id: NodeId)` (method, since 0.4.0): Closes a window. (removes it from the list and starts the close motion, does nothing if not open)
  - Windows hidden by covering or with instant transitions are detached right away.
  - `node_tree` &mut NodeTree: Node tree holding the layer.
  - `window_id` NodeId: Window node to close.
- `pub fn close_all_windows(&mut self, node_tree: &mut NodeTree)` (method, since 0.4.0): Closes all windows. (from the top)
  - Also closes windows with outside close turned off.
  - `node_tree` &mut NodeTree: Node tree holding the layer.
- `pub fn close_top_window(&mut self, node_tree: &mut NodeTree) -> bool` (method, since 0.4.0): Closes the top window. (for the device's back button, does not close a window with outside close turned off, true if closed)
  - `node_tree` &mut NodeTree: Node tree holding the layer.
  - returns bool: true if closed. (false if no window is open or the top window has outside close turned off)

  Example: Letting the window get back first

  ```rust
  use vanilla::{NodeTree, UIWindow, UIWindowDefaultBehavior, 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 = UIWindow::create(&mut node_tree, layer_id, "tutorial", Box::new(UIWindowDefaultBehavior));
  node_tree[window_id].get_component_mut::<UIWindow>().expect("window").set_outside_close_enabled(false);
  UIWindowLayer::open_window_in_tree(&mut node_tree, layer_id, window_id);
  let is_closed = node_tree.with_component_mut::<UIWindowLayer, _>(layer_id, |window_layer, tree| {
  	window_layer.close_top_window(tree)
  });
  assert_eq!(is_closed, Some(false));
  node_tree[window_id].get_component_mut::<UIWindow>().expect("window").set_outside_close_enabled(true);
  let is_closed = node_tree.with_component_mut::<UIWindowLayer, _>(layer_id, |window_layer, tree| {
  	window_layer.close_top_window(tree)
  });
  assert_eq!(is_closed, Some(true));
  ```

- `pub fn scroll_window_at(node_tree: &mut NodeTree, engine: &Engine, layer_id: NodeId, view_input_position: Vector2, wheel_delta: Vector2) -> bool` (static, since 0.4.0): Passes the mouse wheel to the top window. (the scroll view at that spot if the window does not take it, true if taken, original scrollWindowAt)
  - `node_tree` &mut NodeTree: Node tree holding the layer.
  - `engine` &Engine: Engine. (used when passing the wheel to a scroll view)
  - `layer_id` NodeId: Window layer node.
  - `view_input_position` Vector2: Position where the wheel was rolled. (view coordinates)
  - `wheel_delta` Vector2: Amount the wheel was rolled.
  - returns bool: true if the top window or the scroll view at that spot took it. (false if no window is open or nothing takes it)

  Example: Passing the scene's wheel input to the window layer first

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeId, NodeTree, UIWindowLayer, Vector2};
  
  fn handle_wheel(node_tree: &mut NodeTree, engine: &Engine, layer_id: NodeId, wheel_delta: Vector2) -> bool {
  	let view_input_position = engine.get_input_manager().get_view_input_position();
  	UIWindowLayer::scroll_window_at(node_tree, engine, layer_id, view_input_position, wheel_delta)
  }
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let layer_id = UIWindowLayer::create(&mut node_tree, root_id);
  assert!(!handle_wheel(&mut node_tree, &engine, layer_id, Vector2::create(0.0, 120.0)));
  ```

- `pub fn find_scroll_view_at(node_tree: &NodeTree, node_id: NodeId, view_input_position: Vector2) -> Option<NodeId>` (static, since 0.4.0): Finds the frontmost (drawn later) scroll view node covering that spot. (only active nodes, nodes with a wheel receiving slot (Component::is_wheel_receiver) attached, None if none)
  - `node_tree` &NodeTree: Node tree to search.
  - `node_id` NodeId: Node to start searching from. (looks at that node and its descendants from the later children)
  - `view_input_position` Vector2: Position to look at. (view coordinates)
  - returns Option<NodeId>: The frontmost scroll view node covering that spot. (None if the start node is off or there is no such node)
- `pub fn find_window_layer_of(node_tree: &NodeTree, node_id: NodeId) -> Option<NodeId>` (static, since 0.4.0): Finds the window layer node containing the node. (from itself toward the parents, None if none, original findWindowLayerOf)
  - `node_tree` &NodeTree: Node tree to search.
  - `node_id` NodeId: Node to start searching from. (like a button inside a window)
  - returns Option<NodeId>: The closest ancestor (including itself) node with UIWindowLayer attached. (None if none up to the root)

  Example: Finding the window layer from a node inside the panel

  ```rust
  use vanilla::{NodeTree, UIWindow, UIWindowDefaultBehavior, 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 = UIWindow::create(&mut node_tree, layer_id, "inventory", Box::new(UIWindowDefaultBehavior));
  let panel_id = node_tree[window_id].get_component::<UIWindow>().expect("window").get_panel_node().expect("panel");
  let item_id = node_tree.create_child(panel_id, "Item");
  assert_eq!(UIWindowLayer::find_window_layer_of(&node_tree, item_id), Some(layer_id));
  assert_eq!(UIWindowLayer::find_window_layer_of(&node_tree, root_id), None);
  ```

