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
//! Utilities for removing temporary widgets from
//! async factories or components.

use crate::RelmRemoveExt;

trait RemoveTempChild {
    fn remove(&mut self);
}

struct TempWidgetsInner<C: RelmRemoveExt> {
    container: C,
    children: Vec<C::Child>,
}

impl<C: RelmRemoveExt> RemoveTempChild for TempWidgetsInner<C>
where
    C::Child: AsRef<C::Child>,
{
    fn remove(&mut self) {
        for child in &mut self.children {
            self.container.container_remove(&child);
        }
    }
}

/// A type that stores widget containers and their child
/// widgets and removes all children automatically when dropped.
///
/// This mechanism is used by async components and factories
/// to show widgets while the async init function isn't completed.
/// Once the actual widgets are initialized, the temporary loading
/// widgets can be removed again, which is simply done with this type.
pub struct LoadingWidgets {
    containers: Vec<Box<dyn RemoveTempChild>>,
}

impl std::fmt::Debug for LoadingWidgets {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LoadingWidgets")
            .field("containers", &self.containers.len())
            .finish()
    }
}

impl Drop for LoadingWidgets {
    fn drop(&mut self) {
        for child in &mut self.containers {
            child.remove();
        }
    }
}

impl LoadingWidgets {
    fn temp_child<C, W>(container: &C, children: &[W]) -> Box<dyn RemoveTempChild>
    where
        C: RelmRemoveExt + Clone + 'static,
        W: AsRef<C::Child>,
        C::Child: Clone + AsRef<C::Child>,
    {
        let container = container.clone();
        let children = children.iter().map(|c| c.as_ref().clone()).collect();
        let temp_child: TempWidgetsInner<C> = TempWidgetsInner {
            container,
            children,
        };

        Box::new(temp_child)
    }

    /// Create new [`LoadingWidgets`] with one child.
    pub fn new<C, W>(container: &C, child: W) -> Self
    where
        C: RelmRemoveExt + Clone + 'static,
        W: AsRef<C::Child>,
        C::Child: Clone + AsRef<C::Child>,
    {
        Self::with_children(container, &[child])
    }

    /// Create new [`LoadingWidgets`] with multiple children.
    pub fn with_children<C, W>(container: &C, children: &[W]) -> Self
    where
        C: RelmRemoveExt + Clone + 'static,
        W: AsRef<C::Child>,
        C::Child: Clone + AsRef<C::Child>,
    {
        let temp_child = Self::temp_child(container, children);
        Self {
            containers: vec![temp_child],
        }
    }

    /// Add another child to the temporary loading widgets.
    pub fn push<C, W>(&mut self, container: &C, child: W)
    where
        C: RelmRemoveExt + Clone + 'static,
        W: AsRef<C::Child>,
        C::Child: Clone + AsRef<C::Child>,
    {
        let temp_child = Self::temp_child(container, &[child]);
        self.containers.push(temp_child);
    }

    /// Add many children to the temporary loading widgets.
    pub fn add_many<C, W>(&mut self, container: &C, children: &[W])
    where
        C: RelmRemoveExt + Clone + 'static,
        W: AsRef<C::Child>,
        C::Child: Clone + AsRef<C::Child>,
    {
        let temp_child = Self::temp_child(container, children);
        self.containers.push(temp_child);
    }
}