forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBf51dd67Ivan Molodetskikh2 years ago981 commits

tile.rs722 lines, 23.6 KiBHistoryRawDownload
1use std::cmp::max;
2use std::rc::Rc;
3use std::time::Duration;
4
5use smithay::backend::allocator::Fourcc;
6use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
7use smithay::backend::renderer::element::utils::RescaleRenderElement;
8use smithay::backend::renderer::element::{Element, Kind};
9use smithay::backend::renderer::gles::GlesRenderer;
10use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};
11
12use super::focus_ring::{FocusRing, FocusRingRenderElement};
13use super::{
14 LayoutElement, LayoutElementRenderElement, LayoutElementRenderSnapshot, Options,
15 RESIZE_ANIMATION_THRESHOLD,
16};
17use crate::animation::Animation;
18use crate::niri_render_elements;
19use crate::render_helpers::crossfade::CrossfadeRenderElement;
20use crate::render_helpers::offscreen::OffscreenRenderElement;
21use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
22use crate::render_helpers::renderer::NiriRenderer;
23use crate::render_helpers::shaders::Shaders;
24use crate::render_helpers::snapshot::RenderSnapshot;
25use crate::render_helpers::{render_to_encompassing_texture, RenderTarget, ToRenderElement};
26
27/// Toplevel window with decorations.
28#[derive(Debug)]
29pub struct Tile<W: LayoutElement> {
30 /// The toplevel window itself.
31 window: W,
32
33 /// The border around the window.
34 border: FocusRing,
35
36 /// The focus ring around the window.
37 ///
38 /// It's supposed to be on the Workspace, but for the sake of a nicer open animation it's
39 /// currently here.
40 focus_ring: FocusRing,
41
42 /// Whether this tile is fullscreen.
43 ///
44 /// This will update only when the `window` actually goes fullscreen, rather than right away,
45 /// to avoid black backdrop flicker before the window has had a chance to resize.
46 is_fullscreen: bool,
47
48 /// The black backdrop for fullscreen windows.
49 fullscreen_backdrop: SolidColorBuffer,
50
51 /// The size we were requested to fullscreen into.
52 fullscreen_size: Size<i32, Logical>,
53
54 /// The animation upon opening a window.
55 open_animation: Option<Animation>,
56
57 /// The animation of the window resizing.
58 resize_animation: Option<ResizeAnimation>,
59
60 /// The animation of a tile visually moving.
61 move_animation: Option<MoveAnimation>,
62
63 /// Configurable properties of the layout.
64 pub options: Rc<Options>,
65}
66
67niri_render_elements! {
68 TileRenderElement<R> => {
69 LayoutElement = LayoutElementRenderElement<R>,
70 FocusRing = FocusRingRenderElement,
71 SolidColor = SolidColorRenderElement,
72 Offscreen = RescaleRenderElement<OffscreenRenderElement>,
73 Crossfade = CrossfadeRenderElement,
74 }
75}
76
77niri_render_elements! {
78 TileSnapshotContentsRenderElement => {
79 Texture = PrimaryGpuTextureRenderElement,
80 SolidColor = SolidColorRenderElement,
81 }
82}
83
84niri_render_elements! {
85 TileSnapshotRenderElement => {
86 Contents = RescaleRenderElement<TileSnapshotContentsRenderElement>,
87 FocusRing = FocusRingRenderElement,
88 SolidColor = SolidColorRenderElement,
89 }
90}
91
92#[derive(Debug)]
93struct ResizeAnimation {
94 anim: Animation,
95 size_from: Size<i32, Logical>,
96 snapshot: LayoutElementRenderSnapshot,
97}
98
99#[derive(Debug)]
100struct MoveAnimation {
101 anim: Animation,
102 from: Point<i32, Logical>,
103}
104
105impl<W: LayoutElement> Tile<W> {
106 pub fn new(window: W, options: Rc<Options>) -> Self {
107 Self {
108 window,
109 border: FocusRing::new(options.border.into()),
110 focus_ring: FocusRing::new(options.focus_ring),
111 is_fullscreen: false, // FIXME: up-to-date fullscreen right away, but we need size.
112 fullscreen_backdrop: SolidColorBuffer::new((0, 0), [0., 0., 0., 1.]),
113 fullscreen_size: Default::default(),
114 open_animation: None,
115 resize_animation: None,
116 move_animation: None,
117 options,
118 }
119 }
120
121 pub fn update_config(&mut self, options: Rc<Options>) {
122 self.border.update_config(options.border.into());
123 self.focus_ring.update_config(options.focus_ring);
124 self.options = options;
125 }
126
127 pub fn update_window(&mut self) {
128 // FIXME: remove when we can get a fullscreen size right away.
129 if self.fullscreen_size != Size::from((0, 0)) {
130 self.is_fullscreen = self.window.is_fullscreen();
131 }
132
133 if let Some(animate_from) = self.window.take_animation_snapshot() {
134 let size_from = if let Some(resize) = self.resize_animation.take() {
135 // Compute like in animated_window_size(), but using the snapshot geometry (since
136 // the current one is already overwritten).
137 let mut size = animate_from.size;
138
139 let val = resize.anim.value();
140 let size_from = resize.size_from;
141
142 size.w = (size_from.w as f64 + (size.w - size_from.w) as f64 * val).round() as i32;
143 size.h = (size_from.h as f64 + (size.h - size_from.h) as f64 * val).round() as i32;
144
145 size
146 } else {
147 animate_from.size
148 };
149
150 let change = self.window.size().to_point() - size_from.to_point();
151 let change = max(change.x.abs(), change.y.abs());
152 if change > RESIZE_ANIMATION_THRESHOLD {
153 let anim = Animation::new(0., 1., 0., self.options.animations.window_resize.0);
154 self.resize_animation = Some(ResizeAnimation {
155 anim,
156 size_from,
157 snapshot: animate_from,
158 });
159 } else {
160 self.resize_animation = None;
161 }
162 }
163 }
164
165 pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
166 if let Some(anim) = &mut self.open_animation {
167 anim.set_current_time(current_time);
168 if anim.is_done() {
169 self.open_animation = None;
170 }
171 }
172
173 if let Some(resize) = &mut self.resize_animation {
174 resize.anim.set_current_time(current_time);
175 if resize.anim.is_done() {
176 self.resize_animation = None;
177 }
178 }
179
180 if let Some(move_) = &mut self.move_animation {
181 move_.anim.set_current_time(current_time);
182 if move_.anim.is_done() {
183 self.move_animation = None;
184 }
185 }
186
187 let draw_border_with_background = self
188 .window
189 .rules()
190 .draw_border_with_background
191 .unwrap_or_else(|| !self.window.has_ssd());
192 self.border
193 .update(self.animated_window_size(), !draw_border_with_background);
194 self.border.set_active(is_active);
195
196 let draw_focus_ring_with_background = if self.effective_border_width().is_some() {
197 false
198 } else {
199 draw_border_with_background
200 };
201 self.focus_ring
202 .update(self.animated_tile_size(), !draw_focus_ring_with_background);
203 self.focus_ring.set_active(is_active);
204 }
205
206 pub fn are_animations_ongoing(&self) -> bool {
207 self.open_animation.is_some()
208 || self.resize_animation.is_some()
209 || self.move_animation.is_some()
210 }
211
212 pub fn render_offset(&self) -> Point<i32, Logical> {
213 let mut offset = Point::from((0., 0.));
214
215 if let Some(move_) = &self.move_animation {
216 offset += move_.from.to_f64().upscale(move_.anim.value());
217 }
218
219 offset.to_i32_round()
220 }
221
222 pub fn start_open_animation(&mut self) {
223 self.open_animation = Some(Animation::new(
224 0.,
225 1.,
226 0.,
227 self.options.animations.window_open.0,
228 ));
229 }
230
231 pub fn open_animation(&self) -> &Option<Animation> {
232 &self.open_animation
233 }
234
235 pub fn resize_animation(&self) -> Option<&Animation> {
236 self.resize_animation.as_ref().map(|resize| &resize.anim)
237 }
238
239 pub fn animate_move_from(&mut self, from: Point<i32, Logical>) {
240 self.animate_move_from_with_config(from, self.options.animations.window_movement.0);
241 }
242
243 pub fn animate_move_from_with_config(
244 &mut self,
245 from: Point<i32, Logical>,
246 config: niri_config::Animation,
247 ) {
248 let current_offset = self.render_offset();
249
250 // Preserve the previous config if ongoing.
251 let anim = self.move_animation.take().map(|move_| move_.anim);
252 let anim = anim
253 .map(|anim| anim.restarted(1., 0., 0.))
254 .unwrap_or_else(|| Animation::new(1., 0., 0., config));
255
256 self.move_animation = Some(MoveAnimation {
257 anim,
258 from: from + current_offset,
259 });
260 }
261
262 pub fn window(&self) -> &W {
263 &self.window
264 }
265
266 pub fn window_mut(&mut self) -> &mut W {
267 &mut self.window
268 }
269
270 pub fn into_window(self) -> W {
271 self.window
272 }
273
274 pub fn is_fullscreen(&self) -> bool {
275 self.is_fullscreen
276 }
277
278 /// Returns `None` if the border is hidden and `Some(width)` if it should be shown.
279 fn effective_border_width(&self) -> Option<i32> {
280 if self.is_fullscreen {
281 return None;
282 }
283
284 if self.border.is_off() {
285 return None;
286 }
287
288 Some(self.border.width())
289 }
290
291 /// Returns the location of the window's visual geometry within this Tile.
292 pub fn window_loc(&self) -> Point<i32, Logical> {
293 let mut loc = Point::from((0, 0));
294
295 // In fullscreen, center the window in the given size.
296 if self.is_fullscreen {
297 let window_size = self.window.size();
298 let target_size = self.fullscreen_size;
299
300 // Windows aren't supposed to be larger than the fullscreen size, but in case we get
301 // one, leave it at the top-left as usual.
302 if window_size.w < target_size.w {
303 loc.x += (target_size.w - window_size.w) / 2;
304 }
305 if window_size.h < target_size.h {
306 loc.y += (target_size.h - window_size.h) / 2;
307 }
308 }
309
310 if let Some(width) = self.effective_border_width() {
311 loc += (width, width).into();
312 }
313
314 loc
315 }
316
317 pub fn tile_size(&self) -> Size<i32, Logical> {
318 let mut size = self.window.size();
319
320 if self.is_fullscreen {
321 // Normally we'd just return the fullscreen size here, but this makes things a bit
322 // nicer if a fullscreen window is bigger than the fullscreen size for some reason.
323 size.w = max(size.w, self.fullscreen_size.w);
324 size.h = max(size.h, self.fullscreen_size.h);
325 return size;
326 }
327
328 if let Some(width) = self.effective_border_width() {
329 size.w = size.w.saturating_add(width * 2);
330 size.h = size.h.saturating_add(width * 2);
331 }
332
333 size
334 }
335
336 pub fn window_size(&self) -> Size<i32, Logical> {
337 self.window.size()
338 }
339
340 fn animated_window_size(&self) -> Size<i32, Logical> {
341 let mut size = self.window.size();
342
343 if let Some(resize) = &self.resize_animation {
344 let val = resize.anim.value();
345 let size_from = resize.size_from;
346
347 size.w = (size_from.w as f64 + (size.w - size_from.w) as f64 * val).round() as i32;
348 size.w = max(1, size.w);
349 size.h = (size_from.h as f64 + (size.h - size_from.h) as f64 * val).round() as i32;
350 size.h = max(1, size.h);
351 }
352
353 size
354 }
355
356 fn animated_tile_size(&self) -> Size<i32, Logical> {
357 let mut size = self.animated_window_size();
358
359 if self.is_fullscreen {
360 // Normally we'd just return the fullscreen size here, but this makes things a bit
361 // nicer if a fullscreen window is bigger than the fullscreen size for some reason.
362 size.w = max(size.w, self.fullscreen_size.w);
363 size.h = max(size.h, self.fullscreen_size.h);
364 return size;
365 }
366
367 if let Some(width) = self.effective_border_width() {
368 size.w = size.w.saturating_add(width * 2);
369 size.h = size.h.saturating_add(width * 2);
370 }
371
372 size
373 }
374
375 pub fn buf_loc(&self) -> Point<i32, Logical> {
376 let mut loc = Point::from((0, 0));
377 loc += self.window_loc();
378 loc += self.window.buf_loc();
379 loc
380 }
381
382 pub fn is_in_input_region(&self, mut point: Point<f64, Logical>) -> bool {
383 point -= self.window_loc().to_f64();
384 self.window.is_in_input_region(point)
385 }
386
387 pub fn is_in_activation_region(&self, point: Point<f64, Logical>) -> bool {
388 let activation_region = Rectangle::from_loc_and_size((0, 0), self.tile_size());
389 activation_region.to_f64().contains(point)
390 }
391
392 pub fn request_tile_size(&mut self, mut size: Size<i32, Logical>, animate: bool) {
393 // Can't go through effective_border_width() because we might be fullscreen.
394 if !self.border.is_off() {
395 let width = self.border.width();
396 size.w = max(1, size.w - width * 2);
397 size.h = max(1, size.h - width * 2);
398 }
399
400 self.window.request_size(size, animate);
401 }
402
403 pub fn tile_width_for_window_width(&self, size: i32) -> i32 {
404 if self.border.is_off() {
405 size
406 } else {
407 size.saturating_add(self.border.width() * 2)
408 }
409 }
410
411 pub fn tile_height_for_window_height(&self, size: i32) -> i32 {
412 if self.border.is_off() {
413 size
414 } else {
415 size.saturating_add(self.border.width() * 2)
416 }
417 }
418
419 pub fn window_height_for_tile_height(&self, size: i32) -> i32 {
420 if self.border.is_off() {
421 size
422 } else {
423 size.saturating_sub(self.border.width() * 2)
424 }
425 }
426
427 pub fn request_fullscreen(&mut self, size: Size<i32, Logical>) {
428 self.fullscreen_backdrop.resize(size);
429 self.fullscreen_size = size;
430 self.window.request_fullscreen(size);
431 }
432
433 pub fn min_size(&self) -> Size<i32, Logical> {
434 let mut size = self.window.min_size();
435
436 if let Some(width) = self.effective_border_width() {
437 size.w = max(1, size.w);
438 size.h = max(1, size.h);
439
440 size.w = size.w.saturating_add(width * 2);
441 size.h = size.h.saturating_add(width * 2);
442 }
443
444 size
445 }
446
447 pub fn max_size(&self) -> Size<i32, Logical> {
448 let mut size = self.window.max_size();
449
450 if let Some(width) = self.effective_border_width() {
451 if size.w > 0 {
452 size.w = size.w.saturating_add(width * 2);
453 }
454 if size.h > 0 {
455 size.h = size.h.saturating_add(width * 2);
456 }
457 }
458
459 size
460 }
461
462 pub fn draw_border_with_background(&self) -> bool {
463 if self.effective_border_width().is_some() {
464 return false;
465 }
466
467 self.window
468 .rules()
469 .draw_border_with_background
470 .unwrap_or_else(|| !self.window.has_ssd())
471 }
472
473 fn render_inner<R: NiriRenderer>(
474 &self,
475 renderer: &mut R,
476 location: Point<i32, Logical>,
477 scale: Scale<f64>,
478 view_size: Size<i32, Logical>,
479 focus_ring: bool,
480 target: RenderTarget,
481 ) -> impl Iterator<Item = TileRenderElement<R>> {
482 let alpha = if self.is_fullscreen {
483 1.
484 } else {
485 self.window.rules().opacity.unwrap_or(1.).clamp(0., 1.)
486 };
487
488 let window_loc = self.window_loc();
489 let window_size = self.window_size();
490 let animated_window_size = self.animated_window_size();
491 let window_render_loc = location + window_loc;
492 let area = Rectangle::from_loc_and_size(window_render_loc, animated_window_size);
493
494 let gles_renderer = renderer.as_gles_renderer();
495
496 // If we're resizing, try to render a crossfade, or a fallback.
497 let mut crossfade = None;
498 let mut crossfade_fallback = None;
499
500 if let Some(resize) = &self.resize_animation {
501 if Shaders::get(gles_renderer).crossfade.is_some() {
502 if let Some(texture_from) = resize.snapshot.texture(gles_renderer, scale, target) {
503 let window_elements =
504 self.window
505 .render(gles_renderer, Point::from((0, 0)), scale, 1., target);
506 let current = render_to_encompassing_texture(
507 gles_renderer,
508 scale,
509 Transform::Normal,
510 Fourcc::Abgr8888,
511 &window_elements,
512 )
513 .map_err(|err| warn!("error rendering window to texture: {err:?}"))
514 .ok();
515
516 if let Some((texture_current, _sync_point, texture_current_geo)) = current {
517 let elem = CrossfadeRenderElement::new(
518 gles_renderer,
519 area,
520 scale,
521 texture_from.clone(),
522 resize.snapshot.size,
523 (texture_current, texture_current_geo),
524 window_size,
525 resize.anim.clamped_value().clamp(0., 1.) as f32,
526 alpha,
527 )
528 .expect("we checked the crossfade shader above");
529 self.window
530 .set_offscreen_element_id(Some(elem.id().clone()));
531 crossfade = Some(elem.into());
532 }
533 }
534 }
535
536 if crossfade.is_none() {
537 let fallback_buffer = SolidColorBuffer::new(area.size, [1., 0., 0., 1.]);
538 crossfade_fallback = Some(
539 SolidColorRenderElement::from_buffer(
540 &fallback_buffer,
541 area.loc.to_physical_precise_round(scale),
542 scale,
543 alpha,
544 Kind::Unspecified,
545 )
546 .into(),
547 );
548 self.window.set_offscreen_element_id(None);
549 }
550 }
551
552 // If we're not resizing, render the window itself.
553 let mut window = None;
554 if crossfade.is_none() && crossfade_fallback.is_none() {
555 window = Some(
556 self.window
557 .render(renderer, window_render_loc, scale, alpha, target)
558 .into_iter()
559 .map(Into::into),
560 );
561 }
562
563 let rv = crossfade
564 .into_iter()
565 .chain(crossfade_fallback)
566 .chain(window.into_iter().flatten());
567
568 let elem = self.effective_border_width().map(|width| {
569 self.border
570 .render(
571 renderer,
572 location + Point::from((width, width)),
573 scale,
574 view_size,
575 )
576 .map(Into::into)
577 });
578 let rv = rv.chain(elem.into_iter().flatten());
579
580 let elem = focus_ring.then(|| {
581 self.focus_ring
582 .render(renderer, location, scale, view_size)
583 .map(Into::into)
584 });
585 let rv = rv.chain(elem.into_iter().flatten());
586
587 let elem = self.is_fullscreen.then(|| {
588 SolidColorRenderElement::from_buffer(
589 &self.fullscreen_backdrop,
590 location.to_physical_precise_round(scale),
591 scale,
592 1.,
593 Kind::Unspecified,
594 )
595 .into()
596 });
597 rv.chain(elem)
598 }
599
600 pub fn render<R: NiriRenderer>(
601 &self,
602 renderer: &mut R,
603 location: Point<i32, Logical>,
604 scale: Scale<f64>,
605 view_size: Size<i32, Logical>,
606 focus_ring: bool,
607 target: RenderTarget,
608 ) -> impl Iterator<Item = TileRenderElement<R>> {
609 if let Some(anim) = &self.open_animation {
610 let renderer = renderer.as_gles_renderer();
611 let elements =
612 self.render_inner(renderer, location, scale, view_size, focus_ring, target);
613 let elements = elements.collect::<Vec<TileRenderElement<_>>>();
614
615 let elem = OffscreenRenderElement::new(
616 renderer,
617 scale.x as i32,
618 &elements,
619 anim.clamped_value().clamp(0., 1.) as f32,
620 );
621 self.window()
622 .set_offscreen_element_id(Some(elem.id().clone()));
623
624 let mut center = location;
625 center.x += self.tile_size().w / 2;
626 center.y += self.tile_size().h / 2;
627
628 Some(TileRenderElement::Offscreen(
629 RescaleRenderElement::from_element(
630 elem,
631 center.to_physical_precise_round(scale),
632 (anim.value() / 2. + 0.5).max(0.),
633 ),
634 ))
635 .into_iter()
636 .chain(None.into_iter().flatten())
637 } else {
638 self.window().set_offscreen_element_id(None);
639
640 let elements =
641 self.render_inner(renderer, location, scale, view_size, focus_ring, target);
642 None.into_iter().chain(Some(elements).into_iter().flatten())
643 }
644 }
645
646 fn render_snapshot<E, C>(
647 &self,
648 renderer: &mut GlesRenderer,
649 scale: Scale<f64>,
650 view_size: Size<i32, Logical>,
651 contents: Vec<C>,
652 ) -> Vec<TileSnapshotRenderElement>
653 where
654 E: Into<TileSnapshotContentsRenderElement>,
655 C: ToRenderElement<RenderElement = E>,
656 {
657 let alpha = if self.is_fullscreen {
658 1.
659 } else {
660 self.window.rules().opacity.unwrap_or(1.).clamp(0., 1.)
661 };
662
663 let window_size = self.window_size();
664 let animated_window_size = self.animated_window_size();
665 let animated_scale = animated_window_size.to_f64() / window_size.to_f64();
666
667 let mut rv = vec![];
668
669 for baked in contents {
670 let elem = baked.to_render_element(self.window_loc(), scale, alpha, Kind::Unspecified);
671 let elem: TileSnapshotContentsRenderElement = elem.into();
672
673 let origin = self.window_loc().to_physical_precise_round(scale);
674 let elem = RescaleRenderElement::from_element(elem, origin, animated_scale);
675 rv.push(elem.into());
676 }
677
678 if let Some(width) = self.effective_border_width() {
679 rv.extend(
680 self.border
681 .render(renderer, Point::from((width, width)), scale, view_size)
682 .map(Into::into),
683 );
684 }
685
686 if self.is_fullscreen {
687 let elem = SolidColorRenderElement::from_buffer(
688 &self.fullscreen_backdrop,
689 Point::from((0, 0)),
690 scale,
691 1.,
692 Kind::Unspecified,
693 );
694 rv.push(elem.into());
695 }
696
697 rv
698 }
699
700 pub fn take_snapshot_for_close_anim(
701 &self,
702 renderer: &mut GlesRenderer,
703 scale: Scale<f64>,
704 view_size: Size<i32, Logical>,
705 ) -> Option<RenderSnapshot<TileSnapshotRenderElement, TileSnapshotRenderElement>> {
706 let snapshot = self.window.take_unmap_snapshot()?;
707
708 Some(RenderSnapshot {
709 contents: self.render_snapshot(renderer, scale, view_size, snapshot.contents),
710 blocked_out_contents: self.render_snapshot(
711 renderer,
712 scale,
713 view_size,
714 snapshot.blocked_out_contents,
715 ),
716 block_out_from: snapshot.block_out_from,
717 size: self.animated_tile_size(),
718 texture: Default::default(),
719 blocked_out_texture: Default::default(),
720 })
721 }
722}