# web/pane/nodepane (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/web/pane/nodepane/

Engine node split panes. (PaneStyle and Pane of the original web/pane.js built from nodes on desktop, shared types and math in web/pane.rs)
- Nodes take the place of the original DOM elements. The container is a node (get_container), and content is attached as its children with node_tree.add_child. (original appendChild)
  Desktop has no global document, so functions that create and change nodes take a NodeTree. The length unit is node coordinates, in place of the original CSS pixels.
  ```text
    let main_pane = Pane::new(node_tree, PaneOptions { direction: PaneDirection::Horizontal, ..Default::default() });
    let left_pane = Pane::new(node_tree, PaneOptions { size: PaneSize::Fixed(240.0), min_size: 170.0, ..Default::default() });
    main_pane.add_pane(node_tree, &left_pane);
    let panel_id = PaneStyle::create(node_tree, "div", "panel", &PaneStyleOptions::default());
    node_tree.add_child(left_pane.get_container(), panel_id);
    main_pane.attach_to(node_tree, root_id);
  ```
- Like the original, the container paints its background with COLOR_BACKGROUND and clips overflowing children. (overflow: hidden, Mask)
- Like the original, a resizer is 5 thick if draggable and otherwise a line of 1, painted with COLOR_RESIZER, and with COLOR_RESIZER_HOVER while hovered or dragged.
  Pressing and dragging keeps the gap between the grabbed point and the boundary, changes the fixed size of the previous or next child, and lays out again from the root. The cursor is col-resize, row-resize while hovered or dragged.
- A root pane given to attach_to lays out again when the parent node size changes. (original window resize listener and refresh on the next frame)
- PaneStyle::create builds nodes shaped like the original presets. (PaneElement component)
  Common: placed at the top left filling the parent size (position absolute, 100%), font 13px sans-serif, text color COLOR_TEXT.
  panel: background COLOR_PANEL, padding 10. toolbar: background COLOR_TOOLBAR, bottom border 1px COLOR_BORDER, gathered horizontally in the center 10 apart (flex).
  button: own size (position relative, auto), padding 6 16, corner 4, bold text, background COLOR_RESIZER (COLOR_ACCENT if accent, COLOR_WARNING if warning),
  1.2 times brightness and the pointer cursor when hovered, the set_on_click function when pressed and released. input: engine input field (UIInputField), background COLOR_INPUT_BACKGROUND, padding 4.
  textarea: multiline input field with background COLOR_BACKGROUND, text #d4d4d4, monospace 14px, padding 15, tab size 4. text: own size, bold text.
  relative elements are placed inside the parent's padding. Block parents stack them vertically (div fills the width, others take their own width), and flex parents, like CSS Flexbox,
  place the base size (flex-basis, else width or height, own size if that is also auto) along the main axis (flex-direction row, column) and split the remaining space by the flex-grow ratio, or
  shrink by the ratio of flex-shrink times the base size if short. (items hitting min or max are frozen and the rest split again, the automatic minimum of items whose overflow is visible is their own size)
  The cross axis follows align-items (default stretch, flex-start, center, flex-end), and main axis distribution follows justify-content (flex-start, center, flex-end, space-between, space-around, space-evenly) and gap (row gap, column gap).
  With flex-wrap (wrap, wrap-reverse), like CSS Flexbox 9.3, outer hypothetical main sizes are added in order and a new line starts at the item that overflows, and each line grows and shrinks,
  the cross size of a line is the largest outer cross size of its items, and lines are distributed by align-content (the default stretch adds the remaining space evenly to each line). wrap-reverse swaps the start and end of the cross axis.
  The own size of an auto sized element is its text and children (vertical sum for blocks, horizontal sum for horizontal flex) plus padding and border.
- If overflow (overflow-x, overflow-y) is not visible, overflowing children are clipped (corners by border-radius), and with auto, scroll it scrolls with the wheel and by dragging the thumb.
  Like the original scroll box, if it cannot scroll further in that direction it passes the wheel to the parent. The thumb is the thumb color of scrollbar-color (rgba(121, 121, 121, 0.4) of the original editor).
