forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBdde9214Ivan Molodetskikh1 year ago1835 commits

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