1use crate::RelmRemoveExt;
5
6trait RemoveTempChild {
7 fn remove(&mut self);
8}
9
10struct TempWidgetsInner<C: RelmRemoveExt> {
11 container: C,
12 children: Vec<C::Child>,
13}
14
15impl<C: RelmRemoveExt> RemoveTempChild for TempWidgetsInner<C>
16where
17 C::Child: AsRef<C::Child>,
18{
19 fn remove(&mut self) {
20 for child in &mut self.children {
21 self.container.container_remove(&child);
22 }
23 }
24}
25
26pub struct LoadingWidgets {
34 containers: Vec<Box<dyn RemoveTempChild>>,
35}
36
37impl std::fmt::Debug for LoadingWidgets {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 f.debug_struct("LoadingWidgets")
40 .field("containers", &self.containers.len())
41 .finish()
42 }
43}
44
45impl Drop for LoadingWidgets {
46 fn drop(&mut self) {
47 for child in &mut self.containers {
48 child.remove();
49 }
50 }
51}
52
53impl LoadingWidgets {
54 fn temp_child<C, W>(container: C, children: &[W]) -> Box<dyn RemoveTempChild>
55 where
56 C: RelmRemoveExt + 'static,
57 W: AsRef<C::Child>,
58 C::Child: Clone + AsRef<C::Child>,
59 {
60 let children = children.iter().map(|c| c.as_ref().clone()).collect();
61 let temp_child: TempWidgetsInner<C> = TempWidgetsInner {
62 container,
63 children,
64 };
65
66 Box::new(temp_child)
67 }
68
69 pub fn new<C, W>(container: C, child: W) -> Self
71 where
72 C: RelmRemoveExt + 'static,
73 W: AsRef<C::Child>,
74 C::Child: Clone + AsRef<C::Child>,
75 {
76 Self::with_children(container, &[child])
77 }
78
79 pub fn with_children<C, W>(container: C, children: &[W]) -> Self
81 where
82 C: RelmRemoveExt + 'static,
83 W: AsRef<C::Child>,
84 C::Child: Clone + AsRef<C::Child>,
85 {
86 let temp_child = Self::temp_child(container, children);
87 Self {
88 containers: vec![temp_child],
89 }
90 }
91
92 pub fn push<C, W>(&mut self, container: C, child: W)
94 where
95 C: RelmRemoveExt + 'static,
96 W: AsRef<C::Child>,
97 C::Child: Clone + AsRef<C::Child>,
98 {
99 let temp_child = Self::temp_child(container, &[child]);
100 self.containers.push(temp_child);
101 }
102
103 pub fn add_many<C, W>(&mut self, container: C, children: &[W])
105 where
106 C: RelmRemoveExt + 'static,
107 W: AsRef<C::Child>,
108 C::Child: Clone + AsRef<C::Child>,
109 {
110 let temp_child = Self::temp_child(container, children);
111 self.containers.push(temp_child);
112 }
113}