mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
73 lines
2.2 KiB
Rust
73 lines
2.2 KiB
Rust
use crate::core::*;
|
|
|
|
pub struct Stack<'a>(pub &'a[Box<dyn Render + Sync>]);
|
|
|
|
impl<'a> Render for Stack<'a> {
|
|
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
|
let mut area2 = area.clone();
|
|
for layer in self.0.iter() {
|
|
area2 = layer.render(buf, area2)?;
|
|
}
|
|
Ok(area)
|
|
}
|
|
}
|
|
|
|
pub struct Column(pub Vec<Box<dyn Device>>);
|
|
|
|
pub struct Row(pub Vec<Box<dyn Device>>);
|
|
|
|
impl Column {
|
|
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
|
|
Self(items)
|
|
}
|
|
pub fn draw (buf: &mut Buffer, area: Rect, items: &[impl Render], gap: i16)
|
|
-> Usually<(Rect, Vec<Rect>)>
|
|
{
|
|
let mut w = 0u16;
|
|
let mut h = 0u16;
|
|
let mut rects = vec![];
|
|
for (_i, device) in items.iter().enumerate() {
|
|
let y = area.y + h;
|
|
let rect = Rect { x: area.x, y, 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) as i16 + gap).max(0) as u16;
|
|
}
|
|
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
|
|
}
|
|
}
|
|
|
|
impl Render for Column {
|
|
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
|
Ok(Column::draw(buf, area, &self.0, 0)?.0)
|
|
}
|
|
}
|
|
|
|
impl Row {
|
|
pub fn new (items: Vec<Box<dyn Device>>) -> Self {
|
|
Self(items)
|
|
}
|
|
pub fn draw (buf: &mut Buffer, area: Rect, items: &[impl Render], gap: i16)
|
|
-> Usually<(Rect, Vec<Rect>)>
|
|
{
|
|
let mut w = 0u16;
|
|
let mut h = 0u16;
|
|
let mut rects = vec![];
|
|
for (_i, device) in items.iter().enumerate() {
|
|
let x = area.x + w;
|
|
let rect = Rect { x, y: area.y, width: area.width - w, height: area.height };
|
|
let result = device.render(buf, rect)?;
|
|
rects.push(result);
|
|
w = ((w + result.width) as i16 + gap).max(0) as u16;
|
|
h = h.max(result.height);
|
|
}
|
|
Ok((Rect { x: area.x, y: area.y, width: w, height: h }, rects))
|
|
}
|
|
}
|
|
|
|
impl Render for Row {
|
|
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
|
Ok(Row::draw(buf, area, &self.0, 0)?.0)
|
|
}
|
|
}
|