forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB64a9351Ivan Molodetskikh2 years ago1319 commits

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