use crate::hooks::{use_context, use_second_render_pass};
use crate::types::{Align, AppContext, Dim, VAlign};
use crate::types::{ElementWrap, Elements};
use crate::utils::{FlowBucket, FlowConf};
use appy::components::context_provider;
use appy::{derive_component, function_component, with_clone, ComponentBuilder, SnakeFactory};
use std::rc::Rc;
#[derive_component(ComponentBuilder, Default, SnakeFactory)]
pub struct Blk {
left: Dim,
top: Dim,
width: Dim,
height: Dim,
bottom: Dim,
right: Dim,
flow_gap: Dim,
flow_vgap: Dim,
flow_align: Align,
flow_valign: VAlign,
}
impl Blk {
pub fn margin<T>(self: ElementWrap<Blk>, val: T) -> ElementWrap<Blk>
where
Dim: From<T>,
T: Clone,
{
self.left(val.clone())
.top(val.clone())
.right(val.clone())
.bottom(val.clone())
}
}
#[function_component]
fn _blk(props: Blk) -> Elements {
let app_context = use_context::<AppContext>();
let h = Dim::compute_span(app_context.rect.w, props.left, props.width, props.right);
let v = Dim::compute_span(app_context.rect.h, props.top, props.height, props.bottom);
let new_context = app_context.abs(h.0, v.0, h.1, v.1);
use_second_render_pass(Box::new(with_clone!([new_context], move || {
let elements = new_context.flow_elements.take();
let conf = FlowConf {
width: new_context.rect.w,
height: new_context.rect.h,
gap: props.flow_gap.get_abs(),
vgap: props.flow_vgap.get_abs(),
align: props.flow_align,
valign: props.flow_valign,
};
vec![context_provider()
.value(Rc::new(new_context))
.children(FlowBucket::flow(elements, conf))]
})));
vec![context_provider()
.value(Rc::new(new_context))
.children(props.children)]
}