# ui/autolayout/layoutvariable (vanilla.js 0.4.0)

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

Layout solver unknown.

## LayoutVariable

Layout solver unknown.
- A variable that the outside (user code) uses in expressions.
- After the solver solves, the result is applied through setValue when updateVariables() is called.

Example: Solve a constraint and read the variable value

```js
import { LayoutSolver, LayoutVariable, LayoutExpression } from "./build/vanilla.js";

const solver = new LayoutSolver();
const left = new LayoutVariable("left");
const leftExpression = LayoutExpression.fromVariable(left);
const leftConstraint = leftExpression.equalTo(24);
solver.addConstraint(leftConstraint);
const name = left.getName();
const valueBeforeUpdate = left.getValue();
solver.updateVariables();
const valueAfterUpdate = left.getValue();
console.log(name, valueBeforeUpdate, valueAfterUpdate);
```

- `constructor(name)` (constructor, since 0.2.1): Constructor.
  - `name` string: Variable name. (empty string if not a string)
- `setName(name)` (method, since 0.2.1): Sets the name.
  - `name` string: Variable name.
- `getName()` (method, since 0.2.1): Returns the name.
  - returns string: Variable name.
- `setValue(value)` (method, since 0.2.1): Sets the value. (Called inside the solver, direct external calls are not recommended.)
  - `value` number: New value.
- `setValueChangedEvent(valueChangedEvent)` (method, since 0.4.0): Sets the function called when the value changes. (Called only when the solved value differs, so the variable's owner notices the change; null calls nothing)
  - UINode hooks it on its position and size variables to recalculate subtree draw bounds.
  - `valueChangedEvent` ((variable: LayoutVariable) => void) | null: A function receiving the changed variable.

  Example: Knowing when the value changes

  ```js
  import { LayoutVariable } from "./build/vanilla.js";
  
  const width = new LayoutVariable("width");
  width.setValueChangedEvent((variable) => console.log(variable.getName(), variable.getValue()));
  width.setValue(120);
  width.setValue(120);
  ```

- `getValueChangedEvent()` (method, since 0.4.0): Returns the function called when the value changes.
  - returns ((variable: LayoutVariable) => void) | null: The hooked function. (null if none)
- `getValue()` (method, since 0.2.1): Returns the current value. (Valid after updateVariables)
  - returns number: Value set by the last updateVariables. (0 at first)
