Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

activation_token: Provide GTK 4 helper #224

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ futures-util = "0.3"
gdk4wayland = { package = "gdk4-wayland", version = "0.9", optional = true }
gdk4x11 = { package = "gdk4-x11", version = "0.9", optional = true }
glib = { version = "0.20", optional = true }
gtk4 = { version = "0.9", optional = true }
gtk4 = { version = "0.9.3", optional = true }
pipewire = { version = "0.8", optional = true }
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"] }
raw-window-handle = { version = "0.6", optional = true }
Expand Down
42 changes: 42 additions & 0 deletions src/activation_token/gtk4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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<Self> {
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<String>>(get_startup_notify_id(
context.as_ptr().cast(),
std::ptr::null_mut(),
std::ptr::null_mut(),
))
}
} else {
context
.startup_notify_id(gio::AppInfo::NONE, &[])
.map(String::from)
}
.map(Self::from)
}
}
3 changes: 3 additions & 0 deletions src/activation_token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading