Add layer matcher to layer-rule
Browse files at this commit6bcaaf9d21fca9477aa2c45c5d60235662c58056Parent: f022b3c7 changed files +113 -3
- docs/wiki/Configuration:-Layer-Rules.md +17 -0
- niri-config/src/layer_rule.rs +2 -0
- niri-config/src/lib.rs +1 -0
- niri-ipc/src/lib.rs +14 -0
- src/handlers/layer_shell.rs +40 -3
- src/layer/mapped.rs +26 -0
- src/layer/mod.rs +13 -0
docs/wiki/Configuration:-Layer-Rules.mdmodified+17 -0
| @@ -14,6 +14,7 @@ Here are all matchers and properties that a layer rule could have: | ||
| 14 | 14 | layer-rule { |
| 15 | 15 | match namespace="waybar" |
| 16 | 16 | match at-startup=true |
| 17 | + match layer="top" | |
| 17 | 18 | |
| 18 | 19 | // Properties that apply continuously. |
| 19 | 20 | opacity 0.5 |
| @@ -69,6 +70,22 @@ layer-rule { | ||
| 69 | 70 | } |
| 70 | 71 | ``` |
| 71 | 72 | |
| 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 | + | |
| 72 | 89 | ### Dynamic Properties |
| 73 | 90 | |
| 74 | 91 | These properties apply continuously to open layer-shell surfaces. |
niri-config/src/layer_rule.rsmodified+2 -0
| @@ -28,4 +28,6 @@ pub struct Match { | ||
| 28 | 28 | pub namespace: Option<RegexEq>, |
| 29 | 29 | #[knuffel(property)] |
| 30 | 30 | pub at_startup: Option<bool>, |
| 31 | + #[knuffel(property, str)] | |
| 32 | + pub layer: Option<niri_ipc::Layer>, | |
| 31 | 33 | } |
niri-config/src/lib.rsmodified+1 -0
| @@ -1859,6 +1859,7 @@ mod tests { | ||
| 1859 | 1859 | ), |
| 1860 | 1860 | ), |
| 1861 | 1861 | at_startup: None, |
| 1862 | + layer: None, | |
| 1862 | 1863 | }, |
| 1863 | 1864 | ], |
| 1864 | 1865 | excludes: [], |
niri-ipc/src/lib.rsmodified+14 -0
| @@ -1868,6 +1868,20 @@ impl FromStr for Transform { | ||
| 1868 | 1868 | } |
| 1869 | 1869 | } |
| 1870 | 1870 | |
| 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 | + | |
| 1871 | 1885 | impl FromStr for ModeToSet { |
| 1872 | 1886 | type Err = &'static str; |
| 1873 | 1887 |
src/handlers/layer_shell.rsmodified+40 -3
| @@ -3,10 +3,10 @@ use smithay::desktop::{layer_map_for_output, LayerSurface, PopupKind, WindowSurf | ||
| 3 | 3 | use smithay::output::Output; |
| 4 | 4 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; |
| 5 | 5 | 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}; | |
| 7 | 7 | 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, | |
| 10 | 10 | }; |
| 11 | 11 | use smithay::wayland::shell::xdg::PopupSurface; |
| 12 | 12 | |
| @@ -126,8 +126,10 @@ impl State { | ||
| 126 | 126 | let output_size = output_size(&output); |
| 127 | 127 | let scale = output.current_scale().fractional_scale(); |
| 128 | 128 | |
| 129 | + let hook = add_mapped_layer_pre_commit_hook(layer); | |
| 129 | 130 | let mapped = MappedLayer::new( |
| 130 | 131 | layer.clone(), |
| 132 | + hook, | |
| 131 | 133 | rules, |
| 132 | 134 | output_size, |
| 133 | 135 | scale, |
| @@ -142,6 +144,21 @@ impl State { | ||
| 142 | 144 | if prev.is_some() { |
| 143 | 145 | error!("MappedLayer was present for an unmapped surface"); |
| 144 | 146 | } |
| 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 | + } | |
| 145 | 162 | } |
| 146 | 163 | |
| 147 | 164 | // Give focus to newly mapped on-demand surfaces. Some launchers like lxqt-runner rely |
| @@ -204,3 +221,23 @@ impl State { | ||
| 204 | 221 | true |
| 205 | 222 | } |
| 206 | 223 | } |
| 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; | ||
| 4 | 4 | use smithay::backend::renderer::element::Kind; |
| 5 | 5 | use smithay::desktop::{LayerSurface, PopupManager}; |
| 6 | 6 | use smithay::utils::{Logical, Point, Scale, Size}; |
| 7 | +use smithay::wayland::compositor::{remove_pre_commit_hook, HookId}; | |
| 7 | 8 | use smithay::wayland::shell::wlr_layer::{ExclusiveZone, Layer}; |
| 8 | 9 | |
| 9 | 10 | use super::ResolvedLayerRules; |
| @@ -22,9 +23,17 @@ pub struct MappedLayer { | ||
| 22 | 23 | /// The surface itself. |
| 23 | 24 | surface: LayerSurface, |
| 24 | 25 | |
| 26 | + /// Pre-commit hook that we have on all mapped layer surfaces. | |
| 27 | + pre_commit_hook: HookId, | |
| 28 | + | |
| 25 | 29 | /// Up-to-date rules. |
| 26 | 30 | rules: ResolvedLayerRules, |
| 27 | 31 | |
| 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 | + | |
| 28 | 37 | /// Buffer to draw instead of the surface when it should be blocked out. |
| 29 | 38 | block_out_buffer: SolidColorBuffer, |
| 30 | 39 | |
| @@ -52,6 +61,7 @@ niri_render_elements! { | ||
| 52 | 61 | impl MappedLayer { |
| 53 | 62 | pub fn new( |
| 54 | 63 | surface: LayerSurface, |
| 64 | + pre_commit_hook: HookId, | |
| 55 | 65 | rules: ResolvedLayerRules, |
| 56 | 66 | view_size: Size<f64, Logical>, |
| 57 | 67 | scale: f64, |
| @@ -65,7 +75,9 @@ impl MappedLayer { | ||
| 65 | 75 | |
| 66 | 76 | Self { |
| 67 | 77 | surface, |
| 78 | + pre_commit_hook, | |
| 68 | 79 | rules, |
| 80 | + recompute_rules_on_commit: false, | |
| 69 | 81 | block_out_buffer: SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.]), |
| 70 | 82 | view_size, |
| 71 | 83 | scale, |
| @@ -128,6 +140,14 @@ impl MappedLayer { | ||
| 128 | 140 | true |
| 129 | 141 | } |
| 130 | 142 | |
| 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 | + | |
| 131 | 151 | pub fn place_within_backdrop(&self) -> bool { |
| 132 | 152 | if !self.rules.place_within_backdrop { |
| 133 | 153 | return false; |
| @@ -232,3 +252,9 @@ impl MappedLayer { | ||
| 232 | 252 | } |
| 233 | 253 | } |
| 234 | 254 | } |
| 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}; | ||
| 2 | 2 | use niri_config::utils::MergeWith as _; |
| 3 | 3 | use niri_config::{BlockOutFrom, CornerRadius, ShadowRule}; |
| 4 | 4 | use smithay::desktop::LayerSurface; |
| 5 | +use smithay::wayland::shell::wlr_layer::Layer; | |
| 5 | 6 | |
| 6 | 7 | pub mod mapped; |
| 7 | 8 | pub use mapped::MappedLayer; |
| @@ -83,5 +84,17 @@ fn surface_matches(surface: &LayerSurface, m: &Match) -> bool { | ||
| 83 | 84 | } |
| 84 | 85 | } |
| 85 | 86 | |
| 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 | + | |
| 86 | 99 | true |
| 87 | 100 | } |