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

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

DOM screen split pane. (the web version porting PaneStyle and Pane of the original web/pane.js to the DOM as is, common types and calculations in web/pane.rs)
  ```text
    let main_pane = Pane::new(PaneOptions { direction: PaneDirection::Horizontal, ..Default::default() });
    let left_pane = Pane::new(PaneOptions { size: PaneSize::Fixed(240.0), min_size: 170.0, ..Default::default() });
    let view_pane = Pane::new(PaneOptions { min_size: 200.0, ..Default::default() });
    main_pane.add_pane(&left_pane);
    main_pane.add_pane(&view_pane);
    left_pane.get_container().append_child(&PaneStyle::create("div", "panel", &PaneStyleOptions::default()));
    main_pane.attach_to(&body);
  ```
- Differences from the original:
  - PaneStyle::apply styles are a list of (CSS property name, value). Names are CSS names (background-color), not the original object keys (backgroundColor).
    (Rust has no Object.assign, so they are set with style.setProperty)
    If the tag name is invalid, it stops on the spot as in the original. (an exception in the original, a panic in the Rust version)
  - Pane is a handle that shares its state as `Rc<RefCell>`. clone points to the same pane. Children point to their parent with Weak.
    The window resize listener added by attach_to holds the root pane until the end. (same as the original) Panes that were not attach_to'd are freed when all handles are dropped, and dragging their resizers does nothing.
  - The resizer thickness is written to the data-thickness attribute as in the original, but calculations use the value held on the Rust side.
  - Listeners are wasm-bindgen Closures. Those attached to elements are handed to the JS garbage collector with into_js_value, and the element is read from the event's currentTarget.
    (holding the element would make the Rust side keep it alive, so it would not be freed even when removed from the DOM)
  - set_callback can be cleared with None. Calling into the pane again inside the callback is fine. (it is taken out while being called and put back afterward)

## PaneStyle (struct)

Style and element creation utility. (original PaneStyle)

Example: Placing two buttons on a toolbar and attaching it to body (compile only, needs a browser)

```rust
use vanilla::{PaneStyle, PaneStyleOptions};

let body = web_sys::window().unwrap().document().unwrap().body().unwrap();
let toolbar = PaneStyle::create("div", "toolbar", &PaneStyleOptions { style: &[("height", "40px")], ..Default::default() });
let play_button = PaneStyle::create("button", "button", &PaneStyleOptions { text: "Play", accent: true, ..Default::default() });
let reset_button = PaneStyle::create("button", "button", &PaneStyleOptions { text: "Reset", warning: true, ..Default::default() });
toolbar.append_child(&play_button).unwrap();
toolbar.append_child(&reset_button).unwrap();
PaneStyle::apply(&toolbar, &[("justify-content", "flex-start")]);
body.append_child(&toolbar).unwrap();
```

- `pub fn apply(element: &HtmlElement, styles: &[(&str, &str)])` (static, since 0.4.0): Puts several styles into an element at once. ((CSS property name, value) list)
  - `element` &HtmlElement: Element to apply the styles to.
  - `styles` &[(&str, &str)]: List of (CSS property name, value). (names are CSS names like background-color)
- `pub fn create(tag_name: &str, preset: &str, options: &PaneStyleOptions) -> HtmlElement` (static, since 0.4.0): Creates an element with a style preset applied.
  (preset: "panel" | "toolbar" | "button" | "input" | "textarea" | "text", only the common style otherwise)
  - `tag_name` &str: Tag name of the element to create. (panic if it cannot be created or is not an HTML element)
  - `preset` &str: Style preset. ("panel" | "toolbar" | "button" | "input" | "textarea" | "text")
  - `options` &PaneStyleOptions: id, text, additional styles, type, placeholder, button color.
  - returns HtmlElement: The created element. (not attached to the document yet)

## 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 and attaching to body (compile only, needs a browser)

```rust
use vanilla::{Pane, PaneOptions, PaneSize, PaneStyle, PaneStyleOptions};

let body = web_sys::window().unwrap().document().unwrap().body().unwrap();
let main_pane = Pane::new(PaneOptions::default());
let left_pane = Pane::new(PaneOptions { size: PaneSize::Fixed(240.0), min_size: 170.0, ..Default::default() });
let view_pane = Pane::new(PaneOptions { min_size: 200.0, ..Default::default() });
main_pane.add_pane(&left_pane);
main_pane.add_pane(&view_pane);
let panel = PaneStyle::create("div", "panel", &PaneStyleOptions::default());
left_pane.get_container().append_child(&panel).unwrap();
main_pane.set_callback(Some(Box::new(|| {
	println!("layout changed");
})));
main_pane.attach_to(&body);
```

- `pub fn new(options: PaneOptions) -> Pane` (constructor, since 0.4.0): Constructor. (creates the container div and builds the drag listeners to attach to the document)
  - `options` PaneOptions: Pane options.
  - returns Pane: New pane. (the container div is not attached to the document yet)
- `pub fn add_pane(&self, pane: &Pane)` (method, since 0.4.0): Adds a child. (places a resizer in between if there is a previous child)
  - `pane` &Pane: Child pane to attach.
- `pub fn update_layout(&self, x: f64, y: f64, width: f64, height: f64)` (method, since 0.4.0): Layout. (places itself, measures fixed size children first, splits the remaining space among flex children, then places the children and resizers in order)
  - `x` f64: Container left. (pixels, relative to the parent element)
  - `y` f64: Container top. (pixels, relative to the parent element)
  - `width` f64: Container width. (pixels)
  - `height` f64: Container height. (pixels)
- `pub fn reset_layout(&self)` (method, since 0.4.0): Resets the layout. (clears sizes changed by dragging, including children)
- `pub fn refresh(&self)` (method, since 0.4.0): Lays out again to fit the parent element size. (the window size if the parent size is 0, does nothing if there is no parent)
- `pub fn attach_to(&self, parent_element: &Element)` (method, since 0.4.0): Attaches to an element. (lays out again when the window size changes and on the next frame. The window listener holds this pane until the end)
  - `parent_element` &Element: Parent element to attach to. (usually body)
- `pub fn set_visible(&self, is_visible: bool)` (method, since 0.4.0): Shows / hides. (a hidden pane takes no space, and the adjoining resizers are hidden too)
  - `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) -> HtmlElement` (method, since 0.4.0): Container element. (content is attached here)
  - returns HtmlElement: Container div element.
- `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.
