use crate::prelude::*;
#[derive(Default)]
pub struct LayoutBox {
rect: Rect,
}
impl Declare for LayoutBox {
type Builder = FatObj<()>;
#[inline]
fn declarer() -> Self::Builder { FatObj::new(()) }
}
impl<'c> ComposeChild<'c> for LayoutBox {
type Child = Widget<'c>;
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> Widget<'c> {
let mut w = FatObj::new(child);
w.on_performed_layout(move |e| {
let new_rect = e.box_rect().unwrap();
let mut this = this.silent();
if this.rect != new_rect {
this.rect = new_rect;
}
});
w.into_widget()
}
}
impl LayoutBox {
#[inline]
pub fn layout_rect(&self) -> Rect { self.rect }
#[inline]
pub fn layout_pos(&self) -> Point { self.rect.origin }
#[inline]
pub fn layout_size(&self) -> Size { self.rect.size }
#[inline]
pub fn layout_left(&self) -> f32 { self.rect.min_x() }
#[inline]
pub fn layout_top(&self) -> f32 { self.rect.min_y() }
#[inline]
pub fn layout_width(&self) -> f32 { self.rect.width() }
#[inline]
pub fn layout_height(&self) -> f32 { self.rect.height() }
}
#[cfg(test)]
mod tests {
use ribir_dev_helper::*;
use super::*;
use crate::test_helper::*;
widget_layout_test!(
smoke,
WidgetTester::new(fn_widget! {
let mut first_box = @MockBox { size: Size::new(100., 200.) };
let second_box = @MockBox { size: pipe!(*$read(first_box.layout_size())) };
@MockMulti {
@ { [first_box, second_box ] }
}
}),
LayoutCase::default().with_size(Size::new(200., 200.)),
LayoutCase::new(&[0, 0]).with_size(Size::new(100., 200.)),
LayoutCase::new(&[0, 1]).with_size(Size::new(100., 200.))
);
}