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