rosace_layout/sizing.rs
1//! [`Width`] and [`Height`] sizing enumerations for declarative widget sizing.
2
3use rosace_core::render_object::AxisBound;
4
5/// How a widget sizes itself on the *horizontal* axis.
6#[derive(Debug, Clone)]
7pub enum Width {
8 /// Exactly `f32` logical pixels wide.
9 Fixed(f32),
10 /// Expand to fill all available width.
11 Fill,
12 /// Shrink to fit intrinsic content width.
13 Shrink,
14 /// A fraction `0.0–1.0` of the parent's available width.
15 Fraction(f32),
16 /// At least `f32` pixels wide; may grow larger.
17 Min(f32),
18 /// At most `f32` pixels wide; may be smaller.
19 Max(f32),
20 /// Clamped between `(min, max)` pixels.
21 Range(f32, f32),
22}
23
24/// How a widget sizes itself on the *vertical* axis.
25#[derive(Debug, Clone)]
26pub enum Height {
27 /// Exactly `f32` logical pixels tall.
28 Fixed(f32),
29 /// Expand to fill all available height.
30 Fill,
31 /// Shrink to fit intrinsic content height.
32 Shrink,
33 /// A fraction `0.0–1.0` of the parent's available height.
34 Fraction(f32),
35 /// At least `f32` pixels tall; may grow larger.
36 Min(f32),
37 /// At most `f32` pixels tall; may be smaller.
38 Max(f32),
39 /// Clamped between `(min, max)` pixels.
40 Range(f32, f32),
41}
42
43impl Width {
44 /// Convert to an [`AxisBound`] given the parent's `available` width in logical pixels.
45 pub fn to_axis_bound(&self, available: f32) -> AxisBound {
46 match self {
47 Width::Fixed(v) => AxisBound::Bounded(*v),
48 Width::Fill => AxisBound::Bounded(available),
49 Width::Shrink => AxisBound::Shrink,
50 Width::Fraction(f) => AxisBound::Bounded(available * f),
51 Width::Min(v) => AxisBound::Bounded(*v),
52 Width::Max(v) => AxisBound::Bounded(*v),
53 Width::Range(_, max) => AxisBound::Bounded(*max),
54 }
55 }
56}
57
58impl Height {
59 /// Convert to an [`AxisBound`] given the parent's `available` height in logical pixels.
60 pub fn to_axis_bound(&self, available: f32) -> AxisBound {
61 match self {
62 Height::Fixed(v) => AxisBound::Bounded(*v),
63 Height::Fill => AxisBound::Bounded(available),
64 Height::Shrink => AxisBound::Shrink,
65 Height::Fraction(f) => AxisBound::Bounded(available * f),
66 Height::Min(v) => AxisBound::Bounded(*v),
67 Height::Max(v) => AxisBound::Bounded(*v),
68 Height::Range(_, max) => AxisBound::Bounded(*max),
69 }
70 }
71}