use crate::prelude::*;
#[derive(Declare)]
pub struct LayoutBox {
#[declare(skip)]
rect: Rect,
}
impl ComposeChild for LayoutBox {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
widget! {
states { this: this.into_writable() }
DynWidget {
dyns: child,
on_performed_layout: move |ctx| {
let new_rect = ctx.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() -> Widget {
widget! {
MockMulti {
LayoutBox {
id: layout_box,
MockBox { size: Size::new(100., 200.) }
}
MockBox { size: layout_box.rect.size }
}
}
}
widget_layout_test!(
smoke,
{ path = [0], width == 200., height == 200.,}
{ path = [0, 0], width == 100., height == 200.,}
{ path = [0, 1], width == 100., height == 200.,}
);
}