From 5f482a28658db6ff9ece6dd6000b3143406a2e31 Mon Sep 17 00:00:00 2001 From: haffel Date: Sat, 12 Sep 2026 13:50:50 +0200 Subject: [PATCH] Add inactive translucent always-on-top popup --- .agents/CLIPBHOARD_HARDENING_CHECKLIST.md | 4 ++ Cargo.lock | 26 ++++++++ Cargo.toml | 3 +- src/gui.rs | 8 +++ src/platform/linux/mod.rs | 2 + src/platform/linux/x11_window.rs | 80 +++++++++++++++++++++++ 6 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/platform/linux/x11_window.rs diff --git a/.agents/CLIPBHOARD_HARDENING_CHECKLIST.md b/.agents/CLIPBHOARD_HARDENING_CHECKLIST.md index fa82553..b93944b 100644 --- a/.agents/CLIPBHOARD_HARDENING_CHECKLIST.md +++ b/.agents/CLIPBHOARD_HARDENING_CHECKLIST.md @@ -61,6 +61,10 @@ Each bounded iteration must: - [ ] Make popup close hide the window while the background process continues (code path added; desktop verification pending). - [ ] Center the popup on the configured/active monitor. +- [x] Keep the X11 popup above other windows and apply 30% opacity while it is + inactive, returning to full opacity when focused (27 X11-feature tests and + Docker GTK/X11/package build, 2026-09-12); compositor behavior remains live + desktop evidence. - [x] Preserve search query and surviving selection after refresh-triggering actions (17 core tests and Docker GTK/X11 build, 2026-09-10). - [ ] Keep keyboard pause state synchronized with button and status text. - [ ] Implement complete keyboard-first navigation and commands. diff --git a/Cargo.lock b/Cargo.lock index a5b00d3..e18e3bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -153,6 +153,7 @@ dependencies = [ "anyhow", "dirs", "fs2", + "gdk4-x11", "glib", "gtk4", "ksni", @@ -446,6 +447,31 @@ dependencies = [ "system-deps", ] +[[package]] +name = "gdk4-x11" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c3e7380a9a206b170e1b52b5f25581406db816c68f4e7140dbef89a9e5b52ac" +dependencies = [ + "gdk4", + "gdk4-x11-sys", + "gio", + "glib", + "libc", +] + +[[package]] +name = "gdk4-x11-sys" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "070bd50a053f90d7fdf6be1d75672ea0f97c0e5da3a10dc6d02e5defcb0db32f" +dependencies = [ + "gdk4-sys", + "glib-sys", + "libc", + "system-deps", +] + [[package]] name = "generic-array" version = "0.14.7" diff --git a/Cargo.toml b/Cargo.toml index 7e06ef4..e46abb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ default-run = "clipbhoard" [features] default = [] x11 = ["dep:x11rb", "dep:x11-clipboard"] -gui = ["x11", "dep:gtk4", "dep:glib", "dep:ksni"] +gui = ["x11", "dep:gtk4", "dep:gdk4-x11", "dep:glib", "dep:ksni"] [dependencies] anyhow = "1.0" @@ -25,6 +25,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } fs2 = "0.4" gtk4 = { version = "0.9", optional = true } +gdk4-x11 = { version = "0.9", optional = true } glib = { version = "0.20", optional = true } ksni = { version = "0.2.2", optional = true } x11rb = { version = "0.13", features = ["allow-unsafe-code", "xtest"], optional = true } diff --git a/src/gui.rs b/src/gui.rs index a556e45..92dcabc 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -174,6 +174,14 @@ fn build_window( .default_height(420) .child(&root) .build(); + let window_for_opacity = window.clone(); + window.connect_realize(move |_| { + crate::platform::linux::x11_window::configure(&window_for_opacity); + }); + let window_for_opacity = window.clone(); + window.connect_is_active_notify(move |_| { + crate::platform::linux::x11_window::set_active_opacity(&window_for_opacity); + }); window.set_title(Some(&format!("Cliphoard — {}", config.global_shortcut))); pause_button.set_tooltip_text(Some("Pause/resume recording: Ctrl+Alt+R")); let window_for_close = window.clone(); diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index e4fc0f4..1a150c2 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -1,6 +1,8 @@ mod x11_clipboard; mod x11_events; mod x11_input; +#[cfg(feature = "gui")] +pub(crate) mod x11_window; pub use x11_clipboard::{ ClipboardWorkerResult, X11Clipboard, X11ClipboardReader, X11ClipboardWorker, diff --git a/src/platform/linux/x11_window.rs b/src/platform/linux/x11_window.rs new file mode 100644 index 0000000..96bdd3b --- /dev/null +++ b/src/platform/linux/x11_window.rs @@ -0,0 +1,80 @@ +use gtk4::{prelude::*, ApplicationWindow}; +use x11rb::{ + connection::Connection, protocol::xproto::ConnectionExt as X11ConnectionExt, + wrapper::ConnectionExt as X11WrapperExt, +}; + +const ACTIVE_OPACITY: f64 = 1.0; +const INACTIVE_OPACITY: f64 = 0.30; + +pub fn configure(window: &ApplicationWindow) { + set_active_opacity(window); + let Some(surface) = window.surface() else { + return; + }; + let Some(surface) = surface.downcast_ref::() else { + tracing::debug!("popup window is not backed by an X11 surface"); + return; + }; + let xid = surface.xid(); + let Ok((connection, _)) = x11rb::connect(None) else { + tracing::warn!("could not connect to X11 to keep popup above other windows"); + return; + }; + let Ok(state_cookie) = connection.intern_atom(false, b"_NET_WM_STATE") else { + return; + }; + let Ok(state) = state_cookie.reply() else { + return; + }; + let Ok(above_cookie) = connection.intern_atom(false, b"_NET_WM_STATE_ABOVE") else { + return; + }; + let Ok(above) = above_cookie.reply() else { + return; + }; + if let Err(error) = connection.change_property32( + x11rb::protocol::xproto::PropMode::REPLACE, + xid as u32, + state.atom, + x11rb::protocol::xproto::AtomEnum::ATOM, + &[above.atom], + ) { + tracing::warn!(%error, "could not mark popup as always on top"); + } + let _ = connection.flush(); +} + +pub fn set_active_opacity(window: &ApplicationWindow) { + let Some(surface) = window.surface() else { + return; + }; + let Some(surface) = surface.downcast_ref::() else { + return; + }; + let Ok((connection, _)) = x11rb::connect(None) else { + return; + }; + let Ok(opacity_cookie) = connection.intern_atom(false, b"_NET_WM_WINDOW_OPACITY") else { + return; + }; + let Ok(opacity) = opacity_cookie.reply() else { + return; + }; + let value = if window.is_active() { + ACTIVE_OPACITY + } else { + INACTIVE_OPACITY + }; + let scaled = (value * u32::MAX as f64).round() as u32; + if let Err(error) = connection.change_property32( + x11rb::protocol::xproto::PropMode::REPLACE, + surface.xid() as u32, + opacity.atom, + x11rb::protocol::xproto::AtomEnum::CARDINAL, + &[scaled], + ) { + tracing::debug!(%error, "could not update popup opacity"); + } + let _ = connection.flush(); +}