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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::element_tree::{CompCtx, Element, VirtualDom};
use crate::glue::GlobalEventCx;

use crate::element_tree::ReconcileCtx;

use crate::elements::any_element::{AnyStateBox, ElementBox, VirtualDomBox};

use derivative::Derivative;
use std::fmt::Debug;

pub trait Component: Debug + Clone {
    type Props: Clone + Default + Debug + PartialEq + 'static;
    type LocalEvent: 'static;
    type LocalState: Clone + Default + Debug + PartialEq + 'static;

    fn new<ParentCpEvent, ParentCpState>(
        props: Self::Props,
    ) -> ComponentHolder<Self, ParentCpEvent, ParentCpState>;

    fn name() -> &'static str;

    #[doc(hidden)]
    fn call_indirect<ParentCpEvent, ParentCpState>(
        &self,
        prev_state: (Self::LocalState, Option<AnyStateBox>),
        props: Self::Props,
    ) -> (
        ComponentOutput<
            Self::LocalEvent,
            Self::LocalState,
            VirtualDomBox<Self::LocalEvent, Self::LocalState>,
            ParentCpEvent,
            ParentCpState,
        >,
        (Self::LocalState, Option<AnyStateBox>),
    );
}

#[derive(Derivative, Default, PartialEq, Eq, Hash)]
#[derivative(Clone(bound = "Comp::Props: Clone"))]
pub struct ComponentHolder<Comp: Component, ParentCpEvent, ParentCpState> {
    component: Comp,
    props: Comp::Props,
    _marker: std::marker::PhantomData<(ParentCpEvent, ParentCpState)>,
}

#[derive(Derivative, Clone, PartialEq, Eq, Hash)]
#[derivative(Default(bound = "Child: Default"))]
pub struct ComponentOutput<
    ChildCpEvent,
    ChildCpState: Clone + Default + Debug + PartialEq,
    Child: VirtualDom<ChildCpEvent, ChildCpState>,
    ParentCpEvent,
    ParentCpState,
> {
    child: Child,
    name: &'static str,
    _markers: std::marker::PhantomData<(ParentCpEvent, ParentCpState, ChildCpEvent, ChildCpState)>,
}

// ---

impl<Comp: Component, ParentCpEvent, ParentCpState>
    ComponentHolder<Comp, ParentCpEvent, ParentCpState>
{
    pub fn new(component: Comp, props: Comp::Props) -> Self {
        Self {
            component,
            props,
            _marker: Default::default(),
        }
    }
}

impl<Comp: Component, ParentCpEvent, ParentCpState> std::fmt::Debug
    for ComponentHolder<Comp, ParentCpEvent, ParentCpState>
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple(Comp::name()).field(&self.props).finish()
    }
}

impl<
        ChildCpEvent,
        ChildCpState: Clone + Default + Debug + PartialEq,
        Child: VirtualDom<ChildCpEvent, ChildCpState>,
        ParentCpEvent,
        ParentCpState,
    > std::fmt::Debug
    for ComponentOutput<ChildCpEvent, ChildCpState, Child, ParentCpEvent, ParentCpState>
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple(self.name).field(&self.child).finish()
    }
}

// ---

impl<Comp: Component, ParentCpEvent, ParentCpState>
    ComponentHolder<Comp, ParentCpEvent, ParentCpState>
{
    pub fn build_with<ReturnedTree: Element<Comp::LocalEvent, Comp::LocalState> + 'static>(
        _comp: Comp,
        comp_fn: impl Fn(&CompCtx, Comp::Props) -> ReturnedTree,
        prev_state: (Comp::LocalState, Option<AnyStateBox>),
        props: Comp::Props,
    ) -> (
        ComponentOutput<
            Comp::LocalEvent,
            Comp::LocalState,
            VirtualDomBox<Comp::LocalEvent, Comp::LocalState>,
            ParentCpEvent,
            ParentCpState,
        >,
        (Comp::LocalState, Option<AnyStateBox>),
    ) {
        let (local_state, children_state) = prev_state;
        let ctx = CompCtx {
            local_state: &local_state,
        };

        let element_tree = ElementBox::new(comp_fn(&ctx, props));
        let (vdom, new_children_state) = element_tree.build(children_state);

        (
            ComponentOutput {
                child: vdom,
                name: Comp::name(),
                _markers: Default::default(),
            },
            (local_state, new_children_state),
        )
    }
}

