replace old Split component

This commit is contained in:
🪞👃🪞 2024-09-10 23:12:02 +03:00
parent e845d252b7
commit 60406e1d32
9 changed files with 141 additions and 107 deletions

View file

@ -52,6 +52,29 @@ impl<'a, E: Engine> Collect<'a, E> for Collection<'a, E> {
}
}
pub struct Split<
E: Engine,
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = E>)->Usually<()>)->Usually<()>
>(pub F, pub Direction, PhantomData<E>);
impl<
E: Engine,
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = E>)->Usually<()>)->Usually<()>
> Split<E, F> {
#[inline]
pub fn new (direction: Direction, build: F) -> Self {
Self(build, direction, Default::default())
}
#[inline]
pub fn right (build: F) -> Self {
Self::new(Direction::Right, build)
}
#[inline]
pub fn down (build: F) -> Self {
Self::new(Direction::Down, build)
}
}
pub struct Layers<
E: Engine,
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = E>)->Usually<()>)->Usually<()>
@ -61,6 +84,7 @@ impl<
E: Engine,
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = E>)->Usually<()>)->Usually<()>
> Layers<E, F> {
#[inline]
pub fn new (build: F) -> Self {
Self(build, Default::default())
}
@ -83,43 +107,6 @@ impl Direction {
}
}
pub struct Split<'a, E: Engine> {
pub items: Collection<'a, E>,
pub direction: Direction,
pub focus: Option<usize>
}
impl<'a, E: Engine> Split<'a, E> {
pub fn new (direction: Direction) -> Self {
Self {
items: Collection::new(),
direction,
focus: None
}
}
pub fn down () -> Self {
Self::new(Direction::Down)
}
pub fn right () -> Self {
Self::new(Direction::Right)
}
pub fn focus (mut self, focus: Option<usize>) -> Self {
self.focus = focus;
self
}
}
impl<'a, E: Engine> Collect<'a, E> for Split<'a, E> {
fn add_box (mut self, item: Box<dyn Widget<Engine = E> + 'a>) -> Self {
self.items = self.items.add_box(item);
self
}
fn add_ref (mut self, item: &'a dyn Widget<Engine = E>) -> Self {
self.items = self.items.add_ref(item);
self
}
}
/// Override X and Y coordinates, aligning to corner, side, or center of area
pub enum Align<L> {
Center(L),

View file

@ -1,55 +1,82 @@
use crate::*;
impl<'a> Widget for Split<'a, Tui> {
impl<F> Widget for Split<Tui, F>
where
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = Tui>)->Usually<()>)->Usually<()>
{
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
todo!()
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
Ok(Some(self.render_areas(to)?.0))
}
}
// TODO
impl<'a> Split<'a, Tui> {
pub fn render_areas (&self, to: &mut Tui) -> Usually<([u16;4], Vec<Option<[u16;4]>>)> {
let area = to.area();
fn layout (&self, to: [u16;4]) -> Perhaps<[u16;4]> {
let mut w = 0;
let mut h = 0;
let mut areas = vec![];
Ok((match self.direction {
match self.1 {
Direction::Down => {
for component in self.items.0.iter() {
if h >= area.h() {
break
(self.0)(&mut |component| {
if h >= to.h() {
return Ok(())
}
let offset = Offset::Y(h, component as &dyn Widget<Engine = Tui>);
let result = offset.render(to)?;
areas.push(result);
if let Some([_, _, width, height]) = result {
if let Some([_, _, width, height]) = Offset::Y(
h, component as &dyn Widget<Engine = Tui>
).layout(to)? {
h += height;
w = w.max(width)
}
}
[area.x(), area.y(), w, h]
Ok(())
})?;
},
Direction::Right => {
for component in self.items.0.iter() {
if w >= area.x() {
break
(self.0)(&mut |component| {
if w >= to.w() {
return Ok(())
}
let offset = Offset::X(w, component as &dyn Widget<Engine = Tui>);
let result = offset.render(to)?;
areas.push(result);
if let Some([_, _, width, height]) = result {
if let Some([_, _, width, height]) = Offset::X(
w, component as &dyn Widget<Engine = Tui>
).layout(to)? {
w += width;
h = h.max(height)
}
}
[area.x(), area.y(), w, h]
Ok(())
})?;
},
_ => todo!()
}, areas))
};
Ok(Some([to.x(), to.y(), w, h]))
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let area = to.area();
let mut w = 0;
let mut h = 0;
match self.1 {
Direction::Down => {
(self.0)(&mut |component| {
if h >= area.h() {
return Ok(())
}
if let Some([_, _, width, height]) = Offset::Y(
h, component as &dyn Widget<Engine = Tui>
).render(to)? {
h += height;
w = w.max(width)
};
Ok(())
})?;
},
Direction::Right => {
(self.0)(&mut |component| {
if w >= area.w() {
return Ok(())
}
if let Some([_, _, width, height]) = Offset::X(
w, component as &dyn Widget<Engine = Tui>
).render(to)? {
w += width;
h = h.max(height)
};
Ok(())
})?;
},
_ => todo!()
};
Ok(Some([area.x(), area.y(), w, h]))
}
}