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
use std::any::Any;
use std::cell::RefCell;
use std::iter;
use std::rc::Rc;

use crate::api::Layer;
use crate::declarative_macros::handle_vtable_update_optional;
use crate::{
    BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
};

/// A special "control-flow" primitive associated with the `for` statement.
/// Repeat allows for nodes to be rendered dynamically per data specified in `source_expression`.
/// That is: for a `source_expression` of length `n`, `Repeat` will render its
/// template `n` times, each with an embedded component context (`RepeatItem`)
/// with an index `i` and a pointer to that relevant datum `source_expression[i]`
pub struct RepeatInstance {
    pub base: BaseInstance,
}

///Contains modal _vec_ and _range_ variants, describing whether the Repeat source
///is encoded as a Vec<T> (where T is a `dyn Any` properties type) or as a Range<isize>
#[derive(Default)]
pub struct RepeatProperties {
    pub source_expression_vec:
        Option<Box<dyn crate::api::PropertyInstance<Vec<Rc<RefCell<dyn Any>>>>>>,
    pub source_expression_range:
        Option<Box<dyn crate::api::PropertyInstance<std::ops::Range<isize>>>>,
    last_len: usize,
    last_bounds: (f64, f64),
}

pub struct RepeatItem {
    pub elem: Rc<RefCell<dyn Any>>,
    pub i: usize,
}

impl InstanceNode for RepeatInstance {
    fn instantiate(args: InstantiationArgs) -> Rc<Self>
    where
        Self: Sized,
    {
        Rc::new(Self {
            base: BaseInstance::new(
                args,
                InstanceFlags {
                    invisible_to_slot: true,
                    invisible_to_raycasting: true,
                    layer: Layer::DontCare,
                    is_component: false,
                },
            ),
        })
    }

    #[cfg(debug_assertions)]
    fn resolve_debug(
        &self,
        f: &mut std::fmt::Formatter,
        _expanded_node: Option<&ExpandedNode>,
    ) -> std::fmt::Result {
        f.debug_struct("Repeat").finish()
    }

    fn base(&self) -> &BaseInstance {
        &self.base
    }

    fn update(self: Rc<Self>, expanded_node: &Rc<ExpandedNode>, context: &mut RuntimeContext) {
        let new_vec =
            expanded_node.with_properties_unwrapped(|properties: &mut RepeatProperties| {
                handle_vtable_update_optional(
                    context.expression_table(),
                    &expanded_node.stack,
                    properties.source_expression_range.as_mut(),
                    context.globals(),
                );
                handle_vtable_update_optional(
                    context.expression_table(),
                    &expanded_node.stack,
                    properties.source_expression_vec.as_mut(),
                    context.globals(),
                );

                let vec = if let Some(ref source) = properties.source_expression_range {
                    Box::new(
                        source
                            .get()
                            .clone()
                            .map(|v| Rc::new(RefCell::new(v)) as Rc<RefCell<dyn Any>>),
                    ) as Box<dyn ExactSizeIterator<Item = Rc<RefCell<dyn Any>>>>
                } else if let Some(ref source) = properties.source_expression_vec {
                    Box::new(source.get().clone().into_iter())
                        as Box<dyn ExactSizeIterator<Item = Rc<RefCell<dyn Any>>>>
                } else {
                    //A valid Repeat must have a repeat source; presumably this has been gated by the parser / compiler
                    unreachable!();
                };

                let current_len = vec.len();

                let exp_props = expanded_node.layout_properties.borrow();
                let current_bounds = exp_props
                    .as_ref()
                    .map(|t| t.computed_tab.bounds)
                    .unwrap_or_default();
                let update_children =
                    current_len != properties.last_len || current_bounds != properties.last_bounds;

                properties.last_len = current_len;
                properties.last_bounds = current_bounds;
                update_children.then_some(vec)
            });

        if let Some(vec) = new_vec {
            let template_children = self.base().get_instance_children();
            let children_with_envs = iter::repeat(template_children)
                .zip(vec.into_iter())
                .enumerate()
                .flat_map(|(i, (children, elem))| {
                    let new_repeat_item = Rc::new(RefCell::new(RepeatItem {
                        i,
                        elem: Rc::clone(&elem),
                    })) as Rc<RefCell<dyn Any>>;
                    let new_env = expanded_node.stack.push(&new_repeat_item);
                    children
                        .borrow()
                        .clone()
                        .into_iter()
                        .zip(iter::repeat(new_env))
                });
            expanded_node.set_children(children_with_envs, context);
        }
    }

    fn handle_mount(&self, _expanded_node: &Rc<ExpandedNode>, _context: &mut RuntimeContext) {
        // No-op: wait with creating child-nodes until update tick, since the
        // condition has then been evaluated
    }
}