Skip to main content

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`].
5
6use glib::translate::*;
7
8use crate::{NativeDialog, ResponseType, ffi, prelude::*, subclass::prelude::*};
9
10#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
11#[allow(deprecated)]
12pub trait NativeDialogImpl: ObjectImpl + ObjectSubclass<Type: IsA<NativeDialog>> {
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
41#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
42#[allow(deprecated)]
43pub trait NativeDialogImplExt: NativeDialogImpl {
44    fn parent_response(&self, response: ResponseType) {
45        unsafe {
46            let data = Self::type_data();
47            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkNativeDialogClass;
48            if let Some(f) = (*parent_class).response {
49                f(
50                    self.obj()
51                        .unsafe_cast_ref::<NativeDialog>()
52                        .to_glib_none()
53                        .0,
54                    response.into_glib(),
55                )
56            }
57        }
58    }
59
60    fn parent_show(&self) {
61        unsafe {
62            let data = Self::type_data();
63            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkNativeDialogClass;
64            let f = (*parent_class)
65                .show
66                .expect("No parent class impl for \"show\"");
67            f(self
68                .obj()
69                .unsafe_cast_ref::<NativeDialog>()
70                .to_glib_none()
71                .0)
72        }
73    }
74
75    fn parent_hide(&self) {
76        unsafe {
77            let data = Self::type_data();
78            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkNativeDialogClass;
79            let f = (*parent_class)
80                .hide
81                .expect("No parent class impl for \"hide\"");
82            f(self
83                .obj()
84                .unsafe_cast_ref::<NativeDialog>()
85                .to_glib_none()
86                .0)
87        }
88    }
89}
90
91impl<T: NativeDialogImpl> NativeDialogImplExt for T {}
92
93unsafe impl<T: NativeDialogImpl> IsSubclassable<T> for NativeDialog {
94    fn class_init(class: &mut glib::Class<Self>) {
95        Self::parent_class_init::<T>(class);
96
97        assert_initialized_main_thread!();
98
99        let klass = class.as_mut();
100        klass.response = Some(dialog_response::<T>);
101        klass.show = Some(dialog_show::<T>);
102        klass.hide = Some(dialog_hide::<T>);
103    }
104}
105
106unsafe extern "C" fn dialog_response<T: NativeDialogImpl>(
107    ptr: *mut ffi::GtkNativeDialog,
108    responseptr: i32,
109) {
110    unsafe {
111        let instance = &*(ptr as *mut T::Instance);
112        let imp = instance.imp();
113        let res: ResponseType = from_glib(responseptr);
114
115        imp.response(res)
116    }
117}
118
119unsafe extern "C" fn dialog_show<T: NativeDialogImpl>(ptr: *mut ffi::GtkNativeDialog) {
120    unsafe {
121        let instance = &*(ptr as *mut T::Instance);
122        let imp = instance.imp();
123
124        imp.show()
125    }
126}
127
128unsafe extern "C" fn dialog_hide<T: NativeDialogImpl>(ptr: *mut ffi::GtkNativeDialog) {
129    unsafe {
130        let instance = &*(ptr as *mut T::Instance);
131        let imp = instance.imp();
132
133        imp.hide()
134    }
135}