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
use crate::{View, Node, Bind, Context, Event, IdPath, Id, IdentifyExt};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct If<T, F> {
    then_view: Option<T>,
    else_view: Option<F>,
}

impl<T> If<T, ()> {
    pub fn new(condition: bool, then_view: impl FnOnce() -> T) -> Self {
        Self {
            then_view: if condition { Some(then_view()) } else { None },
            else_view: None,
        }
    }
}

impl<T, F> If<T, F> {
    pub fn new_or_else(condition: bool, then_view: impl FnOnce() -> T, else_view: impl FnOnce() -> F) -> Self {
        Self {
            then_view: if condition { Some(then_view()) } else { None },
            else_view: if !condition { Some(else_view()) } else { None },
        }
    }
}

impl<T, F> Bind for If<T, F> where T: Bind, F: Bind {}

impl<T, F> View for If<T, F> where T: View, F: View {
    fn fire(&self, event: &Event, id_path: &IdPath) {
        if let Some(head) = id_path.head() {
            match head {
                Id::Index(0) => {
                    if let Some(ref then_view) = self.then_view {
                        then_view.fire(event, &id_path.tail())
                    }
                },
                Id::Index(1) => {
                    if let Some(ref else_view) = self.else_view {
                        else_view.fire(event, &id_path.tail())
                    }
                },
                i => panic!("Cannot fire event for child id {} on HStack which only has one child", i)
            }
        }
    }

    fn render(&mut self, context: &Context) -> Node {
        if let Some(ref mut then_view) = self.then_view {
            Node::Child { wrapped: Box::new(then_view.render(&context.child(0)).identify(0)) }
        } else if let Some(ref mut else_view) = self.else_view {
            Node::Child { wrapped: Box::new(else_view.render(&context.child(1)).identify(1)) }
        } else {
            Node::Empty {}
        }
    }
}