forge
Explore
Log in

Don't you dare go Hollow

Add open-focused window rule

Browse files at this commit

fc99724aba1c0043eda3ad1b9829da8ae802d310Ivan MolodetskikhParent: 88fbc62
niri-config/src/lib.rsmodified+4 -0
@@ -985,6 +985,8 @@ pub struct WindowRule {
985985 pub open_fullscreen: Option<bool>,
986986 #[knuffel(child, unwrap(argument))]
987987 pub open_floating: Option<bool>,
988+ #[knuffel(child, unwrap(argument))]
989+ pub open_focused: Option<bool>,
988990
989991 // Rules applied dynamically.
990992 #[knuffel(child, unwrap(argument))]
@@ -3142,6 +3144,7 @@ mod tests {
31423144 open-maximized true
31433145 open-fullscreen false
31443146 open-floating false
3147+ open-focused true
31453148
31463149 focus-ring {
31473150 off
@@ -3421,6 +3424,7 @@ mod tests {
34213424 open_maximized: Some(true),
34223425 open_fullscreen: Some(false),
34233426 open_floating: Some(false),
3427+ open_focused: Some(true),
34243428 focus_ring: BorderRule {
34253429 off: true,
34263430 width: Some(FloatOrInt(3.)),
src/handlers/compositor.rsmodified+26 -16
@@ -126,6 +126,32 @@ impl CompositorHandler for State {
126126 // moment, that is here.
127127 let is_floating = rules.compute_open_floating(toplevel);
128128
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+
129155 let parent = toplevel
130156 .parent()
131157 .and_then(|parent| self.niri.layout.find_window_and_output(&parent))
@@ -146,22 +172,6 @@ impl CompositorHandler for State {
146172 let mapped = Mapped::new(window, rules, hook);
147173 let window = mapped.window.clone();
148174
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-
165175 let target = if let Some(p) = &parent {
166176 // Open dialogs next to their parent window.
167177 AddWindowTarget::NextTo(p)
src/window/mod.rsmodified+8 -0
@@ -49,6 +49,9 @@ pub struct ResolvedWindowRules {
4949 /// Whether the window should open floating.
5050 pub open_floating: Option<bool>,
5151
52+ /// Whether the window should open focused.
53+ pub open_focused: Option<bool>,
54+
5255 /// Extra bound on the minimum window width.
5356 pub min_width: Option<u16>,
5457 /// Extra bound on the minimum window height.
@@ -116,6 +119,7 @@ impl ResolvedWindowRules {
116119 open_maximized: None,
117120 open_fullscreen: None,
118121 open_floating: None,
122+ open_focused: None,
119123 min_width: None,
120124 min_height: None,
121125 max_width: None,
@@ -208,6 +212,10 @@ impl ResolvedWindowRules {
208212 resolved.open_floating = Some(x);
209213 }
210214
215+ if let Some(x) = rule.open_focused {
216+ resolved.open_focused = Some(x);
217+ }
218+
211219 if let Some(x) = rule.min_width {
212220 resolved.min_width = Some(x);
213221 }
wiki/Configuration:-Window-Rules.mdmodified+30 -0
@@ -42,6 +42,7 @@ window-rule {
4242 open-maximized true
4343 open-fullscreen true
4444 open-floating true
45+ open-focused false
4546
4647 // Properties that apply continuously.
4748 draw-border-with-background false
@@ -338,6 +339,35 @@ window-rule {
338339 }
339340 ```
340341
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+
341371 ### Dynamic Properties
342372
343373 These properties apply continuously to open windows.