forge
Explore
Log in

Don't you dare go Hollow

Add is-active window rule matcher

Browse files at this commit

b7ed2fb82a19afe73e3e51ef2331ac6ad9c175a0Ivan MolodetskikhParent: f3f02ac
niri-config/src/lib.rsmodified+19 -5
@@ -699,17 +699,21 @@ pub struct WindowRule {
699699 pub draw_border_with_background: Option<bool>,
700700 }
701701
702+// Remember to update the PartialEq impl when adding fields!
702703 #[derive(knuffel::Decode, Debug, Default, Clone)]
703704 pub struct Match {
704705 #[knuffel(property, str)]
705706 pub app_id: Option<Regex>,
706707 #[knuffel(property, str)]
707708 pub title: Option<Regex>,
709+ #[knuffel(property)]
710+ pub is_active: Option<bool>,
708711 }
709712
710713 impl PartialEq for Match {
711714 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)
713717 && self.title.as_ref().map(Regex::as_str) == other.title.as_ref().map(Regex::as_str)
714718 }
715719 }
@@ -1762,6 +1766,7 @@ mod tests {
17621766 window-rule {
17631767 match app-id=".*alacritty"
17641768 exclude title="~"
1769+ exclude is-active=true
17651770
17661771 open-on-output "eDP-1"
17671772 open-maximized true
@@ -1947,11 +1952,20 @@ mod tests {
19471952 matches: vec![Match {
19481953 app_id: Some(Regex::new(".*alacritty").unwrap()),
19491954 title: None,
1955+ is_active: None,
19501956 }],
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+ ],
19551969 open_on_output: Some("eDP-1".to_owned()),
19561970 open_maximized: Some(true),
19571971 open_fullscreen: Some(false),
niri-visual-tests/src/test_window.rsmodified+1 -1
@@ -203,7 +203,7 @@ impl LayoutElement for TestWindow {
203203
204204 fn set_offscreen_element_id(&self, _id: Option<Id>) {}
205205
206- fn set_activated(&self, _active: bool) {}
206+ fn set_activated(&mut self, _active: bool) {}
207207
208208 fn set_bounds(&self, _bounds: Size<i32, Logical>) {}
209209
resources/default-config.kdlmodified+4 -0
@@ -364,6 +364,10 @@ animations {
364364 // Raw KDL strings are helpful here.
365365 exclude app-id=r#"\.unwanted\."#
366366
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+
367371 // Here are the properties that you can set on a window rule.
368372 // These properties apply once, when a window first opens.
369373
src/handlers/xdg_shell.rsmodified+2 -6
@@ -745,12 +745,8 @@ impl State {
745745 .layout
746746 .find_window_and_output_mut(toplevel.wl_surface())
747747 {
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);
754750 let output = output.cloned();
755751 let window = mapped.window.clone();
756752 self.niri.layout.update_window(&window);
src/layout/mod.rsmodified+2 -2
@@ -107,7 +107,7 @@ pub trait LayoutElement {
107107 fn output_enter(&self, output: &Output);
108108 fn output_leave(&self, output: &Output);
109109 fn set_offscreen_element_id(&self, id: Option<Id>);
110- fn set_activated(&self, active: bool);
110+ fn set_activated(&mut self, active: bool);
111111 fn set_bounds(&self, bounds: Size<i32, Logical>);
112112
113113 fn send_pending_configure(&self);
@@ -1893,7 +1893,7 @@ mod tests {
18931893
18941894 fn set_offscreen_element_id(&self, _id: Option<Id>) {}
18951895
1896- fn set_activated(&self, _active: bool) {}
1896+ fn set_activated(&mut self, _active: bool) {}
18971897
18981898 fn set_bounds(&self, _bounds: Size<i32, Logical>) {}
18991899
src/layout/workspace.rsmodified+4 -4
@@ -1550,12 +1550,12 @@ impl<W: LayoutElement> Workspace<W> {
15501550 true
15511551 }
15521552
1553- pub fn refresh(&self, is_active: bool) {
1553+ pub fn refresh(&mut self, is_active: bool) {
15541554 let bounds = self.toplevel_bounds();
15551555
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();
15591559 let active = is_active
15601560 && self.active_column_idx == col_idx
15611561 && col.active_tile_idx == tile_idx;
src/niri.rsmodified+31 -3
@@ -438,6 +438,7 @@ impl State {
438438 self.update_keyboard_focus();
439439 self.refresh_pointer_focus();
440440 foreign_toplevel::refresh(self);
441+ self.niri.refresh_window_rules();
441442 self.niri.redraw_queued_outputs(&mut self.backend);
442443
443444 {
@@ -911,9 +912,9 @@ impl State {
911912
912913 let mut windows = vec![];
913914 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+ }
917918 });
918919 for win in windows {
919920 self.niri.layout.update_window(&win);
@@ -2149,6 +2150,33 @@ impl Niri {
21492150 self.idle_notifier_state.set_is_inhibited(is_inhibited);
21502151 }
21512152
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+
21522180 pub fn render<R: NiriRenderer>(
21532181 &self,
21542182 renderer: &mut R,
src/window/mapped.rsmodified+43 -4
@@ -1,5 +1,6 @@
11 use std::cmp::{max, min};
22
3+use niri_config::WindowRule;
34 use smithay::backend::renderer::element::{AsRenderElements as _, Id};
45 use smithay::desktop::space::SpaceElement as _;
56 use smithay::desktop::Window;
@@ -11,7 +12,7 @@ use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};
1112 use smithay::wayland::compositor::{send_surface_state, with_states};
1213 use smithay::wayland::shell::xdg::{SurfaceCachedState, ToplevelSurface};
1314
14-use super::ResolvedWindowRules;
15+use super::{ResolvedWindowRules, WindowRef};
1516 use crate::layout::{LayoutElement, LayoutElementRenderElement};
1617 use crate::niri::WindowOffscreenId;
1718 use crate::render_helpers::renderer::NiriRenderer;
@@ -22,16 +23,47 @@ pub struct Mapped {
2223
2324 /// Up-to-date rules.
2425 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,
2532 }
2633
2734 impl Mapped {
2835 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+ }
3041 }
3142
3243 pub fn toplevel(&self) -> &ToplevelSurface {
3344 self.window.toplevel().expect("no X11 support")
3445 }
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+ }
3567 }
3668
3769 impl LayoutElement for Mapped {
@@ -155,8 +187,15 @@ impl LayoutElement for Mapped {
155187 data.0.replace(id);
156188 }
157189
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;
160199 }
161200
162201 fn set_bounds(&self, bounds: Size<i32, Logical>) {
src/window/mod.rsmodified+20 -1
@@ -1,4 +1,5 @@
11 use niri_config::{Match, WindowRule};
2+use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
23 use smithay::wayland::compositor::with_states;
34 use smithay::wayland::shell::xdg::{
45 ToplevelSurface, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes,
@@ -84,13 +85,18 @@ impl ResolvedWindowRules {
8485
8586 let toplevel = window.toplevel();
8687 with_states(toplevel.wl_surface(), |states| {
87- let role = states
88+ let mut role = states
8889 .data_map
8990 .get::<XdgToplevelSurfaceData>()
9091 .unwrap()
9192 .lock()
9293 .unwrap();
9394
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+
94100 let mut open_on_output = None;
95101
96102 for rule in rules {
@@ -150,6 +156,19 @@ impl ResolvedWindowRules {
150156 }
151157
152158 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+
153172 if let Some(app_id_re) = &m.app_id {
154173 let Some(app_id) = &role.app_id else {
155174 return false;