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
use dioxus_core::{AttributeValue, ScopeState};
use dioxus_hooks::{use_effect, use_ref, use_state, UseRef};
use freya_common::NodeReferenceLayout;
use freya_node_state::{CustomAttributeValues, NodeReference};
use tokio::sync::mpsc::unbounded_channel;

/// Subscribe to a Node layout changes.
pub fn use_node(cx: &ScopeState) -> (AttributeValue, NodeReferenceLayout) {
    let status = use_state::<NodeReferenceLayout>(cx, NodeReferenceLayout::default);

    let channel = cx.use_hook(|| {
        let (tx, rx) = unbounded_channel::<NodeReferenceLayout>();
        (tx, Some(rx))
    });

    let node_ref = NodeReference(channel.0.clone());

    use_effect(cx, (), move |_| {
        let rx = channel.1.take();
        let status = status.clone();
        cx.spawn(async move {
            let mut rx = rx.unwrap();
            while let Some(new_status) = rx.recv().await {
                if status.current().as_ref() != &new_status {
                    status.set(new_status);
                }
            }
        });
        async move {}
    });

    (
        cx.any_value(CustomAttributeValues::Reference(node_ref)),
        status.get().clone(),
    )
}

/// Silently read the latest layout from a Node.
pub fn use_node_ref(cx: &ScopeState) -> (AttributeValue, &UseRef<NodeReferenceLayout>) {
    let status = use_ref::<NodeReferenceLayout>(cx, NodeReferenceLayout::default);

    let channel = cx.use_hook(|| {
        let (tx, rx) = unbounded_channel::<NodeReferenceLayout>();
        (tx, Some(rx))
    });

    let node_ref = NodeReference(channel.0.clone());

    use_effect(cx, (), move |_| {
        let rx = channel.1.take();
        let status = status.clone();
        cx.spawn(async move {
            let mut rx = rx.unwrap();
            while let Some(new_status) = rx.recv().await {
                if *status.read() != new_status {
                    *status.write_silent() = new_status;
                }
            }
        });
        async move {}
    });

    (
        cx.any_value(CustomAttributeValues::Reference(node_ref)),
        status,
    )
}

#[cfg(test)]
mod test {
    use crate::use_node;
    use freya::prelude::*;
    use freya_testing::{launch_test_with_config, TestingConfig};

    #[tokio::test]
    pub async fn track_size() {
        fn use_node_app(cx: Scope) -> Element {
            let (reference, size) = use_node(cx);

            render!(
                rect {
                    reference: reference,
                    width: "50%",
                    height: "25%",
                    "{size.area.width()}"
                }
            )
        }

        let mut utils = launch_test_with_config(
            use_node_app,
            TestingConfig::default().with_size((500.0, 800.0).into()),
        );

        utils.wait_for_update().await;
        let root = utils.root().get(0);
        assert_eq!(root.get(0).text().unwrap().parse::<f32>(), Ok(500.0 * 0.5));

        utils.set_config(TestingConfig::default().with_size((300.0, 800.0).into()));
        utils.wait_for_update().await;

        let root = utils.root().get(0);
        assert_eq!(root.get(0).text().unwrap().parse::<f32>(), Ok(300.0 * 0.5));
    }
}