diff --git a/docs/wiki/Configuration:-Debug-Options.md b/docs/wiki/Configuration:-Debug-Options.md
index 9825359b..b2fea34f 100644
--- a/docs/wiki/Configuration:-Debug-Options.md
+++ b/docs/wiki/Configuration:-Debug-Options.md
@@ -284,6 +284,8 @@ 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.
+
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 f8fa215e..32f6b038 100644
--- a/docs/wiki/Configuration:-Window-Rules.md
+++ b/docs/wiki/Configuration:-Window-Rules.md
@@ -58,6 +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
focus-ring {
// off
@@ -594,6 +595,25 @@ 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`
+
+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.
+
+When set to `false`, the window will instead be marked as urgent.
+
+```kdl
+// Don't let Firefox PiP steal focus.
+window-rule {
+ match title="^Picture-in-Picture$"
+
+ open-floating true
+ focus-on-xdg-activate false
+}
+```
+
#### `opacity`
Set the opacity of the window.
diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs
index d983a7fc..a7ee1dcd 100644
--- a/niri-config/src/lib.rs
+++ b/niri-config/src/lib.rs
@@ -1800,6 +1800,7 @@ mod tests {
open_focused: Some(
true,
),
+ focus_on_xdg_activate: None,
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 f2bc2ad1..f44a90ee 100644
--- a/niri-config/src/window_rule.rs
+++ b/niri-config/src/window_rule.rs
@@ -34,6 +34,8 @@ pub struct WindowRule {
pub open_floating: Option,
#[knuffel(child, unwrap(argument))]
pub open_focused: Option,
+ #[knuffel(child, unwrap(argument))]
+ pub focus_on_xdg_activate: Option,
// Rules applied dynamically.
#[knuffel(child, unwrap(argument))]
diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs
index cbacbe9c..73fcc2b8 100644
--- a/src/handlers/mod.rs
+++ b/src/handlers/mod.rs
@@ -67,7 +67,7 @@ use smithay::wayland::xdg_activation::{
pub use crate::handlers::xdg_shell::KdeDecorationsModeState;
use crate::input::click_grab::ClickGrab;
use crate::layout::workspace::WorkspaceId;
-use crate::layout::ActivateWindow;
+use crate::layout::{ActivateWindow, LayoutElement};
use crate::niri::{DndIcon, NewClient, State};
use crate::protocols::ext_workspace::{self, ExtWorkspaceHandler, ExtWorkspaceManagerState};
use crate::protocols::foreign_toplevel::{
@@ -815,7 +815,9 @@ 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() {
+ 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 {
diff --git a/src/window/mod.rs b/src/window/mod.rs
index 14527cd2..a086c347 100644
--- a/src/window/mod.rs
+++ b/src/window/mod.rs
@@ -73,6 +73,9 @@ 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,
+
/// Extra bound on the minimum window width.
pub min_width: Option,
/// Extra bound on the minimum window height.
@@ -257,6 +260,10 @@ 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.min_width {
resolved.min_width = Some(x);
}