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

/// Widget types which allow to set or unset their child.
pub trait RelmSetChildExt: ContainerChild {
    /// Set a child for the container or remove it using [`None`].
    fn container_set_child(&self, widget: Option<&impl AsRef<gtk::Widget>>);

    /// Get the child of a container.
    /// Returns [`None`] if the container has no child.
    fn container_get_child(&self) -> Option<gtk::Widget>;
}

macro_rules! set_child_impl {
    ($($type:ty),+) => {
        $(
            impl RelmSetChildExt for $type {
                fn container_set_child(&self, widget: Option<&impl AsRef<gtk::Widget>>) {
                    self.set_child(widget.map(|w| w.as_ref()));
                }

                fn container_get_child(&self) -> Option<gtk::Widget> {
                    self.child()
                }
            }
        )+
    }
}

set_child_impl!(
    gtk::Button,
    gtk::LinkButton,
    gtk::ToggleButton,
    gtk::FlowBoxChild,
    gtk::Frame,
    gtk::ListBoxRow,
    gtk::Popover,
    gtk::Window,
    gtk::ScrolledWindow,
    gtk::ApplicationWindow,
    gtk::Overlay,
    gtk::Revealer,
    gtk::WindowHandle
);

#[cfg(feature = "libadwaita")]
mod libadwaita {
    use super::RelmSetChildExt;
    use adw::prelude::{AdwApplicationWindowExt, AdwWindowExt, BinExt};

    macro_rules! set_child_content_impl {
        ($($type:ty),+) => {
            $(
                impl RelmSetChildExt for $type {
                    fn container_set_child(&self, widget: Option<&impl AsRef<gtk::Widget>>) {
                        self.set_content(widget.map(|w| w.as_ref()));
                    }

                    fn container_get_child(&self) -> Option<gtk::Widget> {
                        self.content()
                    }
                }
            )+
        }
    }

    set_child_content_impl!(adw::Window, adw::ApplicationWindow);
    set_child_impl!(
        adw::Bin,
        adw::Clamp,
        adw::ClampScrollable,
        adw::SplitButton,
        adw::StatusPage,
        adw::ToastOverlay
    );
}