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
use std::{
    cell::RefCell,
    ops::{Deref, DerefMut},
    rc::{Rc, Weak},
};

use crate::{VContainerWidget, VNode, VWidget};

pub type StrongNodeType = Rc<RefCell<VNodeType>>;
pub type WeakNodeType = Weak<RefCell<VNodeType>>;

pub enum VNodeType {
    /// widget which cannot contain other nodes
    Widget(Box<dyn VWidget>),
    /// widget which can hold other widgets
    ContainerWidget(Box<dyn VContainerWidget>),
    /// widget which can hold other widgets but can't be added to other nodes
    TopLevelContainerWidget(Box<dyn VContainerWidget>),
}

impl Deref for VNodeType {
    type Target = dyn VNode;

    fn deref(&self) -> &Self::Target {
        match self {
            VNodeType::Widget(node) => node.deref().as_ref(),
            VNodeType::TopLevelContainerWidget(node) => node.deref().as_ref(),
            VNodeType::ContainerWidget(node) => node.deref().as_ref(),
        }
    }
}

impl DerefMut for VNodeType {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            VNodeType::Widget(node) => node.deref_mut().as_mut(),
            VNodeType::ContainerWidget(node) => node.deref_mut().as_mut(),
            VNodeType::TopLevelContainerWidget(node) => node.deref_mut().as_mut(),
        }
    }
}