forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9ff2f83Ivan Molodetskikh1 year ago2099 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::round_logical_in_physical;
29use crate::utils::transaction::Transaction;
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 Rectangle::new(
370 view_rect.loc - Point::from((border_width, border_width)),
371 view_rect.size,
372 ),
373 radius,
374 self.scale,
375 1.,
376 );
377
378 let radius = if self.is_fullscreen {
379 CornerRadius::default()
380 } else if self.effective_border_width().is_some() {
381 radius
382 } else {
383 rules.geometry_corner_radius.unwrap_or_default()
384 };
385 self.shadow.update_render_elements(
386 self.animated_tile_size(),
387 is_active,
388 radius,
389 self.scale,
390 1.,
391 );
392
393 let draw_focus_ring_with_background = if self.effective_border_width().is_some() {
394 false
395 } else {
396 draw_border_with_background
397 };
398 let radius = radius.expanded_by(self.focus_ring.width() as f32);
399 self.focus_ring.update_render_elements(
400 self.animated_tile_size(),
401 is_active,
402 !draw_focus_ring_with_background,
403 view_rect,
404 radius,
405 self.scale,
406 1.,
407 );
408 }
409
410 pub fn scale(&self) -> f64 {
411 self.scale
412 }
413
414 pub fn render_offset(&self) -> Point<f64, Logical> {
415 let mut offset = Point::from((0., 0.));
416
417 if let Some(move_) = &self.move_x_animation {
418 offset.x += move_.from * move_.anim.value();
419 }
420 if let Some(move_) = &self.move_y_animation {
421 offset.y += move_.from * move_.anim.value();
422 }
423
424 offset += self.interactive_move_offset;
425
426 offset
427 }
428
429 pub fn start_open_animation(&mut self) {
430 self.open_animation = Some(OpenAnimation::new(Animation::new(
431 self.clock.clone(),
432 0.,
433 1.,
434 0.,
435 self.options.animations.window_open.anim,
436 )));
437 }
438
439 pub fn resize_animation(&self) -> Option<&Animation> {
440 self.resize_animation.as_ref().map(|resize| &resize.anim)
441 }
442
443 pub fn animate_move_from(&mut self, from: Point<f64, Logical>) {
444 self.animate_move_x_from(from.x);
445 self.animate_move_y_from(from.y);
446 }
447
448 pub fn animate_move_x_from(&mut self, from: f64) {
449 self.animate_move_x_from_with_config(from, self.options.animations.window_movement.0);
450 }
451
452 pub fn animate_move_x_from_with_config(&mut self, from: f64, config: niri_config::Animation) {
453 let current_offset = self.render_offset().x;
454
455 // Preserve the previous config if ongoing.
456 let anim = self.move_x_animation.take().map(|move_| move_.anim);
457 let anim = anim
458 .map(|anim| anim.restarted(1., 0., 0.))
459 .unwrap_or_else(|| Animation::new(self.clock.clone(), 1., 0., 0., config));
460
461 self.move_x_animation = Some(MoveAnimation {
462 anim,
463 from: from + current_offset,
464 });
465 }
466
467 pub fn animate_move_y_from(&mut self, from: f64) {
468 self.animate_move_y_from_with_config(from, self.options.animations.window_movement.0);
469 }
470
471 pub fn animate_move_y_from_with_config(&mut self, from: f64, config: niri_config::Animation) {
472 let current_offset = self.render_offset().y;
473
474 // Preserve the previous config if ongoing.
475 let anim = self.move_y_animation.take().map(|move_| move_.anim);
476 let anim = anim
477 .map(|anim| anim.restarted(1., 0., 0.))
478 .unwrap_or_else(|| Animation::new(self.clock.clone(), 1., 0., 0., config));
479
480 self.move_y_animation = Some(MoveAnimation {
481 anim,
482 from: from + current_offset,
483 });
484 }
485
486 pub fn stop_move_animations(&mut self) {
487 self.move_x_animation = None;
488 self.move_y_animation = None;
489 }
490
491 pub fn animate_alpha(&mut self, from: f64, to: f64, config: niri_config::Animation) {
492 let from = from.clamp(0., 1.);
493 let to = to.clamp(0., 1.);
494
495 let (current, offscreen) = if let Some(alpha) = self.alpha_animation.take() {
496 (alpha.anim.clamped_value(), alpha.offscreen)
497 } else {
498 (from, OffscreenBuffer::default())
499 };
500
501 self.alpha_animation = Some(AlphaAnimation {
502 anim: Animation::new(self.clock.clone(), current, to, 0., config),
503 hold_after_done: false,
504 offscreen,
505 });
506 }
507
508 pub fn ensure_alpha_animates_to_1(&mut self) {
509 if let Some(alpha) = &self.alpha_animation {
510 if alpha.anim.to() != 1. {
511 // Cancel animation instead of starting a new one because the user likely wants to
512 // see the tile right away.
513 self.alpha_animation = None;
514 }
515 }
516 }
517
518 pub fn hold_alpha_animation_after_done(&mut self) {
519 if let Some(alpha) = &mut self.alpha_animation {
520 alpha.hold_after_done = true;
521 }
522 }
523
524 pub fn window(&self) -> &W {
525 &self.window
526 }
527
528 pub fn window_mut(&mut self) -> &mut W {
529 &mut self.window
530 }
531
532 pub fn is_fullscreen(&self) -> bool {
533 self.is_fullscreen
534 }
535
536 /// Returns `None` if the border is hidden and `Some(width)` if it should be shown.
537 pub fn effective_border_width(&self) -> Option<f64> {
538 if self.is_fullscreen {
539 return None;
540 }
541
542 if self.border.is_off() {
543 return None;
544 }
545
546 Some(self.border.width())
547 }
548
549 /// Returns the location of the window's visual geometry within this Tile.
550 pub fn window_loc(&self) -> Point<f64, Logical> {
551 let mut loc = Point::from((0., 0.));
552
553 // In fullscreen, center the window in the given size.
554 if self.is_fullscreen {
555 let window_size = self.window_size();
556 let target_size = self.view_size;
557
558 // Windows aren't supposed to be larger than the fullscreen size, but in case we get
559 // one, leave it at the top-left as usual.
560 if window_size.w < target_size.w {
561 loc.x += (target_size.w - window_size.w) / 2.;
562 }
563 if window_size.h < target_size.h {
564 loc.y += (target_size.h - window_size.h) / 2.;
565 }
566
567 // Round to physical pixels.
568 loc = loc
569 .to_physical_precise_round(self.scale)
570 .to_logical(self.scale);
571 }
572
573 if let Some(width) = self.effective_border_width() {
574 loc += (width, width).into();
575 }
576
577 loc
578 }
579
580 pub fn tile_size(&self) -> Size<f64, Logical> {
581 let mut size = self.window_size();
582
583 if self.is_fullscreen {
584 // Normally we'd just return the fullscreen size here, but this makes things a bit
585 // nicer if a fullscreen window is bigger than the fullscreen size for some reason.
586 size.w = f64::max(size.w, self.view_size.w);
587 size.h = f64::max(size.h, self.view_size.h);
588 return size;
589 }
590
591 if let Some(width) = self.effective_border_width() {
592 size.w += width * 2.;
593 size.h += width * 2.;
594 }
595
596 size
597 }
598
599 pub fn tile_expected_or_current_size(&self) -> Size<f64, Logical> {
600 let mut size = self.window_expected_or_current_size();
601
602 if self.is_fullscreen {
603 // Normally we'd just return the fullscreen size here, but this makes things a bit
604 // nicer if a fullscreen window is bigger than the fullscreen size for some reason.
605 size.w = f64::max(size.w, self.view_size.w);
606 size.h = f64::max(size.h, self.view_size.h);
607 return size;
608 }
609
610 if let Some(width) = self.effective_border_width() {
611 size.w += width * 2.;
612 size.h += width * 2.;
613 }
614
615 size
616 }
617
618 pub fn window_size(&self) -> Size<f64, Logical> {
619 let mut size = self.window.size().to_f64();
620 size = size
621 .to_physical_precise_round(self.scale)
622 .to_logical(self.scale);
623 size
624 }
625
626 pub fn window_expected_or_current_size(&self) -> Size<f64, Logical> {
627 let size = self.window.expected_size();
628 let mut size = size.unwrap_or_else(|| self.window.size()).to_f64();
629 size = size
630 .to_physical_precise_round(self.scale)
631 .to_logical(self.scale);
632 size
633 }
634
635 pub fn animated_window_size(&self) -> Size<f64, Logical> {
636 let mut size = self.window_size();
637
638 if let Some(resize) = &self.resize_animation {
639 let val = resize.anim.value();
640 let size_from = resize.size_from.to_f64();
641
642 size.w = f64::max(1., size_from.w + (size.w - size_from.w) * val);
643 size.h = f64::max(1., size_from.h + (size.h - size_from.h) * val);
644 size = size
645 .to_physical_precise_round(self.scale)
646 .to_logical(self.scale);
647 }
648
649 size
650 }
651
652 pub fn animated_tile_size(&self) -> Size<f64, Logical> {
653 let mut size = self.animated_window_size();
654
655 if self.is_fullscreen {
656 // Normally we'd just return the fullscreen size here, but this makes things a bit
657 // nicer if a fullscreen window is bigger than the fullscreen size for some reason.
658 size.w = f64::max(size.w, self.view_size.w);
659 size.h = f64::max(size.h, self.view_size.h);
660 return size;
661 }
662
663 if let Some(width) = self.effective_border_width() {
664 size.w += width * 2.;
665 size.h += width * 2.;
666 }
667
668 size
669 }
670
671 pub fn buf_loc(&self) -> Point<f64, Logical> {
672 let mut loc = Point::from((0., 0.));
673 loc += self.window_loc();
674 loc += self.window.buf_loc().to_f64();
675 loc
676 }
677
678 fn is_in_input_region(&self, mut point: Point<f64, Logical>) -> bool {
679 point -= self.window_loc().to_f64();
680 self.window.is_in_input_region(point)
681 }
682
683 fn is_in_activation_region(&self, point: Point<f64, Logical>) -> bool {
684 let activation_region = Rectangle::from_size(self.tile_size());
685 activation_region.contains(point)
686 }
687
688 pub fn hit(&self, point: Point<f64, Logical>) -> Option<HitType> {
689 let offset = self.bob_offset();
690 let point = point - offset;
691
692 if self.is_in_input_region(point) {
693 let win_pos = self.buf_loc() + offset;
694 Some(HitType::Input { win_pos })
695 } else if self.is_in_activation_region(point) {
696 Some(HitType::Activate {
697 is_tab_indicator: false,
698 })
699 } else {
700 None
701 }
702 }
703
704 pub fn request_tile_size(
705 &mut self,
706 mut size: Size<f64, Logical>,
707 animate: bool,
708 transaction: Option<Transaction>,
709 ) {
710 // Can't go through effective_border_width() because we might be fullscreen.
711 if !self.border.is_off() {
712 let width = self.border.width();
713 size.w = f64::max(1., size.w - width * 2.);
714 size.h = f64::max(1., size.h - width * 2.);
715 }
716
717 // The size request has to be i32 unfortunately, due to Wayland. We floor here instead of
718 // round to avoid situations where proportionally-sized columns don't fit on the screen
719 // exactly.
720 self.window
721 .request_size(size.to_i32_floor(), false, animate, transaction);
722 }
723
724 pub fn tile_width_for_window_width(&self, size: f64) -> f64 {
725 if self.border.is_off() {
726 size
727 } else {
728 size + self.border.width() * 2.
729 }
730 }
731
732 pub fn tile_height_for_window_height(&self, size: f64) -> f64 {
733 if self.border.is_off() {
734 size
735 } else {
736 size + self.border.width() * 2.
737 }
738 }
739
740 pub fn window_width_for_tile_width(&self, size: f64) -> f64 {
741 if self.border.is_off() {
742 size
743 } else {
744 size - self.border.width() * 2.
745 }
746 }
747
748 pub fn window_height_for_tile_height(&self, size: f64) -> f64 {
749 if self.border.is_off() {
750 size
751 } else {
752 size - self.border.width() * 2.
753 }
754 }
755
756 pub fn request_fullscreen(&mut self, animate: bool, transaction: Option<Transaction>) {
757 self.window
758 .request_size(self.view_size.to_i32_round(), true, animate, transaction);
759 }
760
761 pub fn min_size_nonfullscreen(&self) -> Size<f64, Logical> {
762 let mut size = self.window.min_size().to_f64();
763
764 // Can't go through effective_border_width() because we might be fullscreen.
765 if !self.border.is_off() {
766 let width = self.border.width();
767
768 size.w = f64::max(1., size.w);
769 size.h = f64::max(1., size.h);
770
771 size.w += width * 2.;
772 size.h += width * 2.;
773 }
774
775 size
776 }
777
778 pub fn max_size_nonfullscreen(&self) -> Size<f64, Logical> {
779 let mut size = self.window.max_size().to_f64();
780
781 // Can't go through effective_border_width() because we might be fullscreen.
782 if !self.border.is_off() {
783 let width = self.border.width();
784
785 if size.w > 0. {
786 size.w += width * 2.;
787 }
788 if size.h > 0. {
789 size.h += width * 2.;
790 }
791 }
792
793 size
794 }
795
796 pub fn bob_offset(&self) -> Point<f64, Logical> {
797 if self.window.rules().baba_is_float != Some(true) {
798 return Point::from((0., 0.));
799 }
800
801 let now = self.clock.now().as_secs_f64();
802 let amplitude = self.view_size.h / 96.;
803 let y = amplitude * ((f64::consts::TAU * now / 3.6).sin() - 1.);
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}