forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

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