diff --git a/pulse-ui/src/layout.rs b/pulse-ui/src/layout.rs index d7cb7fc..44716e7 100644 --- a/pulse-ui/src/layout.rs +++ b/pulse-ui/src/layout.rs @@ -13,16 +13,25 @@ pub struct Rect { pub height: u16, } +#[derive(Debug, Clone)] pub enum LayoutItem { Rows { unit: Size, items: Vec }, Columns { unit: Size, items: Vec }, Widget(Size), } +#[derive(Debug, Clone)] +pub struct Allocation { + pub widget_alloc: Vec, + pub frame_alloc: Vec, +} + impl LayoutItem { - pub fn allocate(&self, alloc: &Rect, layout: &mut Vec) { + pub fn allocate(&self, alloc: &Rect, frame: &mut Vec, widgets: &mut Vec) { match self { Self::Rows { items, .. } | Self::Columns { items, .. } => { + frame.push(*alloc); + let is_row = matches!(self, Self::Rows { .. }); let allocation = if is_row { alloc.height } else { alloc.width }; @@ -63,13 +72,13 @@ impl LayoutItem { curr += unit_alloc; - item.allocate(&item_alloc, layout); - - layout.push(item_alloc); + item.allocate(&item_alloc, frame, widgets); } } - Self::Widget(_) => {} + Self::Widget(_) => { + widgets.push(*alloc); + } } } @@ -113,6 +122,20 @@ impl Size { } } +impl Rect { + pub fn allocate(&self, layout: &LayoutItem) -> Allocation { + let mut widget_alloc = Vec::new(); + let mut frame_alloc = Vec::new(); + + layout.allocate(self, &mut frame_alloc, &mut widget_alloc); + + Allocation { + widget_alloc, + frame_alloc, + } + } +} + pub fn layout(rows: Vec) -> LayoutItem { LayoutItem::Rows { unit: Size::Percent(100), diff --git a/src/main.rs b/src/main.rs index 3fac562..64f5682 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,10 @@ -use pulse_ui::layout::{LayoutItem, Rect, Size}; +use pulse_ui::layout::{LayoutItem, Rect, Size, layout}; fn main() { - let layout = LayoutItem::Columns { - unit: Size::Percent(100), - items: vec![ - LayoutItem::Widget(Size::Flex(16)), - LayoutItem::Widget(Size::Flex(9)), - ], - }; + let layout = layout(vec![ + LayoutItem::Widget(Size::Flex(16)), + LayoutItem::Widget(Size::Flex(9)), + ]); let screen = Rect { x: 0, @@ -16,9 +13,5 @@ fn main() { height: 3000, }; - let mut layout_alloc = Vec::new(); - - layout.allocate(&screen, &mut layout_alloc); - - println!("{:?}", layout_alloc); + println!("{:?}", screen.allocate(&layout)); }