raui_core/widget/component/containers/
wrap_box.rs

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