diff --git a/AGENTS.md b/AGENTS.md index 04f550a..d61797f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -148,11 +148,21 @@ dbus-run-session -- bash -c ' init is flaky. The tray and `gtk-sni-tray-standalone` must share that bus. - Tray icon: bare Xvfb has no StatusNotifierWatcher. With `gtk-sni-tray-standalone -w` (package `haskell-gtk-sni-tray-utils`) under - fluxbox the friglet SNI icon renders (ayatana path - `/org/ayatana/NotificationItem/tray_icon_tray_app_friglet_tray`). + fluxbox the friglet SNI icon renders. Since SNB-656 the Linux tray is + tray-icon's KSNI backend (feature `ksni` in `friglet-tray/Cargo.toml`), not + libayatana: bus name `org.kde.StatusNotifierItem--`, object + `/StatusNotifierItem`, `ItemIsMenu=false`, menu at `/MenuBar`. A host's + left click is `Activate` (toggles the window), right click opens the menu; + e.g. `gdbus call --session -d -o /StatusNotifierItem -m + org.kde.StatusNotifierItem.Activate 0 0`. Without a watcher the tray keeps + running and retries the icon (2 s, doubling to 30 s). `snixembed` is not in Ubuntu apt; `trayer` alone only hosts legacy XEmbed icons, not StatusNotifierItem. xdotool clicks do not reach WebKitGTK under Xvfb — use `FRIGLET_TRAY_SHOW_ON_START=settings` for the Settings form. +- Hyprland popup mode (`friglet-tray/src/popup.rs`) is on whenever + `HYPRLAND_INSTANCE_SIGNATURE` is set; its socket logic is unit-tested + against a fake `.socket.sock` in that file. Keep the socket path under + 108 bytes (`sun_path`) when faking `XDG_RUNTIME_DIR`. ## Packaging (M4) diff --git a/Cargo.lock b/Cargo.lock index daaf5ec..6b6417f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1672,6 +1672,7 @@ dependencies = [ "toml 0.8.23", "tracing", "tracing-subscriber", + "tray-icon", ] [[package]] @@ -2661,6 +2662,24 @@ dependencies = [ "serde", ] +[[package]] +name = "ksni" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "814b44c24cd2cb236c3b8a41c7f08237b452a8e76ecaa81f1cec40b5b678215b" +dependencies = [ + "async-executor", + "async-io", + "async-lock", + "futures-channel", + "futures-lite", + "futures-util", + "pastey", + "serde", + "task-local", + "zbus", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -3310,6 +3329,12 @@ dependencies = [ "windows-link", ] +[[package]] +name = "pastey" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" + [[package]] name = "pear" version = "0.2.9" @@ -4633,6 +4658,15 @@ version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" +[[package]] +name = "task-local" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2972044a9e5e448a506a7ff6f0d03b566d8ef4cd6918a58fc59835a0f8666626" +dependencies = [ + "pin-project-lite", +] + [[package]] name = "tauri" version = "2.12.1" @@ -5436,6 +5470,7 @@ checksum = "b2b9c52859a94554803ccd4a24b98f74148ebc73b90676d783f3490b1bff9d72" dependencies = [ "crossbeam-channel", "dirs 7.0.0", + "ksni", "libappindicator", "muda", "objc2", diff --git a/README.md b/README.md index fa3f09c..f3f74fd 100644 --- a/README.md +++ b/README.md @@ -145,8 +145,9 @@ What to expect from it: `friglet-tray` is a small Tauri v2 system tray app that supervises and monitors the `friglet` daemon over its control socket. It shows live status (scan height, progress, network, Electrum clients, oracle connectivity, SP -address, errors) in the tray menu and in a status window (hidden by default, -opened via the tray menu; closing it hides it again). The tray menu and the +address, errors) in the tray menu and in a status window (hidden by default; +a left click on the tray icon opens it, and so does **Open Status Window** in +the icon's right-click menu; closing it hides it again). The tray menu and the Status tab offer Start/Stop scanning, Start/Stop daemon and Quit; a button or menu item is only enabled when it does what it says (Start scanning is greyed out while a scan runs, Stop daemon while none is running). @@ -220,6 +221,51 @@ The **Wallet** tab is a read-only convenience view, not a spending wallet. It shows found transaction/output counts plus copyable base and per-label Silent Payments addresses. +**Tray icon on Linux.** The icon is a StatusNotifierItem (KSNI over D-Bus), so +it needs a desktop with an SNI tray: Omarchy's bar, KDE Plasma, GNOME with +the AppIndicator extension, Waybar's `tray` module and the like. Trays that +only host legacy XEmbed icons do not show it. When no tray is up yet (the +tray app started before the bar at login) the app keeps running and +registers the icon as soon as one appears. + +**Omarchy / Hyprland: top-bar popup.** Under Hyprland (detected by +`HYPRLAND_INSTANCE_SIGNATURE`; Omarchy 4 runs it) the window opens like +Omarchy's own Wi-Fi and Bluetooth panels: floating, right-aligned just under +the top bar, without a title bar, at a fixed 420×600 (smaller on a short +screen). A left click on the tray icon opens it and a second one closes it; +Esc closes it, and so does focusing another window (clicking it, or a +keybinding). Merely moving the pointer over another window does not, although +Omarchy focuses windows on hover. The right-click menu is unchanged. + +How: before each show the tray asks Hyprland, over its IPC socket, for the +monitor under the pointer, the bar height (the monitor's reserved top) and +`general:gaps_out`, and registers a window rule for its own window with the +Lua API (`hl.window_rule`, Hyprland 0.55+): float, size, and a position +`gaps_out / 2` below the bar and from the right edge. While the popup is open +a second rule sets `no_follow_mouse` on every other window, so hovering them +does not take the focus; hiding the popup turns it off. After the show the +tray checks where the window landed (`hyprctl clients`) and moves it with +dispatchers if it is off, which is also the only placement on a Hyprland +without the Lua API. Nothing is written to your config files. Everywhere +else (GNOME, KDE, other compositors, macOS, Windows) the window is the +ordinary one: a left click on the icon opens it, the menu is on right click. + +If the tray runs without Hyprland's environment (say from a service that +lacks `HYPRLAND_INSTANCE_SIGNATURE`) it opens the ordinary window. A static +rule at the end of `~/.config/hypr/hyprland.lua` still floats it under the +bar (Omarchy 4 syntax, as in Omarchy's own rules; `31` is Omarchy's default +26 px bar plus a 5 px gap). The class is expected to be `friglet-tray`; the +tray logs the one Hyprland reports in popup mode (`Hyprland window class of +the status window` in `friglet-tray.log`), and `hyprctl clients` shows it: + +```lua +o.window({ class = "^friglet-tray$", title = "^Friglet Status$" }, { + float = true, + size = { 420, 600 }, + move = { "(monitor_w-window_w-5)", 31 }, +}) +``` + ```bash # build (Linux needs the Tauri v2 system deps: libwebkit2gtk-4.1-dev, # libayatana-appindicator3-dev, librsvg2-dev, libgtk-3-dev) @@ -229,7 +275,7 @@ cargo build --release -p friglet-tray ./target/release/friglet-tray # optional: show the status window immediately (normally hidden until -# opened from the tray menu — useful for headless / screenshot testing). +# opened from the tray icon — useful for headless / screenshot testing). # Values: 1/true/yes/on/status → Status tab; settings → Settings tab; # wallet → Wallet tab. FRIGLET_TRAY_SHOW_ON_START=1 ./target/release/friglet-tray diff --git a/friglet-tray/Cargo.toml b/friglet-tray/Cargo.toml index 1570a5b..d354076 100644 --- a/friglet-tray/Cargo.toml +++ b/friglet-tray/Cargo.toml @@ -21,5 +21,13 @@ tauri-plugin-clipboard-manager = "2.3.2" tauri-plugin-single-instance = "2" tauri-plugin-autostart = "2" +# Linux: route tray-icon to its KSNI (StatusNotifierItem over D-Bus) backend. +# Tauri enables the libappindicator backend, which never reports clicks +# (libayatana rejects `Activate`), so a left click on the icon did nothing. +# With both features on, tray-icon uses KSNI: `ItemIsMenu=false`, left click +# arrives as `TrayIconEvent::Click`, right click still opens the menu. +[target.'cfg(target_os = "linux")'.dependencies] +tray-icon = { version = "0.25", default-features = false, features = ["ksni"] } + [dev-dependencies] tokio = { version = "1.48.0", features = ["full"] } diff --git a/friglet-tray/src/lib.rs b/friglet-tray/src/lib.rs index 75d03f4..6f2d0e7 100644 --- a/friglet-tray/src/lib.rs +++ b/friglet-tray/src/lib.rs @@ -4,6 +4,7 @@ //! window, and manages the daemon lifecycle (attach or spawn, quit rule). pub mod lifecycle; +pub mod popup; pub mod setup; pub mod view; @@ -15,7 +16,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use friglet_ipc::{Client, DaemonConfig, Request, Response, StatusInfo}; use serde::Serialize; use tauri::menu::{Menu, MenuItem, PredefinedMenuItem}; -use tauri::tray::TrayIconBuilder; +use tauri::tray::{MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent}; use tauri::{AppHandle, Manager, State, WindowEvent}; use tauri_plugin_autostart::ManagerExt; use tokio::process::Child; @@ -935,17 +936,35 @@ fn quit(app: &AppHandle) { (lc.spawned_by_tray, lc.child.take()) }; lifecycle::perform_quit(&state.socket_path, spawned_by_tray, child).await; + // Popup mode: give hover focus back to the other windows. + let popup = app.state::>().inner().clone(); + let _ = tauri::async_runtime::spawn_blocking(move || popup.release()).await; app.exit(0); }); } +/// Show the main window: under Hyprland as the top-bar popup, elsewhere as +/// the ordinary window. fn show_status_window(app: &AppHandle) { if let Some(window) = app.get_webview_window("main") { - let _ = window.show(); - let _ = window.set_focus(); + app.state::>().show(&window); } } +/// Left click on the tray icon: toggle the popup under Hyprland, open the +/// window elsewhere. +fn tray_left_click(app: &AppHandle) { + if let Some(window) = app.get_webview_window("main") { + app.state::>().toggle(&window); + } +} + +/// Esc in the window: closes the popup (no-op outside popup mode). +#[tauri::command] +fn dismiss_window(window: tauri::WebviewWindow, popup: State<'_, Arc>) { + popup.dismiss(&window); +} + /// Show the main window and switch it to the Settings tab once the UI has /// had a chance to load. Used for `FRIGLET_TRAY_SHOW_ON_START=settings` and /// for first-run setup mode. (Synthetic X11 clicks do not reach WebKitGTK @@ -1117,6 +1136,74 @@ fn refresh(state: &AppState, info: Option<&StatusInfo>) -> (DaemonView, ScanView (daemon, scan, spawned_by_tray.unwrap_or(false)) } +const TRAY_ID: &str = "friglet-tray"; +/// Wait before the first retry to register the tray icon; it doubles up to +/// `TRAY_RETRY_MAX`. +const TRAY_RETRY_FIRST: Duration = Duration::from_secs(2); +const TRAY_RETRY_MAX: Duration = Duration::from_secs(30); + +/// Create the tray icon. A left click opens the window (on Hyprland it +/// toggles the top-bar popup, see [`popup`]); the menu is on right click. +/// On Linux the icon is a KSNI StatusNotifierItem (`ItemIsMenu=false`), so +/// SNI hosts send left clicks as `Activate` instead of opening the menu. +fn build_tray(app: &AppHandle, menu: &Menu) -> tauri::Result { + TrayIconBuilder::with_id(TRAY_ID) + .icon( + app.default_window_icon() + .cloned() + .expect("bundled window icon"), + ) + .menu(menu) + // macOS / Windows: menu on right click only, like the Linux hosts. + .show_menu_on_left_click(false) + .tooltip("Friglet") + .on_tray_icon_event(|tray, event| { + if let TrayIconEvent::Click { + button: MouseButton::Left, + button_state: MouseButtonState::Up, + .. + } = event + { + tray_left_click(tray.app_handle()); + } + }) + .on_menu_event(|app, event| { + let state = app.state::>().inner().clone(); + match event.id().as_ref() { + "open-window" => show_status_window(app), + "start-scan" => { + tauri::async_runtime::spawn(async move { + let _ = scan_control(&state, Request::Start).await; + }); + } + "stop-scan" => { + tauri::async_runtime::spawn(async move { + let _ = scan_control(&state, Request::Stop).await; + }); + } + "start-daemon" => { + let app = app.clone(); + tauri::async_runtime::spawn(async move { + let _ = start_daemon_now(&state).await; + // An unconfigured daemon lands in setup mode + // instead of a spawn-fail loop; take the user there. + if state.setup_needed.load(Ordering::SeqCst) { + show_settings_window(&app); + } + }); + } + "stop-daemon" => { + tauri::async_runtime::spawn(async move { + let _ = stop_daemon_now(&state).await; + }); + } + "quit" => quit(app), + _ => {} + } + }) + .build(app) +} + fn setup_tray(app: &tauri::App) -> tauri::Result<()> { let handle = app.handle(); @@ -1160,56 +1247,37 @@ fn setup_tray(app: &tauri::App) -> tauri::Result<()> { ], )?; - let tray = TrayIconBuilder::with_id("friglet-tray") - .icon( - app.default_window_icon() - .cloned() - .expect("bundled window icon"), - ) - .menu(&menu) - .show_menu_on_left_click(true) - .tooltip("Friglet") - .on_menu_event(|app, event| { - let state = app.state::>().inner().clone(); - match event.id().as_ref() { - "open-window" => show_status_window(app), - "start-scan" => { - tauri::async_runtime::spawn(async move { - let _ = scan_control(&state, Request::Start).await; - }); - } - "stop-scan" => { - tauri::async_runtime::spawn(async move { - let _ = scan_control(&state, Request::Stop).await; - }); - } - "start-daemon" => { - let app = app.clone(); - tauri::async_runtime::spawn(async move { - let _ = start_daemon_now(&state).await; - // An unconfigured daemon lands in setup mode - // instead of a spawn-fail loop; take the user there. - if state.setup_needed.load(Ordering::SeqCst) { - show_settings_window(&app); - } - }); - } - "stop-daemon" => { - tauri::async_runtime::spawn(async move { - let _ = stop_daemon_now(&state).await; - }); + // Without a StatusNotifierWatcher (the bar is not up yet at login, or + // the desktop has no SNI tray) the KSNI backend cannot register the icon + // and the build fails. Keep running without it and retry: the window, + // the daemon supervision and first-run setup do not need the icon. + if let Err(e) = build_tray(handle, &menu) { + tracing::warn!(error = %e, "tray icon unavailable; retrying in the background"); + let handle = handle.clone(); + let menu = menu.clone(); + tauri::async_runtime::spawn(async move { + let mut delay = TRAY_RETRY_FIRST; + loop { + tokio::time::sleep(delay).await; + match build_tray(&handle, &menu) { + Ok(_) => { + tracing::info!("tray icon registered"); + break; + } + Err(e) => tracing::debug!(error = %e, "tray icon still unavailable"), } - "quit" => quit(app), - _ => {} + delay = (delay * 2).min(TRAY_RETRY_MAX); } - }) - .build(app)?; + }); + } // Background poller: refresh shared status every second and update the // tray label / tooltip / item enablement when something changed. let state = app.state::>().inner().clone(); + let handle = handle.clone(); tauri::async_runtime::spawn(async move { let mut last_label = String::new(); + let mut had_tray = false; let mut last_enablement: Option<[bool; 4]> = None; let mut last_quit_label: Option<&'static str> = None; loop { @@ -1229,10 +1297,19 @@ fn setup_tray(app: &tauri::App) -> tauri::Result<()> { let (daemon, scan, spawned_by_tray) = refresh(&state, info.as_ref()); let label = tray_label(&daemon, &scan, info.as_ref()); - if label != last_label { - tracing::info!(%label, "tray status label updated"); - let _ = status_item.set_text(&label); - let _ = tray.set_tooltip(Some(tray_tooltip(&daemon, &scan, info.as_ref()))); + let tray = handle.tray_by_id(TRAY_ID); + // A tray icon that appeared late (see the retry above) gets the + // current tooltip too. + let new_tray = tray.is_some() && !had_tray; + had_tray = tray.is_some(); + if label != last_label || new_tray { + if label != last_label { + tracing::info!(%label, "tray status label updated"); + let _ = status_item.set_text(&label); + } + if let Some(tray) = tray { + let _ = tray.set_tooltip(Some(tray_tooltip(&daemon, &scan, info.as_ref()))); + } last_label = label; } @@ -1330,6 +1407,7 @@ pub fn run() { None, )) .manage(state) + .manage(Arc::new(popup::Popup::detect())) .invoke_handler(tauri::generate_handler![ get_status, start_scanning, @@ -1346,19 +1424,41 @@ pub fn run() { set_autostart, start_daemon, stop_daemon, - open_log_folder + open_log_folder, + dismiss_window ]) - .on_window_event(|window, event| { + .on_window_event(|window, event| match event { // Tray-only app: closing the status window hides it. - if let WindowEvent::CloseRequested { api, .. } = event { + WindowEvent::CloseRequested { api, .. } => { api.prevent_close(); - let _ = window.hide(); + match window.app_handle().get_webview_window(window.label()) { + Some(webview) => window + .app_handle() + .state::>() + .hide(&webview), + None => { + let _ = window.hide(); + } + } } + // The popup closes when another window takes the focus. + WindowEvent::Focused(focused) => { + if let Some(webview) = window.app_handle().get_webview_window(window.label()) { + window + .app_handle() + .state::>() + .focus_changed(&webview, *focused); + } + } + _ => {} }) .setup(|app| { #[cfg(target_os = "macos")] app.set_activation_policy(tauri::ActivationPolicy::Accessory); + if let Some(window) = app.get_webview_window("main") { + app.state::>().prepare(&window); + } setup_tray(app)?; if let Some(tab) = show_on_start_env() { diff --git a/friglet-tray/src/popup.rs b/friglet-tray/src/popup.rs new file mode 100644 index 0000000..f74e4e9 --- /dev/null +++ b/friglet-tray/src/popup.rs @@ -0,0 +1,1206 @@ +//! Top-bar popup mode for Hyprland (SNB-656). +//! +//! On Hyprland (Omarchy 4 among others) the status window opens as a +//! floating panel right-aligned under the top bar, like Omarchy's own +//! Wi-Fi / Bluetooth / Tailscale panels, instead of as a tiled window. A +//! left click on the tray icon toggles it; Esc closes it, and so does +//! focusing another window. +//! +//! The mode is on only when `HYPRLAND_INSTANCE_SIGNATURE` is set. It talks +//! to Hyprland's request socket directly and writes no config files. +//! +//! 1. Before each show it reads the monitor under the pointer +//! (`j/cursorpos`, `j/monitors`: bar height from `reserved`) and +//! `general:gaps_out`, and works out where the panel goes, the way +//! Omarchy places its panels (`y = bar + gaps_out / 2`, the same margin +//! on the right). With the Lua API (Hyprland 0.55+, Omarchy 4) it then +//! registers a window rule for our window (float, size, monitor-relative +//! move) with `eval`, so the window maps in place, and turns on a rule +//! that keeps the pointer from focusing *other* windows by hovering them +//! while the popup is open (Omarchy sets `follow_mouse = 1`, so a stray +//! pointer would otherwise steal the focus and close the popup; a click +//! still focuses them). Hiding turns that rule off again. +//! 2. After the show it finds our window in `j/clients` by PID, learns the +//! class Hyprland sees (so later rules match class and title, and it is +//! logged for a static rule), and if the window did not land where +//! planned (no Lua API, or the rule was wiped by a config reload) it +//! floats, resizes and moves it by address: `hl.dsp.window.*` with the +//! Lua API, the legacy dispatchers otherwise. +//! +//! The window is also undecorated and fixed-size (min = max) in this mode: +//! Hyprland floats fixed-size toplevels by itself, so the panel still floats +//! (centred) if every socket call fails. + +use std::io; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use serde::Deserialize; +use tauri::{LogicalSize, WebviewWindow}; + +/// Panel size in logical pixels (the window's configured size). +pub const POPUP_SIZE: (u32, u32) = (420, 600); +/// Hyprland's default `general:gaps_out`, used when it can't be read. +const DEFAULT_GAPS_OUT: f64 = 20.0; +/// Read/write timeout for one request on the Hyprland socket. +#[cfg(unix)] +const IO_TIMEOUT: Duration = Duration::from_millis(500); +/// A focus loss this close before a tray click means the click caused it: +/// the click then leaves the popup closed instead of reopening it. +const BLUR_CLICK_WINDOW: Duration = Duration::from_millis(400); +/// How long to wait before acting on a focus loss, so the focus can settle. +const BLUR_SETTLE: Duration = Duration::from_millis(150); +/// How long to look for our window in `j/clients` after a show. +const MAP_TIMEOUT: Duration = Duration::from_millis(1500); +const MAP_POLL: Duration = Duration::from_millis(50); +/// Lua globals (in Hyprland's config state) holding our rule handles. +const LUA_POPUP_RULE: &str = "friglet_tray_popup_rule"; +const LUA_NOHOVER_RULE: &str = "friglet_tray_nohover_rule"; + +// --------------------------------------------------------------------------- +// Socket +// --------------------------------------------------------------------------- + +/// Hyprland's request socket (`.socket.sock`, the one `hyprctl` uses). +#[derive(Clone, Debug)] +pub struct Hyprland { + socket: PathBuf, +} + +impl Hyprland { + /// The running Hyprland instance, if this process is inside one. + pub fn from_env() -> Option { + let sig = std::env::var("HYPRLAND_INSTANCE_SIGNATURE").ok()?; + let sig = sig.trim(); + if sig.is_empty() { + return None; + } + let runtime = std::env::var_os("XDG_RUNTIME_DIR").map(PathBuf::from); + Some(Self::new(socket_path(runtime.as_deref(), sig))) + } + + pub fn new(socket: PathBuf) -> Self { + Self { socket } + } + + /// Send one request and return the whole reply. Hyprland reads the + /// request as sent (no terminator), answers once and closes. + #[cfg(unix)] + pub fn request(&self, request: &str) -> io::Result { + use std::io::{Read, Write}; + use std::net::Shutdown; + use std::os::unix::net::UnixStream; + + let mut stream = UnixStream::connect(&self.socket)?; + stream.set_read_timeout(Some(IO_TIMEOUT))?; + stream.set_write_timeout(Some(IO_TIMEOUT))?; + stream.write_all(request.as_bytes())?; + let _ = stream.shutdown(Shutdown::Write); + let mut reply = String::new(); + stream.read_to_string(&mut reply)?; + Ok(reply) + } + + #[cfg(not(unix))] + pub fn request(&self, _request: &str) -> io::Result { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "no Hyprland here", + )) + } + + fn json Deserialize<'de>>(&self, request: &str) -> io::Result { + let reply = self.request(&format!("j/{request}"))?; + serde_json::from_str(&reply).map_err(|e| { + io::Error::new( + io::ErrorKind::InvalidData, + format!("{request}: {e}: {}", truncate(&reply)), + ) + }) + } + + pub fn monitors(&self) -> io::Result> { + self.json("monitors") + } + + pub fn clients(&self) -> io::Result> { + self.json("clients") + } + + /// The focused window; `None` when nothing has focus (`{}`). + pub fn active_window(&self) -> io::Result> { + let reply = self.request("j/activewindow")?; + Ok(serde_json::from_str::(&reply) + .ok() + .filter(|c| !c.address.is_empty())) + } + + /// Pointer position, global layout coordinates. + pub fn cursor_pos(&self) -> io::Result<(f64, f64)> { + #[derive(Deserialize)] + struct Pos { + x: f64, + y: f64, + } + let p: Pos = self.json("cursorpos")?; + Ok((p.x, p.y)) + } + + /// `general:gaps_out` (its top value), if readable. + pub fn gaps_out(&self) -> Option { + parse_gaps_out(&self.request("j/getoption general:gaps_out").ok()?) + } +} + +/// `$XDG_RUNTIME_DIR/hypr//.socket.sock` (Hyprland 0.40+), or the +/// older `/tmp/hypr//.socket.sock` when only that one exists. +pub fn socket_path(runtime_dir: Option<&Path>, signature: &str) -> PathBuf { + let legacy = Path::new("/tmp/hypr").join(signature).join(".socket.sock"); + match runtime_dir { + Some(dir) => { + let current = dir.join("hypr").join(signature).join(".socket.sock"); + if !current.exists() && legacy.exists() { + legacy + } else { + current + } + } + None => legacy, + } +} + +fn truncate(s: &str) -> String { + const MAX: usize = 200; + match s.char_indices().nth(MAX) { + Some((i, _)) => format!("{}…", &s[..i]), + None => s.to_string(), + } +} + +// --------------------------------------------------------------------------- +// Replies +// --------------------------------------------------------------------------- + +/// The parts of a `j/monitors` entry the placement needs. Numbers are read +/// as `f64` so integer and float renderings both parse. +#[derive(Clone, Debug, Deserialize)] +pub struct Monitor { + #[serde(default)] + pub name: String, + /// Top-left corner, global layout coordinates. + pub x: f64, + pub y: f64, + /// Physical mode pixels, before the transform. + pub width: f64, + pub height: f64, + #[serde(default = "one")] + pub scale: f64, + #[serde(default)] + pub transform: i64, + #[serde(default)] + pub focused: bool, + /// Space taken by bars (layer-shell exclusive zones): + /// `[left, top, right, bottom]`, logical pixels. + #[serde(default)] + pub reserved: Vec, +} + +fn one() -> f64 { + 1.0 +} + +impl Monitor { + /// Size in layout (logical) pixels, as Hyprland computes it. + pub fn logical_size(&self) -> (f64, f64) { + let scale = if self.scale > 0.0 { self.scale } else { 1.0 }; + // Transforms 1, 3, 5 and 7 rotate by 90° or 270°. + let (w, h) = if self.transform % 2 == 1 { + (self.height, self.width) + } else { + (self.width, self.height) + }; + ((w / scale).round(), (h / scale).round()) + } + + fn reserved(&self, i: usize) -> f64 { + self.reserved.get(i).copied().unwrap_or(0.0).max(0.0) + } + + fn contains(&self, x: f64, y: f64) -> bool { + let (w, h) = self.logical_size(); + x >= self.x && x < self.x + w && y >= self.y && y < self.y + h + } +} + +/// The parts of a `j/clients` / `j/activewindow` entry we use. +#[derive(Clone, Debug, Default, Deserialize)] +#[serde(rename_all = "camelCase", default)] +pub struct Client { + /// `0x…`, as `address:` selectors want it. + pub address: String, + pub pid: i64, + pub class: String, + pub title: String, + pub floating: bool, + /// Global layout coordinates. + pub at: Vec, + pub size: Vec, +} + +/// `general:gaps_out` from a `j/getoption` reply: the first (top) value of +/// the CSS-style gap (`"css"` with the Lua config, `"custom"` with +/// hyprlang), or a plain `"int"`. Omarchy's panels take this first value. +pub fn parse_gaps_out(reply: &str) -> Option { + let v: serde_json::Value = serde_json::from_str(reply).ok()?; + if let Some(n) = v.get("int").and_then(serde_json::Value::as_f64) { + return Some(n); + } + v.get("css") + .or_else(|| v.get("custom")) + .and_then(serde_json::Value::as_str)? + .split(|c: char| c.is_whitespace() || c == ',') + .find(|s| !s.is_empty())? + .parse() + .ok() +} + +// --------------------------------------------------------------------------- +// Placement +// --------------------------------------------------------------------------- + +/// Where the panel goes, in Hyprland layout (logical, global) coordinates. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Placement { + pub x: i32, + pub y: i32, + pub width: i32, + pub height: i32, + /// Distance from the monitor's top edge, and from its right edge to the + /// panel's right edge: what a monitor-relative rule needs. + pub top: i32, + pub right: i32, +} + +impl Placement { + /// Whether `client` already floats here (within a pixel of rounding). + pub fn matches(&self, client: &Client) -> bool { + let near = |a: Option<&f64>, b: i32| a.is_some_and(|a| (a - f64::from(b)).abs() <= 1.0); + client.floating + && near(client.at.first(), self.x) + && near(client.at.get(1), self.y) + && near(client.size.first(), self.width) + && near(client.size.get(1), self.height) + } +} + +/// The monitor the user is looking at: the one under the pointer (the tray +/// icon was just clicked; Omarchy's bar sends no click position), else the +/// focused one, else the first. +pub fn pick_monitor(monitors: &[Monitor], cursor: Option<(f64, f64)>) -> Option<&Monitor> { + cursor + .and_then(|(x, y)| monitors.iter().find(|m| m.contains(x, y))) + .or_else(|| monitors.iter().find(|m| m.focused)) + .or_else(|| monitors.first()) +} + +/// Where the panel goes on `monitor`: right-aligned under the bar with +/// `gaps_out / 2` between them and to the right edge, like Omarchy's +/// panels (`KeyboardPanel`). `size` is the wanted size; it shrinks to fit a +/// small screen. +pub fn place(monitor: &Monitor, gaps_out: f64, size: (u32, u32)) -> Placement { + let (w, h) = monitor.logical_size(); + let gap = (gaps_out / 2.0).round().max(0.0); + let top = monitor.reserved(1) + gap; + let right = monitor.reserved(2) + gap; + let usable_w = w - monitor.reserved(0) - right - gap; + let usable_h = h - top - monitor.reserved(3) - gap; + let width = f64::from(size.0).min(usable_w).max(1.0).round(); + let height = f64::from(size.1).min(usable_h).max(1.0).round(); + Placement { + x: (monitor.x + w - right - width).round() as i32, + y: (monitor.y + top).round() as i32, + width: width as i32, + height: height as i32, + top: top as i32, + right: right as i32, + } +} + +// --------------------------------------------------------------------------- +// Requests +// --------------------------------------------------------------------------- + +/// Escape `s` for a regex (Hyprland matches with RE2, whole string). +pub fn regex_escape(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + for c in s.chars() { + if "\\.+*?()|[]{}^$".contains(c) { + out.push('\\'); + } + out.push(c); + } + out +} + +/// `s` as a Lua double-quoted string literal. +pub fn lua_string(s: &str) -> String { + let mut out = String::with_capacity(s.len() + 2); + out.push('"'); + for c in s.chars() { + match c { + '"' => out.push_str("\\\""), + '\\' => out.push_str("\\\\"), + '\n' => out.push_str("\\n"), + c if c.is_control() => {} + c => out.push(c), + } + } + out.push('"'); + out +} + +/// Lua that (re)registers the popup rule: our window floats at `plan`'s +/// size, `plan.top` below the monitor's top and `plan.right` from its right +/// edge (`move` in a rule is monitor-relative). The previous rule is +/// disabled first: Hyprland has no rule removal, and re-registering a named +/// rule appends effects instead of replacing them. +pub fn popup_rule_lua(title: &str, class: Option<&str>, plan: &Placement) -> String { + let mut matcher = format!( + "title = {}", + lua_string(&format!("^{}$", regex_escape(title))) + ); + if let Some(class) = class { + matcher.push_str(&format!( + ", class = {}", + lua_string(&format!("^{}$", regex_escape(class))) + )); + } + format!( + "if {r} then {r}:set_enabled(false) end; {r} = hl.window_rule({{ match = {{ {matcher} }}, \ + float = true, size = {{ {w}, {h} }}, move = {{ \"(monitor_w-window_w-{right})\", {top} }} }})", + r = LUA_POPUP_RULE, + w = plan.width, + h = plan.height, + right = plan.right, + top = plan.top, + ) +} + +/// Lua that turns on the "hover does not focus other windows" rule: every +/// window except ours gets `no_follow_mouse` while the popup is open, so +/// only a click (or a keybinding) takes the focus away. Created once per +/// config load; the handle expires when a reload wipes the rules. +pub fn nohover_on_lua(title: &str) -> String { + format!( + "if {r} == nil or {r}:is_enabled() == nil then {r} = hl.window_rule({{ enabled = false, \ + match = {{ title = {m} }}, no_follow_mouse = true }}) end; {r}:set_enabled(true)", + r = LUA_NOHOVER_RULE, + m = lua_string(&format!("negative:^{}$", regex_escape(title))), + ) +} + +/// Lua that turns the hover rule off again. +pub fn nohover_off_lua() -> String { + format!( + "if {r} then {r}:set_enabled(false) end", + r = LUA_NOHOVER_RULE + ) +} + +/// Dispatches that float, size and place the window at `address` (global +/// coordinates), as Lua dispatchers or the legacy (hyprlang) ones. +pub fn place_dispatches(lua: bool, address: &str, plan: &Placement) -> Vec { + let (x, y, w, h) = (plan.x, plan.y, plan.width, plan.height); + if lua { + let win = lua_string(&format!("address:{address}")); + vec![ + format!("dispatch hl.dsp.window.float({{ window = {win}, action = \"enable\" }})"), + format!("dispatch hl.dsp.window.resize({{ window = {win}, x = {w}, y = {h} }})"), + format!("dispatch hl.dsp.window.move({{ window = {win}, x = {x}, y = {y} }})"), + ] + } else { + vec![ + format!("dispatch setfloating address:{address}"), + format!("dispatch resizewindowpixel exact {w} {h},address:{address}"), + format!("dispatch movewindowpixel exact {x} {y},address:{address}"), + ] + } +} + +/// What an `eval` reply says. +#[derive(Debug, PartialEq, Eq)] +pub enum EvalReply { + Ok, + /// No Lua API: a hyprlang config (`eval is only supported with the lua + /// config manager`) or a Hyprland without `eval` (`unknown request`). + NoLua, + Error(String), +} + +pub fn classify_eval(reply: &str) -> EvalReply { + let r = reply.trim(); + if r == "ok" || r.is_empty() { + EvalReply::Ok + } else if r.contains("only supported with the lua config manager") || r == "unknown request" { + EvalReply::NoLua + } else { + EvalReply::Error(truncate(r)) + } +} + +// --------------------------------------------------------------------------- +// Popup +// --------------------------------------------------------------------------- + +/// Popup mode state, managed by Tauri as `Arc`. +pub struct Popup { + hypr: Option, + state: Mutex, +} + +#[derive(Default)] +struct PopupState { + /// Our window's class as Hyprland reports it, learned after a show. + class: Option, + /// Whether this Hyprland has the Lua API; `None` until asked. + lua: Option, + /// The popup rule as last registered, to skip re-registering it. + rule: Option, + /// When the popup last closed because it lost the focus. + blurred_at: Option, +} + +impl Popup { + /// Popup mode when running inside Hyprland, the ordinary window + /// otherwise. + pub fn detect() -> Self { + let hypr = Hyprland::from_env(); + if let Some(h) = &hypr { + tracing::info!( + socket = %h.socket.display(), + "Hyprland detected: the status window opens as a top-bar popup" + ); + } + Self::with(hypr) + } + + pub fn with(hypr: Option) -> Self { + Self { + hypr, + state: Mutex::new(PopupState::default()), + } + } + + pub fn enabled(&self) -> bool { + self.hypr.is_some() + } + + /// Once at startup: a fixed-size, undecorated window, which Hyprland + /// floats on its own even if the socket calls fail. Also turns off a + /// hover rule a previous run may have left on (Lua globals outlive us). + pub fn prepare(self: &Arc, window: &WebviewWindow) { + let Some(hypr) = self.hypr.clone() else { + return; + }; + let _ = window.set_decorations(false); + fix_size(window, POPUP_SIZE.0, POPUP_SIZE.1); + let this = Arc::clone(self); + tauri::async_runtime::spawn_blocking(move || this.after_hide(&hypr)); + } + + /// Show the window (as the popup in popup mode) and focus it. + pub fn show(self: &Arc, window: &WebviewWindow) { + let Some(hypr) = self.hypr.clone() else { + let _ = window.show(); + let _ = window.set_focus(); + return; + }; + let this = Arc::clone(self); + let window = window.clone(); + tauri::async_runtime::spawn(async move { + let title = window.title().unwrap_or_default(); + let plan = { + let (this, hypr, title) = (Arc::clone(&this), hypr.clone(), title.clone()); + tauri::async_runtime::spawn_blocking(move || this.before_show(&hypr, &title)) + .await + .ok() + .flatten() + }; + if let Some(p) = plan { + fix_size(&window, p.width as u32, p.height as u32); + } + let _ = window.show(); + let _ = window.set_focus(); + if let Some(p) = plan { + let pid = i64::from(std::process::id()); + let _ = tauri::async_runtime::spawn_blocking(move || { + this.after_show(&hypr, &p, &title, pid) + }) + .await; + } + }); + } + + /// Hide the window; in popup mode also give hover focus back to the + /// other windows. + pub fn hide(self: &Arc, window: &WebviewWindow) { + let _ = window.hide(); + if let Some(hypr) = self.hypr.clone() { + let this = Arc::clone(self); + tauri::async_runtime::spawn_blocking(move || this.after_hide(&hypr)); + } + } + + /// Before quitting: turn the hover rule off (blocking). + pub fn release(&self) { + if let Some(hypr) = &self.hypr { + self.after_hide(hypr); + } + } + + /// Left click on the tray icon: toggle the popup, or open the ordinary + /// window. + pub fn toggle(self: &Arc, window: &WebviewWindow) { + if !self.enabled() { + self.show(window); + return; + } + if window.is_visible().unwrap_or(false) { + tracing::debug!("tray click: closing the popup"); + self.hide(window); + return; + } + let just_blurred = self + .state + .lock() + .unwrap() + .blurred_at + .is_some_and(|t| t.elapsed() < BLUR_CLICK_WINDOW); + if just_blurred { + tracing::debug!("tray click right after a focus loss: leaving the popup closed"); + } else { + self.show(window); + } + } + + /// Esc in the window: closes the popup; nothing for the ordinary window. + pub fn dismiss(self: &Arc, window: &WebviewWindow) { + if self.enabled() { + tracing::debug!("Esc: closing the popup"); + self.hide(window); + } + } + + /// Window focus changed: in popup mode, close when another window took + /// the focus. + pub fn focus_changed(self: &Arc, window: &WebviewWindow, focused: bool) { + let Some(hypr) = self.hypr.clone() else { + return; + }; + if focused { + return; + } + let this = Arc::clone(self); + let window = window.clone(); + tauri::async_runtime::spawn(async move { + tokio::time::sleep(BLUR_SETTLE).await; + if !window.is_visible().unwrap_or(false) || window.is_focused().unwrap_or(false) { + return; + } + // GTK also reports the window unfocused while a dropdown of ours + // is open; Hyprland still has our window active then. + let pid = i64::from(std::process::id()); + let ours = tauri::async_runtime::spawn_blocking(move || our_window_active(&hypr, pid)) + .await + .unwrap_or(false); + if !ours { + tracing::debug!("another window has the focus: closing the popup"); + this.state.lock().unwrap().blurred_at = Some(Instant::now()); + this.hide(&window); + } + }); + } + + /// Blocking: plan the placement and register the rules for the coming + /// show. `None` when Hyprland could not be asked. + fn before_show(&self, hypr: &Hyprland, title: &str) -> Option { + let monitors = match hypr.monitors() { + Ok(m) => m, + Err(e) => { + tracing::warn!(error = %e, "Hyprland monitors unavailable; popup not placed"); + return None; + } + }; + let cursor = hypr.cursor_pos().ok(); + let monitor = pick_monitor(&monitors, cursor)?; + let gaps_out = hypr.gaps_out().unwrap_or(DEFAULT_GAPS_OUT); + let plan = place(monitor, gaps_out, POPUP_SIZE); + tracing::debug!(monitor = %monitor.name, ?plan, "popup placement"); + + let (lua, class, last_rule) = { + let st = self.state.lock().unwrap(); + (st.lua, st.class.clone(), st.rule.clone()) + }; + if lua == Some(false) { + return Some(plan); + } + let rule = popup_rule_lua(title, class.as_deref(), &plan); + let mut code = String::new(); + if last_rule.as_deref() != Some(rule.as_str()) { + code.push_str(&rule); + code.push_str("; "); + } + code.push_str(&nohover_on_lua(title)); + let reply = hypr.request(&format!("eval {code}")); + let mut st = self.state.lock().unwrap(); + match reply.as_deref().map(classify_eval) { + Ok(EvalReply::Ok) => { + st.lua = Some(true); + st.rule = Some(rule); + } + Ok(EvalReply::NoLua) => { + tracing::info!("Hyprland without the Lua API: the popup is placed after it maps"); + st.lua = Some(false); + } + Ok(EvalReply::Error(msg)) => { + tracing::warn!(reply = %msg, "Hyprland refused the popup rule"); + st.lua = Some(true); + st.rule = None; + } + Err(e) => tracing::warn!(error = %e, "Hyprland eval failed"), + } + Some(plan) + } + + /// Blocking, after the show: find our window, learn its class, and put + /// it where `plan` says if the rule did not. + fn after_show(&self, hypr: &Hyprland, plan: &Placement, title: &str, pid: i64) { + let deadline = Instant::now() + MAP_TIMEOUT; + let client = loop { + match hypr.clients() { + Ok(clients) => { + if let Some(c) = clients + .into_iter() + .find(|c| c.pid == pid && (title.is_empty() || c.title == title)) + { + break c; + } + } + Err(e) => { + tracing::warn!(error = %e, "Hyprland clients unavailable"); + return; + } + } + if Instant::now() >= deadline { + tracing::debug!("popup window not listed by Hyprland"); + return; + } + std::thread::sleep(MAP_POLL); + }; + + let lua = { + let mut st = self.state.lock().unwrap(); + if !client.class.is_empty() && st.class.as_deref() != Some(client.class.as_str()) { + tracing::info!(class = %client.class, title = %client.title, "Hyprland window class of the status window"); + st.class = Some(client.class.clone()); + } + if !plan.matches(&client) { + // The rule did not apply (no Lua API, or a config reload + // wiped it): register it again next time. + st.rule = None; + } + st.lua + }; + if plan.matches(&client) { + return; + } + tracing::debug!(at = ?client.at, size = ?client.size, floating = client.floating, "placing the popup by dispatch"); + // Unknown API: Lua first, the legacy dispatchers if that is refused. + let tries: &[bool] = match lua { + Some(true) => &[true], + Some(false) => &[false], + None => &[true, false], + }; + for &lua in tries { + let mut all_ok = true; + for request in place_dispatches(lua, &client.address, plan) { + match hypr.request(&request) { + Ok(r) if r.trim() == "ok" => {} + Ok(r) => { + all_ok = false; + tracing::debug!(%request, reply = %truncate(r.trim()), "dispatch refused"); + } + Err(e) => { + tracing::warn!(error = %e, "Hyprland dispatch failed"); + return; + } + } + } + if all_ok { + return; + } + } + } + + /// Blocking: turn the hover rule off. + fn after_hide(&self, hypr: &Hyprland) { + if self.state.lock().unwrap().lua == Some(false) { + return; + } + match hypr.request(&format!("eval {}", nohover_off_lua())) { + Ok(reply) => { + let lua = match classify_eval(&reply) { + EvalReply::Ok => true, + EvalReply::NoLua => false, + EvalReply::Error(msg) => { + tracing::warn!(reply = %msg, "Hyprland refused to drop the hover rule"); + true + } + }; + self.state.lock().unwrap().lua.get_or_insert(lua); + } + Err(e) => tracing::debug!(error = %e, "Hyprland eval failed"), + } + } +} + +/// Pin the window to `width` x `height` with equal min and max sizes, which +/// Hyprland reads as a fixed-size window and floats. (GTK's own +/// non-resizable mode would size the window to its content instead.) +fn fix_size(window: &WebviewWindow, width: u32, height: u32) { + let size = LogicalSize::new(width, height); + let _ = window.set_min_size(Some(size)); + let _ = window.set_max_size(Some(size)); + let _ = window.set_size(size); +} + +/// Whether Hyprland's active window belongs to process `pid`. A failed +/// query counts as "no": the focus event is trusted then. +fn our_window_active(hypr: &Hyprland, pid: i64) -> bool { + match hypr.active_window() { + Ok(Some(active)) => active.pid == pid, + Ok(None) => false, + Err(e) => { + tracing::debug!(error = %e, "activewindow failed; trusting the focus event"); + false + } + } +} + +#[cfg(all(test, unix))] +mod tests { + use super::*; + use std::io::{Read, Write}; + use std::os::unix::net::UnixListener; + use std::sync::atomic::{AtomicUsize, Ordering}; + + fn monitor(json: &str) -> Monitor { + serde_json::from_str(json).unwrap() + } + + /// Omarchy-like laptop: 2880x1800 panel at scale 2, 26 px bar on top. + const LAPTOP: &str = r#"{"id":0,"name":"eDP-1","width":2880,"height":1800,"refreshRate":60.0, + "x":0,"y":0,"activeWorkspace":{"id":1,"name":"1"},"reserved":[0,26,0,0], + "scale":2.00,"transform":0,"focused":true,"dpmsStatus":true}"#; + + fn laptop() -> Monitor { + monitor(LAPTOP) + } + + #[test] + fn gaps_out_reply_forms() { + // Lua config (Hyprland 0.55+): "css"; hyprlang: "custom"; first value. + assert_eq!( + parse_gaps_out(r#"{"option": "general:gaps_out", "css": "10 10 10 10", "set": true }"#), + Some(10.0) + ); + assert_eq!( + parse_gaps_out(r#"{"option":"general:gaps_out","custom":"8 20 8 20","set":true}"#), + Some(8.0) + ); + assert_eq!( + parse_gaps_out(r#"{"option":"x","int":7,"set":false}"#), + Some(7.0) + ); + assert_eq!(parse_gaps_out("no such option"), None); + assert_eq!(parse_gaps_out(r#"{"css":"a b"}"#), None); + } + + #[test] + fn placement_is_right_aligned_under_the_bar() { + // Logical 1440x900; gaps_out 10 -> 5 px under the bar and on the + // right, as Omarchy's own panels sit. + let p = place(&laptop(), 10.0, POPUP_SIZE); + assert_eq!( + p, + Placement { + x: 1440 - 5 - 420, + y: 26 + 5, + width: 420, + height: 600, + top: 31, + right: 5 + } + ); + } + + #[test] + fn placement_respects_offsets_reserved_space_and_small_screens() { + let m = monitor( + r#"{"name":"HDMI-A-1","x":1440,"y":-200,"width":1366,"height":768, + "scale":1,"transform":0,"reserved":[0,30,40,0]}"#, + ); + let p = place(&m, 20.0, POPUP_SIZE); + assert_eq!((p.x, p.y), (1440 + 1366 - 40 - 10 - 420, -200 + 30 + 10)); + assert_eq!((p.width, p.height, p.top, p.right), (420, 600, 40, 50)); + // A short screen shrinks the panel to fit between bar and bottom. + let tiny = monitor(r#"{"x":0,"y":0,"width":800,"height":500,"reserved":[0,30,0,0]}"#); + let p = place(&tiny, 20.0, POPUP_SIZE); + assert_eq!((p.y, p.height), (40, 500 - 30 - 10 - 10)); + assert_eq!(p.x, 800 - 10 - 420); + } + + #[test] + fn rotated_and_scaled_monitors_use_logical_size() { + let m = monitor( + r#"{"x":0,"y":0,"width":1920,"height":1080,"scale":1,"transform":1,"reserved":[0,26,0,0]}"#, + ); + assert_eq!(m.logical_size(), (1080.0, 1920.0)); + assert_eq!(place(&m, 10.0, POPUP_SIZE).x, 1080 - 5 - 420); + let frac = monitor(r#"{"x":0,"y":0,"width":2560,"height":1600,"scale":1.6}"#); + assert_eq!(frac.logical_size(), (1600.0, 1000.0)); + } + + #[test] + fn monitor_under_the_pointer_wins_then_focused() { + let a = monitor(r#"{"name":"A","x":0,"y":0,"width":1920,"height":1080,"focused":true}"#); + let b = monitor(r#"{"name":"B","x":1920,"y":0,"width":2560,"height":1440}"#); + let all = [a, b]; + assert_eq!(pick_monitor(&all, Some((2000.0, 10.0))).unwrap().name, "B"); + assert_eq!(pick_monitor(&all, Some((-5.0, 10.0))).unwrap().name, "A"); + assert_eq!(pick_monitor(&all, None).unwrap().name, "A"); + assert!(pick_monitor(&[], None).is_none()); + } + + #[test] + fn placement_match_tolerates_rounding_and_needs_floating() { + let p = place(&laptop(), 10.0, POPUP_SIZE); + let client = |floating: bool, at: [f64; 2], size: [f64; 2]| Client { + floating, + at: at.to_vec(), + size: size.to_vec(), + ..Client::default() + }; + assert!(p.matches(&client(true, [1015.0, 31.0], [420.0, 600.0]))); + assert!(p.matches(&client(true, [1016.0, 30.0], [420.0, 600.0]))); + assert!(!p.matches(&client(false, [1015.0, 31.0], [420.0, 600.0]))); + assert!(!p.matches(&client(true, [510.0, 100.0], [420.0, 600.0]))); + assert!(!p.matches(&Client::default())); + } + + #[test] + fn escaping() { + assert_eq!(regex_escape("Friglet Status"), "Friglet Status"); + assert_eq!(regex_escape("a.b(c)"), r"a\.b\(c\)"); + assert_eq!(lua_string(r#"say "hi" \ bye"#), r#""say \"hi\" \\ bye""#); + } + + #[test] + fn rule_and_dispatch_text() { + let p = place(&laptop(), 10.0, POPUP_SIZE); + assert_eq!( + popup_rule_lua("Friglet Status", None, &p), + "if friglet_tray_popup_rule then friglet_tray_popup_rule:set_enabled(false) end; \ + friglet_tray_popup_rule = hl.window_rule({ match = { title = \"^Friglet Status$\" }, \ + float = true, size = { 420, 600 }, move = { \"(monitor_w-window_w-5)\", 31 } })" + ); + assert!( + popup_rule_lua("Friglet Status", Some("friglet-tray"), &p) + .contains(r#"match = { title = "^Friglet Status$", class = "^friglet-tray$" }"#) + ); + assert_eq!( + nohover_on_lua("Friglet Status"), + "if friglet_tray_nohover_rule == nil or friglet_tray_nohover_rule:is_enabled() == nil \ + then friglet_tray_nohover_rule = hl.window_rule({ enabled = false, match = { title = \ + \"negative:^Friglet Status$\" }, no_follow_mouse = true }) end; \ + friglet_tray_nohover_rule:set_enabled(true)" + ); + assert_eq!( + nohover_off_lua(), + "if friglet_tray_nohover_rule then friglet_tray_nohover_rule:set_enabled(false) end" + ); + assert_eq!( + place_dispatches(true, "0x55aa", &p), + [ + r#"dispatch hl.dsp.window.float({ window = "address:0x55aa", action = "enable" })"#, + r#"dispatch hl.dsp.window.resize({ window = "address:0x55aa", x = 420, y = 600 })"#, + r#"dispatch hl.dsp.window.move({ window = "address:0x55aa", x = 1015, y = 31 })"#, + ] + ); + assert_eq!( + place_dispatches(false, "0x55aa", &p), + [ + "dispatch setfloating address:0x55aa", + "dispatch resizewindowpixel exact 420 600,address:0x55aa", + "dispatch movewindowpixel exact 1015 31,address:0x55aa", + ] + ); + } + + #[test] + fn eval_replies() { + assert_eq!(classify_eval("ok"), EvalReply::Ok); + assert_eq!( + classify_eval("eval is only supported with the lua config manager"), + EvalReply::NoLua + ); + assert_eq!(classify_eval("unknown request"), EvalReply::NoLua); + assert!(matches!( + classify_eval("error: [string]:1: attempt to index a nil value"), + EvalReply::Error(_) + )); + } + + #[test] + fn socket_path_prefers_the_runtime_dir() { + let dir = std::env::temp_dir().join(format!("friglet-hypr-path-{}", std::process::id())); + assert_eq!( + socket_path(Some(&dir), "sig"), + dir.join("hypr/sig/.socket.sock") + ); + assert_eq!( + socket_path(None, "sig"), + Path::new("/tmp/hypr/sig/.socket.sock") + ); + } + + // -- fake Hyprland socket --------------------------------------------- + + type Reply = Box String + Send + Sync>; + + /// A fake `.socket.sock`: one request per connection, read to EOF as + /// Hyprland does, answered by `reply`; every request is recorded. + struct FakeHyprland { + hypr: Hyprland, + log: Arc>>, + _dir: PathBuf, + } + + impl FakeHyprland { + fn start(tag: &str, reply: Reply) -> Self { + static N: AtomicUsize = AtomicUsize::new(0); + let dir = std::env::temp_dir().join(format!( + "fh-{}-{}-{tag}", + std::process::id(), + N.fetch_add(1, Ordering::SeqCst) + )); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join(".socket.sock"); + let listener = UnixListener::bind(&path).unwrap(); + let log = Arc::new(Mutex::new(Vec::new())); + let seen = Arc::clone(&log); + std::thread::spawn(move || { + for stream in listener.incoming() { + let Ok(mut stream) = stream else { break }; + let mut req = String::new(); + if stream.read_to_string(&mut req).is_err() { + continue; + } + let answer = reply(&req); + seen.lock().unwrap().push(req); + let _ = stream.write_all(answer.as_bytes()); + } + }); + Self { + hypr: Hyprland::new(path), + log, + _dir: dir, + } + } + + fn requests(&self) -> Vec { + self.log.lock().unwrap().clone() + } + } + + const PID: i64 = 4242; + + fn client_json(at: [i32; 2], size: [i32; 2], floating: bool) -> String { + format!( + r#"[{{"address":"0x5612a0","mapped":true,"hidden":false,"at":[{},{}],"size":[{},{}], + "workspace":{{"id":1,"name":"1"}},"floating":{floating},"monitor":0, + "class":"friglet-tray","title":"Friglet Status","initialClass":"friglet-tray", + "initialTitle":"Friglet Status","pid":{PID}}}, + {{"address":"0x77","floating":false,"class":"kitty","title":"zsh","pid":1, + "at":[5,36],"size":[1000,800]}}]"#, + at[0], at[1], size[0], size[1] + ) + } + + /// A Hyprland 0.55+ with the Lua API that applies our rule on map. + fn lua_hyprland(clients: String) -> Reply { + Box::new(move |req| match req { + "j/monitors" => format!("[{LAPTOP}]"), + "j/cursorpos" => "{\n \"x\": 1300,\n \"y\": 12\n}\n".into(), + "j/getoption general:gaps_out" => { + r#"{"option": "general:gaps_out", "css": "10 10 10 10", "set": true }"#.into() + } + "j/clients" => clients.clone(), + r if r.starts_with("eval ") || r.starts_with("dispatch hl.") => "ok".into(), + _ => "unknown request".into(), + }) + } + + #[test] + fn lua_hyprland_gets_rules_before_the_show_and_nothing_after() { + let fake = FakeHyprland::start( + "lua", + lua_hyprland(client_json([1015, 31], [420, 600], true)), + ); + let popup = Popup::with(Some(fake.hypr.clone())); + + let plan = popup.before_show(&fake.hypr, "Friglet Status").unwrap(); + assert_eq!( + (plan.x, plan.y, plan.width, plan.height), + (1015, 31, 420, 600) + ); + popup.after_show(&fake.hypr, &plan, "Friglet Status", PID); + popup.after_hide(&fake.hypr); + + let reqs = fake.requests(); + assert_eq!( + &reqs[..3], + ["j/monitors", "j/cursorpos", "j/getoption general:gaps_out"] + ); + assert_eq!( + reqs[3], + format!( + "eval {}; {}", + popup_rule_lua("Friglet Status", None, &plan), + nohover_on_lua("Friglet Status") + ) + ); + // Already in place: no dispatch; then the hover rule goes off. + assert_eq!( + reqs[4..], + [ + "j/clients".to_string(), + format!("eval {}", nohover_off_lua()) + ] + ); + let st = popup.state.lock().unwrap(); + assert_eq!(st.lua, Some(true)); + assert_eq!(st.class.as_deref(), Some("friglet-tray")); + } + + #[test] + fn learned_class_joins_the_rule_and_an_unchanged_rule_is_not_resent() { + let fake = FakeHyprland::start( + "relearn", + lua_hyprland(client_json([1015, 31], [420, 600], true)), + ); + let popup = Popup::with(Some(fake.hypr.clone())); + let title = "Friglet Status"; + for _ in 0..3 { + let plan = popup.before_show(&fake.hypr, title).unwrap(); + popup.after_show(&fake.hypr, &plan, title, PID); + } + let evals: Vec = fake + .requests() + .into_iter() + .filter(|r| r.starts_with("eval ")) + .collect(); + let plan = place(&laptop(), 10.0, POPUP_SIZE); + assert_eq!(evals.len(), 3); + // 1st: title only; 2nd: class learned -> rule re-registered with it; + // 3rd: unchanged rule, only the hover rule. + assert!(evals[0].starts_with(&format!("eval {}", popup_rule_lua(title, None, &plan)))); + assert!(evals[1].starts_with(&format!( + "eval {}", + popup_rule_lua(title, Some("friglet-tray"), &plan) + ))); + assert_eq!(evals[2], format!("eval {}", nohover_on_lua(title))); + } + + #[test] + fn a_misplaced_window_is_moved_with_lua_dispatchers() { + // The rule did not apply (say a config reload wiped it): the window + // tiled at the left. + let fake = FakeHyprland::start( + "misplaced", + lua_hyprland(client_json([5, 36], [710, 858], false)), + ); + let popup = Popup::with(Some(fake.hypr.clone())); + let plan = popup.before_show(&fake.hypr, "Friglet Status").unwrap(); + popup.after_show(&fake.hypr, &plan, "Friglet Status", PID); + let reqs = fake.requests(); + let tail = &reqs[reqs.len() - 3..]; + assert_eq!(tail, place_dispatches(true, "0x5612a0", &plan)); + // ...and the rule is sent again on the next show. + assert!(popup.state.lock().unwrap().rule.is_none()); + } + + #[test] + fn hyprland_without_lua_gets_legacy_dispatchers_only() { + let clients = client_json([510, 150], [420, 600], true); + let fake = FakeHyprland::start( + "legacy", + Box::new(move |req| match req { + "j/monitors" => format!("[{LAPTOP}]"), + "j/cursorpos" => r#"{"x": 1300, "y": 12}"#.into(), + "j/getoption general:gaps_out" => { + r#"{"option":"general:gaps_out","custom":"10 10 10 10","set":true}"#.into() + } + "j/clients" => clients.clone(), + r if r.starts_with("eval ") => { + "eval is only supported with the lua config manager".into() + } + r if r.starts_with("dispatch hl.") => "error: 3 [string]:1: syntax error".into(), + r if r.starts_with("dispatch ") => "ok".into(), + _ => "unknown request".into(), + }), + ); + let popup = Popup::with(Some(fake.hypr.clone())); + let plan = popup.before_show(&fake.hypr, "Friglet Status").unwrap(); + popup.after_show(&fake.hypr, &plan, "Friglet Status", PID); + // A second show skips eval entirely. + let plan = popup.before_show(&fake.hypr, "Friglet Status").unwrap(); + popup.after_hide(&fake.hypr); + + let reqs = fake.requests(); + assert_eq!(reqs.iter().filter(|r| r.starts_with("eval ")).count(), 1); + assert!(!reqs.iter().any(|r| r.starts_with("dispatch hl."))); + let legacy: Vec<&String> = reqs.iter().filter(|r| r.starts_with("dispatch ")).collect(); + assert_eq!( + legacy, + place_dispatches(false, "0x5612a0", &plan) + .iter() + .collect::>() + ); + } + + #[test] + fn active_window_check_tells_ours_from_others_and_nothing() { + let answer = Arc::new(Mutex::new(String::new())); + let a = Arc::clone(&answer); + let fake = FakeHyprland::start("active", Box::new(move |_| a.lock().unwrap().clone())); + *answer.lock().unwrap() = + format!(r#"{{"address":"0x1","pid":{PID},"class":"friglet-tray"}}"#); + assert!(our_window_active(&fake.hypr, PID)); + *answer.lock().unwrap() = r#"{"address":"0x2","pid":1,"class":"kitty"}"#.into(); + assert!(!our_window_active(&fake.hypr, PID)); + *answer.lock().unwrap() = "{}".into(); + assert!(!our_window_active(&fake.hypr, PID)); + assert_eq!(fake.requests(), ["j/activewindow"; 3]); + } + + #[test] + fn no_socket_means_no_plan_and_no_panic() { + let hypr = Hyprland::new(std::env::temp_dir().join("friglet-no-such-hypr.sock")); + let popup = Popup::with(Some(hypr.clone())); + assert!(popup.before_show(&hypr, "Friglet Status").is_none()); + popup.after_hide(&hypr); + assert!(!our_window_active(&hypr, PID)); + } +} diff --git a/friglet-tray/ui/main.js b/friglet-tray/ui/main.js index 97efeee..5d64628 100644 --- a/friglet-tray/ui/main.js +++ b/friglet-tray/ui/main.js @@ -780,5 +780,24 @@ $("tab-status").addEventListener("click", () => showTab("status")); $("tab-settings").addEventListener("click", () => showTab("settings")); $("tab-wallet").addEventListener("click", () => showTab("wallet")); +// Esc closes the window when it is the top-bar popup (Hyprland); for the +// ordinary window the command does nothing. It acts on the release of a press +// the page saw: hiding on the press cuts the keystroke in half (in tests under +// Xvfb WebKitGTK then delivered the press again when the window reappeared, +// closing it at once), and an Esc that closes a dropdown never reaches the +// page as a press. +let escPressed = false; +document.addEventListener("keydown", (e) => { + if (e.key === "Escape") escPressed = !e.defaultPrevented; +}); +document.addEventListener("keyup", (e) => { + if (e.key !== "Escape" || !escPressed) return; + escPressed = false; + invoke("dismiss_window").catch((err) => console.error("dismiss_window failed", err)); +}); +window.addEventListener("blur", () => { + escPressed = false; +}); + refresh(); setInterval(refresh, 1000);