0.2.0: constrain map

This commit is contained in:
🪞👃🪞 2025-03-17 00:46:47 +02:00
parent 1774419d93
commit 10a2d17b48
2 changed files with 60 additions and 12 deletions

View file

@ -1,5 +1,5 @@
[workspace.package] [workspace.package]
version = "0.1.2" version = "0.2.0"
[workspace] [workspace]
resolver = "2" resolver = "2"

View file

@ -23,22 +23,70 @@ use crate::*;
Push::x(item_offset, Align::w(Fixed::x(item_width, Fill::y(item)))) Push::x(item_offset, Align::w(Fixed::x(item_width, Fill::y(item))))
} }
pub struct Map<'a, A, B, I, F, G>(pub PhantomData<&'a()>, pub F, pub G) where pub struct Map<
'a,
E,
A,
B,
I: Iterator<Item = A> + Send + Sync, I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync + 'a, F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync; G,
> {
__: PhantomData<&'a (E, B)>,
/// Function that returns iterator over stacked components
get_iterator: F,
/// Function that returns each stacked component
get_item: G,
}
impl<'a, A, B, I, F, G> Map<'a, A, B, I, F, G> where impl<'a, E, A, B, I, F, G> Map<'a, E, A, B, I, F, G> where
I: Iterator<Item = A> + Send + Sync, I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync + 'a, F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync
{ {
pub fn new (f: F, g: G) -> Self { pub fn new (get_iterator: F, get_item: G) -> Self {
Self(Default::default(), f, g) Self {
__: Default::default(),
get_iterator,
get_item
}
} }
} }
impl<'a, E, A, B, I, F, G> Content<E> for Map<'a, A, B, I, F, G> where //impl<'a, E, A, B, I, F> Map<'a, E, A, B, I, F, ()> where
//E: Output,
//B: Content<E>,
//I: Iterator<Item = A> + Send + Sync,
//F: Fn() -> I + Send + Sync + 'a,
//{
//pub fn south <G> (
//get_iterator: F,
//get_content: impl Fn(A, usize)->B + Send + Sync,
//) -> Map<'a, E, A, B, I, F, G> where
//G: Fn(A, usize)->Push<E::Unit, Align<Fixed<E::Unit, Fill<B>>>> + Send + Sync
//{
//let mut offset = 0;
//Map::new(get_iterator, move|item, index|map_south(
//0.into(),
//0.into(),
//get_content(item, index)
//))
//}
//pub fn east <G> (
//get_iterator: F,
//get_content: impl Fn(A, usize)->B + Send + Sync,
//) -> Map<'a, E, A, B, I, F, G> where
//G: Fn(A, usize)->Push<E::Unit, Align<Fixed<E::Unit, Fill<B>>>> + Send + Sync
//{
//let mut offset = 0;
//Map::new(get_iterator, move|item, index|map_east(
//0.into(),
//0.into(),
//get_content(item, index)
//))
//}
//}
impl<'a, E, A, B, I, F, G> Content<E> for Map<'a, E, A, B, I, F, G> where
E: Output, E: Output,
B: Render<E>, B: Render<E>,
I: Iterator<Item = A> + Send + Sync, I: Iterator<Item = A> + Send + Sync,
@ -46,12 +94,12 @@ impl<'a, E, A, B, I, F, G> Content<E> for Map<'a, A, B, I, F, G> where
G: Fn(A, usize)->B + Send + Sync G: Fn(A, usize)->B + Send + Sync
{ {
fn layout (&self, area: E::Area) -> E::Area { fn layout (&self, area: E::Area) -> E::Area {
let Self(_, get_iterator, callback) = self; let Self { get_iterator, get_item, .. } = self;
let mut index = 0; let mut index = 0;
let [mut min_x, mut min_y] = area.center(); let [mut min_x, mut min_y] = area.center();
let [mut max_x, mut max_y] = area.center(); let [mut max_x, mut max_y] = area.center();
for item in get_iterator() { for item in get_iterator() {
let [x,y,w,h] = callback(item, index).layout(area).xywh(); let [x,y,w,h] = get_item(item, index).layout(area).xywh();
min_x = min_x.min(x.into()); min_x = min_x.min(x.into());
min_y = min_y.min(y.into()); min_y = min_y.min(y.into());
max_x = max_x.max((x + w).into()); max_x = max_x.max((x + w).into());
@ -64,11 +112,11 @@ impl<'a, E, A, B, I, F, G> Content<E> for Map<'a, A, B, I, F, G> where
area.center_xy([w.into(), h.into()].into()).into() area.center_xy([w.into(), h.into()].into()).into()
} }
fn render (&self, to: &mut E) { fn render (&self, to: &mut E) {
let Self(_, get_iterator, callback) = self; let Self { get_iterator, get_item, .. } = self;
let mut index = 0; let mut index = 0;
let area = Content::layout(self, to.area()); let area = Content::layout(self, to.area());
for item in get_iterator() { for item in get_iterator() {
let item = callback(item, index); let item = get_item(item, index);
//to.place(area.into(), &item); //to.place(area.into(), &item);
to.place(item.layout(area), &item); to.place(item.layout(area), &item);
index += 1; index += 1;