aboutsummaryrefslogtreecommitdiff
path: root/src/ui/screen_transition.rs
blob: 3b5c215944ab505d06c4049ccca7fa87e0fc92f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::time::Duration;

use smithay::backend::renderer::element::Kind;
use smithay::backend::renderer::gles::GlesTexture;
use smithay::utils::{Scale, Transform};

use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement};
use crate::render_helpers::RenderTarget;

pub const DELAY: Duration = Duration::from_millis(250);
pub const DURATION: Duration = Duration::from_millis(500);

#[derive(Debug)]
pub struct ScreenTransition {
    /// Texture to crossfade from for each render target.
    from_texture: [TextureBuffer<GlesTexture>; 3],
    /// Monotonic time when to start the crossfade.
    start_at: Duration,
    /// Current crossfade alpha.
    alpha: f32,
}

impl ScreenTransition {
    pub fn new(from_texture: [TextureBuffer<GlesTexture>; 3], start_at: Duration) -> Self {
        Self {
            from_texture,
            start_at,
            alpha: 1.,
        }
    }

    pub fn advance_animations(&mut self, current_time: Duration) {
        if self.start_at + DURATION <= current_time {
            self.alpha = 0.;
        } else if self.start_at <= current_time {
            self.alpha = 1. - (current_time - self.start_at).as_secs_f32() / DURATION.as_secs_f32();
        } else {
            self.alpha = 1.;
        }
    }

    pub fn is_done(&self) -> bool {
        self.alpha == 0.
    }

    pub fn update_render_elements(&mut self, scale: Scale<f64>, transform: Transform) {
        // These textures should remain full-screen, even if scale or transform changes.
        for buffer in &mut self.from_texture {
            buffer.set_texture_scale(scale);
            buffer.set_texture_transform(transform);
        }
    }

    pub fn render(&self, target: RenderTarget) -> PrimaryGpuTextureRenderElement {
        let idx = match target {
            RenderTarget::Output => 0,
            RenderTarget::Screencast => 1,
            RenderTarget::ScreenCapture => 2,
        };

        PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer(
            self.from_texture[idx].clone(),
            (0., 0.),
            self.alpha,
            None,
            None,
            Kind::Unspecified,
        ))
    }
}