Add is-active window rule matcher
Browse files at this commitb7ed2fb82a19afe73e3e51ef2331ac6ad9c175a0Parent: f3f02ac9 changed files +126 -26
- niri-config/src/lib.rs +19 -5
- niri-visual-tests/src/test_window.rs +1 -1
- resources/default-config.kdl +4 -0
- src/handlers/xdg_shell.rs +2 -6
- src/layout/mod.rs +2 -2
- src/layout/workspace.rs +4 -4
- src/niri.rs +31 -3
- src/window/mapped.rs +43 -4
- src/window/mod.rs +20 -1
niri-config/src/lib.rsmodified+19 -5
| @@ -699,17 +699,21 @@ pub struct WindowRule { | ||
| 699 | 699 | pub draw_border_with_background: Option<bool>, |
| 700 | 700 | } |
| 701 | 701 | |
| 702 | +// Remember to update the PartialEq impl when adding fields! | |
| 702 | 703 | #[derive(knuffel::Decode, Debug, Default, Clone)] |
| 703 | 704 | pub struct Match { |
| 704 | 705 | #[knuffel(property, str)] |
| 705 | 706 | pub app_id: Option<Regex>, |
| 706 | 707 | #[knuffel(property, str)] |
| 707 | 708 | pub title: Option<Regex>, |
| 709 | + #[knuffel(property)] | |
| 710 | + pub is_active: Option<bool>, | |
| 708 | 711 | } |
| 709 | 712 | |
| 710 | 713 | impl PartialEq for Match { |
| 711 | 714 | fn eq(&self, other: &Self) -> bool { |
| 712 | - self.app_id.as_ref().map(Regex::as_str) == other.app_id.as_ref().map(Regex::as_str) | |
| 715 | + self.is_active == other.is_active | |
| 716 | + && self.app_id.as_ref().map(Regex::as_str) == other.app_id.as_ref().map(Regex::as_str) | |
| 713 | 717 | && self.title.as_ref().map(Regex::as_str) == other.title.as_ref().map(Regex::as_str) |
| 714 | 718 | } |
| 715 | 719 | } |
| @@ -1762,6 +1766,7 @@ mod tests { | ||
| 1762 | 1766 | window-rule { |
| 1763 | 1767 | match app-id=".*alacritty" |
| 1764 | 1768 | exclude title="~" |
| 1769 | + exclude is-active=true | |
| 1765 | 1770 | |
| 1766 | 1771 | open-on-output "eDP-1" |
| 1767 | 1772 | open-maximized true |
| @@ -1947,11 +1952,20 @@ mod tests { | ||
| 1947 | 1952 | matches: vec![Match { |
| 1948 | 1953 | app_id: Some(Regex::new(".*alacritty").unwrap()), |
| 1949 | 1954 | title: None, |
| 1955 | + is_active: None, | |
| 1950 | 1956 | }], |
| 1951 | - excludes: vec![Match { | |
| 1952 | - app_id: None, | |
| 1953 | - title: Some(Regex::new("~").unwrap()), | |
| 1954 | - }], | |
| 1957 | + excludes: vec![ | |
| 1958 | + Match { | |
| 1959 | + app_id: None, | |
| 1960 | + title: Some(Regex::new("~").unwrap()), | |
| 1961 | + is_active: None, | |
| 1962 | + }, | |
| 1963 | + Match { | |
| 1964 | + app_id: None, | |
| 1965 | + title: None, | |
| 1966 | + is_active: Some(true), | |
| 1967 | + }, | |
| 1968 | + ], | |
| 1955 | 1969 | open_on_output: Some("eDP-1".to_owned()), |
| 1956 | 1970 | open_maximized: Some(true), |
| 1957 | 1971 | open_fullscreen: Some(false), |
niri-visual-tests/src/test_window.rsmodified+1 -1
| @@ -203,7 +203,7 @@ impl LayoutElement for TestWindow { | ||
| 203 | 203 | |
| 204 | 204 | fn set_offscreen_element_id(&self, _id: Option<Id>) {} |
| 205 | 205 | |
| 206 | - fn set_activated(&self, _active: bool) {} | |
| 206 | + fn set_activated(&mut self, _active: bool) {} | |
| 207 | 207 | |
| 208 | 208 | fn set_bounds(&self, _bounds: Size<i32, Logical>) {} |
| 209 | 209 |
resources/default-config.kdlmodified+4 -0
| @@ -364,6 +364,10 @@ animations { | ||
| 364 | 364 | // Raw KDL strings are helpful here. |
| 365 | 365 | exclude app-id=r#"\.unwanted\."# |
| 366 | 366 | |
| 367 | + // One more way to match is by whether the window is active | |
| 368 | + // (same as when it uses the active border color). | |
| 369 | + match is-active=true | |
| 370 | + | |
| 367 | 371 | // Here are the properties that you can set on a window rule. |
| 368 | 372 | // These properties apply once, when a window first opens. |
| 369 | 373 |
src/handlers/xdg_shell.rsmodified+2 -6
| @@ -745,12 +745,8 @@ impl State { | ||
| 745 | 745 | .layout |
| 746 | 746 | .find_window_and_output_mut(toplevel.wl_surface()) |
| 747 | 747 | { |
| 748 | - let new_rules = ResolvedWindowRules::compute(window_rules, WindowRef::Mapped(mapped)); | |
| 749 | - drop(config); | |
| 750 | - | |
| 751 | - if mapped.rules != new_rules { | |
| 752 | - mapped.rules = new_rules; | |
| 753 | - | |
| 748 | + if mapped.recompute_window_rules(window_rules) { | |
| 749 | + drop(config); | |
| 754 | 750 | let output = output.cloned(); |
| 755 | 751 | let window = mapped.window.clone(); |
| 756 | 752 | self.niri.layout.update_window(&window); |
src/layout/mod.rsmodified+2 -2
| @@ -107,7 +107,7 @@ pub trait LayoutElement { | ||
| 107 | 107 | fn output_enter(&self, output: &Output); |
| 108 | 108 | fn output_leave(&self, output: &Output); |
| 109 | 109 | fn set_offscreen_element_id(&self, id: Option<Id>); |
| 110 | - fn set_activated(&self, active: bool); | |
| 110 | + fn set_activated(&mut self, active: bool); | |
| 111 | 111 | fn set_bounds(&self, bounds: Size<i32, Logical>); |
| 112 | 112 | |
| 113 | 113 | fn send_pending_configure(&self); |
| @@ -1893,7 +1893,7 @@ mod tests { | ||
| 1893 | 1893 | |
| 1894 | 1894 | fn set_offscreen_element_id(&self, _id: Option<Id>) {} |
| 1895 | 1895 | |
| 1896 | - fn set_activated(&self, _active: bool) {} | |
| 1896 | + fn set_activated(&mut self, _active: bool) {} | |
| 1897 | 1897 | |
| 1898 | 1898 | fn set_bounds(&self, _bounds: Size<i32, Logical>) {} |
| 1899 | 1899 |
src/layout/workspace.rsmodified+4 -4
| @@ -1550,12 +1550,12 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 1550 | 1550 | true |
| 1551 | 1551 | } |
| 1552 | 1552 | |
| 1553 | - pub fn refresh(&self, is_active: bool) { | |
| 1553 | + pub fn refresh(&mut self, is_active: bool) { | |
| 1554 | 1554 | let bounds = self.toplevel_bounds(); |
| 1555 | 1555 | |
| 1556 | - for (col_idx, col) in self.columns.iter().enumerate() { | |
| 1557 | - for (tile_idx, tile) in col.tiles.iter().enumerate() { | |
| 1558 | - let win = tile.window(); | |
| 1556 | + for (col_idx, col) in self.columns.iter_mut().enumerate() { | |
| 1557 | + for (tile_idx, tile) in col.tiles.iter_mut().enumerate() { | |
| 1558 | + let win = tile.window_mut(); | |
| 1559 | 1559 | let active = is_active |
| 1560 | 1560 | && self.active_column_idx == col_idx |
| 1561 | 1561 | && col.active_tile_idx == tile_idx; |
src/niri.rsmodified+31 -3
| @@ -438,6 +438,7 @@ impl State { | ||
| 438 | 438 | self.update_keyboard_focus(); |
| 439 | 439 | self.refresh_pointer_focus(); |
| 440 | 440 | foreign_toplevel::refresh(self); |
| 441 | + self.niri.refresh_window_rules(); | |
| 441 | 442 | self.niri.redraw_queued_outputs(&mut self.backend); |
| 442 | 443 | |
| 443 | 444 | { |
| @@ -911,9 +912,9 @@ impl State { | ||
| 911 | 912 | |
| 912 | 913 | let mut windows = vec![]; |
| 913 | 914 | self.niri.layout.with_windows_mut(|mapped, _| { |
| 914 | - mapped.rules = | |
| 915 | - ResolvedWindowRules::compute(window_rules, WindowRef::Mapped(mapped)); | |
| 916 | - windows.push(mapped.window.clone()); | |
| 915 | + if mapped.recompute_window_rules(window_rules) { | |
| 916 | + windows.push(mapped.window.clone()); | |
| 917 | + } | |
| 917 | 918 | }); |
| 918 | 919 | for win in windows { |
| 919 | 920 | self.niri.layout.update_window(&win); |
| @@ -2149,6 +2150,33 @@ impl Niri { | ||
| 2149 | 2150 | self.idle_notifier_state.set_is_inhibited(is_inhibited); |
| 2150 | 2151 | } |
| 2151 | 2152 | |
| 2153 | + pub fn refresh_window_rules(&mut self) { | |
| 2154 | + let _span = tracy_client::span!("Niri::refresh_window_rules"); | |
| 2155 | + | |
| 2156 | + let config = self.config.borrow(); | |
| 2157 | + let window_rules = &config.window_rules; | |
| 2158 | + | |
| 2159 | + let mut windows = vec![]; | |
| 2160 | + let mut outputs = HashSet::new(); | |
| 2161 | + self.layout.with_windows_mut(|mapped, output| { | |
| 2162 | + if mapped.recompute_window_rules_if_needed(window_rules) { | |
| 2163 | + windows.push(mapped.window.clone()); | |
| 2164 | + | |
| 2165 | + if let Some(output) = output { | |
| 2166 | + outputs.insert(output.clone()); | |
| 2167 | + } | |
| 2168 | + } | |
| 2169 | + }); | |
| 2170 | + drop(config); | |
| 2171 | + | |
| 2172 | + for win in windows { | |
| 2173 | + self.layout.update_window(&win); | |
| 2174 | + } | |
| 2175 | + for output in outputs { | |
| 2176 | + self.queue_redraw(&output); | |
| 2177 | + } | |
| 2178 | + } | |
| 2179 | + | |
| 2152 | 2180 | pub fn render<R: NiriRenderer>( |
| 2153 | 2181 | &self, |
| 2154 | 2182 | renderer: &mut R, |
src/window/mapped.rsmodified+43 -4
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | use std::cmp::{max, min}; |
| 2 | 2 | |
| 3 | +use niri_config::WindowRule; | |
| 3 | 4 | use smithay::backend::renderer::element::{AsRenderElements as _, Id}; |
| 4 | 5 | use smithay::desktop::space::SpaceElement as _; |
| 5 | 6 | use smithay::desktop::Window; |
| @@ -11,7 +12,7 @@ use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform}; | ||
| 11 | 12 | use smithay::wayland::compositor::{send_surface_state, with_states}; |
| 12 | 13 | use smithay::wayland::shell::xdg::{SurfaceCachedState, ToplevelSurface}; |
| 13 | 14 | |
| 14 | -use super::ResolvedWindowRules; | |
| 15 | +use super::{ResolvedWindowRules, WindowRef}; | |
| 15 | 16 | use crate::layout::{LayoutElement, LayoutElementRenderElement}; |
| 16 | 17 | use crate::niri::WindowOffscreenId; |
| 17 | 18 | use crate::render_helpers::renderer::NiriRenderer; |
| @@ -22,16 +23,47 @@ pub struct Mapped { | ||
| 22 | 23 | |
| 23 | 24 | /// Up-to-date rules. |
| 24 | 25 | pub rules: ResolvedWindowRules, |
| 26 | + | |
| 27 | + /// Whether the window rules need to be recomputed. | |
| 28 | + /// | |
| 29 | + /// This is not used in all cases; for example, app ID and title changes recompute the rules | |
| 30 | + /// immediately, rather than setting this flag. | |
| 31 | + pub need_to_recompute_rules: bool, | |
| 25 | 32 | } |
| 26 | 33 | |
| 27 | 34 | impl Mapped { |
| 28 | 35 | pub fn new(window: Window, rules: ResolvedWindowRules) -> Self { |
| 29 | - Self { window, rules } | |
| 36 | + Self { | |
| 37 | + window, | |
| 38 | + rules, | |
| 39 | + need_to_recompute_rules: false, | |
| 40 | + } | |
| 30 | 41 | } |
| 31 | 42 | |
| 32 | 43 | pub fn toplevel(&self) -> &ToplevelSurface { |
| 33 | 44 | self.window.toplevel().expect("no X11 support") |
| 34 | 45 | } |
| 46 | + | |
| 47 | + /// Recomputes the resolved window rules and returns whether they changed. | |
| 48 | + pub fn recompute_window_rules(&mut self, rules: &[WindowRule]) -> bool { | |
| 49 | + self.need_to_recompute_rules = false; | |
| 50 | + | |
| 51 | + let new_rules = ResolvedWindowRules::compute(rules, WindowRef::Mapped(self)); | |
| 52 | + if new_rules == self.rules { | |
| 53 | + return false; | |
| 54 | + } | |
| 55 | + | |
| 56 | + self.rules = new_rules; | |
| 57 | + true | |
| 58 | + } | |
| 59 | + | |
| 60 | + pub fn recompute_window_rules_if_needed(&mut self, rules: &[WindowRule]) -> bool { | |
| 61 | + if !self.need_to_recompute_rules { | |
| 62 | + return false; | |
| 63 | + } | |
| 64 | + | |
| 65 | + self.recompute_window_rules(rules) | |
| 66 | + } | |
| 35 | 67 | } |
| 36 | 68 | |
| 37 | 69 | impl LayoutElement for Mapped { |
| @@ -155,8 +187,15 @@ impl LayoutElement for Mapped { | ||
| 155 | 187 | data.0.replace(id); |
| 156 | 188 | } |
| 157 | 189 | |
| 158 | - fn set_activated(&self, active: bool) { | |
| 159 | - self.window.set_activated(active); | |
| 190 | + fn set_activated(&mut self, active: bool) { | |
| 191 | + let changed = self.toplevel().with_pending_state(|state| { | |
| 192 | + if active { | |
| 193 | + state.states.set(xdg_toplevel::State::Activated) | |
| 194 | + } else { | |
| 195 | + state.states.unset(xdg_toplevel::State::Activated) | |
| 196 | + } | |
| 197 | + }); | |
| 198 | + self.need_to_recompute_rules |= changed; | |
| 160 | 199 | } |
| 161 | 200 | |
| 162 | 201 | fn set_bounds(&self, bounds: Size<i32, Logical>) { |
src/window/mod.rsmodified+20 -1
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | use niri_config::{Match, WindowRule}; |
| 2 | +use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | |
| 2 | 3 | use smithay::wayland::compositor::with_states; |
| 3 | 4 | use smithay::wayland::shell::xdg::{ |
| 4 | 5 | ToplevelSurface, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes, |
| @@ -84,13 +85,18 @@ impl ResolvedWindowRules { | ||
| 84 | 85 | |
| 85 | 86 | let toplevel = window.toplevel(); |
| 86 | 87 | with_states(toplevel.wl_surface(), |states| { |
| 87 | - let role = states | |
| 88 | + let mut role = states | |
| 88 | 89 | .data_map |
| 89 | 90 | .get::<XdgToplevelSurfaceData>() |
| 90 | 91 | .unwrap() |
| 91 | 92 | .lock() |
| 92 | 93 | .unwrap(); |
| 93 | 94 | |
| 95 | + // Ensure server_pending like in Smithay's with_pending_state(). | |
| 96 | + if role.server_pending.is_none() { | |
| 97 | + role.server_pending = Some(role.current_server_state().clone()); | |
| 98 | + } | |
| 99 | + | |
| 94 | 100 | let mut open_on_output = None; |
| 95 | 101 | |
| 96 | 102 | for rule in rules { |
| @@ -150,6 +156,19 @@ impl ResolvedWindowRules { | ||
| 150 | 156 | } |
| 151 | 157 | |
| 152 | 158 | fn window_matches(role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool { |
| 159 | + // Must be ensured by the caller. | |
| 160 | + let server_pending = role.server_pending.as_ref().unwrap(); | |
| 161 | + | |
| 162 | + if let Some(is_active) = m.is_active { | |
| 163 | + // Our "is-active" definition corresponds to the window having a pending Activated state. | |
| 164 | + let pending_activated = server_pending | |
| 165 | + .states | |
| 166 | + .contains(xdg_toplevel::State::Activated); | |
| 167 | + if is_active != pending_activated { | |
| 168 | + return false; | |
| 169 | + } | |
| 170 | + } | |
| 171 | + | |
| 153 | 172 | if let Some(app_id_re) = &m.app_id { |
| 154 | 173 | let Some(app_id) = &role.app_id else { |
| 155 | 174 | return false; |