wip: component refactor

This commit is contained in:
🪞👃🪞 2024-06-21 12:13:11 +03:00
parent 5082bf9fdf
commit c18aa2cbbd
14 changed files with 374 additions and 272 deletions

61
src/layout/container.rs Normal file
View file

@ -0,0 +1,61 @@
use crate::prelude::*;
pub struct Column(pub Vec<Box<dyn Device>>);
impl Column {
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
Self(items)
}
}
pub struct Row(pub Vec<Box<dyn Device>>);
impl Row {
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
Self(items)
}
}
impl Device for Column {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(render_column(&self.0, buf, area)?.0)
}
}
impl Device for Row {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Ok(render_row(&self.0, buf, area)?.0)
}
}
pub fn render_column (items: &[Box<dyn Device>], buf: &mut Buffer, area: Rect)
-> Usually<(Rect, Vec<Rect>)>
{
let mut w = 0u16;
let mut h = 0u16;
let mut rects = vec![];
for (i, device) in items.iter().enumerate() {
let rect = Rect { x: area.x, y: area.y + h, width: area.width, height: area.height - h };
let result = device.render(buf, rect)?;
rects.push(result);
w = w.max(result.width);
h = h + result.height;
}
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
}
pub fn render_row (items: &[Box<dyn Device>], buf: &mut Buffer, area: Rect)
-> Usually<(Rect, Vec<Rect>)>
{
let mut w = 0u16;
let mut h = 0u16;
let mut rects = vec![];
for (i, device) in items.iter().enumerate() {
let rect = Rect { x: area.x + w, y: area.y, width: area.width - w, height: area.height };
let result = device.render(buf, rect)?;
rects.push(result);
w = w + result.width;
h = h.max(result.height);
}
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
}