raui_core/widget/unit/
portal.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::widget::{
    node::{WidgetNode, WidgetNodePrefab},
    unit::{
        content::{ContentBoxItem, ContentBoxItemNode, ContentBoxItemNodePrefab},
        flex::{FlexBoxItem, FlexBoxItemNode, FlexBoxItemNodePrefab},
        grid::{GridBoxItem, GridBoxItemNode, GridBoxItemNodePrefab},
        WidgetUnit, WidgetUnitData,
    },
    WidgetId,
};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PortalBoxSlot {
    Slot(WidgetUnit),
    ContentItem(ContentBoxItem),
    FlexItem(FlexBoxItem),
    GridItem(GridBoxItem),
}

impl Default for PortalBoxSlot {
    fn default() -> Self {
        Self::Slot(Default::default())
    }
}

impl TryFrom<PortalBoxSlotNode> for PortalBoxSlot {
    type Error = ();

    fn try_from(node: PortalBoxSlotNode) -> Result<Self, Self::Error> {
        Ok(match node {
            PortalBoxSlotNode::Slot(node) => PortalBoxSlot::Slot(WidgetUnit::try_from(node)?),
            PortalBoxSlotNode::ContentItem(item) => {
                PortalBoxSlot::ContentItem(ContentBoxItem::try_from(item)?)
            }
            PortalBoxSlotNode::FlexItem(item) => {
                PortalBoxSlot::FlexItem(FlexBoxItem::try_from(item)?)
            }
            PortalBoxSlotNode::GridItem(item) => {
                PortalBoxSlot::GridItem(GridBoxItem::try_from(item)?)
            }
        })
    }
}

#[derive(Debug, Clone)]
pub enum PortalBoxSlotNode {
    Slot(WidgetNode),
    ContentItem(ContentBoxItemNode),
    FlexItem(FlexBoxItemNode),
    GridItem(GridBoxItemNode),
}

impl Default for PortalBoxSlotNode {
    fn default() -> Self {
        Self::Slot(Default::default())
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct PortalBox {
    #[serde(default)]
    pub id: WidgetId,
    #[serde(default)]
    pub slot: Box<PortalBoxSlot>,
    #[serde(default)]
    pub owner: WidgetId,
}

impl WidgetUnitData for PortalBox {
    fn id(&self) -> &WidgetId {
        &self.id
    }

    fn get_children(&self) -> Vec<&WidgetUnit> {
        vec![match &*self.slot {
            PortalBoxSlot::Slot(b) => b,
            PortalBoxSlot::ContentItem(b) => &b.slot,
            PortalBoxSlot::FlexItem(b) => &b.slot,
            PortalBoxSlot::GridItem(b) => &b.slot,
        }]
    }
}

impl TryFrom<PortalBoxNode> for PortalBox {
    type Error = ();

    fn try_from(node: PortalBoxNode) -> Result<Self, Self::Error> {
        let PortalBoxNode { id, slot, owner } = node;
        Ok(Self {
            id,
            slot: Box::new(PortalBoxSlot::try_from(*slot)?),
            owner,
        })
    }
}

#[derive(Debug, Default, Clone)]
pub struct PortalBoxNode {
    pub id: WidgetId,
    pub slot: Box<PortalBoxSlotNode>,
    pub owner: WidgetId,
}

impl From<PortalBoxNode> for WidgetNode {
    fn from(data: PortalBoxNode) -> Self {
        Self::Unit(data.into())
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub(crate) struct PortalBoxNodePrefab {
    #[serde(default)]
    pub id: WidgetId,
    #[serde(default)]
    pub slot: Box<PortalBoxSlotNodePrefab>,
    #[serde(default)]
    pub owner: WidgetId,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) enum PortalBoxSlotNodePrefab {
    Slot(#[serde(default)] WidgetNodePrefab),
    ContentItem(#[serde(default)] ContentBoxItemNodePrefab),
    FlexItem(#[serde(default)] FlexBoxItemNodePrefab),
    GridItem(#[serde(default)] GridBoxItemNodePrefab),
}

impl Default for PortalBoxSlotNodePrefab {
    fn default() -> Self {
        Self::Slot(Default::default())
    }
}