tek/src/view/split.rs
2024-07-09 12:06:21 +03:00

97 lines
3.2 KiB
Rust

use crate::core::*;
pub enum Direction {
Down,
Right,
}
pub struct Split<'a, const N: usize>(pub Direction, pub [&'a (dyn Render + Sync);N]);
impl<'a, const N: usize> Split<'a, N> {
pub fn down (items: [&'a (dyn Render + Sync);N]) -> Self {
Self(Direction::Down, items)
}
}
impl<'a, const N: usize> Render for Split<'a, N> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { mut x, mut y, mut width, mut height } = area;
for item in self.1 {
if width == 0 || height == 0 {
break
}
let result = item.render(buf, Rect { x, y, width, height })?;
match self.0 {
Direction::Down => {
y = y + result.height;
height = height.saturating_sub(result.height);
},
Direction::Right => {
x = x + result.width;
width = width.saturating_sub(result.width);
},
}
}
Ok(area)
}
}
//pub struct Split<'a> {
//a: &'a (dyn Render + Sync),
//b: &'a (dyn Render + Sync),
//direction: Direction,
//reverse: bool
//}
//impl<'a> Split<'a> {
//pub fn lr (a: &'a (dyn Render + Sync), b: &'a (dyn Render + Sync)) -> Self {
//Self { a, b, direction: Direction::X, reverse: false }
//}
//pub fn rl (a: &'a (dyn Render + Sync), b: &'a (dyn Render + Sync)) -> Self {
//Self { a, b, direction: Direction::X, reverse: true }
//}
//pub fn tb (a: &'a (dyn Render + Sync), b: &'a (dyn Render + Sync)) -> Self {
//Self { a, b, direction: Direction::Y, reverse: false }
//}
//pub fn bt (a: &'a (dyn Render + Sync), b: &'a (dyn Render + Sync)) -> Self {
//Self { a, b, direction: Direction::Y, reverse: true }
//}
//fn split (&self, length: u16) -> (u16, u16) {
//let l1 = (length as f64 * self.proportion).round() as u16;
//let l2 = length.saturating_sub(l1);
//(l1, l2)
//}
//}
//impl<'a> Render for Split<'a> {
//fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
//let Rect { x, y, width, height } = area;
//let area1 = if self.reverse { self.b } else { self.a }
//.render(buf, area)?;
//let area2 = if self.reverse { self.a } else { self.b }
//.render(buf, match (self.direction, self.reverse)Rect {
//})?;
//let (area1, area2) = match self.direction {
//Direction::X => {
//let (w1, w2) = self.split(width);
//(Rect { x, y, width: w1, height }, Rect { x: x + w1, y, width: w2, height })
//},
//Direction::Y => {
//let (h1, h2) = self.split(height);
//(Rect { x, y, width, height: h1 }, Rect { x, y: y + h1, width, height: h2 })
//},
//};
//match self.reverse {
//true => {
//self.b.render(buf, area1)?;
//self.a.render(buf, area2)?;
//},
//false => {
//self.a.render(buf, area1)?;
//self.b.render(buf, area2)?;
//},
//};
//Ok(area)
//}
//}