forge
Explore
Log in

Don't you dare go Hollow

Add on-xdg-activate window rule

Browse files at this commit

e9b215fef4b11ad36776553fb8bd45118ef03b03Artem TimofeevParent: 6062844
  • feat
    • add on-xdg-activate window rule
Show full message
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.mdmodified+1 -1
@@ -284,7 +284,7 @@ Most of the time, these fresh tokens will have invalid serials, because the app
284 By default, niri ignores xdg-activation tokens with invalid serials, to prevent windows from randomly stealing focus.284 By default, niri ignores xdg-activation tokens with invalid serials, to prevent windows from randomly stealing focus.
285 This debug flag makes niri honor such tokens, making the aforementioned widely-used apps get focus when clicking on their tray icon or notification.285 This debug flag makes niri honor such tokens, making the aforementioned widely-used apps get focus when clicking on their tray icon or notification.
286 286
287-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.287+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.
288 288
289 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.289 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.
290 Maybe in the future these apps/toolkits (Electron, Qt) are fixed, making this debug flag unnecessary.290 Maybe in the future these apps/toolkits (Electron, Qt) are fixed, making this debug flag unnecessary.
docs/wiki/Configuration:-Window-Rules.mdmodified+16 -6
@@ -58,7 +58,7 @@ window-rule {
58 default-column-display "tabbed"58 default-column-display "tabbed"
59 default-floating-position x=100 y=200 relative-to="bottom-left"59 default-floating-position x=100 y=200 relative-to="bottom-left"
60 scroll-factor 0.7560 scroll-factor 0.75
61- focus-on-xdg-activate true61+ on-xdg-activate "focus"
62 62
63 focus-ring {63 focus-ring {
64 // off64 // off
@@ -595,14 +595,24 @@ window-rule {
595 > 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.595 > 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.
596 > There's no robust way for Firefox to synchronize visibly showing a different tab and changing the window title.596 > There's no robust way for Firefox to synchronize visibly showing a different tab and changing the window title.
597 597
598-#### `focus-on-xdg-activate`598+#### `on-xdg-activate`
599 599
600 <sup>Since: next release</sup>600 <sup>Since: next release</sup>
601 601
602-Set this to `false` to prevent the window from receiving focus when it requests activation.602+Set what niri does when a window requests activation through [xdg-activation](https://wayland.app/protocols/xdg-activation-v1).
603-This is useful for apps that steal focus on incoming messages or when opening popup windows like Picture-in-Picture.603+
604+Values:
605+
606+- `"ignore"`: do nothing: neither focus the window nor mark it urgent.
607+- `"set-urgent"`: always mark the window urgent instead of focusing it.
608+- `"focus"`: always focus the window, even for activation requests without a serial.
604 609
605-When set to `false`, the window will instead be marked as urgent.610+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.
611+Requests with a valid serial focus the target window, and requests without a serial mark it urgent.
612+
613+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.
614+
615+This is useful for apps that steal focus on incoming messages or when opening popup windows like Picture-in-Picture.
606 616
607 ```kdl617 ```kdl
608 // Don't let Firefox PiP steal focus.618 // Don't let Firefox PiP steal focus.
@@ -610,7 +620,7 @@ window-rule {
610 match title="^Picture-in-Picture$"620 match title="^Picture-in-Picture$"
611 621
612 open-floating true622 open-floating true
613- focus-on-xdg-activate false623+ on-xdg-activate "set-urgent"
614 }624 }
615 ```625 ```
616 626
niri-config/src/lib.rsmodified+29 -2
@@ -60,7 +60,7 @@ pub use crate::recent_windows::{MruDirection, MruFilter, MruPreviews, MruScope,
60 pub use crate::utils::FloatOrInt;60 pub use crate::utils::FloatOrInt;
61 use crate::utils::{Flag, MergeWith as _};61 use crate::utils::{Flag, MergeWith as _};
62 pub use crate::window_rule::{62 pub use crate::window_rule::{
63- FloatingPosition, PopupsRule, RelativeTo, ResolvedPopupsRules, WindowRule,63+ FloatingPosition, OnXdgActivate, PopupsRule, RelativeTo, ResolvedPopupsRules, WindowRule,
64 };64 };
65 pub use crate::workspace::{Workspace, WorkspaceLayoutPart};65 pub use crate::workspace::{Workspace, WorkspaceLayoutPart};
66 66
@@ -655,6 +655,30 @@ mod tests {
655 .unwrap()655 .unwrap()
656 }656 }
657 657
658+ #[test]
659+ fn parse_on_xdg_activate() {
660+ let parsed = do_parse(
661+ r#"
662+ window-rule { on-xdg-activate "ignore"; }
663+ window-rule { on-xdg-activate "set-urgent"; }
664+ window-rule { on-xdg-activate "focus"; }
665+ "#,
666+ );
667+
668+ assert_eq!(
669+ parsed
670+ .window_rules
671+ .iter()
672+ .map(|rule| rule.on_xdg_activate)
673+ .collect::<Vec<_>>(),
674+ vec![
675+ Some(OnXdgActivate::Ignore),
676+ Some(OnXdgActivate::SetUrgent),
677+ Some(OnXdgActivate::Focus),
678+ ]
679+ );
680+ }
681+
658 #[test]682 #[test]
659 fn parse() {683 fn parse() {
660 let parsed = do_parse(684 let parsed = do_parse(
@@ -891,6 +915,7 @@ mod tests {
891 default-window-height { fixed 500; }915 default-window-height { fixed 500; }
892 default-column-display "tabbed"916 default-column-display "tabbed"
893 default-floating-position x=100 y=-200 relative-to="bottom-left"917 default-floating-position x=100 y=-200 relative-to="bottom-left"
918+ on-xdg-activate "ignore"
894 919
895 focus-ring {920 focus-ring {
896 off921 off
@@ -1800,7 +1825,9 @@ mod tests {
1800 open_focused: Some(1825 open_focused: Some(
1801 true,1826 true,
1802 ),1827 ),
1803- focus_on_xdg_activate: None,1828+ on_xdg_activate: Some(
1829+ Ignore,
1830+ ),
1804 min_width: None,1831 min_width: None,
1805 min_height: None,1832 min_height: None,
1806 max_width: None,1833 max_width: None,
niri-config/src/window_rule.rsmodified+8 -1
@@ -35,7 +35,7 @@ pub struct WindowRule {
35 #[knuffel(child, unwrap(argument))]35 #[knuffel(child, unwrap(argument))]
36 pub open_focused: Option<bool>,36 pub open_focused: Option<bool>,
37 #[knuffel(child, unwrap(argument))]37 #[knuffel(child, unwrap(argument))]
38- pub focus_on_xdg_activate: Option<bool>,38+ pub on_xdg_activate: Option<OnXdgActivate>,
39 39
40 // Rules applied dynamically.40 // Rules applied dynamically.
41 #[knuffel(child, unwrap(argument))]41 #[knuffel(child, unwrap(argument))]
@@ -151,6 +151,13 @@ pub struct FloatingPosition {
151 pub relative_to: RelativeTo,151 pub relative_to: RelativeTo,
152 }152 }
153 153
154+#[derive(knuffel::DecodeScalar, Debug, Clone, Copy, PartialEq, Eq)]
155+pub enum OnXdgActivate {
156+ Ignore,
157+ SetUrgent,
158+ Focus,
159+}
160+
154 #[derive(knuffel::DecodeScalar, Debug, Default, Clone, Copy, PartialEq, Eq)]161 #[derive(knuffel::DecodeScalar, Debug, Default, Clone, Copy, PartialEq, Eq)]
155 pub enum RelativeTo {162 pub enum RelativeTo {
156 #[default]163 #[default]
src/handlers/mod.rsmodified+21 -9
@@ -815,15 +815,27 @@ impl XdgActivationHandler for State {
815 if token_data.timestamp.elapsed() < XDG_ACTIVATION_TOKEN_TIMEOUT {815 if token_data.timestamp.elapsed() < XDG_ACTIVATION_TOKEN_TIMEOUT {
816 if let Some((mapped, _)) = self.niri.layout.find_window_and_output_mut(&surface) {816 if let Some((mapped, _)) = self.niri.layout.find_window_and_output_mut(&surface) {
817 let window = mapped.window.clone();817 let window = mapped.window.clone();
818- if token_data.user_data.get::<UrgentOnlyMarker>().is_some()818+ match mapped.rules().on_xdg_activate {
819- || mapped.rules().focus_on_xdg_activate == Some(false)819+ Some(niri_config::OnXdgActivate::Ignore) => {}
820- {820+ Some(niri_config::OnXdgActivate::SetUrgent) => {
821- mapped.set_urgent(true);821+ mapped.set_urgent(true);
822- self.niri.queue_redraw_all();822+ self.niri.queue_redraw_all();
823- } else {823+ }
824- self.niri.layout.activate_window(&window);824+ Some(niri_config::OnXdgActivate::Focus) => {
825- self.niri.layer_shell_on_demand_focus = None;825+ self.niri.layout.activate_window(&window);
826- self.niri.queue_redraw_all();826+ self.niri.layer_shell_on_demand_focus = None;
827+ self.niri.queue_redraw_all();
828+ }
829+ None => {
830+ if token_data.user_data.get::<UrgentOnlyMarker>().is_some() {
831+ mapped.set_urgent(true);
832+ self.niri.queue_redraw_all();
833+ } else {
834+ self.niri.layout.activate_window(&window);
835+ self.niri.layer_shell_on_demand_focus = None;
836+ self.niri.queue_redraw_all();
837+ }
838+ }
827 }839 }
828 } else if let Some(unmapped) = self.niri.unmapped_windows.get_mut(&surface) {840 } else if let Some(unmapped) = self.niri.unmapped_windows.get_mut(&surface) {
829 unmapped.activation_token_data = Some(token_data);841 unmapped.activation_token_data = Some(token_data);
src/window/mod.rsmodified+5 -5
@@ -1,7 +1,7 @@
1 use std::cmp::{max, min};1 use std::cmp::{max, min};
2 2
3 use niri_config::utils::MergeWith as _;3 use niri_config::utils::MergeWith as _;
4-use niri_config::window_rule::{Match, WindowRule};4+use niri_config::window_rule::{Match, OnXdgActivate, WindowRule};
5 use niri_config::{5 use niri_config::{
6 BackgroundEffect, BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize,6 BackgroundEffect, BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize,
7 ResolvedPopupsRules, ShadowRule, TabIndicatorRule,7 ResolvedPopupsRules, ShadowRule, TabIndicatorRule,
@@ -73,8 +73,8 @@ pub struct ResolvedWindowRules {
73 /// Whether the window should open focused.73 /// Whether the window should open focused.
74 pub open_focused: Option<bool>,74 pub open_focused: Option<bool>,
75 75
76- /// Whether the window should receive focus on xdg-activation requests.76+ /// What to do on xdg-activation requests.
77- pub focus_on_xdg_activate: Option<bool>,77+ pub on_xdg_activate: Option<OnXdgActivate>,
78 78
79 /// Extra bound on the minimum window width.79 /// Extra bound on the minimum window width.
80 pub min_width: Option<u16>,80 pub min_width: Option<u16>,
@@ -260,8 +260,8 @@ impl ResolvedWindowRules {
260 resolved.open_focused = Some(x);260 resolved.open_focused = Some(x);
261 }261 }
262 262
263- if let Some(x) = rule.focus_on_xdg_activate {263+ if let Some(x) = rule.on_xdg_activate {
264- resolved.focus_on_xdg_activate = Some(x);264+ resolved.on_xdg_activate = Some(x);
265 }265 }
266 266
267 if let Some(x) = rule.min_width {267 if let Some(x) = rule.min_width {