raui_core/widget/
node.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::{
    props::Props,
    widget::{
        component::{WidgetComponent, WidgetComponentPrefab},
        unit::{WidgetUnitNode, WidgetUnitNodePrefab},
    },
    Prefab,
};
use serde::{Deserialize, Serialize};

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Default, Clone)]
pub enum WidgetNode {
    #[default]
    None,
    Component(WidgetComponent),
    Unit(WidgetUnitNode),
    Tuple(Vec<WidgetNode>),
}

impl WidgetNode {
    pub fn is_none(&self) -> bool {
        match self {
            Self::None => true,
            Self::Unit(unit) => unit.is_none(),
            Self::Tuple(v) => v.is_empty(),
            _ => false,
        }
    }

    pub fn is_some(&self) -> bool {
        match self {
            Self::None => false,
            Self::Unit(unit) => unit.is_some(),
            Self::Tuple(v) => !v.is_empty(),
            _ => true,
        }
    }

    pub fn as_component(&self) -> Option<&WidgetComponent> {
        match self {
            Self::Component(c) => Some(c),
            _ => None,
        }
    }

    pub fn as_unit(&self) -> Option<&WidgetUnitNode> {
        match self {
            Self::Unit(u) => Some(u),
            _ => None,
        }
    }

    pub fn as_tuple(&self) -> Option<&[WidgetNode]> {
        match self {
            Self::Tuple(v) => Some(v),
            _ => None,
        }
    }

    pub fn props(&self) -> Option<&Props> {
        match self {
            Self::Component(c) => Some(&c.props),
            Self::Unit(u) => u.props(),
            _ => None,
        }
    }

    pub fn props_mut(&mut self) -> Option<&mut Props> {
        match self {
            Self::Component(c) => Some(&mut c.props),
            Self::Unit(u) => u.props_mut(),
            _ => None,
        }
    }

    pub fn remap_props<F>(&mut self, f: F)
    where
        F: FnMut(Props) -> Props,
    {
        match self {
            Self::Component(c) => c.remap_props(f),
            Self::Unit(u) => u.remap_props(f),
            _ => {}
        }
    }

    pub fn shared_props(&self) -> Option<&Props> {
        match self {
            Self::Component(c) => c.shared_props.as_ref(),
            _ => None,
        }
    }

    pub fn shared_props_mut(&mut self) -> Option<&mut Props> {
        match self {
            Self::Component(c) => {
                if c.shared_props.is_none() {
                    c.shared_props = Some(Default::default());
                }
                c.shared_props.as_mut()
            }
            _ => None,
        }
    }

    pub fn remap_shared_props<F>(&mut self, f: F)
    where
        F: FnMut(Props) -> Props,
    {
        if let Self::Component(c) = self {
            c.remap_shared_props(f);
        }
    }

    pub fn pack_tuple<const N: usize>(data: [WidgetNode; N]) -> Self {
        Self::Tuple(data.into())
    }

    pub fn unpack_tuple<const N: usize>(self) -> [WidgetNode; N] {
        if let WidgetNode::Tuple(mut data) = self {
            let mut iter = data.drain(..);
            std::array::from_fn(|_| iter.next().unwrap_or_default())
        } else {
            std::array::from_fn(|_| WidgetNode::None)
        }
    }
}

impl From<()> for WidgetNode {
    fn from(_: ()) -> Self {
        Self::None
    }
}

impl From<()> for Box<WidgetNode> {
    fn from(_: ()) -> Self {
        Box::new(WidgetNode::None)
    }
}

impl From<WidgetComponent> for WidgetNode {
    fn from(component: WidgetComponent) -> Self {
        Self::Component(component)
    }
}

impl From<WidgetUnitNode> for WidgetNode {
    fn from(unit: WidgetUnitNode) -> Self {
        Self::Unit(unit)
    }
}

impl From<WidgetUnitNode> for Box<WidgetNode> {
    fn from(unit: WidgetUnitNode) -> Self {
        Box::new(WidgetNode::Unit(unit))
    }
}

impl<const N: usize> From<[WidgetNode; N]> for WidgetNode {
    fn from(data: [WidgetNode; N]) -> Self {
        Self::pack_tuple(data)
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub(crate) enum WidgetNodePrefab {
    #[default]
    None,
    Component(WidgetComponentPrefab),
    Unit(WidgetUnitNodePrefab),
    Tuple(Vec<WidgetNodePrefab>),
}

impl Prefab for WidgetNodePrefab {}