forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB2d82fddIvan Molodetskikh1 year ago2536 commits

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