Implement shadows for layer surfaces
Browse files at this commitacd4cb51aa0e013bfec14444ee48a01c60ebaf8aParent: 5ebcae97 changed files +133 -6
- niri-config/src/layer_rule.rs +5 -1
- niri-config/src/lib.rs +2 -0
- src/handlers/layer_shell.rs +3 -2
- src/layer/mapped.rs +36 -1
- src/layer/mod.rs +23 -1
- src/niri.rs +6 -1
- wiki/Configuration:-Layer-Rules.md +58 -0
niri-config/src/layer_rule.rsmodified+5 -1
| @@ -1,4 +1,4 @@ | |||
| 1 | -use crate::{BlockOutFrom, RegexEq}; | 1 | +use crate::{BlockOutFrom, CornerRadius, RegexEq, ShadowRule}; |
| 2 | 2 | ||
| 3 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] | 3 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] |
| 4 | pub struct LayerRule { | 4 | pub struct LayerRule { |
| @@ -11,6 +11,10 @@ pub struct LayerRule { | |||
| 11 | pub opacity: Option<f32>, | 11 | pub opacity: Option<f32>, |
| 12 | #[knuffel(child, unwrap(argument))] | 12 | #[knuffel(child, unwrap(argument))] |
| 13 | pub block_out_from: Option<BlockOutFrom>, | 13 | pub block_out_from: Option<BlockOutFrom>, |
| 14 | + #[knuffel(child, default)] | ||
| 15 | + pub shadow: ShadowRule, | ||
| 16 | + #[knuffel(child)] | ||
| 17 | + pub geometry_corner_radius: Option<CornerRadius>, | ||
| 14 | } | 18 | } |
| 15 | 19 | ||
| 16 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] | 20 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] |
niri-config/src/lib.rsmodified+2 -0
| @@ -3778,6 +3778,8 @@ mod tests { | |||
| 3778 | excludes: vec![], | 3778 | excludes: vec![], |
| 3779 | opacity: None, | 3779 | opacity: None, |
| 3780 | block_out_from: Some(BlockOutFrom::Screencast), | 3780 | block_out_from: Some(BlockOutFrom::Screencast), |
| 3781 | + shadow: ShadowRule::default(), | ||
| 3782 | + geometry_corner_radius: None, | ||
| 3781 | } | 3783 | } |
| 3782 | ], | 3784 | ], |
| 3783 | workspaces: vec![ | 3785 | workspaces: vec![ |
src/handlers/layer_shell.rsmodified+3 -2
| @@ -132,10 +132,11 @@ impl State { | |||
| 132 | 132 | ||
| 133 | // Resolve rules for newly mapped layer surfaces. | 133 | // Resolve rules for newly mapped layer surfaces. |
| 134 | if was_unmapped { | 134 | if was_unmapped { |
| 135 | - let rules = &self.niri.config.borrow().layer_rules; | 135 | + let config = self.niri.config.borrow(); |
| 136 | + let rules = &config.layer_rules; | ||
| 136 | let rules = | 137 | let rules = |
| 137 | ResolvedLayerRules::compute(rules, layer, self.niri.is_at_startup); | 138 | ResolvedLayerRules::compute(rules, layer, self.niri.is_at_startup); |
| 138 | - let mapped = MappedLayer::new(layer.clone(), rules); | 139 | + let mapped = MappedLayer::new(layer.clone(), rules, &config); |
| 139 | let prev = self | 140 | let prev = self |
| 140 | .niri | 141 | .niri |
| 141 | .mapped_layer_surfaces | 142 | .mapped_layer_surfaces |
src/layer/mapped.rsmodified+36 -1
| @@ -1,4 +1,5 @@ | |||
| 1 | use niri_config::layer_rule::LayerRule; | 1 | use niri_config::layer_rule::LayerRule; |
| 2 | +use niri_config::Config; | ||
| 2 | use smithay::backend::renderer::element::surface::{ | 3 | use smithay::backend::renderer::element::surface::{ |
| 3 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, | 4 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, |
| 4 | }; | 5 | }; |
| @@ -7,8 +8,10 @@ use smithay::desktop::{LayerSurface, PopupManager}; | |||
| 7 | use smithay::utils::{Logical, Point, Scale, Size}; | 8 | use smithay::utils::{Logical, Point, Scale, Size}; |
| 8 | 9 | ||
| 9 | use super::ResolvedLayerRules; | 10 | use super::ResolvedLayerRules; |
| 11 | +use crate::layout::shadow::Shadow; | ||
| 10 | use crate::niri_render_elements; | 12 | use crate::niri_render_elements; |
| 11 | use crate::render_helpers::renderer::NiriRenderer; | 13 | use crate::render_helpers::renderer::NiriRenderer; |
| 14 | +use crate::render_helpers::shadow::ShadowRenderElement; | ||
| 12 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; | 15 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 13 | use crate::render_helpers::{RenderTarget, SplitElements}; | 16 | use crate::render_helpers::{RenderTarget, SplitElements}; |
| 14 | 17 | ||
| @@ -22,29 +25,56 @@ pub struct MappedLayer { | |||
| 22 | 25 | ||
| 23 | /// Buffer to draw instead of the surface when it should be blocked out. | 26 | /// Buffer to draw instead of the surface when it should be blocked out. |
| 24 | block_out_buffer: SolidColorBuffer, | 27 | block_out_buffer: SolidColorBuffer, |
| 28 | + | ||
| 29 | + /// The shadow around the surface. | ||
| 30 | + shadow: Shadow, | ||
| 25 | } | 31 | } |
| 26 | 32 | ||
| 27 | niri_render_elements! { | 33 | niri_render_elements! { |
| 28 | LayerSurfaceRenderElement<R> => { | 34 | LayerSurfaceRenderElement<R> => { |
| 29 | Wayland = WaylandSurfaceRenderElement<R>, | 35 | Wayland = WaylandSurfaceRenderElement<R>, |
| 30 | SolidColor = SolidColorRenderElement, | 36 | SolidColor = SolidColorRenderElement, |
| 37 | + Shadow = ShadowRenderElement, | ||
| 31 | } | 38 | } |
| 32 | } | 39 | } |
| 33 | 40 | ||
| 34 | impl MappedLayer { | 41 | impl MappedLayer { |
| 35 | - pub fn new(surface: LayerSurface, rules: ResolvedLayerRules) -> Self { | 42 | + pub fn new(surface: LayerSurface, rules: ResolvedLayerRules, config: &Config) -> Self { |
| 43 | + let mut shadow_config = config.layout.shadow; | ||
| 44 | + // Shadows for layer surfaces need to be explicitly enabled. | ||
| 45 | + shadow_config.on = false; | ||
| 46 | + let shadow_config = rules.shadow.resolve_against(shadow_config); | ||
| 47 | + | ||
| 36 | Self { | 48 | Self { |
| 37 | surface, | 49 | surface, |
| 38 | rules, | 50 | rules, |
| 39 | block_out_buffer: SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.]), | 51 | block_out_buffer: SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.]), |
| 52 | + shadow: Shadow::new(shadow_config), | ||
| 40 | } | 53 | } |
| 41 | } | 54 | } |
| 42 | 55 | ||
| 56 | + pub fn update_config(&mut self, config: &Config) { | ||
| 57 | + let mut shadow_config = config.layout.shadow; | ||
| 58 | + // Shadows for layer surfaces need to be explicitly enabled. | ||
| 59 | + shadow_config.on = false; | ||
| 60 | + let shadow_config = self.rules.shadow.resolve_against(shadow_config); | ||
| 61 | + self.shadow.update_config(shadow_config); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + pub fn update_shaders(&mut self) { | ||
| 65 | + self.shadow.update_shaders(); | ||
| 66 | + } | ||
| 67 | + | ||
| 43 | pub fn update_render_elements(&mut self, size: Size<f64, Logical>, scale: Scale<f64>) { | 68 | pub fn update_render_elements(&mut self, size: Size<f64, Logical>, scale: Scale<f64>) { |
| 44 | // Round to physical pixels. | 69 | // Round to physical pixels. |
| 45 | let size = size.to_physical_precise_round(scale).to_logical(scale); | 70 | let size = size.to_physical_precise_round(scale).to_logical(scale); |
| 46 | 71 | ||
| 47 | self.block_out_buffer.resize(size); | 72 | self.block_out_buffer.resize(size); |
| 73 | + | ||
| 74 | + let radius = self.rules.geometry_corner_radius.unwrap_or_default(); | ||
| 75 | + // FIXME: is_active based on keyboard focus? | ||
| 76 | + self.shadow | ||
| 77 | + .update_render_elements(size, true, radius, scale.x); | ||
| 48 | } | 78 | } |
| 49 | 79 | ||
| 50 | pub fn surface(&self) -> &LayerSurface { | 80 | pub fn surface(&self) -> &LayerSurface { |
| @@ -81,6 +111,7 @@ impl MappedLayer { | |||
| 81 | // Round to physical pixels. | 111 | // Round to physical pixels. |
| 82 | let location = location.to_physical_precise_round(scale).to_logical(scale); | 112 | let location = location.to_physical_precise_round(scale).to_logical(scale); |
| 83 | 113 | ||
| 114 | + // FIXME: take geometry-corner-radius into account. | ||
| 84 | let elem = SolidColorRenderElement::from_buffer( | 115 | let elem = SolidColorRenderElement::from_buffer( |
| 85 | &self.block_out_buffer, | 116 | &self.block_out_buffer, |
| 86 | location, | 117 | location, |
| @@ -117,6 +148,10 @@ impl MappedLayer { | |||
| 117 | ); | 148 | ); |
| 118 | } | 149 | } |
| 119 | 150 | ||
| 151 | + let location = location.to_physical_precise_round(scale).to_logical(scale); | ||
| 152 | + rv.normal | ||
| 153 | + .extend(self.shadow.render(renderer, location).map(Into::into)); | ||
| 154 | + | ||
| 120 | rv | 155 | rv |
| 121 | } | 156 | } |
| 122 | } | 157 | } |
src/layer/mod.rsmodified+23 -1
| @@ -1,5 +1,5 @@ | |||
| 1 | use niri_config::layer_rule::{LayerRule, Match}; | 1 | use niri_config::layer_rule::{LayerRule, Match}; |
| 2 | -use niri_config::BlockOutFrom; | 2 | +use niri_config::{BlockOutFrom, CornerRadius, ShadowRule}; |
| 3 | use smithay::desktop::LayerSurface; | 3 | use smithay::desktop::LayerSurface; |
| 4 | 4 | ||
| 5 | pub mod mapped; | 5 | pub mod mapped; |
| @@ -13,6 +13,12 @@ pub struct ResolvedLayerRules { | |||
| 13 | 13 | ||
| 14 | /// Whether to block out this layer surface from certain render targets. | 14 | /// Whether to block out this layer surface from certain render targets. |
| 15 | pub block_out_from: Option<BlockOutFrom>, | 15 | pub block_out_from: Option<BlockOutFrom>, |
| 16 | + | ||
| 17 | + /// Shadow overrides. | ||
| 18 | + pub shadow: ShadowRule, | ||
| 19 | + | ||
| 20 | + /// Corner radius to assume this layer surface has. | ||
| 21 | + pub geometry_corner_radius: Option<CornerRadius>, | ||
| 16 | } | 22 | } |
| 17 | 23 | ||
| 18 | impl ResolvedLayerRules { | 24 | impl ResolvedLayerRules { |
| @@ -20,6 +26,17 @@ impl ResolvedLayerRules { | |||
| 20 | Self { | 26 | Self { |
| 21 | opacity: None, | 27 | opacity: None, |
| 22 | block_out_from: None, | 28 | block_out_from: None, |
| 29 | + shadow: ShadowRule { | ||
| 30 | + off: false, | ||
| 31 | + on: false, | ||
| 32 | + offset: None, | ||
| 33 | + softness: None, | ||
| 34 | + spread: None, | ||
| 35 | + draw_behind_window: None, | ||
| 36 | + color: None, | ||
| 37 | + inactive_color: None, | ||
| 38 | + }, | ||
| 39 | + geometry_corner_radius: None, | ||
| 23 | } | 40 | } |
| 24 | } | 41 | } |
| 25 | 42 | ||
| @@ -53,6 +70,11 @@ impl ResolvedLayerRules { | |||
| 53 | if let Some(x) = rule.block_out_from { | 70 | if let Some(x) = rule.block_out_from { |
| 54 | resolved.block_out_from = Some(x); | 71 | resolved.block_out_from = Some(x); |
| 55 | } | 72 | } |
| 73 | + if let Some(x) = rule.geometry_corner_radius { | ||
| 74 | + resolved.geometry_corner_radius = Some(x); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + resolved.shadow.merge_with(&rule.shadow); | ||
| 56 | } | 78 | } |
| 57 | 79 | ||
| 58 | resolved | 80 | resolved |
src/niri.rsmodified+6 -1
| @@ -1097,6 +1097,9 @@ impl State { | |||
| 1097 | } | 1097 | } |
| 1098 | 1098 | ||
| 1099 | self.niri.layout.update_config(&config); | 1099 | self.niri.layout.update_config(&config); |
| 1100 | + for mapped in self.niri.mapped_layer_surfaces.values_mut() { | ||
| 1101 | + mapped.update_config(&config); | ||
| 1102 | + } | ||
| 1100 | 1103 | ||
| 1101 | // Create new named workspaces. | 1104 | // Create new named workspaces. |
| 1102 | for ws_config in &config.workspaces { | 1105 | for ws_config in &config.workspaces { |
| @@ -5101,11 +5104,13 @@ impl Niri { | |||
| 5101 | 5104 | ||
| 5102 | let mut changed = false; | 5105 | let mut changed = false; |
| 5103 | { | 5106 | { |
| 5104 | - let rules = &self.config.borrow().layer_rules; | 5107 | + let config = self.config.borrow(); |
| 5108 | + let rules = &config.layer_rules; | ||
| 5105 | 5109 | ||
| 5106 | for mapped in self.mapped_layer_surfaces.values_mut() { | 5110 | for mapped in self.mapped_layer_surfaces.values_mut() { |
| 5107 | if mapped.recompute_layer_rules(rules, self.is_at_startup) { | 5111 | if mapped.recompute_layer_rules(rules, self.is_at_startup) { |
| 5108 | changed = true; | 5112 | changed = true; |
| 5113 | + mapped.update_config(&config); | ||
| 5109 | } | 5114 | } |
| 5110 | } | 5115 | } |
| 5111 | } | 5116 | } |
wiki/Configuration:-Layer-Rules.mdmodified+58 -0
| @@ -19,6 +19,19 @@ layer-rule { | |||
| 19 | opacity 0.5 | 19 | opacity 0.5 |
| 20 | block-out-from "screencast" | 20 | block-out-from "screencast" |
| 21 | // block-out-from "screen-capture" | 21 | // block-out-from "screen-capture" |
| 22 | + | ||
| 23 | + shadow { | ||
| 24 | + on | ||
| 25 | + // off | ||
| 26 | + softness 40 | ||
| 27 | + spread 5 | ||
| 28 | + offset x=0 y=5 | ||
| 29 | + draw-behind-window true | ||
| 30 | + color "#00000064" | ||
| 31 | + // inactive-color "#00000064" | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + geometry-corner-radius 12 | ||
| 22 | } | 35 | } |
| 23 | ``` | 36 | ``` |
| 24 | 37 | ||
| @@ -95,3 +108,48 @@ layer-rule { | |||
| 95 | opacity 0.95 | 108 | opacity 0.95 |
| 96 | } | 109 | } |
| 97 | ``` | 110 | ``` |
| 111 | + | ||
| 112 | +#### `shadow` | ||
| 113 | + | ||
| 114 | +<sup>Since: next release</sup> | ||
| 115 | + | ||
| 116 | +Override the shadow options for the surface. | ||
| 117 | + | ||
| 118 | +These rules have the same options as the normal shadow config in the [layout](./Configuration:-Layout.md) section, so check the documentation there. | ||
| 119 | + | ||
| 120 | +Unlike window shadows, layer surface shadows always need to be enabled with a layer rule. | ||
| 121 | +That is, enabling shadows in the layout config section won't automatically enable them for layer surfaces. | ||
| 122 | + | ||
| 123 | +> [!NOTE] | ||
| 124 | +> Layer surfaces have no way to tell niri about their *visual geometry*. | ||
| 125 | +> For example, if a layer surface includes some invisible margins (like mako), niri has no way of knowing that, and will draw the shadow behind the entire surface, including the invisible margins. | ||
| 126 | +> | ||
| 127 | +> So to use niri shadows, you'll need to configure layer-shell clients to remove their own margins or shadows. | ||
| 128 | + | ||
| 129 | +```kdl | ||
| 130 | +// Add a shadow for fuzzel. | ||
| 131 | +layer-rule { | ||
| 132 | + match namespace="^launcher$" | ||
| 133 | + | ||
| 134 | + shadow { | ||
| 135 | + on | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + // Fuzzel defaults to 10 px rounded corners. | ||
| 139 | + geometry-corner-radius 10 | ||
| 140 | +} | ||
| 141 | +``` | ||
| 142 | + | ||
| 143 | +#### `geometry-corner-radius` | ||
| 144 | + | ||
| 145 | +<sup>Since: next release</sup> | ||
| 146 | + | ||
| 147 | +Set the corner radius of the surface. | ||
| 148 | + | ||
| 149 | +This setting will only affect the shadow—it will round its corners to match the geometry corner radius. | ||
| 150 | + | ||
| 151 | +```kdl | ||
| 152 | +layer-rule { | ||
| 153 | + geometry-corner-radius 12 | ||
| 154 | +} | ||
| 155 | +``` | ||