forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

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