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
use crate::{ContainerChild, RelmSetChildExt};
use gtk::prelude::*;

/// Widget types which can have widgets removed from them.
pub trait RelmRemoveExt: ContainerChild {
    /// Removes the widget from the container
    /// if it is a child of the container.
    fn container_remove(&self, child: &impl AsRef<Self::Child>);
}

impl<T: RelmSetChildExt> RelmRemoveExt for T {
    fn container_remove(&self, child: &impl AsRef<Self::Child>) {
        if let Some(current_child) = self.container_get_child() {
            let remove_child = child.as_ref().upcast_ref();
            if remove_child == &current_child {
                self.container_set_child(None::<&gtk::Widget>);
            }
        }
    }
}

impl RelmRemoveExt for gtk::ListBox {
    fn container_remove(&self, widget: &impl AsRef<Self::Child>) {
        let row = widget.as_ref();
        row.set_child(None::<&gtk::Widget>);
        self.remove(row);
    }
}

impl RelmRemoveExt for gtk::FlowBox {
    fn container_remove(&self, widget: &impl AsRef<Self::Child>) {
        let child = widget.as_ref();
        child.set_child(None::<&gtk::Widget>);
        self.remove(widget.as_ref());
    }
}

#[cfg(feature = "libadwaita")]
impl RelmRemoveExt for adw::PreferencesGroup {
    fn container_remove(&self, widget: &impl AsRef<Self::Child>) {
        use adw::prelude::PreferencesGroupExt;
        self.remove(widget.as_ref());
    }
}

macro_rules! remove_impl {
    ($($type:ty),+) => {
        $(
            impl RelmRemoveExt for $type {
                fn container_remove(&self, widget: &impl AsRef<Self::Child>) {
                    self.remove(widget.as_ref());
                }
            }
        )+
    }
}

macro_rules! remove_child_impl {
    ($($type:ty),+) => {
        $(
            impl RelmRemoveExt for $type {
                fn container_remove(&self, widget: &impl AsRef<Self::Child>) {
                    self.remove_child(widget.as_ref());
                }
            }
        )+
    }
}

remove_impl!(
    gtk::Box,
    gtk::Fixed,
    gtk::Grid,
    gtk::ActionBar,
    gtk::Stack,
    gtk::HeaderBar
);
remove_child_impl!(gtk::InfoBar);

/// Widget types that allow removal of all their children.
pub trait RelmRemoveAllExt {
    /// Remove all children from the container.
    fn remove_all(&self);
}

impl<T: RelmSetChildExt> RelmRemoveAllExt for T {
    fn remove_all(&self) {
        self.container_set_child(None::<&gtk::Widget>);
    }
}

macro_rules! remove_all_impl {
    ($($type:ty),+) => {
        $(
            impl RelmRemoveAllExt for $type {
                fn remove_all(&self) {
                    while let Some(child) = self.last_child() {
                        self.remove(&child);
                    }
                }
            }
        )+
    }
}

remove_all_impl!(gtk::Box, gtk::FlowBox, gtk::Stack, gtk::Grid);

impl RelmRemoveAllExt for gtk::ListBox {
    fn remove_all(&self) {
        while let Some(child) = self.last_child() {
            let row = child
                .downcast::<gtk::ListBoxRow>()
                .expect("The child of `ListBox` is not a `ListBoxRow`.");
            row.set_child(None::<&gtk::Widget>);
            self.remove(&row);
        }
    }
}