ashpd/activation_token/
gtk4.rs

1use glib::translate::from_glib_full;
2use gtk4::{gio, prelude::*};
3
4use crate::ActivationToken;
5
6impl ActivationToken {
7    /// Gets an activation token from a window.
8    ///
9    /// Support for the XDG Activation Protocol was added in GLib 2.76, this
10    /// method will return `None` on older versions.
11    #[cfg_attr(docsrs, doc(cfg(any(feature = "gtk4_wayland", feature = "gtk4_x11"))))]
12    pub fn from_window(widget: &impl IsA<::gtk4::Widget>) -> Option<Self> {
13        if glib::check_version(2, 76, 0).is_some() {
14            #[cfg(feature = "tracing")]
15            tracing::info!("Need glib 2.76 for XDG Activation protocol support");
16
17            return None;
18        }
19
20        let display = widget.as_ref().display();
21        let context = display.app_launch_context();
22
23        // g_app_launch_context_get_startup_notify_id only accepts nullable
24        // parameters since 2.82. On older versions we use the vfunc.
25        if glib::check_version(2, 82, 0).is_some() {
26            unsafe {
27                let klass: *mut gtk4::gio::ffi::GAppLaunchContextClass =
28                    &context.class().parent()? as *const _ as *mut _;
29                let get_startup_notify_id = (*klass).get_startup_notify_id.as_ref()?;
30                from_glib_full::<_, Option<String>>(get_startup_notify_id(
31                    context.as_ptr().cast(),
32                    std::ptr::null_mut(),
33                    std::ptr::null_mut(),
34                ))
35            }
36        } else {
37            context
38                .startup_notify_id(gio::AppInfo::NONE, &[])
39                .map(String::from)
40        }
41        .map(Self::from)
42    }
43}