forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBad3847fIvan Molodetskikh1 year ago2457 commits

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