niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::iter::zip; |
| 2 | |
| 3 | use niri_config::CornerRadius; |
| 4 | use smithay::utils::{Logical, Point, Rectangle, Size}; |
| 5 | |
| 6 | use crate::render_helpers::renderer::NiriRenderer; |
| 7 | use crate::render_helpers::shadow::ShadowRenderElement; |
| 8 | |
| 9 | #[derive(Debug)] |
| 10 | pub struct Shadow { |
| 11 | shader_rects: Vec<Rectangle<f64, Logical>>, |
| 12 | shaders: Vec<ShadowRenderElement>, |
| 13 | config: niri_config::Shadow, |
| 14 | } |
| 15 | |
| 16 | impl Shadow { |
| 17 | pub fn new(config: niri_config::Shadow) -> Self { |
| 18 | Self { |
| 19 | shader_rects: Vec::new(), |
| 20 | shaders: Vec::new(), |
| 21 | config, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | pub fn update_config(&mut self, config: niri_config::Shadow) { |
| 26 | self.config = config; |
| 27 | } |
| 28 | |
| 29 | pub fn update_shaders(&mut self) { |
| 30 | for elem in &mut self.shaders { |
| 31 | elem.damage_all(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | pub fn update_render_elements( |
| 36 | &mut self, |
| 37 | win_size: Size<f64, Logical>, |
| 38 | is_active: bool, |
| 39 | radius: CornerRadius, |
| 40 | scale: f64, |
| 41 | alpha: f32, |
| 42 | ) { |
| 43 | let ceil = |logical: f64| (logical * scale).ceil() / scale; |
| 44 | |
| 45 | // All of this stuff should end up aligned to physical pixels because: |
| 46 | // * Window size is rounded to physical pixels before being passed to this function. |
| 47 | // * We will ceil the corner radii below. |
| 48 | // * We do not divide anything, only add, subtract and multiply by integers. |
| 49 | // * At rendering time, tile positions are rounded to physical pixels. |
| 50 | |
| 51 | let width = self.config.softness; |
| 52 | // Like in CSS box-shadow. |
| 53 | let sigma = width / 2.; |
| 54 | // Adjust width to draw all necessary pixels. |
| 55 | let width = ceil(sigma * 3.); |
| 56 | |
| 57 | let offset = self.config.offset; |
| 58 | let offset = Point::from((ceil(offset.x.0), ceil(offset.y.0))); |
| 59 | |
| 60 | let spread = self.config.spread; |
| 61 | let spread = ceil(spread.abs()).copysign(spread); |
| 62 | let offset = offset - Point::from((spread, spread)); |
| 63 | |
| 64 | let win_radius = radius.fit_to(win_size.w as f32, win_size.h as f32); |
| 65 | |
| 66 | let box_size = if spread >= 0. { |
| 67 | win_size + Size::from((spread, spread)).upscale(2.) |
| 68 | } else { |
| 69 | // This is a saturating sub. |
| 70 | win_size - Size::from((-spread, -spread)).upscale(2.) |
| 71 | }; |
| 72 | let radius = win_radius.expanded_by(spread as f32); |
| 73 | |
| 74 | let shader_size = box_size + Size::from((width, width)).upscale(2.); |
| 75 | |
| 76 | let color = if is_active { |
| 77 | self.config.color |
| 78 | } else { |
| 79 | // Default to slightly more transparent. |
| 80 | self.config |
| 81 | .inactive_color |
| 82 | .unwrap_or(self.config.color * 0.75) |
| 83 | }; |
| 84 | |
| 85 | let shader_geo = Rectangle::new(Point::from((-width, -width)), shader_size); |
| 86 | |
| 87 | // This is actually offset relative to shader_geo, this is handled below. |
| 88 | let window_geo = Rectangle::new(Point::from((0., 0.)), win_size); |
| 89 | |
| 90 | if !self.config.draw_behind_window { |
| 91 | let top_left = ceil(f64::from(win_radius.top_left)); |
| 92 | let top_right = f64::min(win_size.w - top_left, ceil(f64::from(win_radius.top_right))); |
| 93 | let bottom_left = f64::min( |
| 94 | win_size.h - top_left, |
| 95 | ceil(f64::from(win_radius.bottom_left)), |
| 96 | ); |
| 97 | let bottom_right = f64::min( |
| 98 | win_size.h - top_right, |
| 99 | f64::min( |
| 100 | win_size.w - bottom_left, |
| 101 | ceil(f64::from(win_radius.bottom_right)), |
| 102 | ), |
| 103 | ); |
| 104 | |
| 105 | let top_left = Rectangle::new(Point::from((0., 0.)), Size::from((top_left, top_left))); |
| 106 | let top_right = Rectangle::new( |
| 107 | Point::from((win_size.w - top_right, 0.)), |
| 108 | Size::from((top_right, top_right)), |
| 109 | ); |
| 110 | let bottom_right = Rectangle::new( |
| 111 | Point::from((win_size.w - bottom_right, win_size.h - bottom_right)), |
| 112 | Size::from((bottom_right, bottom_right)), |
| 113 | ); |
| 114 | let bottom_left = Rectangle::new( |
| 115 | Point::from((0., win_size.h - bottom_left)), |
| 116 | Size::from((bottom_left, bottom_left)), |
| 117 | ); |
| 118 | |
| 119 | let mut background = |
| 120 | window_geo.subtract_rects([top_left, top_right, bottom_right, bottom_left]); |
| 121 | for rect in &mut background { |
| 122 | rect.loc -= offset; |
| 123 | } |
| 124 | |
| 125 | self.shader_rects = shader_geo.subtract_rects(background); |
| 126 | self.shaders |
| 127 | .resize_with(self.shader_rects.len(), Default::default); |
| 128 | |
| 129 | for (shader, rect) in zip(&mut self.shaders, &mut self.shader_rects) { |
| 130 | shader.update( |
| 131 | rect.size, |
| 132 | Rectangle::new(rect.loc.upscale(-1.), box_size), |
| 133 | color, |
| 134 | sigma as f32, |
| 135 | radius, |
| 136 | scale as f32, |
| 137 | Rectangle::new(window_geo.loc - offset - rect.loc, window_geo.size), |
| 138 | win_radius, |
| 139 | alpha, |
| 140 | ); |
| 141 | |
| 142 | rect.loc += offset; |
| 143 | } |
| 144 | } else { |
| 145 | self.shader_rects.resize_with(1, Default::default); |
| 146 | self.shader_rects[0] = shader_geo; |
| 147 | |
| 148 | self.shaders.resize_with(1, Default::default); |
| 149 | self.shaders[0].update( |
| 150 | shader_geo.size, |
| 151 | Rectangle::new(shader_geo.loc.upscale(-1.), box_size), |
| 152 | color, |
| 153 | sigma as f32, |
| 154 | radius, |
| 155 | scale as f32, |
| 156 | Rectangle::zero(), |
| 157 | Default::default(), |
| 158 | alpha, |
| 159 | ); |
| 160 | |
| 161 | self.shader_rects[0].loc += offset; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub fn render( |
| 166 | &self, |
| 167 | renderer: &mut impl NiriRenderer, |
| 168 | location: Point<f64, Logical>, |
| 169 | push: &mut dyn FnMut(ShadowRenderElement), |
| 170 | ) { |
| 171 | if !self.config.on { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | let has_shadow_shader = ShadowRenderElement::has_shader(renderer); |
| 176 | if !has_shadow_shader { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | for (shader, rect) in zip(&self.shaders, &self.shader_rects) { |
| 181 | push(shader.clone().with_location(location + rect.loc)); |
| 182 | } |
| 183 | } |
| 184 | } |