- Differences from the original:
  - What is attached with addEventListener("click") on an original element is PaneElement::set_on_click.
  - The CSS read by options.style and PaneStyle::apply is what the presets and the original tools (particle, timeline, UI editor) use:
    position(absolute, relative), left, top, right, bottom, width, height, min-width, min-height, max-width, max-height(px, %, auto),
    display(flex, block, none), flex-direction, flex, flex-grow, flex-shrink, flex-basis, justify-content, align-items, gap,
    padding, margin(1 to 4 values and per side), background, background-color, color, font-size, font-weight, font-family, line-height, text-align,
    border, border-top, border-right, border-bottom, border-left, border-width, border-color, border-radius, cursor(pointer),
    overflow, overflow-x, overflow-y, scrollbar-color, scrollbar-width, flex-wrap, flex-flow, align-content, row-gap, column-gap, white-space, text-overflow, opacity.
    opacity is laid down at once over the element and its descendants, like Chrome. (node opacity and an opacity layer, WorldNode::set_opacity_layer)
    Others (such as user-select) are not read. Text does not wrap. (with white-space: nowrap and text-overflow: ellipsis, if overflow is not visible the overflowing end becomes an ellipsis)
  - Scroll bars follow the operating system like Chrome. Windows and Linux use the space-taking Fluent scroll bar (thickness 15, thin 10), and Mac uses an overlay thumb of thickness 8 if the system setting is overlay,
    or a space-taking scroll bar (thickness 15, thin 11) if classic. A space-taking scroll bar sits inside the border and outside the padding, and takes content width always for overflow: scroll and only when overflowing for auto.
    (read_classic_scrollbar_metrics in web/pane.rs, button arrows are not drawn and the track is painted only when scrollbar-color has a second value)
  - For the textarea tag, the engine multiline input field (UIInputField::set_multiline) takes the text. Like the original DOM textarea, it inserts lines, wraps to the width, moves up and down, and scrolls with the wheel when overflowing.
    Uses the preset's tab size 4. Font, color and padding take the style at creation. (fonts changed later with PaneStyle::apply are not carried to the field)
  - The canvas tag is where the original puts the preview engine's canvas. On the desktop, draw the preview scene into a render target and pass it with PaneElement::set_canvas_texture,
    and that screen is drawn in the element's inner box. (The box size is calculate_canvas_box_size; multiply by the device pixel ratio to size the render target) Without it only the background and border are drawn.

## PaneClickCallback (type)

Function called when the button is pressed and released. (original click listener)

```rust
pub type PaneClickCallback = Box<dyn FnMut(&mut ComponentContext)>
```

## Pane (struct) (Clone)

Screen split pane. (original Pane, clones point to the same pane)

Example: Splitting into a list on the left and a view on the right, attaching to the root node, and hiding the list

```rust
use vanilla::{NodeTree, Pane, PaneOptions, PaneSize, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
node_tree[root_id].set_content_size(Vector2::create(1000.0, 600.0));
let main_pane = Pane::new(&mut node_tree, PaneOptions::default());
let left_pane = Pane::new(&mut node_tree, PaneOptions { size: PaneSize::Fixed(240.0), min_size: 170.0, ..Default::default() });
let view_pane = Pane::new(&mut node_tree, PaneOptions { min_size: 200.0, ..Default::default() });
main_pane.add_pane(&mut node_tree, &left_pane);
main_pane.add_pane(&mut node_tree, &view_pane);
main_pane.attach_to(&mut node_tree, root_id);
assert_eq!(node_tree[left_pane.get_container()].get_content_size(), Vector2::create(240.0, 600.0));
assert_eq!(node_tree[view_pane.get_container()].get_local_position(), Vector2::create(245.0, 0.0));
left_pane.set_visible(&mut node_tree, false);
assert_eq!(node_tree[view_pane.get_container()].get_local_position(), Vector2::create(0.0, 0.0));
assert_eq!(node_tree[view_pane.get_container()].get_content_size(), Vector2::create(1000.0, 600.0));
```

