Separated allocation
This commit is contained in:
+28
-5
@@ -13,16 +13,25 @@ pub struct Rect {
|
|||||||
pub height: u16,
|
pub height: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub enum LayoutItem {
|
pub enum LayoutItem {
|
||||||
Rows { unit: Size, items: Vec<LayoutItem> },
|
Rows { unit: Size, items: Vec<LayoutItem> },
|
||||||
Columns { unit: Size, items: Vec<LayoutItem> },
|
Columns { unit: Size, items: Vec<LayoutItem> },
|
||||||
Widget(Size),
|
Widget(Size),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Allocation {
|
||||||
|
pub widget_alloc: Vec<Rect>,
|
||||||
|
pub frame_alloc: Vec<Rect>,
|
||||||
|
}
|
||||||
|
|
||||||
impl LayoutItem {
|
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 {
|
match self {
|
||||||
Self::Rows { items, .. } | Self::Columns { items, .. } => {
|
Self::Rows { items, .. } | Self::Columns { items, .. } => {
|
||||||
|
frame.push(*alloc);
|
||||||
|
|
||||||
let is_row = matches!(self, Self::Rows { .. });
|
let is_row = matches!(self, Self::Rows { .. });
|
||||||
|
|
||||||
let allocation = if is_row { alloc.height } else { alloc.width };
|
let allocation = if is_row { alloc.height } else { alloc.width };
|
||||||
@@ -63,13 +72,13 @@ impl LayoutItem {
|
|||||||
|
|
||||||
curr += unit_alloc;
|
curr += unit_alloc;
|
||||||
|
|
||||||
item.allocate(&item_alloc, layout);
|
item.allocate(&item_alloc, frame, widgets);
|
||||||
|
|
||||||
layout.push(item_alloc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
pub fn layout(rows: Vec<LayoutItem>) -> LayoutItem {
|
||||||
LayoutItem::Rows {
|
LayoutItem::Rows {
|
||||||
unit: Size::Percent(100),
|
unit: Size::Percent(100),
|
||||||
|
|||||||
+6
-13
@@ -1,13 +1,10 @@
|
|||||||
use pulse_ui::layout::{LayoutItem, Rect, Size};
|
use pulse_ui::layout::{LayoutItem, Rect, Size, layout};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let layout = LayoutItem::Columns {
|
let layout = layout(vec![
|
||||||
unit: Size::Percent(100),
|
LayoutItem::Widget(Size::Flex(16)),
|
||||||
items: vec![
|
LayoutItem::Widget(Size::Flex(9)),
|
||||||
LayoutItem::Widget(Size::Flex(16)),
|
]);
|
||||||
LayoutItem::Widget(Size::Flex(9)),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
let screen = Rect {
|
let screen = Rect {
|
||||||
x: 0,
|
x: 0,
|
||||||
@@ -16,9 +13,5 @@ fn main() {
|
|||||||
height: 3000,
|
height: 3000,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut layout_alloc = Vec::new();
|
println!("{:?}", screen.allocate(&layout));
|
||||||
|
|
||||||
layout.allocate(&screen, &mut layout_alloc);
|
|
||||||
|
|
||||||
println!("{:?}", layout_alloc);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user