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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use std::pin::Pin;
use std::marker::PhantomData;
use std::fmt;
use std::rc::Rc;
use std::cell::{RefCell};
use std::ops::{Deref, DerefMut};
use futures::Future;
use me_cell::{MeRefHandle, MeRefMutHandle};

use super::context::Scheduler;
use super::node::*;
use super::backend::{Backend};

pub trait Component<B: Backend>: ComponentTemplate<B> + downcast_rs::Downcast {
    fn new(_ctx: ComponentContext<B, Self>) -> Self where Self: Sized;
    fn created(&mut self) {

    }
    fn attached(&mut self) {

    }
    fn moved(&mut self) {

    }
    fn detached(&mut self) {

    }
}
downcast_rs::impl_downcast!(Component<B> where B: Backend);

pub trait PrerenderableComponent<'a, B: Backend>: Component<B> {
    type PrerenderedData: serde::Serialize + serde::Deserialize<'a>;
    fn get_prerendered_data(&self) -> Pin<Box<dyn Future<Output = Self::PrerenderedData>>>;
    fn apply_prerendered_data(&mut self, _data: &Self::PrerenderedData);
}

pub enum ComponentTemplateOperation {
    Init,
    Update,
}

pub trait ComponentTemplate<B: Backend>: 'static {
    fn template(component: &mut ComponentNodeRefMut<B>, operation: ComponentTemplateOperation) -> Option<Vec<NodeRc<B>>> where Self: Sized {
        if let ComponentTemplateOperation::Update = operation {
            return None
        }
        let mut f = || {
            vec![component.new_virtual_node("slot", VirtualNodeProperty::Slot("", vec![]), vec![]).into()]
        };
        Some(f())
    }
}

#[derive(Clone)]
pub struct ComponentContext<B: Backend, C: Component<B>> {
    node_weak: ComponentWeak<B, C>,
    need_update: Rc<RefCell<Vec<Box<dyn 'static + FnOnce(&mut ComponentNodeRefMut<B>)>>>>,
    scheduler: Rc<Scheduler>,
    phantom_data: PhantomData<C>,
}
impl<B: Backend, C: Component<B>> ComponentContext<B, C> {
    pub(crate) fn new(node_weak: ComponentWeak<B, C>, need_update: Rc<RefCell<Vec<Box<dyn 'static + FnOnce(&mut ComponentNodeRefMut<B>)>>>>, scheduler: Rc<Scheduler>) -> Self {
        Self {
            node_weak,
            need_update,
            scheduler,
            phantom_data: PhantomData,
        }
    }
    fn add_updater(v: &mut Vec<Box<dyn 'static + FnOnce(&mut ComponentNodeRefMut<B>)>>) {
        v.push(Box::new(|c: &mut ComponentNodeRefMut<B>| {
            <C as ComponentTemplate<B>>::template(c, ComponentTemplateOperation::Update);
        }));
    }
    pub fn update(&self) {
        let mut update_callbacks = self.need_update.borrow_mut();
        if update_callbacks.len() == 0 {
            Self::add_updater(&mut update_callbacks);
        }
    }
    pub fn update_then<F: 'static + FnOnce(&mut ComponentRefMut<B, C>)>(&self, callback: F) {
        let mut update_callbacks = self.need_update.borrow_mut();
        if update_callbacks.len() == 0 {
            Self::add_updater(&mut update_callbacks);
        }
        update_callbacks.push(Box::new(|x| {
            callback(&mut x.duplicate().with_type::<C>());
        }));
    }
    pub fn tick<F: 'static + FnOnce(&mut ComponentRefMut<B, C>)>(&self, f: F) {
        let rc = self.node_weak.upgrade().unwrap();
        self.scheduler.add_task(move || {
            f(&mut rc.borrow_mut());
        });
    }
}

