forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

focus_ring.rs274 lines, 9.2 KiBHistoryRawDownload
1use std::iter::zip;
2
3use arrayvec::ArrayVec;
4use niri_config::{CornerRadius, Gradient, GradientRelativeTo};
5use smithay::backend::renderer::element::{Element as _, Kind};
6use smithay::utils::{Logical, Point, Rectangle, Size};
7
8use crate::niri_render_elements;
9use crate::render_helpers::border::BorderRenderElement;
10use crate::render_helpers::renderer::NiriRenderer;
11use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};
12
13#[derive(Debug)]
14pub struct FocusRing {
15 buffers: [SolidColorBuffer; 8],
16 locations: [Point<f64, Logical>; 8],
17 sizes: [Size<f64, Logical>; 8],
18 borders: [BorderRenderElement; 8],
19 full_size: Size<f64, Logical>,
20 is_border: bool,
21 use_border_shader: bool,
22 config: niri_config::FocusRing,
23}
24
25niri_render_elements! {
26 FocusRingRenderElement => {
27 SolidColor = SolidColorRenderElement,
28 Gradient = BorderRenderElement,
29 }
30}
31
32impl FocusRing {
33 pub fn new(config: niri_config::FocusRing) -> Self {
34 Self {
35 buffers: Default::default(),
36 locations: Default::default(),
37 sizes: Default::default(),
38 borders: Default::default(),
39 full_size: Default::default(),
40 is_border: false,
41 use_border_shader: false,
42 config,
43 }
44 }
45
46 pub fn update_config(&mut self, config: niri_config::FocusRing) {
47 self.config = config;
48 }
49
50 pub fn update_shaders(&mut self) {
51 for elem in &mut self.borders {
52 elem.damage_all();
53 }
54 }
55
56 #[allow(clippy::too_many_arguments)]
57 pub fn update_render_elements(
58 &mut self,
59 win_size: Size<f64, Logical>,
60 is_active: bool,
61 is_border: bool,
62 is_urgent: bool,
63 view_rect: Rectangle<f64, Logical>,
64 radius: CornerRadius,
65 scale: f64,
66 alpha: f32,
67 ) {
68 let width = self.config.width.0;
69 self.full_size = win_size + Size::from((width, width)).upscale(2.);
70
71 let color = if is_urgent {
72 self.config.urgent_color
73 } else if is_active {
74 self.config.active_color
75 } else {
76 self.config.inactive_color
77 };
78
79 for buf in &mut self.buffers {
80 buf.set_color(color.to_array_premul());
81 }
82
83 let radius = radius.fit_to(self.full_size.w as f32, self.full_size.h as f32);
84
85 let gradient = if is_urgent {
86 self.config.urgent_gradient
87 } else if is_active {
88 self.config.active_gradient
89 } else {
90 self.config.inactive_gradient
91 };
92
93 self.use_border_shader = radius != CornerRadius::default() || gradient.is_some();
94
95 // Set the defaults for solid color + rounded corners.
96 let gradient = gradient.unwrap_or_else(|| Gradient::from(color));
97
98 let full_rect = Rectangle::new(Point::from((-width, -width)), self.full_size);
99 let gradient_area = match gradient.relative_to {
100 GradientRelativeTo::Window => full_rect,
101 GradientRelativeTo::WorkspaceView => view_rect,
102 };
103
104 let rounded_corner_border_width = if self.is_border {
105 // HACK: increase the border width used for the inner rounded corners a tiny bit to
106 // reduce background bleed.
107 width as f32 + 0.5
108 } else {
109 0.
110 };
111
112 let ceil = |logical: f64| (logical * scale).ceil() / scale;
113
114 // All of this stuff should end up aligned to physical pixels because:
115 // * Window size and border width are rounded to physical pixels before being passed to this
116 // function.
117 // * We will ceil the corner radii below.
118 // * We do not divide anything, only add, subtract and multiply by integers.
119 // * At rendering time, tile positions are rounded to physical pixels.
120
121 if is_border {
122 let top_left = f64::max(width, ceil(f64::from(radius.top_left)));
123 let top_right = f64::min(
124 self.full_size.w - top_left,
125 f64::max(width, ceil(f64::from(radius.top_right))),
126 );
127 let bottom_left = f64::min(
128 self.full_size.h - top_left,
129 f64::max(width, ceil(f64::from(radius.bottom_left))),
130 );
131 let bottom_right = f64::min(
132 self.full_size.h - top_right,
133 f64::min(
134 self.full_size.w - bottom_left,
135 f64::max(width, ceil(f64::from(radius.bottom_right))),
136 ),
137 );
138
139 // Top edge.
140 self.sizes[0] = Size::from((win_size.w + width * 2. - top_left - top_right, width));
141 self.locations[0] = Point::from((-width + top_left, -width));
142
143 // Bottom edge.
144 self.sizes[1] =
145 Size::from((win_size.w + width * 2. - bottom_left - bottom_right, width));
146 self.locations[1] = Point::from((-width + bottom_left, win_size.h));
147
148 // Left edge.
149 self.sizes[2] = Size::from((width, win_size.h + width * 2. - top_left - bottom_left));
150 self.locations[2] = Point::from((-width, -width + top_left));
151
152 // Right edge.
153 self.sizes[3] = Size::from((width, win_size.h + width * 2. - top_right - bottom_right));
154 self.locations[3] = Point::from((win_size.w, -width + top_right));
155
156 // Top-left corner.
157 self.sizes[4] = Size::from((top_left, top_left));
158 self.locations[4] = Point::from((-width, -width));
159
160 // Top-right corner.
161 self.sizes[5] = Size::from((top_right, top_right));
162 self.locations[5] = Point::from((win_size.w + width - top_right, -width));
163
164 // Bottom-right corner.
165 self.sizes[6] = Size::from((bottom_right, bottom_right));
166 self.locations[6] = Point::from((
167 win_size.w + width - bottom_right,
168 win_size.h + width - bottom_right,
169 ));
170
171 // Bottom-left corner.
172 self.sizes[7] = Size::from((bottom_left, bottom_left));
173 self.locations[7] = Point::from((-width, win_size.h + width - bottom_left));
174
175 for (buf, size) in zip(&mut self.buffers, self.sizes) {
176 buf.resize(size);
177 }
178
179 for (border, (loc, size)) in zip(&mut self.borders, zip(self.locations, self.sizes)) {
180 border.update(
181 size,
182 Rectangle::new(gradient_area.loc - loc, gradient_area.size),
183 gradient.in_,
184 gradient.from,
185 gradient.to,
186 ((gradient.angle as f32) - 90.).to_radians(),
187 Rectangle::new(full_rect.loc - loc, full_rect.size),
188 rounded_corner_border_width,
189 radius,
190 scale as f32,
191 alpha,
192 );
193 }
194 } else {
195 self.sizes[0] = self.full_size;
196 self.buffers[0].resize(self.sizes[0]);
197 self.locations[0] = Point::from((-width, -width));
198
199 self.borders[0].update(
200 self.sizes[0],
201 Rectangle::new(gradient_area.loc - self.locations[0], gradient_area.size),
202 gradient.in_,
203 gradient.from,
204 gradient.to,
205 ((gradient.angle as f32) - 90.).to_radians(),
206 Rectangle::new(full_rect.loc - self.locations[0], full_rect.size),
207 rounded_corner_border_width,
208 radius,
209 scale as f32,
210 alpha,
211 );
212 }
213
214 self.is_border = is_border;
215 }
216
217 pub fn render(
218 &self,
219 renderer: &mut impl NiriRenderer,
220 location: Point<f64, Logical>,
221 ) -> impl Iterator<Item = FocusRingRenderElement> {
222 let mut rv = ArrayVec::<_, 8>::new();
223
224 if self.config.off {
225 return rv.into_iter();
226 }
227
228 let border_width = -self.locations[0].y;
229
230 // If drawing as a border with width = 0, then there's nothing to draw.
231 if self.is_border && border_width == 0. {
232 return rv.into_iter();
233 }
234
235 let has_border_shader = BorderRenderElement::has_shader(renderer);
236
237 let mut push = |buffer, border: &BorderRenderElement, location: Point<f64, Logical>| {
238 let elem = if self.use_border_shader && has_border_shader {
239 border.clone().with_location(location).into()
240 } else {
241 let alpha = border.alpha();
242 SolidColorRenderElement::from_buffer(buffer, location, alpha, Kind::Unspecified)
243 .into()
244 };
245 rv.push(elem);
246 };
247
248 if self.is_border {
249 for ((buf, border), loc) in zip(zip(&self.buffers, &self.borders), self.locations) {
250 push(buf, border, location + loc);
251 }
252 } else {
253 push(
254 &self.buffers[0],
255 &self.borders[0],
256 location + self.locations[0],
257 );
258 }
259
260 rv.into_iter()
261 }
262
263 pub fn width(&self) -> f64 {
264 self.config.width.0
265 }
266
267 pub fn is_off(&self) -> bool {
268 self.config.off
269 }
270
271 pub fn config(&self) -> &niri_config::FocusRing {
272 &self.config
273 }
274}