forge
Explore
Log in

Don't you dare go Hollow

Add layer matcher to layer-rule

Browse files at this commit

6bcaaf9d21fca9477aa2c45c5d60235662c58056Ivan MolodetskikhParent: f022b3c
docs/wiki/Configuration:-Layer-Rules.mdmodified+17 -0
@@ -14,6 +14,7 @@ Here are all matchers and properties that a layer rule could have:
1414 layer-rule {
1515 match namespace="waybar"
1616 match at-startup=true
17+ match layer="top"
1718
1819 // Properties that apply continuously.
1920 opacity 0.5
@@ -69,6 +70,22 @@ layer-rule {
6970 }
7071 ```
7172
73+#### `layer`
74+
75+<sup>Since: next release</sup>
76+
77+Matches surfaces on this layer-shell layer.
78+Can be `"background"`, `"bottom"`, `"top"`, or `"overlay"`.
79+
80+```kdl
81+// Make all overlay-layer surfaces FLOAT.
82+layer-rule {
83+ match layer="overlay"
84+
85+ baba-is-float true
86+}
87+```
88+
7289 ### Dynamic Properties
7390
7491 These properties apply continuously to open layer-shell surfaces.
niri-config/src/layer_rule.rsmodified+2 -0
@@ -28,4 +28,6 @@ pub struct Match {
2828 pub namespace: Option<RegexEq>,
2929 #[knuffel(property)]
3030 pub at_startup: Option<bool>,
31+ #[knuffel(property, str)]
32+ pub layer: Option<niri_ipc::Layer>,
3133 }
niri-config/src/lib.rsmodified+1 -0
@@ -1859,6 +1859,7 @@ mod tests {
18591859 ),
18601860 ),
18611861 at_startup: None,
1862+ layer: None,
18621863 },
18631864 ],
18641865 excludes: [],
niri-ipc/src/lib.rsmodified+14 -0
@@ -1868,6 +1868,20 @@ impl FromStr for Transform {
18681868 }
18691869 }
18701870
1871+impl FromStr for Layer {
1872+ type Err = &'static str;
1873+
1874+ fn from_str(s: &str) -> Result<Self, Self::Err> {
1875+ match s {
1876+ "background" => Ok(Self::Background),
1877+ "bottom" => Ok(Self::Bottom),
1878+ "top" => Ok(Self::Top),
1879+ "overlay" => Ok(Self::Overlay),
1880+ _ => Err("invalid layer, can be \"background\", \"bottom\", \"top\" or \"overlay\""),
1881+ }
1882+ }
1883+}
1884+
18711885 impl FromStr for ModeToSet {
18721886 type Err = &'static str;
18731887
src/handlers/layer_shell.rsmodified+40 -3
@@ -3,10 +3,10 @@ use smithay::desktop::{layer_map_for_output, LayerSurface, PopupKind, WindowSurf
33 use smithay::output::Output;
44 use smithay::reexports::wayland_server::protocol::wl_output::WlOutput;
55 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
6-use smithay::wayland::compositor::{get_parent, with_states};
6+use smithay::wayland::compositor::{add_pre_commit_hook, get_parent, with_states, HookId};
77 use smithay::wayland::shell::wlr_layer::{
8- self, Layer, LayerSurface as WlrLayerSurface, LayerSurfaceData, WlrLayerShellHandler,
9- WlrLayerShellState,
8+ self, Layer, LayerSurface as WlrLayerSurface, LayerSurfaceCachedState, LayerSurfaceData,
9+ WlrLayerShellHandler, WlrLayerShellState,
1010 };
1111 use smithay::wayland::shell::xdg::PopupSurface;
1212
@@ -126,8 +126,10 @@ impl State {
126126 let output_size = output_size(&output);
127127 let scale = output.current_scale().fractional_scale();
128128
129+ let hook = add_mapped_layer_pre_commit_hook(layer);
129130 let mapped = MappedLayer::new(
130131 layer.clone(),
132+ hook,
131133 rules,
132134 output_size,
133135 scale,
@@ -142,6 +144,21 @@ impl State {
142144 if prev.is_some() {
143145 error!("MappedLayer was present for an unmapped surface");
144146 }
147+ } else {
148+ // The surface remains mapped.
149+ if let Some(mapped) = self.niri.mapped_layer_surfaces.get_mut(layer) {
150+ // Check if the layer changed.
151+ if mapped.take_recompute_rules_on_commit() {
152+ let config = self.niri.config.borrow();
153+ if mapped
154+ .recompute_layer_rules(&config.layer_rules, self.niri.is_at_startup)
155+ {
156+ mapped.update_config(&config);
157+ }
158+ }
159+ } else {
160+ error!("MappedLayer missing for a mapped surface");
161+ }
145162 }
146163
147164 // Give focus to newly mapped on-demand surfaces. Some launchers like lxqt-runner rely
@@ -204,3 +221,23 @@ impl State {
204221 true
205222 }
206223 }
224+
225+fn add_mapped_layer_pre_commit_hook(layer: &LayerSurface) -> HookId {
226+ add_pre_commit_hook::<State, _>(layer.wl_surface(), move |state, _dh, surface| {
227+ let layer_changed = with_states(surface, |states| {
228+ let mut guard = states.cached_state.get::<LayerSurfaceCachedState>();
229+ let pending_layer = guard.pending().layer;
230+ let current_layer = guard.current().layer;
231+ pending_layer != current_layer
232+ });
233+
234+ if layer_changed {
235+ for mapped in state.niri.mapped_layer_surfaces.values_mut() {
236+ if mapped.surface().wl_surface() == surface {
237+ mapped.set_recompute_rules_on_commit();
238+ break;
239+ }
240+ }
241+ }
242+ })
243+}
src/layer/mapped.rsmodified+26 -0
@@ -4,6 +4,7 @@ use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
44 use smithay::backend::renderer::element::Kind;
55 use smithay::desktop::{LayerSurface, PopupManager};
66 use smithay::utils::{Logical, Point, Scale, Size};
7+use smithay::wayland::compositor::{remove_pre_commit_hook, HookId};
78 use smithay::wayland::shell::wlr_layer::{ExclusiveZone, Layer};
89
910 use super::ResolvedLayerRules;
@@ -22,9 +23,17 @@ pub struct MappedLayer {
2223 /// The surface itself.
2324 surface: LayerSurface,
2425
26+ /// Pre-commit hook that we have on all mapped layer surfaces.
27+ pre_commit_hook: HookId,
28+
2529 /// Up-to-date rules.
2630 rules: ResolvedLayerRules,
2731
32+ /// Whether to recompute layer rules on the next commit.
33+ ///
34+ /// Set in the pre-commit hook when the layer changes; consumed in the commit handler.
35+ recompute_rules_on_commit: bool,
36+
2837 /// Buffer to draw instead of the surface when it should be blocked out.
2938 block_out_buffer: SolidColorBuffer,
3039
@@ -52,6 +61,7 @@ niri_render_elements! {
5261 impl MappedLayer {
5362 pub fn new(
5463 surface: LayerSurface,
64+ pre_commit_hook: HookId,
5565 rules: ResolvedLayerRules,
5666 view_size: Size<f64, Logical>,
5767 scale: f64,
@@ -65,7 +75,9 @@ impl MappedLayer {
6575
6676 Self {
6777 surface,
78+ pre_commit_hook,
6879 rules,
80+ recompute_rules_on_commit: false,
6981 block_out_buffer: SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.]),
7082 view_size,
7183 scale,
@@ -128,6 +140,14 @@ impl MappedLayer {
128140 true
129141 }
130142
143+ pub fn set_recompute_rules_on_commit(&mut self) {
144+ self.recompute_rules_on_commit = true;
145+ }
146+
147+ pub fn take_recompute_rules_on_commit(&mut self) -> bool {
148+ std::mem::take(&mut self.recompute_rules_on_commit)
149+ }
150+
131151 pub fn place_within_backdrop(&self) -> bool {
132152 if !self.rules.place_within_backdrop {
133153 return false;
@@ -232,3 +252,9 @@ impl MappedLayer {
232252 }
233253 }
234254 }
255+
256+impl Drop for MappedLayer {
257+ fn drop(&mut self) {
258+ remove_pre_commit_hook(self.surface.wl_surface(), self.pre_commit_hook.clone());
259+ }
260+}
src/layer/mod.rsmodified+13 -0
@@ -2,6 +2,7 @@ use niri_config::layer_rule::{LayerRule, Match};
22 use niri_config::utils::MergeWith as _;
33 use niri_config::{BlockOutFrom, CornerRadius, ShadowRule};
44 use smithay::desktop::LayerSurface;
5+use smithay::wayland::shell::wlr_layer::Layer;
56
67 pub mod mapped;
78 pub use mapped::MappedLayer;
@@ -83,5 +84,17 @@ fn surface_matches(surface: &LayerSurface, m: &Match) -> bool {
8384 }
8485 }
8586
87+ if let Some(layer) = m.layer {
88+ let surface_layer = match surface.layer() {
89+ Layer::Background => niri_ipc::Layer::Background,
90+ Layer::Bottom => niri_ipc::Layer::Bottom,
91+ Layer::Top => niri_ipc::Layer::Top,
92+ Layer::Overlay => niri_ipc::Layer::Overlay,
93+ };
94+ if layer != surface_layer {
95+ return false;
96+ }
97+ }
98+
8699 true
87100 }