use crate::prelude::*;
#[derive(Declare)]
pub struct LayoutBox {
#[declare(skip)]
rect: Rect,
}
impl ComposeChild for LayoutBox {
type Child = Widget;
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
fn_widget! {
@ $child {
on_performed_layout: move |e| {
let new_rect = e.box_rect().unwrap();
if $this.rect != new_rect {
$this.silent().rect = new_rect;
}
}
}
}
}
}
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 super::*;
use crate::test_helper::*;
use ribir_dev_helper::*;
fn smoke() -> impl WidgetBuilder {
fn_widget! {
let mut first_box = @MockBox { size: Size::new(100., 200.) };
let second_box = @MockBox { size: pipe!($first_box.layout_size()) };
@MockMulti {
@ { [first_box, second_box ] }
}
}
}
widget_layout_test!(
smoke,
{ path = [0], width == 200., height == 200.,}
{ path = [0, 0], width == 100., height == 200.,}
{ path = [0, 1], width == 100., height == 200.,}
);
}