forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

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