forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB90ef202Ivan Molodetskikh1 year ago2349 commits

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