forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBa6baef7Anton Kesy10 months ago2623 commits

tile.rs1440 lines, 50.6 KiBHistoryRawDownload
1use core::f64;
2use std::rc::Rc;
3
4use niri_config::utils::MergeWith as _;
5use niri_config::{Color, CornerRadius, GradientInterpolation};
6use niri_ipc::WindowLayout;
7use smithay::backend::renderer::element::{Element, Kind};
8use smithay::backend::renderer::gles::GlesRenderer;
9use smithay::utils::{Logical, Point, Rectangle, Scale, Size};
10
11use super::focus_ring::{FocusRing, FocusRingRenderElement};
12use super::opening_window::{OpenAnimation, OpeningWindowRenderElement};
13use super::shadow::Shadow;
14use super::{
15 HitType, LayoutElement, LayoutElementRenderElement, LayoutElementRenderSnapshot, Options,
16 SizeFrac, RESIZE_ANIMATION_THRESHOLD,
17};
18use crate::animation::{Animation, Clock};
19use crate::layout::SizingMode;
20use crate::niri_render_elements;
21use crate::render_helpers::border::BorderRenderElement;
22use crate::render_helpers::clipped_surface::{ClippedSurfaceRenderElement, RoundedCornerDamage};
23use crate::render_helpers::damage::ExtraDamage;
24use crate::render_helpers::offscreen::{OffscreenBuffer, OffscreenRenderElement};
25use crate::render_helpers::renderer::NiriRenderer;
26use crate::render_helpers::resize::ResizeRenderElement;
27use crate::render_helpers::shadow::ShadowRenderElement;
28use crate::render_helpers::snapshot::RenderSnapshot;
29use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};
30use crate::render_helpers::RenderTarget;
31use crate::utils::transaction::Transaction;
32use crate::utils::{
33 baba_is_float_offset, round_logical_in_physical, round_logical_in_physical_max1,
34};
35
36/// Toplevel window with decorations.
37#[derive(Debug)]
38pub struct Tile<W: LayoutElement> {
39 /// The toplevel window itself.
40 window: W,
41
42 /// The border around the window.
43 border: FocusRing,
44
45 /// The focus ring around the window.
46 focus_ring: FocusRing,
47
48 /// The shadow around the window.
49 shadow: Shadow,
50
51 /// This tile's current sizing mode.
52 ///
53 /// This will update only when the `window` actually goes maximized or fullscreen, rather than
54 /// right away, to avoid black backdrop flicker before the window has had a chance to resize.
55 sizing_mode: SizingMode,
56
57 /// The black backdrop for fullscreen windows.
58 fullscreen_backdrop: SolidColorBuffer,
59
60 /// Whether the tile should float upon unfullscreening.
61 pub(super) restore_to_floating: bool,
62
63 /// The size that the window should assume when going floating.
64 ///
65 /// This is generally the last size the window had when it was floating. It can be unknown if
66 /// the window starts out in the tiling layout or fullscreen.
67 pub(super) floating_window_size: Option<Size<i32, Logical>>,
68
69 /// The position that the tile should assume when going floating, relative to the floating
70 /// space working area.
71 ///
72 /// This is generally the last position the tile had when it was floating. It can be unknown if
73 /// the window starts out in the tiling layout.
74 pub(super) floating_pos: Option<Point<f64, SizeFrac>>,
75
76 /// Currently selected preset width index when this tile is floating.
77 pub(super) floating_preset_width_idx: Option<usize>,
78
79 /// Currently selected preset height index when this tile is floating.
80 pub(super) floating_preset_height_idx: Option<usize>,
81
82 /// The animation upon opening a window.
83 open_animation: Option<OpenAnimation>,
84
85 /// The animation of the window resizing.
86 resize_animation: Option<ResizeAnimation>,
87
88 /// The animation of a tile visually moving horizontally.
89 move_x_animation: Option<MoveAnimation>,
90
91 /// The animation of a tile visually moving vertically.
92 move_y_animation: Option<MoveAnimation>,
93
94 /// The animation of the tile's opacity.
95 pub(super) alpha_animation: Option<AlphaAnimation>,
96
97 /// Offset during the initial interactive move rubberband.
98 pub(super) interactive_move_offset: Point<f64, Logical>,
99
100 /// Snapshot of the last render for use in the close animation.
101 unmap_snapshot: Option<TileRenderSnapshot>,
102
103 /// Extra damage for clipped surface corner radius changes.
104 rounded_corner_damage: RoundedCornerDamage,
105
106 /// The view size for the tile's workspace.
107 ///
108 /// Used as the fullscreen target size.
109 view_size: Size<f64, Logical>,
110
111 /// Scale of the output the tile is on (and rounds its sizes to).
112 scale: f64,
113
114 /// Clock for driving animations.
115 pub(super) clock: Clock,
116
117 /// Configurable properties of the layout.
118 pub(super) options: Rc<Options>,
119}
120
121niri_render_elements! {
122 TileRenderElement<R> => {
123 LayoutElement = LayoutElementRenderElement<R>,
124 FocusRing = FocusRingRenderElement,
125 SolidColor = SolidColorRenderElement,
126 Opening = OpeningWindowRenderElement,
127 Resize = ResizeRenderElement,
128 Border = BorderRenderElement,
129 Shadow = ShadowRenderElement,
130 ClippedSurface = ClippedSurfaceRenderElement<R>,
131 Offscreen = OffscreenRenderElement,
132 ExtraDamage = ExtraDamage,
133 }
134}
135
136pub type TileRenderSnapshot =
137 RenderSnapshot<TileRenderElement<GlesRenderer>, TileRenderElement<GlesRenderer>>;
138
139#[derive(Debug)]
140struct ResizeAnimation {
141 anim: Animation,
142 size_from: Size<f64, Logical>,
143 snapshot: LayoutElementRenderSnapshot,
144 offscreen: OffscreenBuffer,
145 tile_size_from: Size<f64, Logical>,
146 // If the resize involved the fullscreen state at some point, this is the progress toward the
147 // fullscreen state. Used for things like fullscreen backdrop alpha.
148 //
149 // Note that this can be set even if this specific resize is between two non-fullscreen states,
150 // for example when issuing a new resize during an unfullscreen resize.
151 fullscreen_progress: Option<Animation>,
152 // Similar to above but for fullscreen-or-maximized.
153 expanded_progress: Option<Animation>,
154}
155
156#[derive(Debug)]
157struct MoveAnimation {
158 anim: Animation,
159 from: f64,
160}
161
162#[derive(Debug)]
163pub(super) struct AlphaAnimation {
164 pub(super) anim: Animation,
165 /// Whether the animation should persist after it's done.
166 ///
167 /// This is used by things like interactive move which need to animate alpha to
168 /// semitransparent, then hold it at semitransparent for a while, until the operation
169 /// completes.
170 pub(super) hold_after_done: bool,
171 offscreen: OffscreenBuffer,
172}
173
174impl<W: LayoutElement> Tile<W> {
175 pub fn new(
176 window: W,
177 view_size: Size<f64, Logical>,
178 scale: f64,
179 clock: Clock,
180 options: Rc<Options>,
181 ) -> Self {
182 let rules = window.rules();
183 let border_config = options.layout.border.merged_with(&rules.border);
184 let focus_ring_config = options.layout.focus_ring.merged_with(&rules.focus_ring);
185 let shadow_config = options.layout.shadow.merged_with(&rules.shadow);
186 let sizing_mode = window.sizing_mode();
187
188 Self {
189 window,
190 border: FocusRing::new(border_config.into()),
191 focus_ring: FocusRing::new(focus_ring_config),
192 shadow: Shadow::new(shadow_config),
193 sizing_mode,
194 fullscreen_backdrop: SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.]),
195 restore_to_floating: false,
196 floating_window_size: None,
197 floating_pos: None,
198 floating_preset_width_idx: None,
199 floating_preset_height_idx: None,
200 open_animation: None,
201 resize_animation: None,
202 move_x_animation: None,
203 move_y_animation: None,
204 alpha_animation: None,
205 interactive_move_offset: Point::from((0., 0.)),
206 unmap_snapshot: None,
207 rounded_corner_damage: Default::default(),
208 view_size,
209 scale,
210 clock,
211 options,
212 }
213 }
214
215 pub fn update_config(
216 &mut self,
217 view_size: Size<f64, Logical>,
218 scale: f64,
219 options: Rc<Options>,
220 ) {
221 // If preset widths or heights changed, clear our stored preset index.
222 if self.options.layout.preset_column_widths != options.layout.preset_column_widths {
223 self.floating_preset_width_idx = None;
224 }
225 if self.options.layout.preset_window_heights != options.layout.preset_window_heights {
226 self.floating_preset_height_idx = None;
227 }
228
229 self.view_size = view_size;
230 self.scale = scale;
231 self.options = options;
232
233 let round_max1 = |logical| round_logical_in_physical_max1(self.scale, logical);
234
235 let rules = self.window.rules();
236
237 let mut border_config = self.options.layout.border.merged_with(&rules.border);
238 border_config.width = round_max1(border_config.width);
239 self.border.update_config(border_config.into());
240
241 let mut focus_ring_config = self
242 .options
243 .layout
244 .focus_ring
245 .merged_with(&rules.focus_ring);
246 focus_ring_config.width = round_max1(focus_ring_config.width);
247 self.focus_ring.update_config(focus_ring_config);
248
249 let shadow_config = self.options.layout.shadow.merged_with(&rules.shadow);
250 self.shadow.update_config(shadow_config);
251 }
252
253 pub fn update_shaders(&mut self) {
254 self.border.update_shaders();
255 self.focus_ring.update_shaders();
256 self.shadow.update_shaders();
257 }
258
259 pub fn update_window(&mut self) {
260 let prev_sizing_mode = self.sizing_mode;
261 self.sizing_mode = self.window.sizing_mode();
262
263 if let Some(animate_from) = self.window.take_animation_snapshot() {
264 let params = if let Some(resize) = self.resize_animation.take() {
265 // Compute like in animated_window_size(), but using the snapshot geometry (since
266 // the current one is already overwritten).
267 let mut size = animate_from.size;
268
269 let val = resize.anim.value();
270 let size_from = resize.size_from;
271 let tile_size_from = resize.tile_size_from;
272
273 size.w = size_from.w + (size.w - size_from.w) * val;
274 size.h = size_from.h + (size.h - size_from.h) * val;
275
276 let mut tile_size = animate_from.size;
277 if prev_sizing_mode.is_fullscreen() {
278 tile_size.w = f64::max(tile_size.w, self.view_size.w);
279 tile_size.h = f64::max(tile_size.h, self.view_size.h);
280 } else if prev_sizing_mode.is_normal() && !self.border.is_off() {
281 let width = self.border.width();
282 tile_size.w += width * 2.;
283 tile_size.h += width * 2.;
284 }
285
286 tile_size.w = tile_size_from.w + (tile_size.w - tile_size_from.w) * val;
287 tile_size.h = tile_size_from.h + (tile_size.h - tile_size_from.h) * val;
288
289 let fullscreen_from = resize
290 .fullscreen_progress
291 .map(|anim| anim.clamped_value().clamp(0., 1.))
292 .unwrap_or(if prev_sizing_mode.is_fullscreen() {
293 1.
294 } else {
295 0.
296 });
297
298 let expanded_from = resize
299 .expanded_progress
300 .map(|anim| anim.clamped_value().clamp(0., 1.))
301 .unwrap_or(if prev_sizing_mode.is_normal() { 0. } else { 1. });
302
303 // Also try to reuse the existing offscreen buffer if we have one.
304 (
305 size,
306 tile_size,
307 fullscreen_from,
308 expanded_from,
309 resize.offscreen,
310 )
311 } else {
312 let size = animate_from.size;
313
314 // Compute like in tile_size().
315 let mut tile_size = size;
316 if prev_sizing_mode.is_fullscreen() {
317 tile_size.w = f64::max(tile_size.w, self.view_size.w);
318 tile_size.h = f64::max(tile_size.h, self.view_size.h);
319 } else if prev_sizing_mode.is_normal() && !self.border.is_off() {
320 let width = self.border.width();
321 tile_size.w += width * 2.;
322 tile_size.h += width * 2.;
323 }
324
325 let fullscreen_from = if prev_sizing_mode.is_fullscreen() {
326 1.
327 } else {
328 0.
329 };
330
331 let expanded_from = if prev_sizing_mode.is_normal() { 0. } else { 1. };
332
333 (
334 size,
335 tile_size,
336 fullscreen_from,
337 expanded_from,
338 OffscreenBuffer::default(),
339 )
340 };
341 let (size_from, tile_size_from, fullscreen_from, expanded_from, offscreen) = params;
342
343 let change = self.window.size().to_f64().to_point() - size_from.to_point();
344 let change = f64::max(change.x.abs(), change.y.abs());
345 let tile_change = self.tile_size().to_f64().to_point() - tile_size_from.to_point();
346 let tile_change = f64::max(tile_change.x.abs(), tile_change.y.abs());
347 let change = f64::max(change, tile_change);
348 if change > RESIZE_ANIMATION_THRESHOLD {
349 let anim = Animation::new(
350 self.clock.clone(),
351 0.,
352 1.,
353 0.,
354 self.options.animations.window_resize.anim,
355 );
356
357 let fullscreen_to = if self.sizing_mode.is_fullscreen() {
358 1.
359 } else {
360 0.
361 };
362 let expanded_to = if self.sizing_mode.is_normal() { 0. } else { 1. };
363 let fullscreen_progress = (fullscreen_from != fullscreen_to)
364 .then(|| anim.restarted(fullscreen_from, fullscreen_to, 0.));
365 let expanded_progress = (expanded_from != expanded_to)
366 .then(|| anim.restarted(expanded_from, expanded_to, 0.));
367
368 self.resize_animation = Some(ResizeAnimation {
369 anim,
370 size_from,
371 snapshot: animate_from,
372 offscreen,
373 tile_size_from,
374 fullscreen_progress,
375 expanded_progress,
376 });
377 } else {
378 self.resize_animation = None;
379 }
380 }
381
382 let round_max1 = |logical| round_logical_in_physical_max1(self.scale, logical);
383
384 let rules = self.window.rules();
385 let mut border_config = self.options.layout.border.merged_with(&rules.border);
386 border_config.width = round_max1(border_config.width);
387 self.border.update_config(border_config.into());
388
389 let mut focus_ring_config = self
390 .options
391 .layout
392 .focus_ring
393 .merged_with(&rules.focus_ring);
394 focus_ring_config.width = round_max1(focus_ring_config.width);
395 self.focus_ring.update_config(focus_ring_config);
396
397 let shadow_config = self.options.layout.shadow.merged_with(&rules.shadow);
398 self.shadow.update_config(shadow_config);
399
400 let window_size = self.window_size();
401 let radius = rules
402 .geometry_corner_radius
403 .unwrap_or_default()
404 .fit_to(window_size.w as f32, window_size.h as f32);
405 self.rounded_corner_damage.set_corner_radius(radius);
406 self.rounded_corner_damage.set_size(window_size);
407 }
408
409 pub fn advance_animations(&mut self) {
410 if let Some(open) = &mut self.open_animation {
411 if open.is_done() {
412 self.open_animation = None;
413 }
414 }
415
416 if let Some(resize) = &mut self.resize_animation {
417 if resize.anim.is_done() {
418 self.resize_animation = None;
419 }
420 }
421
422 if let Some(move_) = &mut self.move_x_animation {
423 if move_.anim.is_done() {
424 self.move_x_animation = None;
425 }
426 }
427 if let Some(move_) = &mut self.move_y_animation {
428 if move_.anim.is_done() {
429 self.move_y_animation = None;
430 }
431 }
432
433 if let Some(alpha) = &mut self.alpha_animation {
434 if !alpha.hold_after_done && alpha.anim.is_done() {
435 self.alpha_animation = None;
436 }
437 }
438 }
439
440 pub fn are_animations_ongoing(&self) -> bool {
441 self.are_transitions_ongoing() || self.window.rules().baba_is_float == Some(true)
442 }
443
444 pub fn are_transitions_ongoing(&self) -> bool {
445 self.open_animation.is_some()
446 || self.resize_animation.is_some()
447 || self.move_x_animation.is_some()
448 || self.move_y_animation.is_some()
449 || self
450 .alpha_animation
451 .as_ref()
452 .is_some_and(|alpha| !alpha.anim.is_done())
453 }
454
455 pub fn update_render_elements(&mut self, is_active: bool, view_rect: Rectangle<f64, Logical>) {
456 let rules = self.window.rules();
457 let animated_tile_size = self.animated_tile_size();
458 let expanded_progress = self.expanded_progress();
459
460 let draw_border_with_background = rules
461 .draw_border_with_background
462 .unwrap_or_else(|| !self.window.has_ssd());
463 let border_width = self.visual_border_width().unwrap_or(0.);
464
465 // Do the inverse of tile_size() in order to handle the unfullscreen animation for windows
466 // that were smaller than the fullscreen size, and therefore their animated_window_size() is
467 // currently much smaller than the tile size.
468 let mut border_window_size = animated_tile_size;
469 border_window_size.w -= border_width * 2.;
470 border_window_size.h -= border_width * 2.;
471
472 let radius = rules
473 .geometry_corner_radius
474 .map_or(CornerRadius::default(), |radius| {
475 radius.expanded_by(border_width as f32)
476 })
477 .scaled_by(1. - expanded_progress as f32);
478 self.border.update_render_elements(
479 border_window_size,
480 is_active,
481 !draw_border_with_background,
482 self.window.is_urgent(),
483 Rectangle::new(
484 view_rect.loc - Point::from((border_width, border_width)),
485 view_rect.size,
486 ),
487 radius,
488 self.scale,
489 1. - expanded_progress as f32,
490 );
491
492 let radius = if self.visual_border_width().is_some() {
493 radius
494 } else {
495 rules
496 .geometry_corner_radius
497 .unwrap_or_default()
498 .scaled_by(1. - expanded_progress as f32)
499 };
500 self.shadow.update_render_elements(
501 animated_tile_size,
502 is_active,
503 radius,
504 self.scale,
505 1. - expanded_progress as f32,
506 );
507
508 let draw_focus_ring_with_background = if self.border.is_off() {
509 draw_border_with_background
510 } else {
511 false
512 };
513 let radius = radius.expanded_by(self.focus_ring.width() as f32);
514 self.focus_ring.update_render_elements(
515 animated_tile_size,
516 is_active,
517 !draw_focus_ring_with_background,
518 self.window.is_urgent(),
519 view_rect,
520 radius,
521 self.scale,
522 1. - expanded_progress as f32,
523 );
524
525 self.fullscreen_backdrop.resize(animated_tile_size);
526 }
527
528 pub fn scale(&self) -> f64 {
529 self.scale
530 }
531
532 pub fn render_offset(&self) -> Point<f64, Logical> {
533 let mut offset = Point::from((0., 0.));
534
535 if let Some(move_) = &self.move_x_animation {
536 offset.x += move_.from * move_.anim.value();
537 }
538 if let Some(move_) = &self.move_y_animation {
539 offset.y += move_.from * move_.anim.value();
540 }
541
542 offset += self.interactive_move_offset;
543
544 offset
545 }
546
547 pub fn start_open_animation(&mut self) {
548 self.open_animation = Some(OpenAnimation::new(Animation::new(
549 self.clock.clone(),
550 0.,
551 1.,
552 0.,
553 self.options.animations.window_open.anim,
554 )));
555 }
556
557 pub fn resize_animation(&self) -> Option<&Animation> {
558 self.resize_animation.as_ref().map(|resize| &resize.anim)
559 }
560
561 pub fn animate_move_from(&mut self, from: Point<f64, Logical>) {
562 self.animate_move_x_from(from.x);
563 self.animate_move_y_from(from.y);
564 }
565
566 pub fn animate_move_x_from(&mut self, from: f64) {
567 self.animate_move_x_from_with_config(from, self.options.animations.window_movement.0);
568 }
569
570 pub fn animate_move_x_from_with_config(&mut self, from: f64, config: niri_config::Animation) {
571 let current_offset = self.render_offset().x;
572
573 // Preserve the previous config if ongoing.
574 let anim = self.move_x_animation.take().map(|move_| move_.anim);
575 let anim = anim
576 .map(|anim| anim.restarted(1., 0., 0.))
577 .unwrap_or_else(|| Animation::new(self.clock.clone(), 1., 0., 0., config));
578
579 self.move_x_animation = Some(MoveAnimation {
580 anim,
581 from: from + current_offset,
582 });
583 }
584
585 pub fn animate_move_y_from(&mut self, from: f64) {
586 self.animate_move_y_from_with_config(from, self.options.animations.window_movement.0);
587 }
588
589 pub fn animate_move_y_from_with_config(&mut self, from: f64, config: niri_config::Animation) {
590 let current_offset = self.render_offset().y;
591
592 // Preserve the previous config if ongoing.
593 let anim = self.move_y_animation.take().map(|move_| move_.anim);
594 let anim = anim
595 .map(|anim| anim.restarted(1., 0., 0.))
596 .unwrap_or_else(|| Animation::new(self.clock.clone(), 1., 0., 0., config));
597
598 self.move_y_animation = Some(MoveAnimation {
599 anim,
600 from: from + current_offset,
601 });
602 }
603
604 pub fn offset_move_y_anim_current(&mut self, offset: f64) {
605 if let Some(move_) = self.move_y_animation.as_mut() {
606 // If the anim is almost done, there's little point trying to offset it; we can let
607 // things jump. If it turns out like a bad idea, we could restart the anim instead.
608 let value = move_.anim.value();
609 if value > 0.001 {
610 move_.from += offset / value;
611 }
612 }
613 }
614
615 pub fn stop_move_animations(&mut self) {
616 self.move_x_animation = None;
617 self.move_y_animation = None;
618 }
619
620 pub fn animate_alpha(&mut self, from: f64, to: f64, config: niri_config::Animation) {
621 let from = from.clamp(0., 1.);
622 let to = to.clamp(0., 1.);
623
624 let (current, offscreen) = if let Some(alpha) = self.alpha_animation.take() {
625 (alpha.anim.clamped_value(), alpha.offscreen)
626 } else {
627 (from, OffscreenBuffer::default())
628 };
629
630 self.alpha_animation = Some(AlphaAnimation {
631 anim: Animation::new(self.clock.clone(), current, to, 0., config),
632 hold_after_done: false,
633 offscreen,
634 });
635 }
636
637 pub fn ensure_alpha_animates_to_1(&mut self) {
638 if let Some(alpha) = &self.alpha_animation {
639 if alpha.anim.to() != 1. {
640 // Cancel animation instead of starting a new one because the user likely wants to
641 // see the tile right away.
642 self.alpha_animation = None;
643 }
644 }
645 }
646
647 pub fn hold_alpha_animation_after_done(&mut self) {
648 if let Some(alpha) = &mut self.alpha_animation {
649 alpha.hold_after_done = true;
650 }
651 }
652
653 pub fn window(&self) -> &W {
654 &self.window
655 }
656
657 pub fn window_mut(&mut self) -> &mut W {
658 &mut self.window
659 }
660
661 pub fn sizing_mode(&self) -> SizingMode {
662 self.sizing_mode
663 }
664
665 fn fullscreen_progress(&self) -> f64 {
666 if let Some(resize) = &self.resize_animation {
667 if let Some(anim) = &resize.fullscreen_progress {
668 return anim.clamped_value().clamp(0., 1.);
669 }
670 }
671
672 if self.sizing_mode.is_fullscreen() {
673 1.
674 } else {
675 0.
676 }
677 }
678
679 fn expanded_progress(&self) -> f64 {
680 if let Some(resize) = &self.resize_animation {
681 if let Some(anim) = &resize.expanded_progress {
682 return anim.clamped_value().clamp(0., 1.);
683 }
684 }
685
686 if self.sizing_mode.is_normal() {
687 0.
688 } else {
689 1.
690 }
691 }
692
693 /// Returns `None` if the border is hidden and `Some(width)` if it should be shown.
694 pub fn effective_border_width(&self) -> Option<f64> {
695 if !self.sizing_mode.is_normal() {
696 return None;
697 }
698
699 if self.border.is_off() {
700 return None;
701 }
702
703 Some(self.border.width())
704 }
705
706 fn visual_border_width(&self) -> Option<f64> {
707 if self.border.is_off() {
708 return None;
709 }
710
711 let expanded_progress = self.expanded_progress();
712
713 // Only hide the border when fully expanded to avoid jarring border appearance.
714 if expanded_progress == 1. {
715 return None;
716 }
717
718 // FIXME: would be cool to, like, gradually resize the border from full width to 0 during
719 // fullscreening, but the rest of the code isn't quite ready for that yet. It needs to
720 // handle things like computing intermediate tile size when an animated resize starts during
721 // an animated unfullscreen resize.
722 Some(self.border.width())
723 }
724
725 /// Returns the location of the window's visual geometry within this Tile.
726 pub fn window_loc(&self) -> Point<f64, Logical> {
727 let mut loc = Point::from((0., 0.));
728
729 let window_size = self.animated_window_size();
730 let target_size = self.animated_tile_size();
731
732 // Center the window within its tile.
733 //
734 // - Without borders, the sizes match, so this difference is zero.
735 // - Borders always match from all sides, so this difference is pre-rounded to physical.
736 // - In fullscreen, if the window is smaller than the tile, then it gets centered, otherwise
737 // the tile size matches the window.
738 // - During animations, the window remains centered within the tile; this is important for
739 // the to/from fullscreen animation.
740 loc.x += (target_size.w - window_size.w) / 2.;
741 loc.y += (target_size.h - window_size.h) / 2.;
742
743 // Round to physical pixels.
744 loc = loc
745 .to_physical_precise_round(self.scale)
746 .to_logical(self.scale);
747
748 loc
749 }
750
751 pub fn tile_size(&self) -> Size<f64, Logical> {
752 let mut size = self.window_size();
753
754 if self.sizing_mode.is_fullscreen() {
755 // Normally we'd just return the fullscreen size here, but this makes things a bit
756 // nicer if a fullscreen window is bigger than the fullscreen size for some reason.
757 size.w = f64::max(size.w, self.view_size.w);
758 size.h = f64::max(size.h, self.view_size.h);
759 return size;
760 }
761
762 if let Some(width) = self.effective_border_width() {
763 size.w += width * 2.;
764 size.h += width * 2.;
765 }
766
767 size
768 }
769
770 pub fn tile_expected_or_current_size(&self) -> Size<f64, Logical> {
771 let mut size = self.window_expected_or_current_size();
772
773 if self.sizing_mode.is_fullscreen() {
774 // Normally we'd just return the fullscreen size here, but this makes things a bit
775 // nicer if a fullscreen window is bigger than the fullscreen size for some reason.
776 size.w = f64::max(size.w, self.view_size.w);
777 size.h = f64::max(size.h, self.view_size.h);
778 return size;
779 }
780
781 if let Some(width) = self.effective_border_width() {
782 size.w += width * 2.;
783 size.h += width * 2.;
784 }
785
786 size
787 }
788
789 pub fn window_size(&self) -> Size<f64, Logical> {
790 let mut size = self.window.size().to_f64();
791 size = size
792 .to_physical_precise_round(self.scale)
793 .to_logical(self.scale);
794 size
795 }
796
797 pub fn window_expected_or_current_size(&self) -> Size<f64, Logical> {
798 let size = self.window.expected_size();
799 let mut size = size.unwrap_or_else(|| self.window.size()).to_f64();
800 size = size
801 .to_physical_precise_round(self.scale)
802 .to_logical(self.scale);
803 size
804 }
805
806 pub fn animated_window_size(&self) -> Size<f64, Logical> {
807 let mut size = self.window_size();
808
809 if let Some(resize) = &self.resize_animation {
810 let val = resize.anim.value();
811 let size_from = resize.size_from.to_f64();
812
813 size.w = f64::max(1., size_from.w + (size.w - size_from.w) * val);
814 size.h = f64::max(1., size_from.h + (size.h - size_from.h) * val);
815 size = size
816 .to_physical_precise_round(self.scale)
817 .to_logical(self.scale);
818 }
819
820 size
821 }
822
823 pub fn animated_tile_size(&self) -> Size<f64, Logical> {
824 let mut size = self.tile_size();
825
826 if let Some(resize) = &self.resize_animation {
827 let val = resize.anim.value();
828 let size_from = resize.tile_size_from.to_f64();
829
830 size.w = f64::max(1., size_from.w + (size.w - size_from.w) * val);
831 size.h = f64::max(1., size_from.h + (size.h - size_from.h) * val);
832 size = size
833 .to_physical_precise_round(self.scale)
834 .to_logical(self.scale);
835 }
836
837 size
838 }
839
840 pub fn buf_loc(&self) -> Point<f64, Logical> {
841 let mut loc = Point::from((0., 0.));
842 loc += self.window_loc();
843 loc += self.window.buf_loc().to_f64();
844 loc
845 }
846
847 /// Returns a partially-filled [`WindowLayout`].
848 ///
849 /// Only the sizing properties that a [`Tile`] can fill are filled.
850 pub fn ipc_layout_template(&self) -> WindowLayout {
851 WindowLayout {
852 pos_in_scrolling_layout: None,
853 tile_size: self.tile_size().into(),
854 window_size: self.window().size().into(),
855 tile_pos_in_workspace_view: None,
856 window_offset_in_tile: self.window_loc().into(),
857 }
858 }
859
860 fn is_in_input_region(&self, mut point: Point<f64, Logical>) -> bool {
861 point -= self.window_loc().to_f64();
862 self.window.is_in_input_region(point)
863 }
864
865 fn is_in_activation_region(&self, point: Point<f64, Logical>) -> bool {
866 let activation_region = Rectangle::from_size(self.tile_size());
867 activation_region.contains(point)
868 }
869
870 pub fn hit(&self, point: Point<f64, Logical>) -> Option<HitType> {
871 let offset = self.bob_offset();
872 let point = point - offset;
873
874 if self.is_in_input_region(point) {
875 let win_pos = self.buf_loc() + offset;
876 Some(HitType::Input { win_pos })
877 } else if self.is_in_activation_region(point) {
878 Some(HitType::Activate {
879 is_tab_indicator: false,
880 })
881 } else {
882 None
883 }
884 }
885
886 pub fn request_tile_size(
887 &mut self,
888 mut size: Size<f64, Logical>,
889 animate: bool,
890 transaction: Option<Transaction>,
891 ) {
892 // Can't go through effective_border_width() because we might be fullscreen.
893 if !self.border.is_off() {
894 let width = self.border.width();
895 size.w = f64::max(1., size.w - width * 2.);
896 size.h = f64::max(1., size.h - width * 2.);
897 }
898
899 // The size request has to be i32 unfortunately, due to Wayland. We floor here instead of
900 // round to avoid situations where proportionally-sized columns don't fit on the screen
901 // exactly.
902 self.window.request_size(
903 size.to_i32_floor(),
904 SizingMode::Normal,
905 animate,
906 transaction,
907 );
908 }
909
910 pub fn tile_width_for_window_width(&self, size: f64) -> f64 {
911 if self.border.is_off() {
912 size
913 } else {
914 size + self.border.width() * 2.
915 }
916 }
917
918 pub fn tile_height_for_window_height(&self, size: f64) -> f64 {
919 if self.border.is_off() {
920 size
921 } else {
922 size + self.border.width() * 2.
923 }
924 }
925
926 pub fn window_width_for_tile_width(&self, size: f64) -> f64 {
927 if self.border.is_off() {
928 size
929 } else {
930 size - self.border.width() * 2.
931 }
932 }
933
934 pub fn window_height_for_tile_height(&self, size: f64) -> f64 {
935 if self.border.is_off() {
936 size
937 } else {
938 size - self.border.width() * 2.
939 }
940 }
941
942 pub fn request_maximized(
943 &mut self,
944 size: Size<f64, Logical>,
945 animate: bool,
946 transaction: Option<Transaction>,
947 ) {
948 self.window.request_size(
949 size.to_i32_round(),
950 SizingMode::Maximized,
951 animate,
952 transaction,
953 );
954 }
955
956 pub fn request_fullscreen(&mut self, animate: bool, transaction: Option<Transaction>) {
957 self.window.request_size(
958 self.view_size.to_i32_round(),
959 SizingMode::Fullscreen,
960 animate,
961 transaction,
962 );
963 }
964
965 pub fn min_size_nonfullscreen(&self) -> Size<f64, Logical> {
966 let mut size = self.window.min_size().to_f64();
967
968 // Can't go through effective_border_width() because we might be fullscreen.
969 if !self.border.is_off() {
970 let width = self.border.width();
971
972 size.w = f64::max(1., size.w);
973 size.h = f64::max(1., size.h);
974
975 size.w += width * 2.;
976 size.h += width * 2.;
977 }
978
979 size
980 }
981
982 pub fn max_size_nonfullscreen(&self) -> Size<f64, Logical> {
983 let mut size = self.window.max_size().to_f64();
984
985 // Can't go through effective_border_width() because we might be fullscreen.
986 if !self.border.is_off() {
987 let width = self.border.width();
988
989 if size.w > 0. {
990 size.w += width * 2.;
991 }
992 if size.h > 0. {
993 size.h += width * 2.;
994 }
995 }
996
997 size
998 }
999
1000 pub fn bob_offset(&self) -> Point<f64, Logical> {
1001 if self.window.rules().baba_is_float != Some(true) {
1002 return Point::from((0., 0.));
1003 }
1004
1005 let y = baba_is_float_offset(self.clock.now(), self.view_size.h);
1006 let y = round_logical_in_physical(self.scale, y);
1007 Point::from((0., y))
1008 }
1009
1010 fn render_inner<R: NiriRenderer>(
1011 &self,
1012 renderer: &mut R,
1013 location: Point<f64, Logical>,
1014 focus_ring: bool,
1015 target: RenderTarget,
1016 push: &mut dyn FnMut(TileRenderElement<R>),
1017 ) {
1018 let _span = tracy_client::span!("Tile::render_inner");
1019
1020 let scale = Scale::from(self.scale);
1021 let fullscreen_progress = self.fullscreen_progress();
1022 let expanded_progress = self.expanded_progress();
1023
1024 let win_alpha = if self.window.is_ignoring_opacity_window_rule() {
1025 1.
1026 } else {
1027 let alpha = self.window.rules().opacity.unwrap_or(1.).clamp(0., 1.);
1028
1029 // Interpolate towards alpha = 1. at fullscreen.
1030 let p = fullscreen_progress as f32;
1031 alpha * (1. - p) + 1. * p
1032 };
1033
1034 // This is here rather than in render_offset() because render_offset() is currently assumed
1035 // by the code to be temporary. So, for example, interactive move will try to "grab" the
1036 // tile at its current render offset and reset the render offset to zero by cancelling the
1037 // tile move animations. On the other hand, bob_offset() is not resettable, so adding it in
1038 // render_offset() would cause obvious animation glitches.
1039 //
1040 // This isn't to say that adding it here is perfect; indeed, it kind of breaks view_rect
1041 // passed to update_render_elements(). But, it works well enough for what it is.
1042 let location = location + self.bob_offset();
1043
1044 let window_loc = self.window_loc();
1045 let window_size = self.window_size().to_f64();
1046 let animated_window_size = self.animated_window_size();
1047 let window_render_loc = location + window_loc;
1048 let area = Rectangle::new(window_render_loc, animated_window_size);
1049
1050 let rules = self.window.rules();
1051
1052 // Clip to geometry including during the fullscreen animation to help with buggy clients
1053 // that submit a full-sized buffer before acking the fullscreen state (Firefox).
1054 let clip_to_geometry = fullscreen_progress < 1. && rules.clip_to_geometry == Some(true);
1055 let radius = rules
1056 .geometry_corner_radius
1057 .unwrap_or_default()
1058 .scaled_by(1. - expanded_progress as f32);
1059
1060 // Popups go on top, whether it's resize or not.
1061 self.window.render_popups(
1062 renderer,
1063 window_render_loc,
1064 scale,
1065 win_alpha,
1066 target,
1067 &mut |elem| push(elem.into()),
1068 );
1069
1070 // If we're resizing, try to render a shader, or a fallback.
1071 let mut pushed_resize = false;
1072 if let Some(resize) = &self.resize_animation {
1073 if ResizeRenderElement::has_shader(renderer) {
1074 let gles_renderer = renderer.as_gles_renderer();
1075
1076 if let Some(texture_from) = resize.snapshot.texture(gles_renderer, scale, target) {
1077 let mut window_elements = Vec::new();
1078 self.window.render_normal(
1079 gles_renderer,
1080 Point::from((0., 0.)),
1081 scale,
1082 1.,
1083 target,
1084 &mut |elem| window_elements.push(elem),
1085 );
1086
1087 let current = resize
1088 .offscreen
1089 .render(gles_renderer, scale, &window_elements)
1090 .map_err(|err| warn!("error rendering window to texture: {err:?}"))
1091 .ok();
1092
1093 // Clip blocked-out resizes unconditionally because they use solid color render
1094 // elements.
1095 let clip_to_geometry = if target
1096 .should_block_out(resize.snapshot.block_out_from)
1097 && target.should_block_out(rules.block_out_from)
1098 {
1099 true
1100 } else {
1101 clip_to_geometry
1102 };
1103
1104 if let Some((elem_current, _sync_point, mut data)) = current {
1105 let texture_current = elem_current.texture().clone();
1106 // The offset and size are computed in physical pixels and converted to
1107 // logical with the same `scale`, so converting them back with rounding
1108 // inside the geometry() call gives us the same physical result back.
1109 let texture_current_geo = elem_current.geometry(scale);
1110
1111 let elem = ResizeRenderElement::new(
1112 area,
1113 scale,
1114 texture_from.clone(),
1115 resize.snapshot.size,
1116 (texture_current, texture_current_geo),
1117 window_size,
1118 resize.anim.value() as f32,
1119 resize.anim.clamped_value().clamp(0., 1.) as f32,
1120 radius,
1121 clip_to_geometry,
1122 win_alpha,
1123 );
1124
1125 // We're drawing the resize shader, not the offscreen directly.
1126 data.id = elem.id().clone();
1127
1128 // This is not a problem for split popups as the code will look for them by
1129 // original id when it doesn't find them on the offscreen.
1130 self.window.set_offscreen_data(Some(data));
1131 push(elem.into());
1132 pushed_resize = true;
1133 }
1134 }
1135 }
1136
1137 if !pushed_resize {
1138 let fallback_buffer = SolidColorBuffer::new(area.size, [1., 0., 0., 1.]);
1139 let elem = SolidColorRenderElement::from_buffer(
1140 &fallback_buffer,
1141 area.loc,
1142 win_alpha,
1143 Kind::Unspecified,
1144 );
1145 push(elem.into());
1146 pushed_resize = true;
1147 }
1148 }
1149
1150 // If we're not resizing, render the window itself.
1151 let has_border_shader = BorderRenderElement::has_shader(renderer);
1152 if !pushed_resize {
1153 let geo = Rectangle::new(window_render_loc, window_size);
1154 let radius = radius.fit_to(window_size.w as f32, window_size.h as f32);
1155
1156 let clip_shader = ClippedSurfaceRenderElement::shader(renderer).cloned();
1157 let clip = |elem| match elem {
1158 LayoutElementRenderElement::Wayland(elem) => {
1159 // If we should clip to geometry, render a clipped window.
1160 if clip_to_geometry {
1161 if let Some(shader) = clip_shader.clone() {
1162 if ClippedSurfaceRenderElement::will_clip(&elem, scale, geo, radius) {
1163 return ClippedSurfaceRenderElement::new(
1164 elem,
1165 scale,
1166 geo,
1167 shader.clone(),
1168 radius,
1169 )
1170 .into();
1171 }
1172 }
1173 }
1174
1175 // Otherwise, render it normally.
1176 LayoutElementRenderElement::Wayland(elem).into()
1177 }
1178 LayoutElementRenderElement::SolidColor(elem) => {
1179 // In this branch we're rendering a blocked-out window with a solid
1180 // color. We need to render it with a rounded corner shader even if
1181 // clip_to_geometry is false, because in this case we're assuming that
1182 // the unclipped window CSD already has corners rounded to the
1183 // user-provided radius, so our blocked-out rendering should match that
1184 // radius.
1185 if radius != CornerRadius::default() && has_border_shader {
1186 return BorderRenderElement::new(
1187 geo.size,
1188 Rectangle::from_size(geo.size),
1189 GradientInterpolation::default(),
1190 Color::from_color32f(elem.color()),
1191 Color::from_color32f(elem.color()),
1192 0.,
1193 Rectangle::from_size(geo.size),
1194 0.,
1195 radius,
1196 scale.x as f32,
1197 1.,
1198 )
1199 .with_location(geo.loc)
1200 .into();
1201 }
1202
1203 // Otherwise, render the solid color as is.
1204 LayoutElementRenderElement::SolidColor(elem).into()
1205 }
1206 };
1207
1208 if clip_to_geometry && clip_shader.is_some() {
1209 let damage = self.rounded_corner_damage.element();
1210 push(damage.with_location(window_render_loc).into());
1211 }
1212
1213 self.window.render_normal(
1214 renderer,
1215 window_render_loc,
1216 scale,
1217 win_alpha,
1218 target,
1219 &mut |elem| push(clip(elem)),
1220 );
1221 }
1222
1223 if fullscreen_progress > 0. {
1224 let alpha = fullscreen_progress as f32;
1225
1226 // During the un/fullscreen animation, render a border element in order to use the
1227 // animated corner radius.
1228 if fullscreen_progress < 1. && has_border_shader {
1229 let border_width = self.visual_border_width().unwrap_or(0.);
1230 let radius = rules
1231 .geometry_corner_radius
1232 .map_or(CornerRadius::default(), |radius| {
1233 radius.expanded_by(border_width as f32)
1234 })
1235 .scaled_by(1. - expanded_progress as f32);
1236
1237 let size = self.fullscreen_backdrop.size();
1238 let color = self.fullscreen_backdrop.color();
1239 let elem = BorderRenderElement::new(
1240 size,
1241 Rectangle::from_size(size),
1242 GradientInterpolation::default(),
1243 Color::from_color32f(color),
1244 Color::from_color32f(color),
1245 0.,
1246 Rectangle::from_size(size),
1247 0.,
1248 radius,
1249 scale.x as f32,
1250 alpha,
1251 )
1252 .with_location(location);
1253 push(elem.into());
1254 } else {
1255 let elem = SolidColorRenderElement::from_buffer(
1256 &self.fullscreen_backdrop,
1257 location,
1258 alpha,
1259 Kind::Unspecified,
1260 );
1261 push(elem.into());
1262 }
1263 }
1264
1265 if let Some(width) = self.visual_border_width() {
1266 self.border.render(
1267 renderer,
1268 location + Point::from((width, width)),
1269 &mut |elem| push(elem.into()),
1270 );
1271 }
1272
1273 // Hide the focus ring when maximized/fullscreened. It's not normally visible anyway due to
1274 // being outside the monitor or obscured by a solid colored bar, but it is visible under
1275 // semitransparent bars in maximized state (which is a bit weird) and in the overview (also
1276 // a bit weird).
1277 if focus_ring && expanded_progress < 1. {
1278 self.focus_ring
1279 .render(renderer, location, &mut |elem| push(elem.into()));
1280 }
1281
1282 if expanded_progress < 1. {
1283 self.shadow
1284 .render(renderer, location, &mut |elem| push(elem.into()));
1285 }
1286 }
1287
1288 pub fn render<R: NiriRenderer>(
1289 &self,
1290 renderer: &mut R,
1291 location: Point<f64, Logical>,
1292 focus_ring: bool,
1293 target: RenderTarget,
1294 push: &mut dyn FnMut(TileRenderElement<R>),
1295 ) {
1296 let _span = tracy_client::span!("Tile::render");
1297
1298 let scale = Scale::from(self.scale);
1299
1300 let tile_alpha = self
1301 .alpha_animation
1302 .as_ref()
1303 .map_or(1., |alpha| alpha.anim.clamped_value()) as f32;
1304
1305 let mut pushed = false;
1306 self.window().set_offscreen_data(None);
1307
1308 if let Some(open) = &self.open_animation {
1309 let renderer = renderer.as_gles_renderer();
1310 let mut elements = Vec::new();
1311 self.render_inner(
1312 renderer,
1313 Point::from((0., 0.)),
1314 focus_ring,
1315 target,
1316 &mut |elem| elements.push(elem),
1317 );
1318 match open.render(
1319 renderer,
1320 &elements,
1321 self.animated_tile_size(),
1322 location,
1323 scale,
1324 tile_alpha,
1325 ) {
1326 Ok((elem, data)) => {
1327 self.window().set_offscreen_data(Some(data));
1328 push(elem.into());
1329 pushed = true;
1330 }
1331 Err(err) => {
1332 warn!("error rendering window opening animation: {err:?}");
1333 }
1334 }
1335 } else if let Some(alpha) = &self.alpha_animation {
1336 let renderer = renderer.as_gles_renderer();
1337 let mut elements = Vec::new();
1338 self.render_inner(
1339 renderer,
1340 Point::from((0., 0.)),
1341 focus_ring,
1342 target,
1343 &mut |elem| elements.push(elem),
1344 );
1345 match alpha.offscreen.render(renderer, scale, &elements) {
1346 Ok((elem, _sync, data)) => {
1347 let offset = elem.offset();
1348 let elem = elem.with_alpha(tile_alpha).with_offset(location + offset);
1349
1350 self.window().set_offscreen_data(Some(data));
1351 push(elem.into());
1352 pushed = true;
1353 }
1354 Err(err) => {
1355 warn!("error rendering tile to offscreen for alpha animation: {err:?}");
1356 }
1357 }
1358 }
1359
1360 if !pushed {
1361 self.render_inner(renderer, location, focus_ring, target, &mut |elem| {
1362 push(elem)
1363 });
1364 }
1365 }
1366
1367 pub fn store_unmap_snapshot_if_empty(&mut self, renderer: &mut GlesRenderer) {
1368 if self.unmap_snapshot.is_some() {
1369 return;
1370 }
1371
1372 self.unmap_snapshot = Some(self.render_snapshot(renderer));
1373 }
1374
1375 fn render_snapshot(&self, renderer: &mut GlesRenderer) -> TileRenderSnapshot {
1376 let _span = tracy_client::span!("Tile::render_snapshot");
1377
1378 let mut contents = Vec::new();
1379 self.render(
1380 renderer,
1381 Point::from((0., 0.)),
1382 false,
1383 RenderTarget::Output,
1384 &mut |elem| contents.push(elem),
1385 );
1386
1387 // A bit of a hack to render blocked out as for screencast, but I think it's fine here.
1388 let mut blocked_out_contents = Vec::new();
1389 self.render(
1390 renderer,
1391 Point::from((0., 0.)),
1392 false,
1393 RenderTarget::Screencast,
1394 &mut |elem| blocked_out_contents.push(elem),
1395 );
1396
1397 RenderSnapshot {
1398 contents,
1399 blocked_out_contents,
1400 block_out_from: self.window.rules().block_out_from,
1401 size: self.animated_tile_size(),
1402 texture: Default::default(),
1403 blocked_out_texture: Default::default(),
1404 }
1405 }
1406
1407 pub fn take_unmap_snapshot(&mut self) -> Option<TileRenderSnapshot> {
1408 self.unmap_snapshot.take()
1409 }
1410
1411 pub fn border(&self) -> &FocusRing {
1412 &self.border
1413 }
1414
1415 pub fn focus_ring(&self) -> &FocusRing {
1416 &self.focus_ring
1417 }
1418
1419 pub fn options(&self) -> &Rc<Options> {
1420 &self.options
1421 }
1422
1423 #[cfg(test)]
1424 pub fn view_size(&self) -> Size<f64, Logical> {
1425 self.view_size
1426 }
1427
1428 #[cfg(test)]
1429 pub fn verify_invariants(&self) {
1430 use approx::assert_abs_diff_eq;
1431
1432 assert_eq!(self.sizing_mode, self.window.sizing_mode());
1433
1434 let scale = self.scale;
1435 let size = self.tile_size();
1436 let rounded = size.to_physical_precise_round(scale).to_logical(scale);
1437 assert_abs_diff_eq!(size.w, rounded.w, epsilon = 1e-5);
1438 assert_abs_diff_eq!(size.h, rounded.h, epsilon = 1e-5);
1439 }
1440}