API ReferenceLayout Values
Constraints
Validated box bounds with every constructor and combinator.
Constraints are the validated bounds shared by every layout algorithm.
new panics on invalid bounds; try_new returns Result.
Constructors
use incular::prelude::*;
fn main() {
let loose = Constraints::loose(Size::new(300.0, 200.0));
let tight = Constraints::tight(Size::new(100.0, 40.0));
let checked =
Constraints::try_new(0.0, 300.0, 0.0, 200.0).expect("constraints");
println!("loose max: {}", loose.max_width());
println!("tight min: {}", tight.min_width());
println!("checked max: {}", checked.max_height());
}Validity: minimums finite and >= 0; maximums not NaN and >= minimums.
| Constructor | Bounds |
|---|---|
new(min_w, max_w, min_h, max_h) | Exact bounds; panics if invalid |
try_new(..) | Same as new, returns Result |
tight(size) | min == max == size |
loose(size) | 0..size |
unbounded() | 0..∞ both axes |
Tight versus loose in one window — copy, paste, and run:
use incular::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let exact = Constraints::tight(Size::new(200.0, 48.0));
let flexible = Constraints::loose(Size::new(200.0, 120.0));
let checked = Constraints::try_new(0.0, 300.0, 0.0, 200.0).expect("constraints");
let app = Application::new(move |_cx| {
Container::builder()
.padding(EdgeInsets::all(24.0))
.alignment(Alignment::CENTER)
.child(
Column::builder()
.main_axis_size(MainAxisSize::Min)
.spacing(8.0)
.children(Vec::<Widget>::from([
ConstrainedBox::new(
exact,
Text::new("tight 200x48"),
).into(),
ConstrainedBox::new(
flexible,
Text::new("loose"),
).into(),
ConstrainedBox::new(
checked,
Text::new("checked"),
).into(),
]))
.build(),
)
.build()
.into()
})?;
incular::run(app)?;
Ok(())
}Methods
use incular::prelude::*;
fn main() {
let c = Constraints::loose(Size::new(300.0, 200.0));
println!("min: {} max: {}", c.min_width(), c.max_width());
println!("clamped: {:?}", c.constrain(Size::new(500.0, 500.0)));
println!("loosened: {:?}", c.loosen().min_width());
println!("bounded: {}", c.is_width_bounded());
println!("biggest: {:?}", c.biggest());
}Used by: Container,
ConstrainedBox.