forge
Explore
Log in

Don't you dare go Hollow

Implement window rule reloading and min/max size rules

Browse files at this commit

c61361de3ca4484387f39b067eadc612908560ebIvan MolodetskikhParent: 3963f53
niri-config/src/lib.rsmodified+11 -0
@@ -674,6 +674,7 @@ pub struct WindowRule {
674 #[knuffel(children(name = "exclude"))]674 #[knuffel(children(name = "exclude"))]
675 pub excludes: Vec<Match>,675 pub excludes: Vec<Match>,
676 676
677+ // Rules applied at initial configure.
677 #[knuffel(child)]678 #[knuffel(child)]
678 pub default_column_width: Option<DefaultColumnWidth>,679 pub default_column_width: Option<DefaultColumnWidth>,
679 #[knuffel(child, unwrap(argument))]680 #[knuffel(child, unwrap(argument))]
@@ -682,6 +683,16 @@ pub struct WindowRule {
682 pub open_maximized: Option<bool>,683 pub open_maximized: Option<bool>,
683 #[knuffel(child, unwrap(argument))]684 #[knuffel(child, unwrap(argument))]
684 pub open_fullscreen: Option<bool>,685 pub open_fullscreen: Option<bool>,
686+
687+ // Rules applied dynamically.
688+ #[knuffel(child, unwrap(argument))]
689+ pub min_width: Option<u16>,
690+ #[knuffel(child, unwrap(argument))]
691+ pub min_height: Option<u16>,
692+ #[knuffel(child, unwrap(argument))]
693+ pub max_width: Option<u16>,
694+ #[knuffel(child, unwrap(argument))]
695+ pub max_height: Option<u16>,
685 }696 }
686 697
687 #[derive(knuffel::Decode, Debug, Default, Clone)]698 #[derive(knuffel::Decode, Debug, Default, Clone)]
resources/default-config.kdlmodified+16 -0
@@ -362,6 +362,8 @@ animations {
362 exclude app-id=r#"\.unwanted\."#362 exclude app-id=r#"\.unwanted\."#
363 363
364 // Here are the properties that you can set on a window rule.364 // Here are the properties that you can set on a window rule.
365+ // These properties apply once, when a window first opens.
366+
365 // You can override the default column width.367 // You can override the default column width.
366 default-column-width { proportion 0.75; }368 default-column-width { proportion 0.75; }
367 369
@@ -377,6 +379,20 @@ animations {
377 open-fullscreen true379 open-fullscreen true
378 // You can also set this to false to prevent a window from opening fullscreen.380 // You can also set this to false to prevent a window from opening fullscreen.
379 // open-fullscreen false381 // open-fullscreen false
382+
383+ // The following properties apply dynamically while a window is open.
384+
385+ // You can amend the window's minimum and maximum size in logical pixels.
386+ // Keep in mind that the window itself always has a final say in its size.
387+ // These values instruct niri to never ask the window to be smaller than
388+ // the minimum you set, or to be bigger than the maximum you set.
389+ min-width 100
390+ max-width 200
391+ min-height 300
392+ // Caveat: max-height will only apply to automatically-sized windows
393+ // if it is equal to min-height. Either set this equal to min-height,
394+ // or change the window height manually for this to apply.
395+ max-height 300
380 }396 }
381 397
382 // Here's a useful example. Work around WezTerm's initial configure bug398 // Here's a useful example. Work around WezTerm's initial configure bug
src/handlers/xdg_shell.rsmodified+21 -82
@@ -1,4 +1,3 @@
1-use niri_config::{Match, WindowRule};
2 use smithay::desktop::{1 use smithay::desktop::{
3 find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, LayerSurface,2 find_popup_root_surface, get_popup_toplevel_coords, layer_map_for_output, LayerSurface,
4 PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window,3 PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window,
@@ -20,7 +19,7 @@ use smithay::wayland::shell::wlr_layer::Layer;
20 use smithay::wayland::shell::xdg::decoration::XdgDecorationHandler;19 use smithay::wayland::shell::xdg::decoration::XdgDecorationHandler;
21 use smithay::wayland::shell::xdg::{20 use smithay::wayland::shell::xdg::{
22 PopupSurface, PositionerState, ToplevelSurface, XdgPopupSurfaceData, XdgShellHandler,21 PopupSurface, PositionerState, ToplevelSurface, XdgPopupSurfaceData, XdgShellHandler,
23- XdgShellState, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes,22+ XdgShellState, XdgToplevelSurfaceData,
24 };23 };
25 use smithay::wayland::xdg_foreign::{XdgForeignHandler, XdgForeignState};24 use smithay::wayland::xdg_foreign::{XdgForeignHandler, XdgForeignState};
26 use smithay::{25 use smithay::{
@@ -31,82 +30,6 @@ use crate::layout::workspace::ColumnWidth;
31 use crate::niri::{PopupGrabState, State};30 use crate::niri::{PopupGrabState, State};
32 use crate::window::{InitialConfigureState, ResolvedWindowRules, Unmapped};31 use crate::window::{InitialConfigureState, ResolvedWindowRules, Unmapped};
33 32
34-fn window_matches(role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool {
35- if let Some(app_id_re) = &m.app_id {
36- let Some(app_id) = &role.app_id else {
37- return false;
38- };
39- if !app_id_re.is_match(app_id) {
40- return false;
41- }
42- }
43-
44- if let Some(title_re) = &m.title {
45- let Some(title) = &role.title else {
46- return false;
47- };
48- if !title_re.is_match(title) {
49- return false;
50- }
51- }
52-
53- true
54-}
55-
56-pub fn resolve_window_rules(
57- rules: &[WindowRule],
58- toplevel: &ToplevelSurface,
59-) -> ResolvedWindowRules {
60- let _span = tracy_client::span!("resolve_window_rules");
61-
62- let mut resolved = ResolvedWindowRules::default();
63-
64- with_states(toplevel.wl_surface(), |states| {
65- let role = states
66- .data_map
67- .get::<XdgToplevelSurfaceData>()
68- .unwrap()
69- .lock()
70- .unwrap();
71-
72- let mut open_on_output = None;
73-
74- for rule in rules {
75- if !(rule.matches.is_empty() || rule.matches.iter().any(|m| window_matches(&role, m))) {
76- continue;
77- }
78-
79- if rule.excludes.iter().any(|m| window_matches(&role, m)) {
80- continue;
81- }
82-
83- if let Some(x) = rule
84- .default_column_width
85- .as_ref()
86- .map(|d| d.0.map(ColumnWidth::from))
87- {
88- resolved.default_width = Some(x);
89- }
90-
91- if let Some(x) = rule.open_on_output.as_deref() {
92- open_on_output = Some(x);
93- }
94-
95- if let Some(x) = rule.open_maximized {
96- resolved.open_maximized = Some(x);
97- }
98-
99- if let Some(x) = rule.open_fullscreen {
100- resolved.open_fullscreen = Some(x);
101- }
102- }
103-
104- resolved.open_on_output = open_on_output.map(|x| x.to_owned());
105- });
106-
107- resolved
108-}
109-
110 impl XdgShellHandler for State {33 impl XdgShellHandler for State {
111 fn xdg_shell_state(&mut self) -> &mut XdgShellState {34 fn xdg_shell_state(&mut self) -> &mut XdgShellState {
112 &mut self.niri.xdg_shell_state35 &mut self.niri.xdg_shell_state
@@ -574,7 +497,7 @@ impl State {
574 };497 };
575 498
576 let config = self.niri.config.borrow();499 let config = self.niri.config.borrow();
577- let rules = resolve_window_rules(&config.window_rules, toplevel);500+ let rules = ResolvedWindowRules::compute(&config.window_rules, toplevel);
578 501
579 // Pick the target monitor. First, check if we had an output set in the window rules.502 // Pick the target monitor. First, check if we had an output set in the window rules.
580 let mon = rules503 let mon = rules
@@ -807,14 +730,30 @@ impl State {
807 }730 }
808 731
809 pub fn update_window_rules(&mut self, toplevel: &ToplevelSurface) {732 pub fn update_window_rules(&mut self, toplevel: &ToplevelSurface) {
810- let resolve = || resolve_window_rules(&self.niri.config.borrow().window_rules, toplevel);733+ let resolve =
734+ || ResolvedWindowRules::compute(&self.niri.config.borrow().window_rules, toplevel);
811 735
812 if let Some(unmapped) = self.niri.unmapped_windows.get_mut(toplevel.wl_surface()) {736 if let Some(unmapped) = self.niri.unmapped_windows.get_mut(toplevel.wl_surface()) {
813 if let InitialConfigureState::Configured { rules, .. } = &mut unmapped.state {737 if let InitialConfigureState::Configured { rules, .. } = &mut unmapped.state {
814 *rules = resolve();738 *rules = resolve();
815 }739 }
816- } else if let Some(mapped) = self.niri.layout.find_window_mut(toplevel.wl_surface()) {740+ } else if let Some((mapped, output)) = self
817- mapped.rules = resolve();741+ .niri
742+ .layout
743+ .find_window_and_output_mut(toplevel.wl_surface())
744+ {
745+ let new_rules = resolve();
746+ if mapped.rules != new_rules {
747+ mapped.rules = new_rules;
748+
749+ let output = output.cloned();
750+ let window = mapped.window.clone();
751+ self.niri.layout.update_window(&window);
752+
753+ if let Some(output) = output {
754+ self.niri.queue_redraw(output);
755+ }
756+ }
818 }757 }
819 }758 }
820 }759 }
src/layout/mod.rsmodified+41 -17
@@ -643,13 +643,30 @@ impl<W: LayoutElement> Layout<W> {
643 }643 }
644 }644 }
645 645
646- pub fn find_window_mut(&mut self, wl_surface: &WlSurface) -> Option<&mut W> {646+ pub fn find_window_and_output(&self, wl_surface: &WlSurface) -> Option<(&W, &Output)> {
647+ if let MonitorSet::Normal { monitors, .. } = &self.monitor_set {
648+ for mon in monitors {
649+ for ws in &mon.workspaces {
650+ if let Some(window) = ws.find_wl_surface(wl_surface) {
651+ return Some((window, &mon.output));
652+ }
653+ }
654+ }
655+ }
656+
657+ None
658+ }
659+
660+ pub fn find_window_and_output_mut(
661+ &mut self,
662+ wl_surface: &WlSurface,
663+ ) -> Option<(&mut W, Option<&Output>)> {
647 match &mut self.monitor_set {664 match &mut self.monitor_set {
648 MonitorSet::Normal { monitors, .. } => {665 MonitorSet::Normal { monitors, .. } => {
649 for mon in monitors {666 for mon in monitors {
650 for ws in &mut mon.workspaces {667 for ws in &mut mon.workspaces {
651 if let Some(window) = ws.find_wl_surface_mut(wl_surface) {668 if let Some(window) = ws.find_wl_surface_mut(wl_surface) {
652- return Some(window);669+ return Some((window, Some(&mon.output)));
653 }670 }
654 }671 }
655 }672 }
@@ -657,21 +674,7 @@ impl<W: LayoutElement> Layout<W> {
657 MonitorSet::NoOutputs { workspaces } => {674 MonitorSet::NoOutputs { workspaces } => {
658 for ws in workspaces {675 for ws in workspaces {
659 if let Some(window) = ws.find_wl_surface_mut(wl_surface) {676 if let Some(window) = ws.find_wl_surface_mut(wl_surface) {
660- return Some(window);677+ return Some((window, None));
661- }
662- }
663- }
664- }
665-
666- None
667- }
668-
669- pub fn find_window_and_output(&self, wl_surface: &WlSurface) -> Option<(&W, &Output)> {
670- if let MonitorSet::Normal { monitors, .. } = &self.monitor_set {
671- for mon in monitors {
672- for ws in &mon.workspaces {
673- if let Some(window) = ws.find_wl_surface(wl_surface) {
674- return Some((window, &mon.output));
675 }678 }
676 }679 }
677 }680 }
@@ -851,6 +854,27 @@ impl<W: LayoutElement> Layout<W> {
851 }854 }
852 }855 }
853 856
857+ pub fn with_windows_mut(&mut self, mut f: impl FnMut(&mut W, Option<&Output>)) {
858+ match &mut self.monitor_set {
859+ MonitorSet::Normal { monitors, .. } => {
860+ for mon in monitors {
861+ for ws in &mut mon.workspaces {
862+ for win in ws.windows_mut() {
863+ f(win, Some(&mon.output));
864+ }
865+ }
866+ }
867+ }
868+ MonitorSet::NoOutputs { workspaces } => {
869+ for ws in workspaces {
870+ for win in ws.windows_mut() {
871+ f(win, None);
872+ }
873+ }
874+ }
875+ }
876+ }
877+
854 fn active_monitor(&mut self) -> Option<&mut Monitor<W>> {878 fn active_monitor(&mut self) -> Option<&mut Monitor<W>> {
855 let MonitorSet::Normal {879 let MonitorSet::Normal {
856 monitors,880 monitors,
src/niri.rsmodified+30 -1
@@ -114,7 +114,7 @@ use crate::utils::spawning::CHILD_ENV;
114 use crate::utils::{114 use crate::utils::{
115 center, center_f64, get_monotonic_time, make_screenshot_path, output_size, write_png_rgba8,115 center, center_f64, get_monotonic_time, make_screenshot_path, output_size, write_png_rgba8,
116 };116 };
117-use crate::window::{Mapped, Unmapped};117+use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped};
118 use crate::{animation, niri_render_elements};118 use crate::{animation, niri_render_elements};
119 119
120 const CLEAR_COLOR: [f32; 4] = [0.2, 0.2, 0.2, 1.];120 const CLEAR_COLOR: [f32; 4] = [0.2, 0.2, 0.2, 1.];
@@ -761,6 +761,7 @@ impl State {
761 let mut reload_xkb = None;761 let mut reload_xkb = None;
762 let mut libinput_config_changed = false;762 let mut libinput_config_changed = false;
763 let mut output_config_changed = false;763 let mut output_config_changed = false;
764+ let mut window_rules_changed = false;
764 let mut old_config = self.niri.config.borrow_mut();765 let mut old_config = self.niri.config.borrow_mut();
765 766
766 // Reload the cursor.767 // Reload the cursor.
@@ -802,6 +803,10 @@ impl State {
802 self.niri.hotkey_overlay.on_hotkey_config_updated();803 self.niri.hotkey_overlay.on_hotkey_config_updated();
803 }804 }
804 805
806+ if config.window_rules != old_config.window_rules {
807+ window_rules_changed = true;
808+ }
809+
805 *old_config = config;810 *old_config = config;
806 811
807 // Release the borrow.812 // Release the borrow.
@@ -865,6 +870,30 @@ impl State {
865 }870 }
866 }871 }
867 872
873+ if window_rules_changed {
874+ let _span = tracy_client::span!("recompute window rules");
875+
876+ let window_rules = &self.niri.config.borrow().window_rules;
877+
878+ for unmapped in self.niri.unmapped_windows.values_mut() {
879+ if let InitialConfigureState::Configured { rules, .. } = &mut unmapped.state {
880+ *rules = ResolvedWindowRules::compute(
881+ window_rules,
882+ unmapped.window.toplevel().expect("no X11 support"),
883+ );
884+ }
885+ }
886+
887+ let mut windows = vec![];
888+ self.niri.layout.with_windows_mut(|mapped, _| {
889+ mapped.rules = ResolvedWindowRules::compute(window_rules, mapped.toplevel());
890+ windows.push(mapped.window.clone());
891+ });
892+ for win in windows {
893+ self.niri.layout.update_window(&win);
894+ }
895+ }
896+
868 // Can't really update xdg-decoration settings since we have to hide the globals for CSD897 // Can't really update xdg-decoration settings since we have to hide the globals for CSD
869 // due to the SDL2 bug... I don't imagine clients are prepared for the xdg-decoration898 // due to the SDL2 bug... I don't imagine clients are prepared for the xdg-decoration
870 // global suddenly appearing? Either way, right now it's live-reloaded in a sense that new899 // global suddenly appearing? Either way, right now it's live-reloaded in a sense that new
src/window/mapped.rsmodified+32 -4
@@ -1,3 +1,5 @@
1+use std::cmp::{max, min};
2+
1 use smithay::backend::renderer::element::{AsRenderElements as _, Id};3 use smithay::backend::renderer::element::{AsRenderElements as _, Id};
2 use smithay::desktop::space::SpaceElement as _;4 use smithay::desktop::space::SpaceElement as _;
3 use smithay::desktop::Window;5 use smithay::desktop::Window;
@@ -82,17 +84,43 @@ impl LayoutElement for Mapped {
82 }84 }
83 85
84 fn min_size(&self) -> Size<i32, Logical> {86 fn min_size(&self) -> Size<i32, Logical> {
85- with_states(self.toplevel().wl_surface(), |state| {87+ let mut size = with_states(self.toplevel().wl_surface(), |state| {
86 let curr = state.cached_state.current::<SurfaceCachedState>();88 let curr = state.cached_state.current::<SurfaceCachedState>();
87 curr.min_size89 curr.min_size
88- })90+ });
91+
92+ if let Some(x) = self.rules.min_width {
93+ size.w = max(size.w, i32::from(x));
94+ }
95+ if let Some(x) = self.rules.min_height {
96+ size.h = max(size.h, i32::from(x));
97+ }
98+
99+ size
89 }100 }
90 101
91 fn max_size(&self) -> Size<i32, Logical> {102 fn max_size(&self) -> Size<i32, Logical> {
92- with_states(self.toplevel().wl_surface(), |state| {103+ let mut size = with_states(self.toplevel().wl_surface(), |state| {
93 let curr = state.cached_state.current::<SurfaceCachedState>();104 let curr = state.cached_state.current::<SurfaceCachedState>();
94 curr.max_size105 curr.max_size
95- })106+ });
107+
108+ if let Some(x) = self.rules.max_width {
109+ if size.w == 0 {
110+ size.w = i32::from(x);
111+ } else if x > 0 {
112+ size.w = min(size.w, i32::from(x));
113+ }
114+ }
115+ if let Some(x) = self.rules.max_height {
116+ if size.h == 0 {
117+ size.h = i32::from(x);
118+ } else if x > 0 {
119+ size.h = min(size.h, i32::from(x));
120+ }
121+ }
122+
123+ size
96 }124 }
97 125
98 fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool {126 fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool {
src/window/mod.rsmodified+106 -1
@@ -1,3 +1,9 @@
1+use niri_config::{Match, WindowRule};
2+use smithay::wayland::compositor::with_states;
3+use smithay::wayland::shell::xdg::{
4+ ToplevelSurface, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes,
5+};
6+
1 use crate::layout::workspace::ColumnWidth;7 use crate::layout::workspace::ColumnWidth;
2 8
3 pub mod mapped;9 pub mod mapped;
@@ -7,7 +13,7 @@ pub mod unmapped;
7 pub use unmapped::{InitialConfigureState, Unmapped};13 pub use unmapped::{InitialConfigureState, Unmapped};
8 14
9 /// Rules fully resolved for a window.15 /// Rules fully resolved for a window.
10-#[derive(Debug, Default)]16+#[derive(Debug, Default, PartialEq)]
11 pub struct ResolvedWindowRules {17 pub struct ResolvedWindowRules {
12 /// Default width for this window.18 /// Default width for this window.
13 ///19 ///
@@ -24,4 +30,103 @@ pub struct ResolvedWindowRules {
24 30
25 /// Whether the window should open fullscreen.31 /// Whether the window should open fullscreen.
26 pub open_fullscreen: Option<bool>,32 pub open_fullscreen: Option<bool>,
33+
34+ /// Extra bound on the minimum window width.
35+ pub min_width: Option<u16>,
36+ /// Extra bound on the minimum window height.
37+ pub min_height: Option<u16>,
38+ /// Extra bound on the maximum window width.
39+ pub max_width: Option<u16>,
40+ /// Extra bound on the maximum window height.
41+ pub max_height: Option<u16>,
42+}
43+
44+impl ResolvedWindowRules {
45+ pub fn compute(rules: &[WindowRule], toplevel: &ToplevelSurface) -> Self {
46+ let _span = tracy_client::span!("ResolvedWindowRules::compute");
47+
48+ let mut resolved = ResolvedWindowRules::default();
49+
50+ with_states(toplevel.wl_surface(), |states| {
51+ let role = states
52+ .data_map
53+ .get::<XdgToplevelSurfaceData>()
54+ .unwrap()
55+ .lock()
56+ .unwrap();
57+
58+ let mut open_on_output = None;
59+
60+ for rule in rules {
61+ if !(rule.matches.is_empty()
62+ || rule.matches.iter().any(|m| window_matches(&role, m)))
63+ {
64+ continue;
65+ }
66+
67+ if rule.excludes.iter().any(|m| window_matches(&role, m)) {
68+ continue;
69+ }
70+
71+ if let Some(x) = rule
72+ .default_column_width
73+ .as_ref()
74+ .map(|d| d.0.map(ColumnWidth::from))
75+ {
76+ resolved.default_width = Some(x);
77+ }
78+
79+ if let Some(x) = rule.open_on_output.as_deref() {
80+ open_on_output = Some(x);
81+ }
82+
83+ if let Some(x) = rule.open_maximized {
84+ resolved.open_maximized = Some(x);
85+ }
86+
87+ if let Some(x) = rule.open_fullscreen {
88+ resolved.open_fullscreen = Some(x);
89+ }
90+
91+ if let Some(x) = rule.min_width {
92+ resolved.min_width = Some(x);
93+ }
94+ if let Some(x) = rule.min_height {
95+ resolved.min_height = Some(x);
96+ }
97+ if let Some(x) = rule.max_width {
98+ resolved.max_width = Some(x);
99+ }
100+ if let Some(x) = rule.max_height {
101+ resolved.max_height = Some(x);
102+ }
103+ }
104+
105+ resolved.open_on_output = open_on_output.map(|x| x.to_owned());
106+ });
107+
108+ resolved
109+ }
110+}
111+
112+fn window_matches(role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool {
113+ if let Some(app_id_re) = &m.app_id {
114+ let Some(app_id) = &role.app_id else {
115+ return false;
116+ };
117+ if !app_id_re.is_match(app_id) {
118+ return false;
119+ }
120+ }
121+
122+ if let Some(title_re) = &m.title {
123+ let Some(title) = &role.title else {
124+ return false;
125+ };
126+ if !title_re.is_match(title) {
127+ return false;
128+ }
129+ }
130+
131+ true
27 }132 }