Working layout system

This commit is contained in:
2026-07-14 11:21:31 -04:00
parent 926fec243e
commit 11111a107e
2 changed files with 53 additions and 85 deletions
+41 -66
View File
@@ -14,100 +14,68 @@ pub struct Rect {
} }
pub enum LayoutItem { pub enum LayoutItem {
Row { Rows { unit: Size, items: Vec<LayoutItem> },
width: Size, Columns { unit: Size, items: Vec<LayoutItem> },
height: Size, Widget(Size),
items: Vec<LayoutItem>,
},
Column {
width: Size,
height: Size,
items: Vec<LayoutItem>,
},
Widget {
width: Size,
height: Size,
},
} }
impl LayoutItem { impl LayoutItem {
pub fn allocate(&self, current_alloc: &Rect, alloc: &mut Vec<Rect>) { pub fn allocate(&self, alloc: &Rect, layout: &mut Vec<Rect>) {
match self { match self {
Self::Row { items, .. } => { Self::Rows { items, .. } | Self::Columns { items, .. } => {
let remaining = current_alloc.height let is_row = matches!(self, Self::Rows { .. });
let allocation = if is_row { alloc.height } else { alloc.width };
let remaining = allocation
- items - items
.iter() .iter()
.map(|item| item.get_size().1.get_fixed(current_alloc.height)) .map(|item| item.get_size().get_fixed(alloc.height))
.sum::<u16>(); .sum::<u16>();
let total_weight = current_alloc.height let total_weight = items
- items
.iter() .iter()
.map(|item| item.get_size().1.get_flex()) .map(|item| item.get_size().get_flex())
.sum::<u16>(); .sum::<u16>();
let mut curr = current_alloc.y; let mut curr = if is_row { alloc.y } else { alloc.x };
for item in items { for item in items {
let size = item.get_size(); let size = item.get_size();
let item_alloc = Rect { let item_alloc = if is_row {
width: current_alloc.width, Rect {
height: size.1.get(current_alloc.height, remaining, total_weight), width: alloc.width,
x: current_alloc.x, height: size.get(allocation, remaining, total_weight),
x: alloc.x,
y: curr, y: curr,
}
} else {
Rect {
width: size.get(allocation, remaining, total_weight),
height: alloc.height,
x: curr,
y: alloc.y,
}
}; };
curr += item_alloc.height; 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, .. } => { Self::Widget(_) => {}
let remaining = current_alloc.width
- items
.iter()
.map(|item| item.get_size().0.get_fixed(current_alloc.width))
.sum::<u16>();
let total_weight = current_alloc.height
- items
.iter()
.map(|item| item.get_size().0.get_flex())
.sum::<u16>();
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 {
}
}
pub fn get_size<'a>(&'a self) -> (&'a Size, &'a Size) {
match self { match self {
Self::Row { width, height, .. } => (width, height), Self::Rows { unit, .. } => unit,
Self::Column { width, height, .. } => (width, height), Self::Columns { unit, .. } => unit,
Self::Widget { width, height } => (width, height), Self::Widget(unit) => unit,
} }
} }
} }
@@ -136,3 +104,10 @@ impl Size {
} }
} }
} }
pub fn layout(rows: Vec<LayoutItem>) -> LayoutItem {
LayoutItem::Rows {
unit: Size::Percent(100),
items: rows,
}
}
+9 -16
View File
@@ -1,31 +1,24 @@
use pulse_ui::layout::{LayoutItem, Rect, Size}; use pulse_ui::layout::{LayoutItem, Rect, Size};
fn main() { fn main() {
let layout = LayoutItem::Row { let layout = LayoutItem::Columns {
width: Size::Percent(100), unit: Size::Percent(100),
height: Size::Percent(100),
items: vec![ items: vec![
LayoutItem::Widget { LayoutItem::Widget(Size::Flex(16)),
width: Size::Percent(100), LayoutItem::Widget(Size::Flex(9)),
height: Size::Flex(1),
},
LayoutItem::Widget {
width: Size::Percent(100),
height: Size::Flex(1),
},
], ],
}; };
let screen = Rect { let screen = Rect {
x: 0, x: 0,
y: 0, y: 0,
width: 60, width: 3000,
height: 12, 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);
} }