#[derive(Clone)]
pub struct ComponentRc<B: Backend, C: Component<B>> {
    n: ComponentNodeRc<B>,
    phantom_data: std::marker::PhantomData<C>,
}
impl<B: Backend, C: Component<B>> ComponentRc<B, C> {
    pub fn into_node(self) -> ComponentNodeRc<B> {
        self.n
    }
    pub fn as_node(&self) -> &ComponentNodeRc<B> {
        &self.n
    }
    pub fn borrow<'a>(&'a self) -> ComponentRef<'a, B, C> {
        self.n.borrow().with_type::<C>()
    }
    pub fn borrow_mut<'a>(&'a self) -> ComponentRefMut<'a, B, C> {
        self.n.borrow_mut().with_type::<C>()
    }
    pub fn borrow_with<'a: 'b, 'b, U>(&'b self, source: &'b U) -> ComponentRef<'b, B, C> where U: ElementRef<'a, B> {
        self.n.borrow_with(source).with_type::<C>()
    }
    pub fn borrow_mut_with<'a: 'b, 'b, U>(&'b self, source: &'b mut U) -> ComponentRefMut<'b, B, C> where U: ElementRefMut<'a, B> {
        self.n.borrow_mut_with(source).with_type::<C>()
    }
    pub unsafe fn borrow_mut_unsafe_with<'a: 'b, 'b, 'c, U>(&'c self, source: &'b mut U) -> ComponentRefMut<'c, B, C> where U: ElementRefMut<'a, B> {
        self.n.borrow_mut_unsafe_with(source).with_type::<C>()
    }
    pub fn downgrade(&self) -> ComponentWeak<B, C> {
        self.n.downgrade().with_type::<C>()
    }
}
impl<B: Backend, C: Component<B>> From<ComponentNodeRc<B>> for ComponentRc<B, C> {
    fn from(n: ComponentNodeRc<B>) -> Self {
        Self {
            n,
            phantom_data: std::marker::PhantomData
        }
    }
}

#[derive(Clone)]
pub struct ComponentWeak<B: Backend, C: Component<B>> {
    n: ComponentNodeWeak<B>,
    phantom_data: std::marker::PhantomData<C>,
}
impl<B: Backend, C: Component<B>> ComponentWeak<B, C> {
    pub fn into_node(self) -> ComponentNodeWeak<B> {
        self.n
    }
    pub fn as_node(&self) -> &ComponentNodeWeak<B> {
        &self.n
    }
    pub fn upgrade(&self) -> Option<ComponentRc<B, C>> {
        self.n.upgrade().map(|x| {
            x.with_type::<C>()
        })
    }
}
impl<B: Backend, C: Component<B>> From<ComponentNodeWeak<B>> for ComponentWeak<B, C> {
    fn from(n: ComponentNodeWeak<B>) -> Self {
        Self {
            n,
            phantom_data: std::marker::PhantomData
        }
    }
}

pub struct ComponentRef<'a, B: Backend, C: Component<B>> {
    n: ComponentNodeRef<'a, B>,
    phantom_data: std::marker::PhantomData<C>,
}
impl<'a, B: Backend, C: Component<B>> ComponentRef<'a, B, C> {
    pub fn into_node(self) -> ComponentNodeRef<'a, B> {
        self.n
    }
    pub fn as_node(&self) -> &ComponentNodeRef<'a, B> {
        &self.n
    }
    pub fn backend_element(&self) -> &B::BackendElement {
        self.n.backend_element()
    }
    pub fn owner(&self) -> Option<ComponentNodeRc<B>> {
        self.n.owner()
    }
    pub fn marked(&self, r: &str) -> Option<NodeRc<B>> {
        self.n.marked(r)
    }
    pub fn marked_native_node(&self, r: &str) -> Option<NativeNodeRc<B>> {
        self.n.marked_native_node(r)
    }
    pub fn marked_component_node(&self, r: &str) -> Option<ComponentNodeRc<B>> {
        self.n.marked_component_node(r)
    }
    pub fn marked_component<D: Component<B>>(&self, r: &str) -> Option<ComponentRc<B, D>> {
        self.n.marked_component(r)
    }
    pub fn to_html<T: std::io::Write>(&self, s: &mut T) -> std::io::Result<()> {
        self.n.to_html(s)
    }
}
impl<'a, B: Backend, C: Component<B>> Deref for ComponentRef<'a, B, C> {
    type Target = C;
    fn deref(&self) -> &C {
        self.n.as_component()
    }
}
impl<'a, B: Backend, C: Component<B>> ElementRef<'a, B> for ComponentRef<'a, B, C> {
    fn backend(&self) -> &Rc<B> {
        self.n.backend()
    }
    fn as_me_ref_handle(&self) -> &MeRefHandle<'a> {
        self.n.as_me_ref_handle()
    }
}
impl<'a, B: Backend, C: Component<B>> From<ComponentNodeRef<'a, B>> for ComponentRef<'a, B, C> {
    fn from(n: ComponentNodeRef<'a, B>) -> Self {
        Self {
            n,
            phantom_data: std::marker::PhantomData
        }
    }
}
impl<'a, B: Backend, C: Component<B>> fmt::Debug for ComponentRef<'a, B, C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.n)
    }
}

