forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBdde9214Ivan Molodetskikh1 year ago1835 commits

shadow.rs178 lines, 6.0 KiBHistoryRawDownload
1use std::iter::zip;
2
3use niri_config::CornerRadius;
4use smithay::utils::{Logical, Point, Rectangle, Size};
5
6use crate::render_helpers::renderer::NiriRenderer;
7use crate::render_helpers::shadow::ShadowRenderElement;
8
9#[derive(Debug)]
10pub struct Shadow {
11 shader_rects: Vec<Rectangle<f64, Logical>>,
12 shaders: Vec<ShadowRenderElement>,
13 config: niri_config::Shadow,
14}
15
16impl 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.0;
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 = ceil(self.config.spread.0);
61 let offset = offset - Point::from((spread, spread));
62
63 let win_radius = radius.fit_to(win_size.w as f32, win_size.h as f32);
64
65 let box_size = win_size + Size::from((spread, spread)).upscale(2.);
66 let radius = win_radius.expanded_by(spread as f32);
67
68 let shader_size = box_size + Size::from((width, width)).upscale(2.);
69
70 let color = if is_active {
71 self.config.color
72 } else {
73 // Default to slightly more transparent.
74 self.config
75 .inactive_color
76 .unwrap_or(self.config.color * 0.75)
77 };
78
79 let shader_geo = Rectangle::new(Point::from((-width, -width)), shader_size);
80
81 // This is actually offset relative to shader_geo, this is handled below.
82 let window_geo = Rectangle::new(Point::from((0., 0.)), win_size);
83
84 if !self.config.draw_behind_window {
85 let top_left = ceil(f64::from(win_radius.top_left));
86 let top_right = f64::min(win_size.w - top_left, ceil(f64::from(win_radius.top_right)));
87 let bottom_left = f64::min(
88 win_size.h - top_left,
89 ceil(f64::from(win_radius.bottom_left)),
90 );
91 let bottom_right = f64::min(
92 win_size.h - top_right,
93 f64::min(
94 win_size.w - bottom_left,
95 ceil(f64::from(win_radius.bottom_right)),
96 ),
97 );
98
99 let top_left = Rectangle::new(Point::from((0., 0.)), Size::from((top_left, top_left)));
100 let top_right = Rectangle::new(
101 Point::from((win_size.w - top_right, 0.)),
102 Size::from((top_right, top_right)),
103 );
104 let bottom_right = Rectangle::new(
105 Point::from((win_size.w - bottom_right, win_size.h - bottom_right)),
106 Size::from((bottom_right, bottom_right)),
107 );
108 let bottom_left = Rectangle::new(
109 Point::from((0., win_size.h - bottom_left)),
110 Size::from((bottom_left, bottom_left)),
111 );
112
113 let mut background =
114 window_geo.subtract_rects([top_left, top_right, bottom_right, bottom_left]);
115 for rect in &mut background {
116 rect.loc -= offset;
117 }
118
119 self.shader_rects = shader_geo.subtract_rects(background);
120 self.shaders
121 .resize_with(self.shader_rects.len(), Default::default);
122
123 for (shader, rect) in zip(&mut self.shaders, &mut self.shader_rects) {
124 shader.update(
125 rect.size,
126 Rectangle::new(rect.loc.upscale(-1.), box_size),
127 color,
128 sigma as f32,
129 radius,
130 scale as f32,
131 Rectangle::new(window_geo.loc - offset - rect.loc, window_geo.size),
132 win_radius,
133 alpha,
134 );
135
136 rect.loc += offset;
137 }
138 } else {
139 self.shader_rects.resize_with(1, Default::default);
140 self.shader_rects[0] = shader_geo;
141
142 self.shaders.resize_with(1, Default::default);
143 self.shaders[0].update(
144 shader_geo.size,
145 Rectangle::new(shader_geo.loc.upscale(-1.), box_size),
146 color,
147 sigma as f32,
148 radius,
149 scale as f32,
150 Rectangle::zero(),
151 Default::default(),
152 alpha,
153 );
154
155 self.shader_rects[0].loc += offset;
156 }
157 }
158
159 pub fn render(
160 &self,
161 renderer: &mut impl NiriRenderer,
162 location: Point<f64, Logical>,
163 ) -> impl Iterator<Item = ShadowRenderElement> + '_ {
164 if !self.config.on {
165 return None.into_iter().flatten();
166 }
167
168 let has_shadow_shader = ShadowRenderElement::has_shader(renderer);
169 if !has_shadow_shader {
170 return None.into_iter().flatten();
171 }
172
173 let rv = zip(&self.shaders, &self.shader_rects)
174 .map(move |(shader, rect)| shader.clone().with_location(location + rect.loc));
175
176 Some(rv).into_iter().flatten()
177 }
178}