From 68981e636bd691dbb149b662e6b168f14b3c161c Mon Sep 17 00:00:00 2001 From: Maximiliano Sandoval Date: Wed, 24 Jul 2024 14:06:39 +0200 Subject: [PATCH] activation_token: Provide GTK 4 helper Unfortunately, `startup_notify_id` is not nullable so we do the same as GTK. See https://gitlab.gnome.org/GNOME/gtk/-/commit/6efd1a9dad49b42db778bdc07020dfcf8845e2c8 --- src/activation_token/gtk4.rs | 48 ++++++++++++++++++++++++++++++++++++ src/activation_token/mod.rs | 3 +++ 2 files changed, 51 insertions(+) create mode 100644 src/activation_token/gtk4.rs diff --git a/src/activation_token/gtk4.rs b/src/activation_token/gtk4.rs new file mode 100644 index 000000000..48e2d46c3 --- /dev/null +++ b/src/activation_token/gtk4.rs @@ -0,0 +1,48 @@ +use glib::translate::from_glib_full; +use gtk4::{gio, prelude::*}; + +use crate::ActivationToken; + +impl ActivationToken { + /// Gets an activation token from a window. + /// + /// Support for the XDG Activation Protocol was added in GLib 2.76, this + /// method will return `None` on older versions. + pub fn from_window(widget: &impl IsA<::gtk4::Widget>) -> Option { + if glib::check_version(2, 76, 0).is_some() { + #[cfg(feature = "tracing")] + tracing::info!("Need glib 2.76 for XDG Activation protocol support"); + + return None; + } + + let display = widget.as_ref().display(); + let context = display.app_launch_context(); + + // g_app_launch_context_get_startup_notify_id only accepts nullable + // parameters since 2.82. On older versions we use the vfunc. + if glib::check_version(2, 82, 0).is_some() { + unsafe { + let klass: *mut gtk4::gio::ffi::GAppLaunchContextClass = + std::ptr::addr_of!((*context.class().parent()?)) as *mut _; + let get_startup_notify_id = (*klass).get_startup_notify_id.as_ref()?; + from_glib_full::<_, Option>(get_startup_notify_id( + context.as_ptr().cast(), + std::ptr::null_mut(), + std::ptr::null_mut(), + )) + } + } else { + unsafe { + from_glib_full::<_, Option>( + gio::ffi::g_app_launch_context_get_startup_notify_id( + context.as_ptr().cast(), + std::ptr::null_mut(), + std::ptr::null_mut(), + ), + ) + } + } + .map(Self::from) + } +} diff --git a/src/activation_token/mod.rs b/src/activation_token/mod.rs index 46836bb04..990971dc7 100644 --- a/src/activation_token/mod.rs +++ b/src/activation_token/mod.rs @@ -3,6 +3,9 @@ use std::ops::Deref; use serde::{Deserialize, Serialize}; use zbus::zvariant::Type; +#[cfg(any(feature = "gtk4_wayland", feature = "gtk4_x11"))] +mod gtk4; + /// A token that can be used to activate an application. /// /// No guarantees are made for the token structure.