Skip to main content

cranpose_ui/modifier/
weight.rs

1use cranpose_foundation::plain_element;
2
3use super::{Modifier, inspector_metadata};
4use crate::modifier_nodes::WeightElement;
5
6impl Modifier {
7    /// Gives this child a share of the space that the unweighted children leave on the main axis of
8    /// a `Row` or `Column`, in proportion to `weight`. The child fills its share. Compose's
9    /// `Modifier.weight`.
10    pub fn weight(self, weight: f32) -> Self {
11        self.weight_with_fill(weight, true)
12    }
13
14    /// Like [`Self::weight`]. With `fill` set to false the child may be smaller than its share.
15    pub fn weight_with_fill(self, weight: f32, fill: bool) -> Self {
16        let modifier = Self::from_dyn_element(plain_element(WeightElement::new(weight, fill)))
17            .with_inspector_metadata(inspector_metadata("weight", move |info| {
18                info.add_property("weight", weight.to_string());
19                info.add_property("fill", fill.to_string());
20            }));
21        self.then(modifier)
22    }
23
24    /// Same as [`Self::weight_with_fill`], for a child of a `Column`.
25    #[expect(non_snake_case, reason = "names follow Compose's casing")]
26    pub fn columnWeight(self, weight: f32, fill: bool) -> Self {
27        self.weight_with_fill(weight, fill)
28    }
29
30    /// Same as [`Self::weight_with_fill`], for a child of a `Row`.
31    #[expect(non_snake_case, reason = "names follow Compose's casing")]
32    pub fn rowWeight(self, weight: f32, fill: bool) -> Self {
33        self.weight_with_fill(weight, fill)
34    }
35}