fix(linux): provide the XEmbed tray helper and i3 rules

Use the existing GTK 3 helper to render tray icons and menus for both desktop interfaces on X11. Ship optional i3 rules for centering GP Connect windows in Arch packages.
This commit is contained in:
Kevin Yue committed 2026-12-05 00:10:43 +08:00
1 parent ff10375b80
commit 9864560f99
11 files changed
+254

No files matched your search

+4
View File
@@ -26,5 +26,9 @@ tokio-tungstenite = "0.30"
tempfile.workspace = true
reqwest = { workspace = true, features = ["stream"] }
[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))'.dependencies]
gtk = "0.18"
serde_json.workspace = true
[features]
default = ["tauri/custom-protocol"]
+8
View File
@@ -25,6 +25,10 @@ const VERSION: &str = concat!(
#[derive(Parser)]
#[command(version = VERSION)]
struct Cli {
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
#[arg(long, hide = true, conflicts_with = "service_credential_on_stdin")]
xembed_tray: bool,
#[arg(long, help = "Read the service credential from stdin")]
service_credential_on_stdin: bool,
@@ -42,6 +46,10 @@ struct Cli {
impl Cli {
fn run(&self) -> anyhow::Result<()> {
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
if self.xembed_tray {
return crate::xembed_tray::run();
}
let credential = self.read_credential()?;
let credential_lease = monitor_credential_lease().context("Failed to monitor service credential lifetime")?;
anyhow::ensure!(
+2
View File
@@ -3,3 +3,5 @@ pub(crate) mod downloader;
pub(crate) mod updater;
pub mod cli;
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
mod xembed_tray;
@@ -0,0 +1,187 @@
//! GTK 3 owns the XEmbed plug and menu on the user session's X11 display.
use gpapi::process::xembed_tray::{MenuEntry, Request};
use gtk::{
glib::{self, translate::ToGlibPtr},
prelude::*,
};
use std::{
cell::RefCell,
io::{self, BufRead, Cursor, Write},
ptr,
rc::Rc,
};
glib::wrapper! {
struct StatusIcon(Object<gtk::ffi::GtkStatusIcon>);
match fn {
type_ => || gtk::ffi::gtk_status_icon_get_type(),
}
}
pub(crate) fn run() -> anyhow::Result<()> {
gtk::init()?;
let icon: StatusIcon = glib::Object::builder()
.property("title", "GP Connect")
.property("visible", false)
.build();
let menu = Rc::new(RefCell::new(gtk::Menu::new()));
for signal in ["activate", "popup-menu"] {
let menu = menu.clone();
icon.connect_local(signal, false, move |arguments| {
let icon = arguments[0].get::<StatusIcon>().expect("GtkStatusIcon signal instance");
let button = arguments.get(1).and_then(|value| value.get::<u32>().ok()).unwrap_or(0);
let time = arguments
.get(2)
.and_then(|value| value.get::<u32>().ok())
.unwrap_or_else(gtk::current_event_time);
popup(&icon, &menu.borrow(), button, time);
None
});
}
let (requests, mut input) = tokio::sync::mpsc::channel(8);
std::thread::Builder::new()
.name("xembed-tray-input".into())
.spawn(move || {
for line in io::stdin().lock().lines() {
let request = line
.map_err(anyhow::Error::from)
.and_then(|line| Ok(serde_json::from_str::<Request>(&line)?));
match request {
Ok(request) => {
if requests.blocking_send(request).is_err() {
break;
}
}
Err(error) => {
log::warn!("Could not read XEmbed tray request: {error:#}");
break;
}
}
}
})?;
let main_loop = glib::MainLoop::new(None, false);
let exit = main_loop.clone();
glib::MainContext::default().spawn_local(async move {
while let Some(request) = input.recv().await {
if let Err(error) = apply(&icon, &menu, request) {
log::warn!("Could not update XEmbed tray: {error:#}");
}
}
menu.borrow().popdown();
icon.set_property("visible", false);
exit.quit();
});
main_loop.run();
Ok(())
}
fn popup(icon: &StatusIcon, menu: &gtk::Menu, button: u32, time: u32) {
// GTK positions the menu against the plug on the panel's configured edge.
// The status icon outlives the popup and all of its positioning callbacks.
unsafe {
gtk::ffi::gtk_menu_popup(
menu.to_glib_none().0,
ptr::null_mut(),
ptr::null_mut(),
Some(position_menu),
icon.as_ptr().cast(),
button,
time,
);
}
}
unsafe extern "C" fn position_menu(
menu: *mut gtk::ffi::GtkMenu,
x: *mut i32,
y: *mut i32,
push_in: *mut glib::ffi::gboolean,
icon: glib::ffi::gpointer,
) {
// GtkMenu passes back the live GtkStatusIcon pointer supplied by popup.
unsafe { gtk::ffi::gtk_status_icon_position_menu(menu, x, y, push_in, icon.cast()) }
}
fn apply(icon: &StatusIcon, menu: &RefCell<gtk::Menu>, request: Request) -> anyhow::Result<()> {
let png = match request {
Request::Frame {
png,
tooltip,
menu: entries,
} => {
icon.set_property("tooltip-text", tooltip);
menu.borrow().popdown();
*menu.borrow_mut() = build_menu(entries);
png
}
Request::Icon(png) => png,
};
let pixbuf = gtk::gdk_pixbuf::Pixbuf::from_read(Cursor::new(png))?;
icon.set_property("pixbuf", pixbuf);
icon.set_property("visible", true);
Ok(())
}
fn build_menu(entries: Vec<MenuEntry>) -> gtk::Menu {
let menu = gtk::Menu::new();
for entry in entries {
let item = match entry {
MenuEntry::Separator => gtk::SeparatorMenuItem::new().upcast(),
MenuEntry::Label(label) => {
let item = gtk::MenuItem::with_label(&label);
item.set_sensitive(false);
item
}
MenuEntry::Action {
id,
label,
enabled,
checked,
} => {
let item = if let Some(checked) = checked {
let item = gtk::CheckMenuItem::with_label(&label);
item.set_draw_as_radio(true);
item.set_active(checked);
item.upcast()
} else {
gtk::MenuItem::with_label(&label)
};
item.set_sensitive(enabled);
item.connect_activate(move |item| {
if let Some(checked) = checked {
let item = item.downcast_ref::<gtk::CheckMenuItem>().expect("checkable tray item");
// GTK toggles before this handler. Restoring the model's check emits
// activate again, so ignore the emission that restores the check.
if item.is_active() == checked {
return;
}
item.set_active(checked);
if checked {
return;
}
}
let mut output = io::stdout().lock();
let sent = serde_json::to_writer(&mut output, &id)
.map_err(io::Error::other)
.and_then(|()| output.write_all(b"\n"))
.and_then(|()| output.flush());
if let Err(error) = sent {
log::warn!("Could not send XEmbed tray action: {error}");
}
});
item
}
MenuEntry::Submenu { label, enabled, items } => {
let item = gtk::MenuItem::with_label(&label);
item.set_sensitive(enabled);
item.set_submenu(Some(&build_menu(items)));
item
}
};
menu.append(&item);
}
menu.show_all();
menu
}