- `pub fn new(node_tree: &mut NodeTree, options: PaneOptions) -> Pane` (constructor, since 0.4.0): Constructor. (creates a detached container node. Background and clipping)
  - `node_tree` &mut NodeTree: Tree in which to create the container node.
  - `options` PaneOptions: Pane options.
  - returns Pane: New pane. (the container node is not attached anywhere yet, attach it with attach_to or the parent's add_pane)
- `pub fn add_pane(&self, node_tree: &mut NodeTree, pane: &Pane)` (method, since 0.4.0): Adds a child. (places a resizer node in between if there is a previous child)
  - `node_tree` &mut NodeTree: Node tree.
  - `pane` &Pane: Child pane to attach. (its container node becomes a child of this pane's container)
- `pub fn update_layout(&self, node_tree: &mut NodeTree, x: f64, y: f64, width: f64, height: f64)` (method, since 0.4.0): Layout. (places itself, and places the children and resizers with the same computation. Original updateLayout)
  - `node_tree` &mut NodeTree: Node tree.
  - `x` f64: Container left. (parent node coordinates)
  - `y` f64: Container top. (parent node coordinates)
  - `width` f64: Container width.
  - `height` f64: Container height.
- `pub fn reset_layout(&self)` (method, since 0.4.0): Resets the layout. (clears sizes changed by dragging, including children)
- `pub fn refresh(&self, node_tree: &mut NodeTree)` (method, since 0.4.0): Lays out again to fit the parent node size. (does nothing if there is no parent)
  - `node_tree` &mut NodeTree: Node tree.
- `pub fn attach_to(&self, node_tree: &mut NodeTree, parent_id: NodeId)` (method, since 0.4.0): Attaches to a node. (lays out on the spot, and lays out again later when the parent size changes. The window resize listener of the original attachTo)
  - `node_tree` &mut NodeTree: Node tree.
  - `parent_id` NodeId: Parent node to attach to.
- `pub fn set_visible(&self, node_tree: &mut NodeTree, is_visible: bool)` (method, since 0.4.0): Shows / hides. (a hidden pane takes no space, and the adjoining resizers are hidden too)
  - `node_tree` &mut NodeTree: Node tree.
  - `is_visible` bool: Whether to show it.
- `pub fn is_visible(&self) -> bool` (method, since 0.4.0): Whether visible.
  - returns bool: True if visible.
- `pub fn is_flex(&self) -> bool` (method, since 0.4.0): Whether flex. (initial size is Flex)
  - returns bool: True if the initial size is Flex.
- `pub fn get_initial_size(&self) -> PaneSize` (method, since 0.4.0): Initial size.
  - returns PaneSize: Initial size given at creation. (PaneOptions::size)
- `pub fn get_fixed_size(&self) -> f64` (method, since 0.4.0): Fixed size. (pixels, measured from the initial size if 0)
  - returns f64: Fixed size. (pixels, the value if set by dragging or the initial size is Fixed, 0 after reset_layout or otherwise)
- `pub fn set_fixed_size(&self, fixed_size: f64)` (method, since 0.4.0): Sets the fixed size. (used from the next layout. Not used for flex panes)
  - `fixed_size` f64: New fixed size. (pixels, measured from the initial size if 0)
- `pub fn get_min_size(&self) -> f64` (method, since 0.4.0): Minimum size.
  - returns f64: Minimum size. (pixels)
- `pub fn get_max_size(&self) -> f64` (method, since 0.4.0): Maximum size.
  - returns f64: Largest size. (pixels)
- `pub fn get_container(&self) -> NodeId` (method, since 0.4.0): Container node. (content is attached as its children)
  - returns NodeId: Container node.
- `pub fn set_callback(&self, resize_callback: Option<PaneResizeCallback>)` (method, since 0.4.0): Sets the layout callback. (called every time update_layout finishes. None clears it)
  - `resize_callback` Option<PaneResizeCallback>: Function called whenever update_layout finishes. (None clears it)
- `pub fn is_resizable(&self) -> bool` (method, since 0.4.0): Whether it can be dragged with a resizer.
  - returns bool: True if it can be dragged with a resizer.
- `pub fn get_resizable_edges(&self) -> PaneResizableEdges` (method, since 0.4.0): Edges that can be dragged.
  - returns PaneResizableEdges: Edges that can be dragged.

## PaneElement (struct) (Component)

Element built by PaneStyle. (styles and text of the original DOM element, block and flex layout, overflow clipping and scrolling, button presses)

Example: Calling a function when a button in a panel is pressed and released

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{Engine, EngineConfiguration, NodeTree, PaneElement, PaneStyle, PaneStyleOptions, TouchKind, 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(400.0, 300.0));
let panel_id = PaneStyle::create(&mut node_tree, "div", "panel", &PaneStyleOptions::default());
let button_id = PaneStyle::create(&mut node_tree, "button", "button", &PaneStyleOptions { text: "Save", ..Default::default() });
node_tree.add_child(root_id, panel_id);
node_tree.add_child(panel_id, button_id);
let click_count = Rc::new(Cell::new(0));
let click_count_for_callback = click_count.clone();
let button = node_tree[button_id].get_component_mut::<PaneElement>().unwrap();
assert_eq!(button.get_tag_name(), "button");
assert_eq!(button.get_text(), "Save");
button.set_on_click(Some(Box::new(move |_context| {
	click_count_for_callback.set(click_count_for_callback.get() + 1);
})));
node_tree.tick(&engine, 1.0 / 60.0);
node_tree.tick(&engine, 1.0 / 60.0);
assert_eq!(node_tree[button_id].get_local_position(), Vector2::create(10.0, 10.0));
let button_size = node_tree[button_id].get_content_size();
let button_center = Vector2::create(10.0 + button_size.x * 0.5, 10.0 + button_size.y * 0.5);
node_tree.dispatch_touch(button_id, &engine, TouchKind::Press, button_center);
node_tree.dispatch_touch(button_id, &engine, TouchKind::Release, button_center);
assert_eq!(click_count.get(), 1);
```

- `pub fn set_canvas_texture(&mut self, canvas_texture: Option<GlTexture>)` (method, since 0.4.0): Sets the screen the canvas tag draws. (Where the original tools run a preview engine in the canvas element; on the desktop pass the color texture of a render target the preview scene was drawn into)
  - A canvas tag element stretches this texture over the box inside its padding and border, on top of the background and border. It is treated as a texture whose row 0 is at the bottom, like a render target texture.
  - Sizing the render target to that box times the device pixel ratio, like the original canvas, draws it 1:1. (calculate_canvas_box_size)
  - `canvas_texture` Option<GlTexture>: The color texture to draw. (None draws only the background and border)

  Example: Put a render target with the preview drawn into a Pane canvas element

  ```rust
  use vanilla::{NodeId, NodeTree, PaneElement, RenderTarget};
  
  fn show_preview(node_tree: &mut NodeTree, canvas_id: NodeId, render_target: &RenderTarget) {
  	if let Some(element) = node_tree[canvas_id].get_component_mut::<PaneElement>() {
  		element.set_canvas_texture(render_target.get_color_texture(0));
  	}
  }
  ```

- `pub fn get_canvas_texture(&self) -> Option<GlTexture>` (method, since 0.4.0): The screen the canvas tag draws.
  - returns Option<GlTexture>: The color texture passed with set_canvas_texture. (None if none)
- `pub fn calculate_canvas_box_size(&self, content_size: Vector2) -> Vector2` (method, since 0.4.0): The size of the box the canvas tag draws the screen in. (The content size minus padding and border, in node coordinates, where the original canvas element has clientWidth, clientHeight)
  - `content_size` Vector2: The content size of the element node. (node_tree[node].get_content_size())
  - returns Vector2: The width and height of the box. (Multiplied by the device pixel ratio, the render target size)
- `pub fn set_on_click(&mut self, click_callback: Option<PaneClickCallback>)` (method, since 0.4.0): Function to call when pressed and released. (the original element's addEventListener("click"))
  - `click_callback` Option<PaneClickCallback>: Function called when pressed and released. (called only for button preset elements, None clears it)
- `pub fn set_text(&mut self, text: &str)` (method, since 0.4.0): Sets the text. (original innerText)
  - `text` &str: Text to show.
- `pub fn get_text(&self) -> &str` (method, since 0.4.0): Text.
  - returns &str: The shown text. (the text of a textarea is held by the input field, so this is empty)
- `pub fn get_tag_name(&self) -> &str` (method, since 0.4.0): Tag name. (lowercase)
  - returns &str: Lowercase tag name.
- `pub fn get_scroll_offset(&self) -> Vector2` (method, since 0.4.0): Scroll offset. (the original scrollLeft, scrollTop)
  - returns Vector2: Scrolled amount. (x is scrollLeft, y is scrollTop)
- `pub fn set_scroll_offset(&mut self, scroll_offset: Vector2)` (method, since 0.4.0): Sets the scroll offset. (sets the original scrollLeft, scrollTop. clamped to within the end of the content on the next layout)
  - `scroll_offset` Vector2: Amount to scroll. (negative becomes 0)
- `pub fn apply_style(&mut self, property_name: &str, property_value: &str)` (method, since 0.4.0): Puts in one CSS property. (reads only the properties in the header comment, others are ignored)
  - `property_name` &str: CSS property name. (a name like background-color)
  - `property_value` &str: Value. (leading and trailing spaces are trimmed)

## PaneStyle (struct)

Style and element creation utility. (original PaneStyle)

Example: Placing a button on a toolbar and changing the style later

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, PaneStyle, PaneStyleOptions, 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(600.0, 40.0));
let toolbar_id = PaneStyle::create(&mut node_tree, "div", "toolbar", &PaneStyleOptions::default());
let play_button_id = PaneStyle::create(&mut node_tree, "button", "button", &PaneStyleOptions { text: "Play", accent: true, ..Default::default() });
node_tree.add_child(root_id, toolbar_id);
node_tree.add_child(toolbar_id, play_button_id);
PaneStyle::apply(&mut node_tree, toolbar_id, &[("justify-content", "flex-start"), ("padding", "0 8px")]);
node_tree.tick(&engine, 1.0 / 60.0);
node_tree.tick(&engine, 1.0 / 60.0);
assert_eq!(node_tree[toolbar_id].get_content_size(), Vector2::create(600.0, 40.0));
assert_eq!(node_tree[play_button_id].get_local_position().x, 8.0);
```

- `pub fn apply(node_tree: &mut NodeTree, node_id: NodeId, styles: &[(&str, &str)])` (static, since 0.4.0): Puts several styles into an element at once. ((CSS property name, value) list, does nothing for a node without a PaneElement)
  - `node_tree` &mut NodeTree: Node tree.
  - `node_id` NodeId: Element node to apply the styles to.
  - `styles` &[(&str, &str)]: List of (CSS property name, value).
- `pub fn create(node_tree: &mut NodeTree, tag_name: &str, preset: &str, options: &PaneStyleOptions) -> NodeId` (static, since 0.4.0): Creates an element node with a style preset applied. (a detached node, attach it with node_tree.add_child)
  (preset: "panel" | "toolbar" | "button" | "input" | "textarea" | "text", only the common style otherwise)
  - `node_tree` &mut NodeTree: Tree to create the node in.
  - `tag_name` &str: Tag name. (for input and textarea the engine input field takes the text)
  - `preset` &str: Style preset. ("panel" | "toolbar" | "button" | "input" | "textarea" | "text")
  - `options` &PaneStyleOptions: id, text, additional styles, placeholder, button color.
  - returns NodeId: The created element node. (a detached node)
