niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·90ef202·Ivan Molodetskikh·1 year ago·2349 commits
| 1 | use niri_config::{Config, LayerRule}; |
| 2 | use smithay::backend::renderer::element::surface::{ |
| 3 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, |
| 4 | }; |
| 5 | use smithay::backend::renderer::element::Kind; |
| 6 | use smithay::desktop::{LayerSurface, PopupManager}; |
| 7 | use smithay::utils::{Logical, Point, Scale, Size}; |
| 8 | use smithay::wayland::shell::wlr_layer::{ExclusiveZone, Layer}; |
| 9 | |
| 10 | use super::ResolvedLayerRules; |
| 11 | use crate::animation::Clock; |
| 12 | use crate::layout::shadow::Shadow; |
| 13 | use crate::niri_render_elements; |
| 14 | use crate::render_helpers::renderer::NiriRenderer; |
| 15 | use crate::render_helpers::shadow::ShadowRenderElement; |
| 16 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 17 | use crate::render_helpers::{RenderTarget, SplitElements}; |
| 18 | use crate::utils::{baba_is_float_offset, round_logical_in_physical}; |
| 19 | |
| 20 | #[derive(Debug)] |
| 21 | pub struct MappedLayer { |
| 22 | /// The surface itself. |
| 23 | surface: LayerSurface, |
| 24 | |
| 25 | /// Up-to-date rules. |
| 26 | rules: ResolvedLayerRules, |
| 27 | |
| 28 | /// Buffer to draw instead of the surface when it should be blocked out. |
| 29 | block_out_buffer: SolidColorBuffer, |
| 30 | |
| 31 | /// The shadow around the surface. |
| 32 | shadow: Shadow, |
| 33 | |
| 34 | /// The view size for the layer surface's output. |
| 35 | view_size: Size<f64, Logical>, |
| 36 | |
| 37 | /// Scale of the output the layer surface is on (and rounds its sizes to). |
| 38 | scale: f64, |
| 39 | |
| 40 | /// Clock for driving animations. |
| 41 | clock: Clock, |
| 42 | } |
| 43 | |
| 44 | niri_render_elements! { |
| 45 | LayerSurfaceRenderElement<R> => { |
| 46 | Wayland = WaylandSurfaceRenderElement<R>, |
| 47 | SolidColor = SolidColorRenderElement, |
| 48 | Shadow = ShadowRenderElement, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl MappedLayer { |
| 53 | pub fn new( |
| 54 | surface: LayerSurface, |
| 55 | rules: ResolvedLayerRules, |
| 56 | view_size: Size<f64, Logical>, |
| 57 | scale: f64, |
| 58 | clock: Clock, |
| 59 | config: &Config, |
| 60 | ) -> Self { |
| 61 | let mut shadow_config = config.layout.shadow; |
| 62 | // Shadows for layer surfaces need to be explicitly enabled. |
| 63 | shadow_config.on = false; |
| 64 | let shadow_config = rules.shadow.resolve_against(shadow_config); |
| 65 | |
| 66 | Self { |
| 67 | surface, |
| 68 | rules, |
| 69 | block_out_buffer: SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.]), |
| 70 | view_size, |
| 71 | scale, |
| 72 | shadow: Shadow::new(shadow_config), |
| 73 | clock, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pub fn update_config(&mut self, config: &Config) { |
| 78 | let mut shadow_config = config.layout.shadow; |
| 79 | // Shadows for layer surfaces need to be explicitly enabled. |
| 80 | shadow_config.on = false; |
| 81 | let shadow_config = self.rules.shadow.resolve_against(shadow_config); |
| 82 | self.shadow.update_config(shadow_config); |
| 83 | } |
| 84 | |
| 85 | pub fn update_shaders(&mut self) { |
| 86 | self.shadow.update_shaders(); |
| 87 | } |
| 88 | |
| 89 | pub fn update_sizes(&mut self, view_size: Size<f64, Logical>, scale: f64) { |
| 90 | self.view_size = view_size; |
| 91 | self.scale = scale; |
| 92 | } |
| 93 | |
| 94 | pub fn update_render_elements(&mut self, size: Size<f64, Logical>) { |
| 95 | // Round to physical pixels. |
| 96 | let size = size |
| 97 | .to_physical_precise_round(self.scale) |
| 98 | .to_logical(self.scale); |
| 99 | |
| 100 | self.block_out_buffer.resize(size); |
| 101 | |
| 102 | let radius = self.rules.geometry_corner_radius.unwrap_or_default(); |
| 103 | // FIXME: is_active based on keyboard focus? |
| 104 | self.shadow |
| 105 | .update_render_elements(size, true, radius, self.scale, 1.); |
| 106 | } |
| 107 | |
| 108 | pub fn are_animations_ongoing(&self) -> bool { |
| 109 | self.rules.baba_is_float |
| 110 | } |
| 111 | |
| 112 | pub fn surface(&self) -> &LayerSurface { |
| 113 | &self.surface |
| 114 | } |
| 115 | |
| 116 | pub fn rules(&self) -> &ResolvedLayerRules { |
| 117 | &self.rules |
| 118 | } |
| 119 | |
| 120 | /// Recomputes the resolved layer rules and returns whether they changed. |
| 121 | pub fn recompute_layer_rules(&mut self, rules: &[LayerRule], is_at_startup: bool) -> bool { |
| 122 | let new_rules = ResolvedLayerRules::compute(rules, &self.surface, is_at_startup); |
| 123 | if new_rules == self.rules { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | self.rules = new_rules; |
| 128 | true |
| 129 | } |
| 130 | |
| 131 | pub fn place_within_backdrop(&self) -> bool { |
| 132 | if !self.rules.place_within_backdrop { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if self.surface.layer() != Layer::Background { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | let state = self.surface.cached_state(); |
| 141 | if state.exclusive_zone != ExclusiveZone::DontCare { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | true |
| 146 | } |
| 147 | |
| 148 | pub fn bob_offset(&self) -> Point<f64, Logical> { |
| 149 | if !self.rules.baba_is_float { |
| 150 | return Point::from((0., 0.)); |
| 151 | } |
| 152 | |
| 153 | let y = baba_is_float_offset(self.clock.now(), self.view_size.h); |
| 154 | let y = round_logical_in_physical(self.scale, y); |
| 155 | Point::from((0., y)) |
| 156 | } |
| 157 | |
| 158 | pub fn render<R: NiriRenderer>( |
| 159 | &self, |
| 160 | renderer: &mut R, |
| 161 | location: Point<f64, Logical>, |
| 162 | target: RenderTarget, |
| 163 | ) -> SplitElements<LayerSurfaceRenderElement<R>> { |
| 164 | let mut rv = SplitElements::default(); |
| 165 | |
| 166 | let scale = Scale::from(self.scale); |
| 167 | let alpha = self.rules.opacity.unwrap_or(1.).clamp(0., 1.); |
| 168 | let location = location + self.bob_offset(); |
| 169 | |
| 170 | if target.should_block_out(self.rules.block_out_from) { |
| 171 | // Round to physical pixels. |
| 172 | let location = location.to_physical_precise_round(scale).to_logical(scale); |
| 173 | |
| 174 | // FIXME: take geometry-corner-radius into account. |
| 175 | let elem = SolidColorRenderElement::from_buffer( |
| 176 | &self.block_out_buffer, |
| 177 | location, |
| 178 | alpha, |
| 179 | Kind::Unspecified, |
| 180 | ); |
| 181 | rv.normal.push(elem.into()); |
| 182 | } else { |
| 183 | // Layer surfaces don't have extra geometry like windows. |
| 184 | let buf_pos = location; |
| 185 | |
| 186 | let surface = self.surface.wl_surface(); |
| 187 | for (popup, popup_offset) in PopupManager::popups_for_surface(surface) { |
| 188 | // Layer surfaces don't have extra geometry like windows. |
| 189 | let offset = popup_offset - popup.geometry().loc; |
| 190 | |
| 191 | rv.popups.extend(render_elements_from_surface_tree( |
| 192 | renderer, |
| 193 | popup.wl_surface(), |
| 194 | (buf_pos + offset.to_f64()).to_physical_precise_round(scale), |
| 195 | scale, |
| 196 | alpha, |
| 197 | Kind::ScanoutCandidate, |
| 198 | )); |
| 199 | } |
| 200 | |
| 201 | rv.normal = render_elements_from_surface_tree( |
| 202 | renderer, |
| 203 | surface, |
| 204 | buf_pos.to_physical_precise_round(scale), |
| 205 | scale, |
| 206 | alpha, |
| 207 | Kind::ScanoutCandidate, |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | let location = location.to_physical_precise_round(scale).to_logical(scale); |
| 212 | rv.normal |
| 213 | .extend(self.shadow.render(renderer, location).map(Into::into)); |
| 214 | |
| 215 | rv |
| 216 | } |
| 217 | } |