relm4/extensions/
set_child.rs

1use super::ContainerChild;
2use gtk::prelude::*;
3
4/// Widget types which allow to set or unset their child.
5pub trait RelmSetChildExt: ContainerChild {
6    /// Set a child for the container or remove it using [`None`].
7    fn container_set_child(&self, widget: Option<&impl AsRef<Self::Child>>);
8
9    /// Get the child of a container.
10    /// Returns [`None`] if the container has no child.
11    fn container_get_child(&self) -> Option<Self::Child>;
12}
13
14macro_rules! set_child_impl {
15    ($($type:ty), +) => {
16        $(
17            impl RelmSetChildExt for $type {
18                fn container_set_child(&self, widget: Option<&impl AsRef<Self::Child>>) {
19                    self.set_child(widget.map(|w| w.as_ref()));
20                }
21
22                fn container_get_child(&self) -> Option<Self::Child> {
23                    self.child()
24                }
25            }
26        )+
27    }
28}
29
30set_child_impl!(
31    gtk::Button,
32    gtk::LinkButton,
33    gtk::ToggleButton,
34    gtk::FlowBoxChild,
35    gtk::Frame,
36    gtk::ListBoxRow,
37    gtk::Popover,
38    gtk::Window,
39    gtk::ScrolledWindow,
40    gtk::ApplicationWindow,
41    gtk::Overlay,
42    gtk::Revealer,
43    gtk::WindowHandle,
44    gtk::Expander,
45    gtk::AspectFrame
46);
47
48#[cfg(feature = "libadwaita")]
49#[cfg_attr(docsrs, doc(cfg(feature = "libadwaita")))]
50mod libadwaita {
51    use super::RelmSetChildExt;
52    use adw::prelude::{AdwApplicationWindowExt, AdwWindowExt, BinExt};
53
54    macro_rules! set_child_content_impl {
55        ($($type:ty),+) => {
56            $(
57                impl RelmSetChildExt for $type {
58                    fn container_set_child(&self, widget: Option<&impl AsRef<Self::Child>>) {
59                        self.set_content(widget.map(|w| w.as_ref()));
60                    }
61
62                    fn container_get_child(&self) -> Option<Self::Child> {
63                        self.content()
64                    }
65                }
66            )+
67        }
68    }
69
70    set_child_content_impl!(adw::Window, adw::ApplicationWindow);
71    set_child_impl!(
72        adw::Bin,
73        adw::Clamp,
74        adw::ClampScrollable,
75        adw::SplitButton,
76        adw::StatusPage,
77        adw::ToastOverlay
78    );
79
80    #[cfg(all(feature = "libadwaita", feature = "gnome_45"))]
81    #[cfg_attr(docsrs, doc(cfg(all(feature = "libadwaita", feature = "gnome_45"))))]
82    mod gnome_45 {
83        use super::RelmSetChildExt;
84        use adw::prelude::{BreakpointBinExt, NavigationPageExt};
85
86        set_child_impl!(adw::NavigationPage);
87        set_child_impl!(adw::BreakpointBin);
88        set_child_content_impl!(adw::NavigationSplitView);
89        set_child_content_impl!(adw::OverlaySplitView);
90        set_child_content_impl!(adw::ToolbarView);
91    }
92}