From 926fec243e498ec1f88fc9572a9ce0e69c5d8372 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 14 Jul 2026 10:55:57 -0400 Subject: [PATCH] Testing layout --- pulse-ui/src/layout.rs | 138 +++++++++++++++++++++++++++++++++++++++++ pulse-ui/src/lib.rs | 1 + pulse-ui/src/main.rs | 3 - src/main.rs | 30 ++++++++- 4 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 pulse-ui/src/layout.rs create mode 100644 pulse-ui/src/lib.rs delete mode 100644 pulse-ui/src/main.rs diff --git a/pulse-ui/src/layout.rs b/pulse-ui/src/layout.rs new file mode 100644 index 0000000..5a566ef --- /dev/null +++ b/pulse-ui/src/layout.rs @@ -0,0 +1,138 @@ +#[derive(Debug, Clone, Copy)] +pub enum Size { + Fixed(u16), + Flex(u16), + Percent(u16), +} + +#[derive(Debug, Clone, Copy)] +pub struct Rect { + pub x: u16, + pub y: u16, + pub width: u16, + pub height: u16, +} + +pub enum LayoutItem { + Row { + width: Size, + height: Size, + items: Vec, + }, + Column { + width: Size, + height: Size, + items: Vec, + }, + Widget { + width: Size, + height: Size, + }, +} + +impl LayoutItem { + pub fn allocate(&self, current_alloc: &Rect, alloc: &mut Vec) { + match self { + Self::Row { items, .. } => { + let remaining = current_alloc.height + - items + .iter() + .map(|item| item.get_size().1.get_fixed(current_alloc.height)) + .sum::(); + + let total_weight = current_alloc.height + - items + .iter() + .map(|item| item.get_size().1.get_flex()) + .sum::(); + + let mut curr = current_alloc.y; + + 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, + }; + + curr += item_alloc.height; + + item.allocate(&item_alloc, alloc); + + alloc.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 { .. } => {} + } + } + + pub fn get_size<'a>(&'a self) -> (&'a Size, &'a Size) { + match self { + Self::Row { width, height, .. } => (width, height), + Self::Column { width, height, .. } => (width, height), + Self::Widget { width, height } => (width, height), + } + } +} + +impl Size { + pub fn get_fixed(&self, alloc: u16) -> u16 { + match self { + Self::Fixed(v) => *v, + Self::Percent(v) => alloc / (v / 100), + Self::Flex(_) => 0, + } + } + + pub fn get_flex(&self) -> u16 { + match self { + Self::Flex(f) => *f, + _ => 0, + } + } + + pub fn get(&self, alloc: u16, remaining: u16, total_weight: u16) -> u16 { + match self { + Self::Fixed(v) => *v, + Self::Percent(v) => alloc / (v / 100), + Self::Flex(v) => remaining * v / total_weight, + } + } +} diff --git a/pulse-ui/src/lib.rs b/pulse-ui/src/lib.rs new file mode 100644 index 0000000..dd64619 --- /dev/null +++ b/pulse-ui/src/lib.rs @@ -0,0 +1 @@ +pub mod layout; diff --git a/pulse-ui/src/main.rs b/pulse-ui/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/pulse-ui/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/src/main.rs b/src/main.rs index e7a11a9..eb86651 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,31 @@ +use pulse_ui::layout::{LayoutItem, Rect, Size}; + fn main() { - println!("Hello, world!"); + let layout = LayoutItem::Row { + width: Size::Percent(100), + height: 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), + }, + ], + }; + + let screen = Rect { + x: 0, + y: 0, + width: 60, + height: 12, + }; + + let mut alloc = Vec::new(); + + layout.allocate(&screen, &mut alloc); + + println!("{:?}", alloc); }