raui_core/widget/component/containers/
wrap_box.rs

1use crate::{
2    unpack_named_slots,
3    widget::{
4        context::WidgetContext,
5        node::WidgetNode,
6        unit::size::{SizeBoxNode, SizeBoxSizeValue},
7        utils::Rect,
8    },
9    PropsData,
10};
11use serde::{Deserialize, Serialize};
12
13#[derive(PropsData, Debug, Clone, Serialize, Deserialize)]
14#[props_data(crate::props::PropsData)]
15#[prefab(crate::Prefab)]
16pub struct WrapBoxProps {
17    #[serde(default)]
18    pub margin: Rect,
19    #[serde(default = "WrapBoxProps::default_fill")]
20    pub fill: bool,
21}
22
23impl Default for WrapBoxProps {
24    fn default() -> Self {
25        Self {
26            margin: Default::default(),
27            fill: Self::default_fill(),
28        }
29    }
30}
31
32impl WrapBoxProps {
33    fn default_fill() -> bool {
34        true
35    }
36}
37
38pub fn wrap_box(context: WidgetContext) -> WidgetNode {
39    let WidgetContext {
40        id,
41        props,
42        named_slots,
43        ..
44    } = context;
45    unpack_named_slots!(named_slots => content);
46
47    let WrapBoxProps { margin, fill } = props.read_cloned_or_default();
48    let (width, height) = if fill {
49        (SizeBoxSizeValue::Fill, SizeBoxSizeValue::Fill)
50    } else {
51        (SizeBoxSizeValue::Content, SizeBoxSizeValue::Content)
52    };
53
54    SizeBoxNode {
55        id: id.to_owned(),
56        props: props.clone(),
57        slot: Box::new(content),
58        margin,
59        width,
60        height,
61        ..Default::default()
62    }
63    .into()
64}