x-graphics 0.2.1

Graphics framework for X
Documentation
use crate::{
    base::Dimension,
    canvas::{Canvas, CanvasBackend},
    device::{DeviceContext, DeviceContextBackend},
    error::GraphicsError,
    DefaultCanvas, DefaultDeviceContext, DefaultLayer,
};

pub trait LayerBackend: Dimension {
    type DeviceContextType: DeviceContextBackend;
    type CanvasType: CanvasBackend;

    fn new(context: Option<&Self::DeviceContextType>, width: usize, height: usize, canvas: &Self::CanvasType) -> Result<Self, GraphicsError>
    where
        Self: Sized;
}

pub struct Layer<T: LayerBackend> {
    pub(crate) backend: T,
}

impl<T: LayerBackend> Layer<T> {
    pub fn new(
        context: Option<&DeviceContext<T::DeviceContextType>>,
        width: usize,
        height: usize,
        canvas: &Canvas<T::CanvasType>,
    ) -> Result<Self, GraphicsError> {
        Ok(Self {
            backend: T::new(context.map(|ctx| &ctx.backend), width, height, &canvas.backend)?,
        })
    }
}

impl Layer<DefaultLayer> {
    pub fn default_new(
        context: Option<&DeviceContext<DefaultDeviceContext>>,
        width: usize,
        height: usize,
        canvas: &Canvas<DefaultCanvas>,
    ) -> Result<Self, GraphicsError> {
        Self::new(context, width, height, canvas)
    }
}