# ui/autolayout/layoutvariable (vanilla-engine 0.4.0)

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

Layout solver unknown. (LayoutVariable of the original ui/autolayout/layoutvariable.js)
- A variable user code uses in expressions. After the solver solves, calling update_variables puts the result in via set_value.
- Differences from the original:
  - In the original, several places (nodes, expressions, the solver) point to one object. The Rust version is a cheap Clone handle holding an Rc, so
    cloned handles see the same name and value. Equality and hashing go by the variable the handle points to, like the original's object identity.
  - get_id, which the original lacks, reads the unique number assigned to each variable.
  - The name and value are kept in RefCell and Cell so they change through &self.

## LayoutVariableEvent (type)

Function that receives the changed variable. (the function of the original setValueChangedEvent)

```rust
pub type LayoutVariableEvent = Rc<dyn Fn(&LayoutVariable)>
```

## LayoutVariable (struct) (Clone, PartialEq, Eq, Hash, fmt::Debug)

Layout solver unknown. (clones point to the same variable)

Example: Reading the value the solver solved, cloned handles see the same value

```rust
use vanilla::{LayoutExpression, LayoutSolver, LayoutVariable};

let solver = LayoutSolver::new();
let left = LayoutVariable::new("left");
let width = LayoutVariable::new("width");
let right = LayoutVariable::new("right");
solver.add_constraint(&LayoutExpression::from_variable(&left).equal_to(16)).unwrap();
solver.add_constraint(&LayoutExpression::from_variable(&width).equal_to(200)).unwrap();
solver.add_constraint(&LayoutExpression::from_variable(&right).equal_to(LayoutExpression::from_variable(&left).add(&width))).unwrap();
solver.update_variables();
assert!((right.get_value() - 216.0).abs() < 1e-9);

let shared = right.clone();
assert_eq!(shared, right);
assert_eq!(shared.get_id(), right.get_id());
assert_eq!(shared.get_value(), right.get_value());
```

- `pub fn new(name: &str) -> LayoutVariable` (constructor, since 0.4.0): Constructor. (value is 0)
  - `name` &str: Name. (for display, variables created separately are different even with the same name)
  - returns LayoutVariable: A new variable handle with a new unique number.
- `pub fn get_id(&self) -> u64` (method, since 0.4.0): Returns the unique number.
  - returns u64: Unique number. (increases by one in creation order from 1, cloned handles have the same number)
- `pub fn set_name(&self, name: &str)` (method, since 0.4.0): Sets the name.
  - `name` &str: New name. (visible to all cloned handles)
- `pub fn get_name(&self) -> String` (method, since 0.4.0): Returns the name.
  - returns String: Copy of the name.
- `pub fn set_value(&self, value: f64)` (method, since 0.4.0): Sets the value. (called by the solver, calling it directly from outside is not recommended)
  - `value` f64: Value to set.
- `pub fn set_value_changed_event(&self, value_changed_event: Option<LayoutVariableEvent>)` (method, since 0.4.0): Sets the function to call when the value changes. (original setValueChangedEvent, called only when the value solved by the solver differs, not called if None)
  - Lets the owner of the variable notice the change. (the original UINode sets it on its position and size variables to recompute subtree draw bounds. The Rust UINode writes the solution into the node transform, so the transform setters report it)
  - `value_changed_event` Option<LayoutVariableEvent>: Function that receives the changed variable. None clears it.

  Example: Notice when the value changes

  ```rust
  use std::cell::Cell;
  use std::rc::Rc;
  use vanilla::LayoutVariable;
  
  let width = LayoutVariable::new("width");
  let changed_count = Rc::new(Cell::new(0));
  let counter = changed_count.clone();
  width.set_value_changed_event(Some(Rc::new(move |variable: &LayoutVariable| {
  	assert_eq!(variable.get_value(), 120.0);
  	counter.set(counter.get() + 1);
  })));
  width.set_value(120.0);
  width.set_value(120.0);
  assert_eq!(changed_count.get(), 1);
  assert!(width.get_value_changed_event().is_some());
  ```

- `pub fn get_value_changed_event(&self) -> Option<LayoutVariableEvent>` (method, since 0.4.0): Returns the function called when the value changes. (original getValueChangedEvent)
  - returns Option<LayoutVariableEvent>: The set function. None if there is none.
- `pub fn get_value(&self) -> f64` (method, since 0.4.0): Returns the current value. (correct after update_variables)
  - returns f64: Current value. (initially 0)
