x_graphics/
layer.rs

1use crate::{
2    base::Dimension,
3    canvas::{Canvas, CanvasBackend},
4    device::{DeviceContext, DeviceContextBackend},
5    error::GraphicsError,
6    DefaultCanvas, DefaultDeviceContext, DefaultLayer,
7};
8
9pub trait LayerBackend: Dimension {
10    type DeviceContextType: DeviceContextBackend;
11    type CanvasType: CanvasBackend;
12
13    fn new(context: Option<&Self::DeviceContextType>, width: usize, height: usize, canvas: &Self::CanvasType) -> Result<Self, GraphicsError>
14    where
15        Self: Sized;
16}
17
18pub struct Layer<T: LayerBackend> {
19    pub(crate) backend: T,
20}
21
22impl<T: LayerBackend> Layer<T> {
23    pub fn new(
24        context: Option<&DeviceContext<T::DeviceContextType>>,
25        width: usize,
26        height: usize,
27        canvas: &Canvas<T::CanvasType>,
28    ) -> Result<Self, GraphicsError> {
29        Ok(Self {
30            backend: T::new(context.map(|ctx| &ctx.backend), width, height, &canvas.backend)?,
31        })
32    }
33}
34
35impl Layer<DefaultLayer> {
36    pub fn default_new(
37        context: Option<&DeviceContext<DefaultDeviceContext>>,
38        width: usize,
39        height: usize,
40        canvas: &Canvas<DefaultCanvas>,
41    ) -> Result<Self, GraphicsError> {
42        Self::new(context, width, height, canvas)
43    }
44}