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
use glory_core::reflow::{self, Revisable};
use glory_core::{Scope, ViewId, Widget};

use crate::TruckExt;

#[derive(Debug, Default)]
pub struct Graff {
    name: String,
}

impl Graff {
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }
}

impl Widget for Graff {
    fn build(&mut self, ctx: &mut Scope) {
        ctx.truck.stuffs().bind_view(ctx.view_id());

        cfg_if! {
            if #[cfg(feature = "__single_holder")] {
                let stuff = reflow::untrack(|| ctx.truck.remove_stuff(&self.name));
            } else {
                let stuff = reflow::untrack(ctx.holder_id(), || ctx.truck.remove_stuff(&self.name));
            }
        }
        if let Some(stuff) = stuff {
            let view_id = (stuff.0)(ctx);
            ctx.attach_child(&view_id);
        }
    }

    fn patch(&mut self, ctx: &mut Scope) {
        cfg_if! {
            if #[cfg(feature = "__single_holder")] {
                let stuff = glory_core::reflow::untrack(|| ctx.truck.remove_stuff(&self.name));
            } else {
                let stuff = glory_core::reflow::untrack(ctx.holder_id(), || ctx.truck.remove_stuff(&self.name));
            }
        }
        if let Some(stuff) = stuff {
            let view_ids: Vec<ViewId> = ctx.child_views().keys().cloned().collect();
            if !view_ids.is_empty() {
                cfg_if! {
                    if #[cfg(feature = "__single_holder")] {
                        glory_core::reflow::batch(|| {
                            for view_id in &view_ids {
                                ctx.detach_child(view_id);
                            }
                        });
                    } else {
                        glory_core::reflow::batch(ctx.holder_id(), || {
                            for view_id in &view_ids {
                                ctx.detach_child(view_id);
                            }
                        });
                    }
                }
            }
            glory_core::info!("[routing]: mounting stuff {:?}", self.name);
            let view_id = (stuff.0)(ctx);
            ctx.attach_child(&view_id);
        }
    }
}