forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBa6baef7Anton Kesy10 months ago2623 commits

tab_indicator.rs412 lines, 14.1 KiBHistoryRawDownload
1use std::iter::zip;
2use std::mem;
3
4use niri_config::{CornerRadius, Gradient, GradientRelativeTo, TabIndicatorPosition};
5use smithay::utils::{Logical, Point, Rectangle, Size};
6
7use super::tile::Tile;
8use super::LayoutElement;
9use crate::animation::{Animation, Clock};
10use crate::niri_render_elements;
11use crate::render_helpers::border::BorderRenderElement;
12use crate::render_helpers::renderer::NiriRenderer;
13use crate::utils::{
14 floor_logical_in_physical_max1, round_logical_in_physical, round_logical_in_physical_max1,
15};
16
17#[derive(Debug)]
18pub struct TabIndicator {
19 shader_locs: Vec<Point<f64, Logical>>,
20 shaders: Vec<BorderRenderElement>,
21 open_anim: Option<Animation>,
22 config: niri_config::TabIndicator,
23}
24
25#[derive(Debug)]
26pub struct TabInfo {
27 /// Gradient for the tab indicator.
28 pub gradient: Gradient,
29 /// Tab geometry in the same coordinate system as the area.
30 pub geometry: Rectangle<f64, Logical>,
31}
32
33niri_render_elements! {
34 TabIndicatorRenderElement => {
35 Gradient = BorderRenderElement,
36 }
37}
38
39impl TabIndicator {
40 pub fn new(config: niri_config::TabIndicator) -> Self {
41 Self {
42 shader_locs: Vec::new(),
43 shaders: Vec::new(),
44 open_anim: None,
45 config,
46 }
47 }
48
49 pub fn update_config(&mut self, config: niri_config::TabIndicator) {
50 self.config = config;
51 }
52
53 pub fn update_shaders(&mut self) {
54 for elem in &mut self.shaders {
55 elem.damage_all();
56 }
57 }
58
59 pub fn advance_animations(&mut self) {
60 if let Some(anim) = &mut self.open_anim {
61 if anim.is_done() {
62 self.open_anim = None;
63 }
64 }
65 }
66
67 pub fn are_animations_ongoing(&self) -> bool {
68 self.open_anim.is_some()
69 }
70
71 pub fn start_open_animation(&mut self, clock: Clock, config: niri_config::Animation) {
72 self.open_anim = Some(Animation::new(clock, 0., 1., 0., config));
73 }
74
75 fn tab_rects(
76 &self,
77 area: Rectangle<f64, Logical>,
78 count: usize,
79 scale: f64,
80 ) -> impl Iterator<Item = Rectangle<f64, Logical>> {
81 let round = |logical: f64| round_logical_in_physical(scale, logical);
82 let round_max1 = |logical: f64| round_logical_in_physical_max1(scale, logical);
83
84 let progress = self.open_anim.as_ref().map_or(1., |a| a.value().max(0.));
85
86 let width = round_max1(self.config.width);
87 let gap = self.config.gap;
88 let gap = round_max1(gap.abs()).copysign(gap);
89 let gaps_between = round_max1(self.config.gaps_between_tabs);
90
91 let position = self.config.position;
92 let side = match position {
93 TabIndicatorPosition::Left | TabIndicatorPosition::Right => area.size.h,
94 TabIndicatorPosition::Top | TabIndicatorPosition::Bottom => area.size.w,
95 };
96 let total_prop = self.config.length.total_proportion.unwrap_or(0.5);
97 let min_length = round(side * total_prop.clamp(0., 2.));
98
99 // Compute px_per_tab before applying the animation to gaps_between in order to avoid it
100 // growing and shrinking over the duration of the animation.
101 let pixel = 1. / scale;
102 let shortest_length = count as f64 * (pixel + gaps_between) - gaps_between;
103 let length = f64::max(min_length, shortest_length);
104 let px_per_tab = (length + gaps_between) / count as f64 - gaps_between;
105
106 let px_per_tab = px_per_tab * progress;
107 let gaps_between = round(self.config.gaps_between_tabs * progress);
108
109 let length = count as f64 * (px_per_tab + gaps_between) - gaps_between;
110 let px_per_tab = floor_logical_in_physical_max1(scale, px_per_tab);
111 let floored_length = count as f64 * (px_per_tab + gaps_between) - gaps_between;
112 let mut ones_left = ((length - floored_length) / pixel).round() as usize;
113
114 let mut shader_loc = Point::from((-gap - width, round((side - length) / 2.)));
115 match position {
116 TabIndicatorPosition::Left => (),
117 TabIndicatorPosition::Right => shader_loc.x = area.size.w + gap,
118 TabIndicatorPosition::Top => mem::swap(&mut shader_loc.x, &mut shader_loc.y),
119 TabIndicatorPosition::Bottom => {
120 shader_loc.x = shader_loc.y;
121 shader_loc.y = area.size.h + gap;
122 }
123 }
124 shader_loc += area.loc;
125
126 (0..count).map(move |_| {
127 let mut px_per_tab = px_per_tab;
128 if ones_left > 0 {
129 ones_left -= 1;
130 px_per_tab += pixel;
131 }
132
133 let loc = shader_loc;
134
135 match position {
136 TabIndicatorPosition::Left | TabIndicatorPosition::Right => {
137 shader_loc.y += px_per_tab + gaps_between
138 }
139 TabIndicatorPosition::Top | TabIndicatorPosition::Bottom => {
140 shader_loc.x += px_per_tab + gaps_between
141 }
142 }
143
144 let size = match position {
145 TabIndicatorPosition::Left | TabIndicatorPosition::Right => {
146 Size::from((width, px_per_tab))
147 }
148 TabIndicatorPosition::Top | TabIndicatorPosition::Bottom => {
149 Size::from((px_per_tab, width))
150 }
151 };
152
153 Rectangle::new(loc, size)
154 })
155 }
156
157 #[allow(clippy::too_many_arguments)]
158 pub fn update_render_elements(
159 &mut self,
160 enabled: bool,
161 // Geometry of the tabs area.
162 area: Rectangle<f64, Logical>,
163 // View rect relative to the tabs area.
164 area_view_rect: Rectangle<f64, Logical>,
165 // Tab count, should match the tabs iterator length.
166 tab_count: usize,
167 tabs: impl Iterator<Item = TabInfo>,
168 is_active: bool,
169 scale: f64,
170 ) {
171 if !enabled || self.config.off {
172 self.shader_locs.clear();
173 self.shaders.clear();
174 return;
175 }
176
177 let count = tab_count;
178 if self.config.hide_when_single_tab && count == 1 {
179 self.shader_locs.clear();
180 self.shaders.clear();
181 return;
182 }
183
184 self.shaders.resize_with(count, Default::default);
185 self.shader_locs.resize_with(count, Default::default);
186
187 let position = self.config.position;
188 let radius = self.config.corner_radius as f32;
189 let shared_rounded_corners = self.config.gaps_between_tabs == 0.;
190 let mut tabs_left = tab_count;
191
192 let rects = self.tab_rects(area, count, scale);
193 for ((shader, loc), (tab, rect)) in zip(
194 zip(&mut self.shaders, &mut self.shader_locs),
195 zip(tabs, rects),
196 ) {
197 *loc = rect.loc;
198
199 let mut gradient_area = match tab.gradient.relative_to {
200 GradientRelativeTo::Window => tab.geometry,
201 GradientRelativeTo::WorkspaceView => area_view_rect,
202 };
203 gradient_area.loc -= *loc;
204
205 let mut color_from = tab.gradient.from;
206 let mut color_to = tab.gradient.to;
207 if !is_active {
208 color_from *= 0.5;
209 color_to *= 0.5;
210 }
211
212 let radius = if shared_rounded_corners && tab_count > 1 {
213 if tabs_left == tab_count {
214 // First tab.
215 match position {
216 TabIndicatorPosition::Left | TabIndicatorPosition::Right => CornerRadius {
217 top_left: radius,
218 top_right: radius,
219 bottom_right: 0.,
220 bottom_left: 0.,
221 },
222 TabIndicatorPosition::Top | TabIndicatorPosition::Bottom => CornerRadius {
223 top_left: radius,
224 top_right: 0.,
225 bottom_right: 0.,
226 bottom_left: radius,
227 },
228 }
229 } else if tabs_left == 1 {
230 // Last tab.
231 match position {
232 TabIndicatorPosition::Left | TabIndicatorPosition::Right => CornerRadius {
233 top_left: 0.,
234 top_right: 0.,
235 bottom_right: radius,
236 bottom_left: radius,
237 },
238 TabIndicatorPosition::Top | TabIndicatorPosition::Bottom => CornerRadius {
239 top_left: 0.,
240 top_right: radius,
241 bottom_right: radius,
242 bottom_left: 0.,
243 },
244 }
245 } else {
246 // Tab in the middle.
247 CornerRadius::default()
248 }
249 } else {
250 // Separate tabs, or the only tab.
251 CornerRadius::from(radius)
252 };
253 let radius = radius.fit_to(rect.size.w as f32, rect.size.h as f32);
254 tabs_left -= 1;
255
256 shader.update(
257 rect.size,
258 gradient_area,
259 tab.gradient.in_,
260 color_from,
261 color_to,
262 ((tab.gradient.angle as f32) - 90.).to_radians(),
263 Rectangle::from_size(rect.size),
264 0.,
265 radius,
266 scale as f32,
267 1.,
268 );
269 }
270 }
271
272 pub fn hit(
273 &self,
274 area: Rectangle<f64, Logical>,
275 tab_count: usize,
276 scale: f64,
277 point: Point<f64, Logical>,
278 ) -> Option<usize> {
279 if self.config.off {
280 return None;
281 }
282
283 let count = tab_count;
284 if self.config.hide_when_single_tab && count == 1 {
285 return None;
286 }
287
288 self.tab_rects(area, count, scale)
289 .enumerate()
290 .find_map(|(idx, rect)| rect.contains(point).then_some(idx))
291 }
292
293 pub fn render(
294 &self,
295 renderer: &mut impl NiriRenderer,
296 pos: Point<f64, Logical>,
297 push: &mut dyn FnMut(TabIndicatorRenderElement),
298 ) {
299 let has_border_shader = BorderRenderElement::has_shader(renderer);
300 if !has_border_shader {
301 return;
302 }
303
304 for (shader, loc) in zip(&self.shaders, &self.shader_locs) {
305 let elem = shader.clone().with_location(pos + *loc);
306 push(TabIndicatorRenderElement::from(elem));
307 }
308 }
309
310 /// Extra size occupied by the tab indicator.
311 pub fn extra_size(&self, tab_count: usize, scale: f64) -> Size<f64, Logical> {
312 if self.config.off
313 || !self.config.place_within_column
314 || (self.config.hide_when_single_tab && tab_count == 1)
315 {
316 return Size::from((0., 0.));
317 }
318
319 let round = |logical: f64| round_logical_in_physical(scale, logical);
320 let width = round(self.config.width);
321 let gap = round(self.config.gap);
322
323 // No, I am *not* falling into the rabbit hole of "what if the tab indicator is wide enough
324 // that it peeks from the other side of the window".
325 let size = f64::max(0., width + gap);
326
327 match self.config.position {
328 TabIndicatorPosition::Left | TabIndicatorPosition::Right => Size::from((size, 0.)),
329 TabIndicatorPosition::Top | TabIndicatorPosition::Bottom => Size::from((0., size)),
330 }
331 }
332
333 /// Offset of the tabbed content due to space occupied by the tab indicator.
334 pub fn content_offset(&self, tab_count: usize, scale: f64) -> Point<f64, Logical> {
335 match self.config.position {
336 TabIndicatorPosition::Left | TabIndicatorPosition::Top => {
337 self.extra_size(tab_count, scale).to_point()
338 }
339 TabIndicatorPosition::Right | TabIndicatorPosition::Bottom => Point::from((0., 0.)),
340 }
341 }
342
343 pub fn config(&self) -> niri_config::TabIndicator {
344 self.config
345 }
346}
347
348impl TabInfo {
349 pub fn from_tile<W: LayoutElement>(
350 tile: &Tile<W>,
351 position: Point<f64, Logical>,
352 is_active: bool,
353 is_urgent: bool,
354 config: &niri_config::TabIndicator,
355 ) -> Self {
356 let rules = tile.window().rules();
357 let rule = rules.tab_indicator;
358
359 let gradient_from_rule = || {
360 let (color, gradient) = if is_urgent {
361 (rule.urgent_color, rule.urgent_gradient)
362 } else if is_active {
363 (rule.active_color, rule.active_gradient)
364 } else {
365 (rule.inactive_color, rule.inactive_gradient)
366 };
367 let color = color.map(Gradient::from);
368 gradient.or(color)
369 };
370
371 let gradient_from_config = || {
372 let (color, gradient) = if is_urgent {
373 (config.urgent_color, config.urgent_gradient)
374 } else if is_active {
375 (config.active_color, config.active_gradient)
376 } else {
377 (config.inactive_color, config.inactive_gradient)
378 };
379 let color = color.map(Gradient::from);
380 gradient.or(color)
381 };
382
383 let gradient_from_border = || {
384 // Come up with tab indicator gradient matching the focus ring or the border, whichever
385 // one is enabled.
386 let focus_ring_config = tile.focus_ring().config();
387 let border_config = tile.border().config();
388 let config = if focus_ring_config.off {
389 border_config
390 } else {
391 focus_ring_config
392 };
393
394 let (color, gradient) = if is_urgent {
395 (config.urgent_color, config.urgent_gradient)
396 } else if is_active {
397 (config.active_color, config.active_gradient)
398 } else {
399 (config.inactive_color, config.inactive_gradient)
400 };
401 gradient.unwrap_or_else(|| Gradient::from(color))
402 };
403
404 let gradient = gradient_from_rule()
405 .or_else(gradient_from_config)
406 .unwrap_or_else(gradient_from_border);
407
408 let geometry = Rectangle::new(position, tile.animated_tile_size());
409
410 TabInfo { gradient, geometry }
411 }
412}