From e9b215fef4b11ad36776553fb8bd45118ef03b03 Mon Sep 17 00:00:00 2001 From: Artem Timofeev <39891735+atimofeev@users.noreply.github.com> Date: Tue, 18 Aug 2026 17:00:56 +0200 Subject: [PATCH] feat(config): add on-xdg-activate window rule Add ignore, set-urgent, and focus actions. Keep unset behavior dependent on activation token validity while explicit focus always focuses. --- docs/wiki/Configuration:-Debug-Options.md | 2 +- docs/wiki/Configuration:-Window-Rules.md | 22 +++++++++++----- niri-config/src/lib.rs | 31 +++++++++++++++++++++-- niri-config/src/window_rule.rs | 9 ++++++- src/handlers/mod.rs | 30 +++++++++++++++------- src/window/mod.rs | 10 ++++---- 6 files changed, 80 insertions(+), 24 deletions(-) diff --git a/docs/wiki/Configuration:-Debug-Options.md b/docs/wiki/Configuration:-Debug-Options.md index b2fea34f..4e27b861 100644 --- a/docs/wiki/Configuration:-Debug-Options.md +++ b/docs/wiki/Configuration:-Debug-Options.md @@ -284,7 +284,7 @@ Most of the time, these fresh tokens will have invalid serials, because the app By default, niri ignores xdg-activation tokens with invalid serials, to prevent windows from randomly stealing focus. This debug flag makes niri honor such tokens, making the aforementioned widely-used apps get focus when clicking on their tray icon or notification. -Use the [`focus-on-xdg-activate` window rule](./Configuration:-Window-Rules.md#focus-on-xdg-activate) to control whether individual windows receive focus for accepted xdg-activation requests. +Use the [`on-xdg-activate` window rule](./Configuration:-Window-Rules.md#on-xdg-activate) to control what niri does for individual windows when it accepts an xdg-activation request. Amusingly, clicking on a notification sends the app a perfectly valid activation token from the notification daemon, but these apps seem to simply ignore it. Maybe in the future these apps/toolkits (Electron, Qt) are fixed, making this debug flag unnecessary. diff --git a/docs/wiki/Configuration:-Window-Rules.md b/docs/wiki/Configuration:-Window-Rules.md index 32f6b038..b04bfcd0 100644 --- a/docs/wiki/Configuration:-Window-Rules.md +++ b/docs/wiki/Configuration:-Window-Rules.md @@ -58,7 +58,7 @@ window-rule { default-column-display "tabbed" default-floating-position x=100 y=200 relative-to="bottom-left" scroll-factor 0.75 - focus-on-xdg-activate true + on-xdg-activate "focus" focus-ring { // off @@ -595,14 +595,24 @@ window-rule { > This is because window title (and app ID) are not double-buffered in the Wayland protocol, so they are not tied to specific window contents. > There's no robust way for Firefox to synchronize visibly showing a different tab and changing the window title. -#### `focus-on-xdg-activate` +#### `on-xdg-activate` Since: next release -Set this to `false` to prevent the window from receiving focus when it requests activation. -This is useful for apps that steal focus on incoming messages or when opening popup windows like Picture-in-Picture. +Set what niri does when a window requests activation through [xdg-activation](https://wayland.app/protocols/xdg-activation-v1). + +Values: + +- `"ignore"`: do nothing: neither focus the window nor mark it urgent. +- `"set-urgent"`: always mark the window urgent instead of focusing it. +- `"focus"`: always focus the window, even for activation requests without a serial. -When set to `false`, the window will instead be marked as urgent. +The default behavior, when this rule is unset, picks between focusing and marking the window urgent based on the serial that the application provides when creating the activation token. +Requests with a valid serial focus the target window, and requests without a serial mark it urgent. + +Requests with a set, but invalid, serial, are always ignored, which you can change with the [`honor-xdg-activation-with-invalid-serial`](https://niri-wm.github.io/niri/Configuration%3A-Debug-Options.html#honor-xdg-activation-with-invalid-serial) debug flag. + +This is useful for apps that steal focus on incoming messages or when opening popup windows like Picture-in-Picture. ```kdl // Don't let Firefox PiP steal focus. @@ -610,7 +620,7 @@ window-rule { match title="^Picture-in-Picture$" open-floating true - focus-on-xdg-activate false + on-xdg-activate "set-urgent" } ``` diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index a7ee1dcd..f026f9bc 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -60,7 +60,7 @@ pub use crate::recent_windows::{MruDirection, MruFilter, MruPreviews, MruScope, pub use crate::utils::FloatOrInt; use crate::utils::{Flag, MergeWith as _}; pub use crate::window_rule::{ - FloatingPosition, PopupsRule, RelativeTo, ResolvedPopupsRules, WindowRule, + FloatingPosition, OnXdgActivate, PopupsRule, RelativeTo, ResolvedPopupsRules, WindowRule, }; pub use crate::workspace::{Workspace, WorkspaceLayoutPart}; @@ -655,6 +655,30 @@ mod tests { .unwrap() } + #[test] + fn parse_on_xdg_activate() { + let parsed = do_parse( + r#" + window-rule { on-xdg-activate "ignore"; } + window-rule { on-xdg-activate "set-urgent"; } + window-rule { on-xdg-activate "focus"; } + "#, + ); + + assert_eq!( + parsed + .window_rules + .iter() + .map(|rule| rule.on_xdg_activate) + .collect::>(), + vec![ + Some(OnXdgActivate::Ignore), + Some(OnXdgActivate::SetUrgent), + Some(OnXdgActivate::Focus), + ] + ); + } + #[test] fn parse() { let parsed = do_parse( @@ -891,6 +915,7 @@ mod tests { default-window-height { fixed 500; } default-column-display "tabbed" default-floating-position x=100 y=-200 relative-to="bottom-left" + on-xdg-activate "ignore" focus-ring { off @@ -1800,7 +1825,9 @@ mod tests { open_focused: Some( true, ), - focus_on_xdg_activate: None, + on_xdg_activate: Some( + Ignore, + ), min_width: None, min_height: None, max_width: None, diff --git a/niri-config/src/window_rule.rs b/niri-config/src/window_rule.rs index f44a90ee..0aeae2cc 100644 --- a/niri-config/src/window_rule.rs +++ b/niri-config/src/window_rule.rs @@ -35,7 +35,7 @@ pub struct WindowRule { #[knuffel(child, unwrap(argument))] pub open_focused: Option, #[knuffel(child, unwrap(argument))] - pub focus_on_xdg_activate: Option, + pub on_xdg_activate: Option, // Rules applied dynamically. #[knuffel(child, unwrap(argument))] @@ -151,6 +151,13 @@ pub struct FloatingPosition { pub relative_to: RelativeTo, } +#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq, Eq)] +pub enum OnXdgActivate { + Ignore, + SetUrgent, + Focus, +} + #[derive(knuffel::DecodeScalar, Debug, Default, Clone, Copy, PartialEq, Eq)] pub enum RelativeTo { #[default] diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 73fcc2b8..bd2d9a2e 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -815,15 +815,27 @@ impl XdgActivationHandler for State { if token_data.timestamp.elapsed() < XDG_ACTIVATION_TOKEN_TIMEOUT { if let Some((mapped, _)) = self.niri.layout.find_window_and_output_mut(&surface) { let window = mapped.window.clone(); - if token_data.user_data.get::().is_some() - || mapped.rules().focus_on_xdg_activate == Some(false) - { - mapped.set_urgent(true); - self.niri.queue_redraw_all(); - } else { - self.niri.layout.activate_window(&window); - self.niri.layer_shell_on_demand_focus = None; - self.niri.queue_redraw_all(); + match mapped.rules().on_xdg_activate { + Some(niri_config::OnXdgActivate::Ignore) => {} + Some(niri_config::OnXdgActivate::SetUrgent) => { + mapped.set_urgent(true); + self.niri.queue_redraw_all(); + } + Some(niri_config::OnXdgActivate::Focus) => { + self.niri.layout.activate_window(&window); + self.niri.layer_shell_on_demand_focus = None; + self.niri.queue_redraw_all(); + } + None => { + if token_data.user_data.get::().is_some() { + mapped.set_urgent(true); + self.niri.queue_redraw_all(); + } else { + self.niri.layout.activate_window(&window); + self.niri.layer_shell_on_demand_focus = None; + self.niri.queue_redraw_all(); + } + } } } else if let Some(unmapped) = self.niri.unmapped_windows.get_mut(&surface) { unmapped.activation_token_data = Some(token_data); diff --git a/src/window/mod.rs b/src/window/mod.rs index a086c347..1134510e 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -1,7 +1,7 @@ use std::cmp::{max, min}; use niri_config::utils::MergeWith as _; -use niri_config::window_rule::{Match, WindowRule}; +use niri_config::window_rule::{Match, OnXdgActivate, WindowRule}; use niri_config::{ BackgroundEffect, BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize, ResolvedPopupsRules, ShadowRule, TabIndicatorRule, @@ -73,8 +73,8 @@ pub struct ResolvedWindowRules { /// Whether the window should open focused. pub open_focused: Option, - /// Whether the window should receive focus on xdg-activation requests. - pub focus_on_xdg_activate: Option, + /// What to do on xdg-activation requests. + pub on_xdg_activate: Option, /// Extra bound on the minimum window width. pub min_width: Option, @@ -260,8 +260,8 @@ impl ResolvedWindowRules { resolved.open_focused = Some(x); } - if let Some(x) = rule.focus_on_xdg_activate { - resolved.focus_on_xdg_activate = Some(x); + if let Some(x) = rule.on_xdg_activate { + resolved.on_xdg_activate = Some(x); } if let Some(x) = rule.min_width {