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

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

Layout constraint strength. (LayoutStrength of the original ui/autolayout/layoutstrength.js)
- The Cassowary, Kiwi standard strength system. Decides which side to keep first when constraints conflict.
- REQUIRED must be kept, and an error is returned if the solver cannot solve it.
- STRONG, MEDIUM, WEAK are kept as much as possible, but yield in order of strength on conflict.
- Strengths are composed with create_strength(a, b, c, weight).
- The original's static fields are associated constants, and its static functions are associated functions. weight, which could be omitted, is taken as an Option.

## LayoutStrength (struct)

Layout constraint strength.
- A name slot holding no value. Use only the associated constants and associated functions. Strengths are f64.

Example: A strong constraint beats a weak constraint

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

let solver = LayoutSolver::new();
let x = LayoutVariable::new("x");
solver.add_constraint(&LayoutExpression::from_variable(&x).equal_to(10).with_strength(LayoutStrength::WEAK)).unwrap();
solver.add_constraint(&LayoutExpression::from_variable(&x).equal_to(50).with_strength(LayoutStrength::STRONG)).unwrap();
solver.update_variables();
assert!((x.get_value() - 50.0).abs() < 1e-6);
```

- `pub const REQUIRED: f64 = 1001001000.0` (static, since 0.4.0): Strength that must be kept. (1001001000, same as create_strength(1000, 1000, 1000))
  - returns f64
- `pub const STRONG: f64 = 1000000.0` (static, since 0.4.0): Strong. (1000000, same as create_strength(1, 0, 0))
  - returns f64
- `pub const MEDIUM: f64 = 1000.0` (static, since 0.4.0): Medium. (1000, same as create_strength(0, 1, 0))
  - returns f64
- `pub const WEAK: f64 = 1.0` (static, since 0.4.0): Weak. (1, same as create_strength(0, 0, 1))
  - returns f64
- `pub fn create_strength(a: f64, b: f64, c: f64, weight: Option<f64>) -> f64` (static, since 0.4.0): Composes a strength.
  - Multiplies the three components (a, b, c) by weight (1 if absent), then clamps each to 0 ~ 1000.
  - The result is in the 0 ~ REQUIRED range.
  - `a` f64: strong component. (multiplied by weight, clamped to 0 ~ 1000, then multiplied by 1000000)
  - `b` f64: medium component. (multiplied by weight, clamped to 0 ~ 1000, then multiplied by 1000)
  - `c` f64: weak component. (multiplied by weight and clamped to 0 ~ 1000)
  - `weight` Option<f64>: Weight to multiply the three components by. 1 if None.
  - returns f64: Strength summing the three components. (0 ~ REQUIRED)

  Example: Clamping each component and summing

  ```rust
  use vanilla::LayoutStrength;
  
  assert_eq!(LayoutStrength::create_strength(1.0, 0.0, 0.0, None), LayoutStrength::STRONG);
  assert_eq!(LayoutStrength::create_strength(0.0, 2.0, 0.0, Some(3.0)), 6000.0);
  assert_eq!(LayoutStrength::create_strength(2000.0, 0.0, 5.0, None), 1000000000.0 + 5.0);
  ```

- `pub fn clip_strength(strength: f64) -> f64` (static, since 0.4.0): Clamps a strength. (0 ~ REQUIRED)
  - `strength` f64: Strength to clamp.
  - returns f64: Strength clamped to 0 ~ REQUIRED.
- `pub fn from_priority(priority: f64) -> f64` (static, since 0.4.0): Turns a UIKit priority (1 ~ 1000) into a strength.
  - 1000 or more is REQUIRED, 1 ~ 999 is a medium tier weight (750000 for 750), and 0 or less is 0.
  - `priority` f64: UIKit-style priority. (a LayoutPriority value, 1 ~ 1000)
  - returns f64: Strength. (REQUIRED if 1000 or more, 0 if 0 or less, priority * 1000 in between)

  Example: Convert priorities to strengths

  ```rust
  use vanilla::{LayoutPriority, LayoutStrength};
  
  assert_eq!(LayoutStrength::from_priority(LayoutPriority::REQUIRED), LayoutStrength::REQUIRED);
  assert_eq!(LayoutStrength::from_priority(LayoutPriority::DEFAULT_HIGH), 750000.0);
  assert_eq!(LayoutStrength::from_priority(-10.0), 0.0);
  ```

