tek/layout/src/map.rs

65 lines
2 KiB
Rust

use crate::*;
pub fn map_south<O: Output>(
item_offset: O::Unit,
item_height: O::Unit,
item: impl Content<O>
) -> impl Content<O> {
Push::y(item_offset,
Align::n(Fixed::y(item_height,
Fill::x(item))))
}
pub struct Map<'a, A, B, I, F, G>(pub PhantomData<&'a()>, pub F, pub G) where
I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync;
impl<'a, A, B, I, F, G> Map<'a, A, B, I, F, G> where
I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync
{
pub fn new (f: F, g: G) -> Self {
Self(Default::default(), f, g)
}
}
impl<'a, E, A, B, I, F, G> Content<E> for Map<'a, A, B, I, F, G> where
E: Output,
B: Render<E>,
I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync
{
fn layout (&self, area: E::Area) -> E::Area {
let Self(_, get_iterator, callback) = self;
let mut index = 0;
let [mut min_x, mut min_y] = area.center();
let [mut max_x, mut max_y] = area.center();
for item in get_iterator() {
let area = callback(item, index).layout(area).xywh();
let [x,y,w,h] = area.xywh();
min_x = min_x.min(x.into());
min_y = min_y.min(y.into());
max_x = max_x.max((x + w).into());
max_y = max_y.max((y + h).into());
index += 1;
}
let w = max_x - min_x;
let h = max_y - min_y;
//[min_x.into(), min_y.into(), w.into(), h.into()].into()
area.center_xy([w.into(), h.into()].into()).into()
}
fn render (&self, to: &mut E) {
let Self(_, get_iterator, callback) = self;
let mut index = 0;
//let area = self.layout(to.area());
for item in get_iterator() {
let item = callback(item, index);
//to.place(area.into(), &item);
to.place(item.layout(to.area()), &item);
index += 1;
}
}
}