1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use crate::{
    unpack_named_slots,
    widget::{
        context::WidgetContext,
        node::WidgetNode,
        unit::size::{SizeBoxNode, SizeBoxSizeValue},
        utils::Rect,
    },
    PropsData,
};
use serde::{Deserialize, Serialize};

#[derive(PropsData, Debug, Default, Clone, Serialize, Deserialize)]
#[props_data(crate::props::PropsData)]
#[prefab(crate::Prefab)]
pub struct WrapBoxProps {
    #[serde(default)]
    pub margin: Rect,
    #[serde(default)]
    pub fill: bool,
}

pub fn wrap_box(context: WidgetContext) -> WidgetNode {
    let WidgetContext {
        id,
        props,
        named_slots,
        ..
    } = context;
    unpack_named_slots!(named_slots => content);

    let WrapBoxProps { margin, fill } = props.read_cloned_or_default();
    let (width, height) = if fill {
        (SizeBoxSizeValue::Fill, SizeBoxSizeValue::Fill)
    } else {
        (SizeBoxSizeValue::Content, SizeBoxSizeValue::Content)
    };

    SizeBoxNode {
        id: id.to_owned(),
        props: props.clone(),
        slot: Box::new(content),
        margin,
        width,
        height,
        ..Default::default()
    }
    .into()
}