From 11111a107eea6d66da00ffd61b9dc5da0dc24403 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 14 Jul 2026 11:21:31 -0400 Subject: [PATCH] Working layout system --- pulse-ui/src/layout.rs | 113 ++++++++++++++++------------------------- src/main.rs | 25 ++++----- 2 files changed, 53 insertions(+), 85 deletions(-) diff --git a/pulse-ui/src/layout.rs b/pulse-ui/src/layout.rs index 5a566ef..7fda39c 100644 --- a/pulse-ui/src/layout.rs +++ b/pulse-ui/src/layout.rs @@ -14,100 +14,68 @@ pub struct Rect { } pub enum LayoutItem { - Row { - width: Size, - height: Size, - items: Vec, - }, - Column { - width: Size, - height: Size, - items: Vec, - }, - Widget { - width: Size, - height: Size, - }, + Rows { unit: Size, items: Vec }, + Columns { unit: Size, items: Vec }, + Widget(Size), } impl LayoutItem { - pub fn allocate(&self, current_alloc: &Rect, alloc: &mut Vec) { + pub fn allocate(&self, alloc: &Rect, layout: &mut Vec) { match self { - Self::Row { items, .. } => { - let remaining = current_alloc.height + Self::Rows { items, .. } | Self::Columns { items, .. } => { + let is_row = matches!(self, Self::Rows { .. }); + + let allocation = if is_row { alloc.height } else { alloc.width }; + + let remaining = allocation - items .iter() - .map(|item| item.get_size().1.get_fixed(current_alloc.height)) + .map(|item| item.get_size().get_fixed(alloc.height)) .sum::(); - let total_weight = current_alloc.height - - items - .iter() - .map(|item| item.get_size().1.get_flex()) - .sum::(); + let total_weight = items + .iter() + .map(|item| item.get_size().get_flex()) + .sum::(); - let mut curr = current_alloc.y; + let mut curr = if is_row { alloc.y } else { alloc.x }; for item in items { let size = item.get_size(); - let item_alloc = Rect { - width: current_alloc.width, - height: size.1.get(current_alloc.height, remaining, total_weight), - x: current_alloc.x, - y: curr, + let item_alloc = if is_row { + Rect { + width: alloc.width, + height: size.get(allocation, remaining, total_weight), + x: alloc.x, + y: curr, + } + } else { + Rect { + width: size.get(allocation, remaining, total_weight), + height: alloc.height, + x: curr, + y: alloc.y, + } }; curr += item_alloc.height; - item.allocate(&item_alloc, alloc); + item.allocate(&item_alloc, layout); - alloc.push(item_alloc); + layout.push(item_alloc); } } - Self::Column { items, .. } => { - let remaining = current_alloc.width - - items - .iter() - .map(|item| item.get_size().0.get_fixed(current_alloc.width)) - .sum::(); - - let total_weight = current_alloc.height - - items - .iter() - .map(|item| item.get_size().0.get_flex()) - .sum::(); - - let mut curr = current_alloc.x; - - for item in items { - let size = item.get_size(); - - let item_alloc = Rect { - width: size.1.get(current_alloc.width, remaining, total_weight), - height: current_alloc.height, - x: curr, - y: current_alloc.y, - }; - - curr += item_alloc.width; - - item.allocate(&item_alloc, alloc); - - alloc.push(item_alloc); - } - } - - Self::Widget { .. } => {} + Self::Widget(_) => {} } } - pub fn get_size<'a>(&'a self) -> (&'a Size, &'a Size) { + pub fn get_size<'a>(&'a self) -> &'a Size { match self { - Self::Row { width, height, .. } => (width, height), - Self::Column { width, height, .. } => (width, height), - Self::Widget { width, height } => (width, height), + Self::Rows { unit, .. } => unit, + Self::Columns { unit, .. } => unit, + Self::Widget(unit) => unit, } } } @@ -136,3 +104,10 @@ impl Size { } } } + +pub fn layout(rows: Vec) -> LayoutItem { + LayoutItem::Rows { + unit: Size::Percent(100), + items: rows, + } +} diff --git a/src/main.rs b/src/main.rs index eb86651..3fac562 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,24 @@ use pulse_ui::layout::{LayoutItem, Rect, Size}; fn main() { - let layout = LayoutItem::Row { - width: Size::Percent(100), - height: Size::Percent(100), + let layout = LayoutItem::Columns { + unit: Size::Percent(100), items: vec![ - LayoutItem::Widget { - width: Size::Percent(100), - height: Size::Flex(1), - }, - LayoutItem::Widget { - width: Size::Percent(100), - height: Size::Flex(1), - }, + LayoutItem::Widget(Size::Flex(16)), + LayoutItem::Widget(Size::Flex(9)), ], }; let screen = Rect { x: 0, y: 0, - width: 60, - height: 12, + width: 3000, + height: 3000, }; - let mut alloc = Vec::new(); + let mut layout_alloc = Vec::new(); - layout.allocate(&screen, &mut alloc); + layout.allocate(&screen, &mut layout_alloc); - println!("{:?}", alloc); + println!("{:?}", layout_alloc); }