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 gtk::prelude::*;

use crate::RelmSetChildExt;

/// Widget types which can have widgets attached to them.
pub trait RelmContainerExt {
    /// Add widget as child to container.
    fn container_add(&self, widget: &impl AsRef<gtk::Widget>);
}

impl<T: RelmSetChildExt> RelmContainerExt for T {
    fn container_add(&self, widget: &impl AsRef<gtk::Widget>) {
        self.container_set_child(Some(widget));
    }
}

impl RelmContainerExt for gtk::Dialog {
    fn container_add(&self, widget: &impl AsRef<gtk::Widget>) {
        self.content_area().append(widget.as_ref());
    }
}

#[cfg(feature = "libadwaita")]
impl RelmContainerExt for adw::PreferencesGroup {
    fn container_add(&self, widget: &impl AsRef<gtk::Widget>) {
        use adw::prelude::PreferencesGroupExt;
        self.add(widget.as_ref());
    }
}

#[cfg(feature = "libadwaita")]
impl RelmContainerExt for adw::Squeezer {
    fn container_add(&self, widget: &impl AsRef<gtk::Widget>) {
        self.add(widget.as_ref());
    }
}

macro_rules! append_impl {
    ($($type:ty),+) => {
        $(
            impl RelmContainerExt for $type {
                fn container_add(&self, widget: &impl AsRef<gtk::Widget>) {
                    self.append(widget.as_ref());
                }
            }
        )+
    }
}

macro_rules! add_child_impl {
    ($($type:ty),+) => {
        $(
            impl RelmContainerExt for $type {
                fn container_add(&self, widget: &impl AsRef<gtk::Widget>) {
                    self.add_child(widget.as_ref());
                }
            }
        )+
    }
}

append_impl!(gtk::Box, gtk::ListBox);
add_child_impl!(gtk::InfoBar, gtk::Stack);

#[cfg(feature = "libadwaita")]
append_impl!(adw::Leaflet, adw::Carousel, adw::TabView);