Skip to main content

rosace_widgets/tree/
spacer.rs

1use rosace_core::types::Size;
2use super::{Widget, LayoutCtx, PaintCtx};
3
4/// A fixed-size gap (invisible). Use inside Row/Column.
5pub struct Spacer {
6    pub width: f32,
7    pub height: f32,
8}
9
10impl Spacer {
11    pub fn new(size: f32) -> Self { Self { width: size, height: size } }
12    pub fn w(width: f32) -> Self { Self { width, height: 0.0 } }
13    pub fn h(height: f32) -> Self { Self { width: 0.0, height } }
14    /// A fixed w x h gap (absorbs SizedBox::gap - D095).
15    pub fn gap(width: f32, height: f32) -> Self { Self { width, height } }
16}
17
18impl Widget for Spacer {
19    fn layout(&self, _ctx: &LayoutCtx) -> Size {
20        Size { width: self.width, height: self.height }
21    }
22    fn paint(&self, _ctx: &mut PaintCtx) {}
23}
24
25/// Fills remaining space in a Row or Column (flex weight 1 by default).
26///
27/// Wrap any widget with `Expanded::new(child)` to make it fill leftover space.
28pub struct Expanded {
29    pub factor: f32,
30    pub child: Option<Box<dyn Widget>>,
31}
32
33impl Expanded {
34    /// Empty space filler (no child).
35    pub fn empty() -> Self { Self { factor: 1.0, child: None } }
36
37    /// Expand `child` to fill available space.
38    pub fn new(child: impl Widget + 'static) -> Self {
39        Self { factor: 1.0, child: Some(Box::new(child)) }
40    }
41
42    pub fn with_factor(mut self, f: f32) -> Self { self.factor = f; self }
43}
44
45impl Widget for Expanded {
46    fn children(&self) -> super::Children<'_> {
47        match &self.child {
48            Some(c) => super::Children::One(&**c),
49            None => super::Children::None,
50        }
51    }
52
53    // layout, paint: protocol defaults (delegate to the child; empty
54    // Expanded is sized entirely by the flex pool).
55
56    fn flex_factor(&self) -> f32 { self.factor }
57}