summaryrefslogtreecommitdiffstats
path: root/renderer/src/window/compositor.rs
blob: ad78d8bf25e1d480f6931b3bedc4f4b78681f8db (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use crate::{Backend, Color, Error, Renderer, Settings, Viewport};

use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};

pub use iced_graphics::window::compositor::{Information, SurfaceError};

pub enum Compositor<Theme> {
    Wgpu(iced_wgpu::window::Compositor<Theme>),
}

pub enum Surface {
    Wgpu(iced_wgpu::window::Surface),
}

impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
    type Settings = Settings;
    type Renderer = Renderer<Theme>;
    type Surface = Surface;

    fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(
        settings: Self::Settings,
        compatible_window: Option<&W>,
    ) -> Result<(Self, Self::Renderer), Error> {
        let (compositor, backend) = iced_wgpu::window::compositor::new(
            iced_wgpu::Settings {
                default_font: settings.default_font,
                default_text_size: settings.default_text_size,
                antialiasing: settings.antialiasing,
                ..iced_wgpu::Settings::from_env()
            },
            compatible_window,
        )?;

        Ok((
            Self::Wgpu(compositor),
            Renderer::new(Backend::Wgpu(backend)),
        ))
    }

    fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
        &mut self,
        window: &W,
    ) -> Surface {
        match self {
            Self::Wgpu(compositor) => {
                Surface::Wgpu(compositor.create_surface(window))
            }
        }
    }

    fn configure_surface(
        &mut self,
        surface: &mut Surface,
        width: u32,
        height: u32,
    ) {
        match (self, surface) {
            (Self::Wgpu(compositor), Surface::Wgpu(surface)) => {
                compositor.configure_surface(surface, width, height);
            }
        }
    }

    fn fetch_information(&self) -> Information {
        match self {
            Self::Wgpu(compositor) => compositor.fetch_information(),
        }
    }

    fn present<T: AsRef<str>>(
        &mut self,
        renderer: &mut Self::Renderer,
        surface: &mut Self::Surface,
        viewport: &Viewport,
        background_color: Color,
        overlay: &[T],
    ) -> Result<(), SurfaceError> {
        renderer.with_primitives(|backend, primitives| {
            match (self, backend, surface) {
                (
                    Self::Wgpu(compositor),
                    Backend::Wgpu(backend),
                    Surface::Wgpu(surface),
                ) => iced_wgpu::window::compositor::present(
                    compositor,
                    backend,
                    surface,
                    primitives,
                    viewport,
                    background_color,
                    overlay,
                ),
            }
        })
    }
}