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
+44 -69
View File
@@ -14,100 +14,68 @@ pub struct Rect {
}
pub enum LayoutItem {
Row {
width: Size,
height: Size,
items: Vec<LayoutItem>,
},
Column {
width: Size,
height: Size,
items: Vec<LayoutItem>,
},
Widget {
width: Size,
height: Size,
},
Rows { unit: Size, items: Vec<LayoutItem> },
Columns { unit: Size, items: Vec<LayoutItem> },
Widget(Size),
}
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 {
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::<u16>();
let total_weight = current_alloc.height
- items
.iter()
.map(|item| item.get_size().1.get_flex())
.sum::<u16>();
let total_weight = items
.iter()
.map(|item| item.get_size().get_flex())
.sum::<u16>();
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::<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 { .. } => {}
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 {
LayoutItem::Rows {
unit: Size::Percent(100),
items: rows,
}
}