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
//! macros to implement functions of node trait

/// impl as_any and as_any_mut for node
#[macro_export]
macro_rules! impl_node_trait_as_any {
    () => {
        fn as_any(&self) -> &dyn Any {
            self
        }
        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
    };
}

/// impl as_any and as_any_mut for node
#[macro_export]
macro_rules! impl_node_trait_as_node {
    () => {
        fn as_node(&self) -> &dyn Node {
            self
        }
        fn as_node_mut(&mut self) -> &mut dyn Node {
            self
        }
    };
}

/// impl is_dirty, mark_dirty and mark_clean for node
/// need to have is_dirty:bool as a struct field
#[macro_export]
macro_rules! impl_node_trait_dirty {
    () => {
        fn is_dirty(&self) -> bool {
            self.is_dirty
        }
        fn mark_dirty(&mut self) {
            self.is_dirty = true
        }
        fn mark_clean(&mut self) {
            self.is_dirty = false
        }
    };
}

/// impl NodeComponent
#[macro_export]
macro_rules! impl_node_component {
    ($name:ident) => {
        impl NodeComponent for $name {
            impl_node_trait_dirty!();
        }
    };
}

/// impl container trait, should have child field
#[macro_export]
macro_rules! impl_container {
    ($name:ident) => {
        impl Container for $name {
            fn get_child(&self) -> &Option<StrongNodeType> {
                &self.child
            }

            fn get_child_mut(&mut self) -> &mut Option<StrongNodeType> {
                &mut self.child
            }

            fn as_container(&self) -> &dyn Container {
                self
            }

            fn as_container_mut(&mut self) -> &mut dyn Container {
                self
            }
        }
    };
}

/// impl container trait, should have child field
#[macro_export]
macro_rules! impl_component_node {
    ($name:ident) => {
        impl ComponentNode for $name {
            fn get_self_substitute(&self) -> &Option<WeakNodeType> {
                &self.self_substitute
            }

            fn get_self_substitute_mut(&mut self) -> &mut Option<WeakNodeType> {
                &mut self.self_substitute
            }
        }
    };
    ($name:ident impl_dirty) => {
        impl ComponentNode for $name {
            fn get_self_substitute(&self) -> &Option<WeakNodeType> {
                &self.self_substitute
            }

            fn get_self_substitute_mut(&mut self) -> &mut Option<WeakNodeType> {
                &mut self.self_substitute
            }

            impl_node_trait_dirty!();
        }
    };
}

/// impl get_child, get_child_mut, get_parent
#[macro_export]
macro_rules! impl_node_getters {
    () => {
        fn get_parent(&self) -> &WeakNodeType {
            &self.parent
        }
        fn get_sibling(&self) -> &Option<StrongNodeType> {
            &self.sibling
        }

        fn get_sibling_mut(&mut self) -> &mut Option<StrongNodeType> {
            &mut self.sibling
        }
    };
}


#[macro_export]
macro_rules! impl_widget_node_deref {
    ($name:ident $target:path) => {
        impl Deref for $name {
            type Target = $target;

            fn deref(&self) -> &Self::Target {
                &self.widget
            }
        }
    };
}