gtk4/subclass/
native_dialog.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! Traits intended for subclassing [`NativeDialog`](crate::NativeDialog).
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, NativeDialog, ResponseType};
9
10#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
11#[allow(deprecated)]
12pub trait NativeDialogImpl: NativeDialogImplExt + ObjectImpl {
13    /// class handler for the `GtkNativeDialog::response` signal
14    fn response(&self, response: ResponseType) {
15        self.parent_response(response)
16    }
17
18    /// Shows the dialog on the display.
19    ///
20    /// When the user accepts the state of the dialog the dialog will
21    /// be automatically hidden and the [`response`][struct@crate::NativeDialog#response]
22    /// signal will be emitted.
23    ///
24    /// Multiple calls while the dialog is visible will be ignored.
25    fn show(&self) {
26        self.parent_show()
27    }
28
29    /// Hides the dialog if it is visible, aborting any interaction.
30    ///
31    /// Once this is called the [`response`][struct@crate::NativeDialog#response] signal
32    /// will *not* be emitted until after the next call to
33    /// [`NativeDialogExt::show()`][crate::prelude::NativeDialogExt::show()].
34    ///
35    /// If the dialog is not visible this does nothing.
36    fn hide(&self) {
37        self.parent_hide()
38    }
39}
40
41mod sealed {
42    pub trait Sealed {}
43    impl<T: super::NativeDialogImplExt> Sealed for T {}
44}
45
46#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
47#[allow(deprecated)]
48pub trait NativeDialogImplExt: sealed::Sealed + ObjectSubclass {
49    fn parent_response(&self, response: ResponseType) {
50        unsafe {
51            let data = Self::type_data();
52            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkNativeDialogClass;
53            if let Some(f) = (*parent_class).response {
54                f(
55                    self.obj()
56                        .unsafe_cast_ref::<NativeDialog>()
57                        .to_glib_none()
58                        .0,
59                    response.into_glib(),
60                )
61            }
62        }
63    }
64
65    fn parent_show(&self) {
66        unsafe {
67            let data = Self::type_data();
68            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkNativeDialogClass;
69            let f = (*parent_class)
70                .show
71                .expect("No parent class impl for \"show\"");
72            f(self
73                .obj()
74                .unsafe_cast_ref::<NativeDialog>()
75                .to_glib_none()
76                .0)
77        }
78    }
79
80    fn parent_hide(&self) {
81        unsafe {
82            let data = Self::type_data();
83            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkNativeDialogClass;
84            let f = (*parent_class)
85                .hide
86                .expect("No parent class impl for \"hide\"");
87            f(self
88                .obj()
89                .unsafe_cast_ref::<NativeDialog>()
90                .to_glib_none()
91                .0)
92        }
93    }
94}
95
96impl<T: NativeDialogImpl> NativeDialogImplExt for T {}
97
98unsafe impl<T: NativeDialogImpl> IsSubclassable<T> for NativeDialog {
99    fn class_init(class: &mut glib::Class<Self>) {
100        Self::parent_class_init::<T>(class);
101
102        assert_initialized_main_thread!();
103
104        let klass = class.as_mut();
105        klass.response = Some(dialog_response::<T>);
106        klass.show = Some(dialog_show::<T>);
107        klass.hide = Some(dialog_hide::<T>);
108    }
109}
110
111unsafe extern "C" fn dialog_response<T: NativeDialogImpl>(
112    ptr: *mut ffi::GtkNativeDialog,
113    responseptr: i32,
114) {
115    let instance = &*(ptr as *mut T::Instance);
116    let imp = instance.imp();
117    let res: ResponseType = from_glib(responseptr);
118
119    imp.response(res)
120}
121
122unsafe extern "C" fn dialog_show<T: NativeDialogImpl>(ptr: *mut ffi::GtkNativeDialog) {
123    let instance = &*(ptr as *mut T::Instance);
124    let imp = instance.imp();
125
126    imp.show()
127}
128
129unsafe extern "C" fn dialog_hide<T: NativeDialogImpl>(ptr: *mut ffi::GtkNativeDialog) {
130    let instance = &*(ptr as *mut T::Instance);
131    let imp = instance.imp();
132
133    imp.hide()
134}