Separated allocation

This commit is contained in:
2026-07-14 11:35:12 -04:00
parent 49c0c586e2
commit 929213d3ad
2 changed files with 34 additions and 18 deletions
+28 -5
View File
@@ -13,16 +13,25 @@ pub struct Rect {
pub height: u16,
}
#[derive(Debug, Clone)]
pub enum LayoutItem {
Rows { unit: Size, items: Vec<LayoutItem> },
Columns { unit: Size, items: Vec<LayoutItem> },
Widget(Size),
}
#[derive(Debug, Clone)]
pub struct Allocation {
pub widget_alloc: Vec<Rect>,
pub frame_alloc: Vec<Rect>,
}
impl LayoutItem {
pub fn allocate(&self, alloc: &Rect, layout: &mut Vec<Rect>) {
pub fn allocate(&self, alloc: &Rect, frame: &mut Vec<Rect>, widgets: &mut Vec<Rect>) {
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 {
LayoutItem::Rows {
unit: Size::Percent(100),
+6 -13
View File
@@ -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));
}