pub struct ComponentRefMut<'a, B: Backend, C: Component<B>> {
    n: ComponentNodeRefMut<'a, B>,
    phantom_data: std::marker::PhantomData<C>,
}
impl<'a, B: Backend, C: Component<B>> ComponentRefMut<'a, B, C> {
    pub fn as_node(&mut self) -> &mut ComponentNodeRefMut<'a, B> {
        &mut self.n
    }
    pub fn to_ref<'b>(&'b self) -> ComponentRef<'b, B, C> where 'a: 'b {
        self.n.to_ref().into()
    }
    pub fn backend_element(&self) -> &B::BackendElement {
        self.n.backend_element()
    }
    pub fn owner(&self) -> Option<ComponentNodeRc<B>> {
        self.n.owner()
    }
    pub fn to_html<T: std::io::Write>(&self, s: &mut T) -> std::io::Result<()> {
        self.n.to_html(s)
    }
    pub fn apply_updates(&mut self) {
        self.n.apply_updates();
    }
    pub fn force_apply_updates(&mut self) {
        self.n.force_apply_updates::<C>();
    }
    pub fn marked(&self, r: &str) -> Option<NodeRc<B>> {
        self.n.marked(r)
    }
    pub fn marked_native_node(&self, r: &str) -> Option<NativeNodeRc<B>> {
        self.n.marked_native_node(r)
    }
    pub fn marked_component_node(&self, r: &str) -> Option<ComponentNodeRc<B>> {
        self.n.marked_component_node(r)
    }
    pub fn marked_component<D: Component<B>>(&self, r: &str) -> Option<ComponentRc<B, D>> {
        self.n.marked_component(r)
    }
}
impl<'a, B: Backend, C: Component<B>> Drop for ComponentRefMut<'a, B, C> {
    fn drop(&mut self) {
        self.apply_updates();
    }
}
impl<'a, B: Backend, C: Component<B>> Deref for ComponentRefMut<'a, B, C> {
    type Target = C;
    fn deref(&self) -> &C {
        self.n.as_component()
    }
}
impl<'a, B: Backend, C: Component<B>> DerefMut for ComponentRefMut<'a, B, C> {
    fn deref_mut(&mut self) -> &mut C {
        self.n.as_component_mut()
    }
}
impl<'a, B: Backend, C: Component<B>> ElementRefMut<'a, B> for ComponentRefMut<'a, B, C> {
    fn backend(&self) -> &Rc<B> {
        self.n.backend()
    }
    fn as_me_ref_mut_handle<'b>(&'b mut self) -> &'b mut MeRefMutHandle<'a> where 'a: 'b {
        self.n.as_me_ref_mut_handle()
    }
}
impl<'a, B: Backend, C: Component<B>> From<ComponentNodeRefMut<'a, B>> for ComponentRefMut<'a, B, C> {
    fn from(n: ComponentNodeRefMut<'a, B>) -> Self {
        Self {
            n,
            phantom_data: std::marker::PhantomData
        }
    }
}
impl<'a, B: Backend, C: Component<B>> fmt::Debug for ComponentRefMut<'a, B, C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.n)
    }
}

pub struct EmptyComponent { }
impl<B: Backend> Component<B> for EmptyComponent {
    fn new(_ctx: ComponentContext<B, Self>) -> Self {
        Self { }
    }
}
impl<B: Backend> ComponentTemplate<B> for EmptyComponent {
    // empty
}