Add open-focused window rule
Browse files at this commitfc99724aba1c0043eda3ad1b9829da8ae802d310Parent: 88fbc624 changed files +68 -16
- niri-config/src/lib.rs +4 -0
- src/handlers/compositor.rs +26 -16
- src/window/mod.rs +8 -0
- wiki/Configuration:-Window-Rules.md +30 -0
niri-config/src/lib.rsmodified+4 -0
| @@ -985,6 +985,8 @@ pub struct WindowRule { | ||
| 985 | 985 | pub open_fullscreen: Option<bool>, |
| 986 | 986 | #[knuffel(child, unwrap(argument))] |
| 987 | 987 | pub open_floating: Option<bool>, |
| 988 | + #[knuffel(child, unwrap(argument))] | |
| 989 | + pub open_focused: Option<bool>, | |
| 988 | 990 | |
| 989 | 991 | // Rules applied dynamically. |
| 990 | 992 | #[knuffel(child, unwrap(argument))] |
| @@ -3142,6 +3144,7 @@ mod tests { | ||
| 3142 | 3144 | open-maximized true |
| 3143 | 3145 | open-fullscreen false |
| 3144 | 3146 | open-floating false |
| 3147 | + open-focused true | |
| 3145 | 3148 | |
| 3146 | 3149 | focus-ring { |
| 3147 | 3150 | off |
| @@ -3421,6 +3424,7 @@ mod tests { | ||
| 3421 | 3424 | open_maximized: Some(true), |
| 3422 | 3425 | open_fullscreen: Some(false), |
| 3423 | 3426 | open_floating: Some(false), |
| 3427 | + open_focused: Some(true), | |
| 3424 | 3428 | focus_ring: BorderRule { |
| 3425 | 3429 | off: true, |
| 3426 | 3430 | width: Some(FloatOrInt(3.)), |
src/handlers/compositor.rsmodified+26 -16
| @@ -126,6 +126,32 @@ impl CompositorHandler for State { | ||
| 126 | 126 | // moment, that is here. |
| 127 | 127 | let is_floating = rules.compute_open_floating(toplevel); |
| 128 | 128 | |
| 129 | + // Figure out if we should activate the window. | |
| 130 | + let activate = rules.open_focused.map(|focus| { | |
| 131 | + if focus { | |
| 132 | + ActivateWindow::Yes | |
| 133 | + } else { | |
| 134 | + ActivateWindow::No | |
| 135 | + } | |
| 136 | + }); | |
| 137 | + let activate = activate.unwrap_or_else(|| { | |
| 138 | + // Check the token timestamp again in case the window took a while between | |
| 139 | + // requesting activation and mapping. | |
| 140 | + let token = activation_token_data.filter(|token| { | |
| 141 | + token.timestamp.elapsed() < XDG_ACTIVATION_TOKEN_TIMEOUT | |
| 142 | + }); | |
| 143 | + if token.is_some() { | |
| 144 | + ActivateWindow::Yes | |
| 145 | + } else { | |
| 146 | + let config = self.niri.config.borrow(); | |
| 147 | + if config.debug.strict_new_window_focus_policy { | |
| 148 | + ActivateWindow::No | |
| 149 | + } else { | |
| 150 | + ActivateWindow::Smart | |
| 151 | + } | |
| 152 | + } | |
| 153 | + }); | |
| 154 | + | |
| 129 | 155 | let parent = toplevel |
| 130 | 156 | .parent() |
| 131 | 157 | .and_then(|parent| self.niri.layout.find_window_and_output(&parent)) |
| @@ -146,22 +172,6 @@ impl CompositorHandler for State { | ||
| 146 | 172 | let mapped = Mapped::new(window, rules, hook); |
| 147 | 173 | let window = mapped.window.clone(); |
| 148 | 174 | |
| 149 | - // Check the token timestamp again in case the window took a while between | |
| 150 | - // requesting activation and mapping. | |
| 151 | - let activate = match activation_token_data | |
| 152 | - .filter(|token| token.timestamp.elapsed() < XDG_ACTIVATION_TOKEN_TIMEOUT) | |
| 153 | - { | |
| 154 | - Some(_) => ActivateWindow::Yes, | |
| 155 | - None => { | |
| 156 | - let config = self.niri.config.borrow(); | |
| 157 | - if config.debug.strict_new_window_focus_policy { | |
| 158 | - ActivateWindow::No | |
| 159 | - } else { | |
| 160 | - ActivateWindow::Smart | |
| 161 | - } | |
| 162 | - } | |
| 163 | - }; | |
| 164 | - | |
| 165 | 175 | let target = if let Some(p) = &parent { |
| 166 | 176 | // Open dialogs next to their parent window. |
| 167 | 177 | AddWindowTarget::NextTo(p) |
src/window/mod.rsmodified+8 -0
| @@ -49,6 +49,9 @@ pub struct ResolvedWindowRules { | ||
| 49 | 49 | /// Whether the window should open floating. |
| 50 | 50 | pub open_floating: Option<bool>, |
| 51 | 51 | |
| 52 | + /// Whether the window should open focused. | |
| 53 | + pub open_focused: Option<bool>, | |
| 54 | + | |
| 52 | 55 | /// Extra bound on the minimum window width. |
| 53 | 56 | pub min_width: Option<u16>, |
| 54 | 57 | /// Extra bound on the minimum window height. |
| @@ -116,6 +119,7 @@ impl ResolvedWindowRules { | ||
| 116 | 119 | open_maximized: None, |
| 117 | 120 | open_fullscreen: None, |
| 118 | 121 | open_floating: None, |
| 122 | + open_focused: None, | |
| 119 | 123 | min_width: None, |
| 120 | 124 | min_height: None, |
| 121 | 125 | max_width: None, |
| @@ -208,6 +212,10 @@ impl ResolvedWindowRules { | ||
| 208 | 212 | resolved.open_floating = Some(x); |
| 209 | 213 | } |
| 210 | 214 | |
| 215 | + if let Some(x) = rule.open_focused { | |
| 216 | + resolved.open_focused = Some(x); | |
| 217 | + } | |
| 218 | + | |
| 211 | 219 | if let Some(x) = rule.min_width { |
| 212 | 220 | resolved.min_width = Some(x); |
| 213 | 221 | } |
wiki/Configuration:-Window-Rules.mdmodified+30 -0
| @@ -42,6 +42,7 @@ window-rule { | ||
| 42 | 42 | open-maximized true |
| 43 | 43 | open-fullscreen true |
| 44 | 44 | open-floating true |
| 45 | + open-focused false | |
| 45 | 46 | |
| 46 | 47 | // Properties that apply continuously. |
| 47 | 48 | draw-border-with-background false |
| @@ -338,6 +339,35 @@ window-rule { | ||
| 338 | 339 | } |
| 339 | 340 | ``` |
| 340 | 341 | |
| 342 | +#### `open-focused` | |
| 343 | + | |
| 344 | +<sup>Since: next release</sup> | |
| 345 | + | |
| 346 | +Set this to `false` to prevent this window from being automatically focused upon opening. | |
| 347 | + | |
| 348 | +```kdl | |
| 349 | +// Don't give focus to the GIMP startup splash screen. | |
| 350 | +window-rule { | |
| 351 | + match app-id="^gimp" title="^GIMP Startup$" | |
| 352 | + | |
| 353 | + open-focused false | |
| 354 | +} | |
| 355 | +``` | |
| 356 | + | |
| 357 | +You can also set this to `true` to focus the window, even if normally it wouldn't get auto-focused. | |
| 358 | + | |
| 359 | +```kdl | |
| 360 | +// Always focus the KeePassXC-Browser unlock dialog. | |
| 361 | +// | |
| 362 | +// This dialog opens parented to the KeePassXC window rather than the browser, | |
| 363 | +// so it doesn't get auto-focused by default. | |
| 364 | +window-rule { | |
| 365 | + match app-id=r#"^org\.keepassxc\.KeePassXC$"# title="^Unlock Database - KeePassXC$" | |
| 366 | + | |
| 367 | + open-focused true | |
| 368 | +} | |
| 369 | +``` | |
| 370 | + | |
| 341 | 371 | ### Dynamic Properties |
| 342 | 372 | |
| 343 | 373 | These properties apply continuously to open windows. |