Implement at-startup window rule
Browse files at this commitcbfc682f9abc9b4c02ea3c0f63a4403d037f14a4Parent: c64d9e56 changed files +107 -33
- niri-config/src/lib.rs +6 -0
- src/handlers/xdg_shell.rs +11 -5
- src/niri.rs +54 -22
- src/window/mapped.rs +8 -4
- src/window/mod.rs +10 -2
- wiki/Configuration:-Window-Rules.md +18 -0
niri-config/src/lib.rsmodified+6 -0
| @@ -764,6 +764,8 @@ pub struct Match { | |||
| 764 | pub is_focused: Option<bool>, | 764 | pub is_focused: Option<bool>, |
| 765 | #[knuffel(property)] | 765 | #[knuffel(property)] |
| 766 | pub is_active_in_column: Option<bool>, | 766 | pub is_active_in_column: Option<bool>, |
| 767 | + #[knuffel(property)] | ||
| 768 | + pub at_startup: Option<bool>, | ||
| 767 | } | 769 | } |
| 768 | 770 | ||
| 769 | impl PartialEq for Match { | 771 | impl PartialEq for Match { |
| @@ -771,6 +773,7 @@ impl PartialEq for Match { | |||
| 771 | self.is_active == other.is_active | 773 | self.is_active == other.is_active |
| 772 | && self.is_focused == other.is_focused | 774 | && self.is_focused == other.is_focused |
| 773 | && self.is_active_in_column == other.is_active_in_column | 775 | && self.is_active_in_column == other.is_active_in_column |
| 776 | + && self.at_startup == other.at_startup | ||
| 774 | && self.app_id.as_ref().map(Regex::as_str) == other.app_id.as_ref().map(Regex::as_str) | 777 | && self.app_id.as_ref().map(Regex::as_str) == other.app_id.as_ref().map(Regex::as_str) |
| 775 | && self.title.as_ref().map(Regex::as_str) == other.title.as_ref().map(Regex::as_str) | 778 | && self.title.as_ref().map(Regex::as_str) == other.title.as_ref().map(Regex::as_str) |
| 776 | } | 779 | } |
| @@ -2590,6 +2593,7 @@ mod tests { | |||
| 2590 | is_active: None, | 2593 | is_active: None, |
| 2591 | is_focused: None, | 2594 | is_focused: None, |
| 2592 | is_active_in_column: None, | 2595 | is_active_in_column: None, |
| 2596 | + at_startup: None, | ||
| 2593 | }], | 2597 | }], |
| 2594 | excludes: vec![ | 2598 | excludes: vec![ |
| 2595 | Match { | 2599 | Match { |
| @@ -2598,6 +2602,7 @@ mod tests { | |||
| 2598 | is_active: None, | 2602 | is_active: None, |
| 2599 | is_focused: None, | 2603 | is_focused: None, |
| 2600 | is_active_in_column: None, | 2604 | is_active_in_column: None, |
| 2605 | + at_startup: None, | ||
| 2601 | }, | 2606 | }, |
| 2602 | Match { | 2607 | Match { |
| 2603 | app_id: None, | 2608 | app_id: None, |
| @@ -2605,6 +2610,7 @@ mod tests { | |||
| 2605 | is_active: Some(true), | 2610 | is_active: Some(true), |
| 2606 | is_focused: Some(false), | 2611 | is_focused: Some(false), |
| 2607 | is_active_in_column: None, | 2612 | is_active_in_column: None, |
| 2613 | + at_startup: None, | ||
| 2608 | }, | 2614 | }, |
| 2609 | ], | 2615 | ], |
| 2610 | open_on_output: Some("eDP-1".to_owned()), | 2616 | open_on_output: Some("eDP-1".to_owned()), |
src/handlers/xdg_shell.rsmodified+11 -5
| @@ -581,8 +581,11 @@ impl State { | |||
| 581 | }; | 581 | }; |
| 582 | 582 | ||
| 583 | let config = self.niri.config.borrow(); | 583 | let config = self.niri.config.borrow(); |
| 584 | - let rules = | 584 | + let rules = ResolvedWindowRules::compute( |
| 585 | - ResolvedWindowRules::compute(&config.window_rules, WindowRef::Unmapped(unmapped)); | 585 | + &config.window_rules, |
| 586 | + WindowRef::Unmapped(unmapped), | ||
| 587 | + self.niri.is_at_startup, | ||
| 588 | + ); | ||
| 586 | 589 | ||
| 587 | let Unmapped { window, state } = unmapped; | 590 | let Unmapped { window, state } = unmapped; |
| 588 | 591 | ||
| @@ -876,8 +879,11 @@ impl State { | |||
| 876 | let window_rules = &config.window_rules; | 879 | let window_rules = &config.window_rules; |
| 877 | 880 | ||
| 878 | if let Some(unmapped) = self.niri.unmapped_windows.get_mut(toplevel.wl_surface()) { | 881 | if let Some(unmapped) = self.niri.unmapped_windows.get_mut(toplevel.wl_surface()) { |
| 879 | - let new_rules = | 882 | + let new_rules = ResolvedWindowRules::compute( |
| 880 | - ResolvedWindowRules::compute(window_rules, WindowRef::Unmapped(unmapped)); | 883 | + window_rules, |
| 884 | + WindowRef::Unmapped(unmapped), | ||
| 885 | + self.niri.is_at_startup, | ||
| 886 | + ); | ||
| 881 | if let InitialConfigureState::Configured { rules, .. } = &mut unmapped.state { | 887 | if let InitialConfigureState::Configured { rules, .. } = &mut unmapped.state { |
| 882 | *rules = new_rules; | 888 | *rules = new_rules; |
| 883 | } | 889 | } |
| @@ -886,7 +892,7 @@ impl State { | |||
| 886 | .layout | 892 | .layout |
| 887 | .find_window_and_output_mut(toplevel.wl_surface()) | 893 | .find_window_and_output_mut(toplevel.wl_surface()) |
| 888 | { | 894 | { |
| 889 | - if mapped.recompute_window_rules(window_rules) { | 895 | + if mapped.recompute_window_rules(window_rules, self.niri.is_at_startup) { |
| 890 | drop(config); | 896 | drop(config); |
| 891 | let output = output.cloned(); | 897 | let output = output.cloned(); |
| 892 | let window = mapped.window.clone(); | 898 | let window = mapped.window.clone(); |
src/niri.rsmodified+54 -22
| @@ -156,6 +156,9 @@ pub struct Niri { | |||
| 156 | 156 | ||
| 157 | pub start_time: Instant, | 157 | pub start_time: Instant, |
| 158 | 158 | ||
| 159 | + /// Whether the at-startup=true window rules are active. | ||
| 160 | + pub is_at_startup: bool, | ||
| 161 | + | ||
| 159 | // Each workspace corresponds to a Space. Each workspace generally has one Output mapped to it, | 162 | // Each workspace corresponds to a Space. Each workspace generally has one Output mapped to it, |
| 160 | // however it may have none (when there are no outputs connected) or mutiple (when mirroring). | 163 | // however it may have none (when there are no outputs connected) or mutiple (when mirroring). |
| 161 | pub layout: Layout<Mapped>, | 164 | pub layout: Layout<Mapped>, |
| @@ -1014,27 +1017,7 @@ impl State { | |||
| 1014 | } | 1017 | } |
| 1015 | 1018 | ||
| 1016 | if window_rules_changed { | 1019 | if window_rules_changed { |
| 1017 | - let _span = tracy_client::span!("recompute window rules"); | 1020 | + self.niri.recompute_window_rules(); |
| 1018 | - | ||
| 1019 | - let window_rules = &self.niri.config.borrow().window_rules; | ||
| 1020 | - | ||
| 1021 | - for unmapped in self.niri.unmapped_windows.values_mut() { | ||
| 1022 | - let new_rules = | ||
| 1023 | - ResolvedWindowRules::compute(window_rules, WindowRef::Unmapped(unmapped)); | ||
| 1024 | - if let InitialConfigureState::Configured { rules, .. } = &mut unmapped.state { | ||
| 1025 | - *rules = new_rules; | ||
| 1026 | - } | ||
| 1027 | - } | ||
| 1028 | - | ||
| 1029 | - let mut windows = vec![]; | ||
| 1030 | - self.niri.layout.with_windows_mut(|mapped, _| { | ||
| 1031 | - if mapped.recompute_window_rules(window_rules) { | ||
| 1032 | - windows.push(mapped.window.clone()); | ||
| 1033 | - } | ||
| 1034 | - }); | ||
| 1035 | - for win in windows { | ||
| 1036 | - self.niri.layout.update_window(&win, None); | ||
| 1037 | - } | ||
| 1038 | } | 1021 | } |
| 1039 | 1022 | ||
| 1040 | if shaders_changed { | 1023 | if shaders_changed { |
| @@ -1472,6 +1455,18 @@ impl Niri { | |||
| 1472 | }) | 1455 | }) |
| 1473 | .unwrap(); | 1456 | .unwrap(); |
| 1474 | 1457 | ||
| 1458 | + event_loop | ||
| 1459 | + .insert_source( | ||
| 1460 | + Timer::from_duration(Duration::from_secs(60)), | ||
| 1461 | + |_, _, state| { | ||
| 1462 | + let _span = tracy_client::span!("startup timeout"); | ||
| 1463 | + state.niri.is_at_startup = false; | ||
| 1464 | + state.niri.recompute_window_rules(); | ||
| 1465 | + TimeoutAction::Drop | ||
| 1466 | + }, | ||
| 1467 | + ) | ||
| 1468 | + .unwrap(); | ||
| 1469 | + | ||
| 1475 | drop(config_); | 1470 | drop(config_); |
| 1476 | Self { | 1471 | Self { |
| 1477 | config, | 1472 | config, |
| @@ -1483,6 +1478,7 @@ impl Niri { | |||
| 1483 | socket_name, | 1478 | socket_name, |
| 1484 | display_handle, | 1479 | display_handle, |
| 1485 | start_time: Instant::now(), | 1480 | start_time: Instant::now(), |
| 1481 | + is_at_startup: true, | ||
| 1486 | 1482 | ||
| 1487 | layout, | 1483 | layout, |
| 1488 | global_space: Space::default(), | 1484 | global_space: Space::default(), |
| @@ -2474,7 +2470,7 @@ impl Niri { | |||
| 2474 | let mut windows = vec![]; | 2470 | let mut windows = vec![]; |
| 2475 | let mut outputs = HashSet::new(); | 2471 | let mut outputs = HashSet::new(); |
| 2476 | self.layout.with_windows_mut(|mapped, output| { | 2472 | self.layout.with_windows_mut(|mapped, output| { |
| 2477 | - if mapped.recompute_window_rules_if_needed(window_rules) { | 2473 | + if mapped.recompute_window_rules_if_needed(window_rules, self.is_at_startup) { |
| 2478 | windows.push(mapped.window.clone()); | 2474 | windows.push(mapped.window.clone()); |
| 2479 | 2475 | ||
| 2480 | if let Some(output) = output { | 2476 | if let Some(output) = output { |
| @@ -3901,6 +3897,42 @@ impl Niri { | |||
| 3901 | // We don't actually need to queue a redraw because the point is to freeze the screen for a | 3897 | // We don't actually need to queue a redraw because the point is to freeze the screen for a |
| 3902 | // bit, and even if the delay was zero, we're drawing the same contents anyway. | 3898 | // bit, and even if the delay was zero, we're drawing the same contents anyway. |
| 3903 | } | 3899 | } |
| 3900 | + | ||
| 3901 | + pub fn recompute_window_rules(&mut self) { | ||
| 3902 | + let _span = tracy_client::span!("Niri::recompute_window_rules"); | ||
| 3903 | + | ||
| 3904 | + let changed = { | ||
| 3905 | + let window_rules = &self.config.borrow().window_rules; | ||
| 3906 | + | ||
| 3907 | + for unmapped in self.unmapped_windows.values_mut() { | ||
| 3908 | + let new_rules = ResolvedWindowRules::compute( | ||
| 3909 | + window_rules, | ||
| 3910 | + WindowRef::Unmapped(unmapped), | ||
| 3911 | + self.is_at_startup, | ||
| 3912 | + ); | ||
| 3913 | + if let InitialConfigureState::Configured { rules, .. } = &mut unmapped.state { | ||
| 3914 | + *rules = new_rules; | ||
| 3915 | + } | ||
| 3916 | + } | ||
| 3917 | + | ||
| 3918 | + let mut windows = vec![]; | ||
| 3919 | + self.layout.with_windows_mut(|mapped, _| { | ||
| 3920 | + if mapped.recompute_window_rules(window_rules, self.is_at_startup) { | ||
| 3921 | + windows.push(mapped.window.clone()); | ||
| 3922 | + } | ||
| 3923 | + }); | ||
| 3924 | + let changed = !windows.is_empty(); | ||
| 3925 | + for win in windows { | ||
| 3926 | + self.layout.update_window(&win, None); | ||
| 3927 | + } | ||
| 3928 | + changed | ||
| 3929 | + }; | ||
| 3930 | + | ||
| 3931 | + if changed { | ||
| 3932 | + // FIXME: granular. | ||
| 3933 | + self.queue_redraw_all(); | ||
| 3934 | + } | ||
| 3935 | + } | ||
| 3904 | } | 3936 | } |
| 3905 | 3937 | ||
| 3906 | pub struct ClientState { | 3938 | pub struct ClientState { |
src/window/mapped.rsmodified+8 -4
| @@ -120,10 +120,10 @@ impl Mapped { | |||
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | /// Recomputes the resolved window rules and returns whether they changed. | 122 | /// Recomputes the resolved window rules and returns whether they changed. |
| 123 | - pub fn recompute_window_rules(&mut self, rules: &[WindowRule]) -> bool { | 123 | + pub fn recompute_window_rules(&mut self, rules: &[WindowRule], is_at_startup: bool) -> bool { |
| 124 | self.need_to_recompute_rules = false; | 124 | self.need_to_recompute_rules = false; |
| 125 | 125 | ||
| 126 | - let new_rules = ResolvedWindowRules::compute(rules, WindowRef::Mapped(self)); | 126 | + let new_rules = ResolvedWindowRules::compute(rules, WindowRef::Mapped(self), is_at_startup); |
| 127 | if new_rules == self.rules { | 127 | if new_rules == self.rules { |
| 128 | return false; | 128 | return false; |
| 129 | } | 129 | } |
| @@ -132,12 +132,16 @@ impl Mapped { | |||
| 132 | true | 132 | true |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | - pub fn recompute_window_rules_if_needed(&mut self, rules: &[WindowRule]) -> bool { | 135 | + pub fn recompute_window_rules_if_needed( |
| 136 | + &mut self, | ||
| 137 | + rules: &[WindowRule], | ||
| 138 | + is_at_startup: bool, | ||
| 139 | + ) -> bool { | ||
| 136 | if !self.need_to_recompute_rules { | 140 | if !self.need_to_recompute_rules { |
| 137 | return false; | 141 | return false; |
| 138 | } | 142 | } |
| 139 | 143 | ||
| 140 | - self.recompute_window_rules(rules) | 144 | + self.recompute_window_rules(rules, is_at_startup) |
| 141 | } | 145 | } |
| 142 | 146 | ||
| 143 | pub fn is_focused(&self) -> bool { | 147 | pub fn is_focused(&self) -> bool { |
src/window/mod.rsmodified+10 -2
| @@ -135,7 +135,7 @@ impl ResolvedWindowRules { | |||
| 135 | } | 135 | } |
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | - pub fn compute(rules: &[WindowRule], window: WindowRef) -> Self { | 138 | + pub fn compute(rules: &[WindowRule], window: WindowRef, is_at_startup: bool) -> Self { |
| 139 | let _span = tracy_client::span!("ResolvedWindowRules::compute"); | 139 | let _span = tracy_client::span!("ResolvedWindowRules::compute"); |
| 140 | 140 | ||
| 141 | let mut resolved = ResolvedWindowRules::empty(); | 141 | let mut resolved = ResolvedWindowRules::empty(); |
| @@ -158,7 +158,15 @@ impl ResolvedWindowRules { | |||
| 158 | let mut open_on_workspace = None; | 158 | let mut open_on_workspace = None; |
| 159 | 159 | ||
| 160 | for rule in rules { | 160 | for rule in rules { |
| 161 | - let matches = |m| window_matches(window, &role, m); | 161 | + let matches = |m: &Match| { |
| 162 | + if let Some(at_startup) = m.at_startup { | ||
| 163 | + if at_startup != is_at_startup { | ||
| 164 | + return false; | ||
| 165 | + } | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + window_matches(window, &role, m) | ||
| 169 | + }; | ||
| 162 | 170 | ||
| 163 | if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) { | 171 | if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) { |
| 164 | continue; | 172 | continue; |
wiki/Configuration:-Window-Rules.mdmodified+18 -0
| @@ -33,6 +33,7 @@ window-rule { | |||
| 33 | match is-active=true | 33 | match is-active=true |
| 34 | match is-focused=false | 34 | match is-focused=false |
| 35 | match is-active-in-column=true | 35 | match is-active-in-column=true |
| 36 | + match at-startup=true | ||
| 36 | 37 | ||
| 37 | // Properties that apply once upon window opening. | 38 | // Properties that apply once upon window opening. |
| 38 | default-column-width { proportion 0.75; } | 39 | default-column-width { proportion 0.75; } |
| @@ -192,6 +193,23 @@ window-rule { | |||
| 192 | } | 193 | } |
| 193 | ``` | 194 | ``` |
| 194 | 195 | ||
| 196 | +#### `at-startup` | ||
| 197 | + | ||
| 198 | +<sup>Since: 0.1.6</sup> | ||
| 199 | + | ||
| 200 | +Can be `true` or `false`. | ||
| 201 | +Matches during the first 60 seconds after starting niri. | ||
| 202 | + | ||
| 203 | +This is useful for properties like `open-on-output` which you may want to apply only right after starting niri. | ||
| 204 | + | ||
| 205 | +``` | ||
| 206 | +// Open windows on the HDMI-A-1 monitor at niri startup, but not afterwards. | ||
| 207 | +window-rule { | ||
| 208 | + match at-startup=true | ||
| 209 | + open-on-output "HDMI-A-1" | ||
| 210 | +} | ||
| 211 | +``` | ||
| 212 | + | ||
| 195 | ### Window Opening Properties | 213 | ### Window Opening Properties |
| 196 | 214 | ||
| 197 | These properties apply once, when a window first opens. | 215 | These properties apply once, when a window first opens. |