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

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

Layout constraint. (LayoutConstraint of the original ui/autolayout/layoutconstraint.js)
- Form: expression { ==, <=, >= } 0
- Usually created by LayoutExpression's equal_to, less_than_or_equal_to, greater_than_or_equal_to.
- Strength is the priority on conflict. Clamped to 0 ~ REQUIRED at creation.
- Differences from the original:
  - The original tells constraints registered in the solver apart by object identity. The Rust version is a cheap Clone handle holding an Rc, so
    cloned handles are the same constraint, and equality and hashing go by the constraint the handle points to. (created separately, they are different constraints even with the same expression)
  - strength, which could be omitted (default REQUIRED), is required.

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

Layout constraint. (clones point to the same constraint)

Example: Creating a constraint from an expression and adding it to the solver

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

let solver = LayoutSolver::new();
let width = LayoutVariable::new("width");
let minimum_width = LayoutExpression::from_variable(&width).greater_than_or_equal_to(120);
assert_eq!(minimum_width.get_relation(), LayoutRelation::GreaterThanOrEqual);
assert_eq!(minimum_width.get_strength(), LayoutStrength::REQUIRED);
solver.add_constraint(&minimum_width).unwrap();
solver.add_constraint(&LayoutExpression::from_variable(&width).equal_to(80).with_strength(LayoutStrength::STRONG)).unwrap();
solver.update_variables();
assert!((width.get_value() - 120.0).abs() < 1e-6);
assert!(solver.has_constraint(&minimum_width.clone()));
```

- `pub fn new(expression: LayoutExpression, relation: LayoutRelation, strength: f64) -> LayoutConstraint` (constructor, since 0.4.0): Constructor. (strength is clamped to 0 ~ REQUIRED)
  - `expression` LayoutExpression: Constraint expression. (the left side of the expression relation 0 form)
  - `relation` LayoutRelation: Relation between the expression and 0.
  - `strength` f64: Strength. (clamped to 0 ~ LayoutStrength::REQUIRED)
  - returns LayoutConstraint: New constraint handle.

  Example: width - 100 >= 0 at strong strength

  ```rust
  use vanilla::{LayoutConstraint, LayoutExpression, LayoutRelation, LayoutStrength, LayoutVariable};
  
  let width = LayoutVariable::new("width");
  let expression = LayoutExpression::from_variable(&width).subtract(100);
  let constraint = LayoutConstraint::new(expression, LayoutRelation::GreaterThanOrEqual, LayoutStrength::STRONG);
  assert_eq!(constraint.get_strength(), LayoutStrength::STRONG);
  assert_eq!(constraint.get_expression().get_constant(), -100.0);
  
  let clipped = LayoutConstraint::new(LayoutExpression::from_variable(&width), LayoutRelation::Equal, 5e9);
  assert_eq!(clipped.get_strength(), LayoutStrength::REQUIRED);
  ```

- `pub fn get_expression(&self) -> &LayoutExpression` (method, since 0.4.0): Returns the expression.
  - returns &LayoutExpression: Reference to the constraint expression. (self - other expression if created with a comparison function)
- `pub fn get_relation(&self) -> LayoutRelation` (method, since 0.4.0): Returns the relation.
  - returns LayoutRelation: Relation between the expression and 0.
- `pub fn get_strength(&self) -> f64` (method, since 0.4.0): Returns the strength.
  - returns f64: Strength. (the value clamped to 0 ~ REQUIRED at creation)
- `pub fn with_strength(&self, new_strength: f64) -> LayoutConstraint` (method, since 0.4.0): Returns a new constraint differing only in strength. (for registering again with only the priority changed)
  - `new_strength` f64: New strength. (clamped to 0 ~ LayoutStrength::REQUIRED)
  - returns LayoutConstraint: A new constraint with the same expression and relation but a different strength. (a separately created constraint, so not equal to self)

  Example: Changing a weakly added constraint to strong and adding it again

  ```rust
  use vanilla::{LayoutExpression, LayoutSolver, LayoutStrength, LayoutVariable};
  
  let solver = LayoutSolver::new();
  let x = LayoutVariable::new("x");
  let preferred = LayoutExpression::from_variable(&x).equal_to(100).with_strength(LayoutStrength::WEAK);
  solver.add_constraint(&preferred).unwrap();
  let stronger = preferred.with_strength(LayoutStrength::STRONG);
  assert_ne!(stronger, preferred);
  solver.remove_constraint(&preferred).unwrap();
  solver.add_constraint(&stronger).unwrap();
  assert!(solver.has_constraint(&stronger));
  assert!(!solver.has_constraint(&preferred));
  ```

