forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9ff2f83Ivan Molodetskikh1 year ago2099 commits

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