forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

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