impl<Comp: Component, ParentCpEvent, ParentCpState> Element<ParentCpEvent, ParentCpState>
    for ComponentHolder<Comp, ParentCpEvent, ParentCpState>
{
    type Event = Comp::LocalEvent;
    type AggregateChildrenState = (Comp::LocalState, Option<AnyStateBox>);
    type BuildOutput = ComponentOutput<
        Comp::LocalEvent,
        Comp::LocalState,
        VirtualDomBox<Comp::LocalEvent, Comp::LocalState>,
        ParentCpEvent,
        ParentCpState,
    >;

    // TODO - add spans
    fn build(
        self,
        prev_state: Self::AggregateChildrenState,
    ) -> (Self::BuildOutput, Self::AggregateChildrenState) {
        self.component.call_indirect(prev_state, self.props)
    }
}

/// ---

impl<
        ChildCpEvent,
        ChildCpState: Clone + Default + Debug + PartialEq,
        Child: VirtualDom<ChildCpEvent, ChildCpState>,
        ParentCpEvent,
        ParentCpState,
    > VirtualDom<ParentCpEvent, ParentCpState>
    for ComponentOutput<ChildCpEvent, ChildCpState, Child, ParentCpEvent, ParentCpState>
{
    type Event = ChildCpEvent;
    type AggregateChildrenState = (ChildCpState, Child::AggregateChildrenState);
    type TargetWidgetSeq = Child::TargetWidgetSeq;

    // TODO - add spans
    fn init_tree(&self) -> Child::TargetWidgetSeq {
        self.child.init_tree()
    }

    fn reconcile(
        &self,
        other: &Self,
        widget_seq: &mut Child::TargetWidgetSeq,
        ctx: &mut ReconcileCtx,
    ) {
        self.child.reconcile(&other.child, widget_seq, ctx);
    }

    fn process_local_event(
        &self,
        _component_state: &mut ParentCpState,
        children_state: &mut Self::AggregateChildrenState,
        widget_seq: &mut Child::TargetWidgetSeq,
        cx: &mut GlobalEventCx,
    ) -> Option<Self::Event> {
        self.child
            .process_event(&mut children_state.0, &mut children_state.1, widget_seq, cx)
    }
}

#[cfg(test)]
mod tests {
    #![allow(dead_code)]

    use crate as panoramix;

    #[derive(Debug, Default, Clone, PartialEq, Hash)]
    struct MyComponent;

    type MyPropsType = ();
    type MyLocalEvent = panoramix::NoEvent;
    type MyLocalState = u16;

    impl MyComponent {
        fn new<ParentCpEvent, ParentCpState>(
            props: MyPropsType,
        ) -> impl panoramix::Element<ParentCpEvent, ParentCpState> {
            <Self as panoramix::elements::component::Component>::new(props)
        }

        fn render(
            _ctx: &panoramix::CompCtx,
            _my_props: MyPropsType,
        ) -> impl panoramix::Element<MyLocalEvent, MyLocalState> {
            panoramix::elements::EmptyElement::new()
        }
    }

    impl panoramix::elements::component::Component for MyComponent {
        type Props = MyPropsType;
        type LocalState = MyLocalState;
        type LocalEvent = MyLocalEvent;

        fn new<ParentCpEvent, ParentCpState>(
            props: Self::Props,
        ) -> panoramix::elements::backend::ComponentHolder<Self, ParentCpEvent, ParentCpState>
        {
            panoramix::elements::backend::ComponentHolder::new(MyComponent, props)
        }

        fn name() -> &'static str {
            "MyComponent"
        }

        fn call_indirect<ParentCpEvent, ParentCpState>(
            &self,
            prev_state: (
                Self::LocalState,
                Option<panoramix::elements::any_element::AnyStateBox>,
            ),
            props: Self::Props,
        ) -> (
            panoramix::elements::component::ComponentOutput<
                Self::LocalEvent,
                Self::LocalState,
                panoramix::elements::any_element::VirtualDomBox<Self::LocalEvent, Self::LocalState>,
                ParentCpEvent,
                ParentCpState,
            >,
            (
                Self::LocalState,
                Option<panoramix::elements::any_element::AnyStateBox>,
            ),
        ) {
            panoramix::elements::backend::ComponentHolder::build_with(
                MyComponent,
                &MyComponent::render,
                prev_state,
                props,
            )
        }
    }

    use crate::element_tree::assign_empty_state_type;
    use crate::element_tree::Element;
    use insta::assert_debug_snapshot;
    use test_env_log::test;

    #[test]
    fn call_component() {
        let my_component = MyComponent::new(());
        assign_empty_state_type(&my_component);

        let (component_result, _state) = my_component.build(Default::default());
        assert_debug_snapshot!(component_result);

        //let prev_state = (999, Default::default());
        //let (component_result, component_state) = my_component.build(prev_state);
        //assert_eq!(component_state.0, 999);

        // TODO - local state
        // TODO - process_event
    }

    // TODO
    // - Widget test
    // - Events
}