kael 0.2.0

GPU-accelerated native UI framework for Rust — build desktop apps with Metal, DirectX, and Vulkan rendering
Documentation
use refineable::Refineable as _;

use crate::{
    App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId, IntoElement, Pixels,
    Style, StyleRefinement, Styled, Window,
};

/// Hidden trait backing the overloaded [`canvas`] helper.
#[doc(hidden)]
pub trait CanvasConstructor<Arg2> {
    /// The canvas type produced by the constructor.
    type Output;

    /// Construct a canvas element from the given arguments.
    fn into_canvas(self, arg2: Arg2) -> Self::Output;
}

/// Construct a canvas element.
///
/// This helper supports two forms:
///
/// - `canvas(prepaint, paint)` keeps the legacy low-level API that passes bounds through a
///   prepaint phase before painting.
/// - `canvas(size, draw)` creates an immediate-mode canvas powered by [`crate::DrawContext`].
pub fn canvas<Arg1, Arg2>(arg1: Arg1, arg2: Arg2) -> <Arg1 as CanvasConstructor<Arg2>>::Output
where
    Arg1: CanvasConstructor<Arg2>,
{
    arg1.into_canvas(arg2)
}

/// Construct a canvas element from explicit prepaint and paint closures.
///
/// This is the non-overloaded form of [`canvas`]. Prefer it when closure
/// parameter types should be inferred (the overloaded [`canvas`] helper
/// requires fully annotated closure signatures).
pub fn canvas_with_prepaint<T>(
    prepaint: impl 'static + for<'a, 'b> FnOnce(Bounds<Pixels>, &'a mut Window, &'b mut App) -> T,
    paint: impl 'static + for<'a, 'b> FnOnce(Bounds<Pixels>, T, &'a mut Window, &'b mut App),
) -> Canvas<T> {
    Canvas {
        prepaint: Some(Box::new(prepaint)),
        paint: Some(Box::new(paint)),
        style: StyleRefinement::default(),
    }
}

/// A canvas element, meant for accessing the low level paint API without defining a whole
/// custom element
pub struct Canvas<T> {
    prepaint: Option<Box<dyn for<'a, 'b> FnOnce(Bounds<Pixels>, &'a mut Window, &'b mut App) -> T>>,
    paint: Option<Box<dyn for<'a, 'b> FnOnce(Bounds<Pixels>, T, &'a mut Window, &'b mut App)>>,
    style: StyleRefinement,
}

impl<T, FPrepaint, FPaint> CanvasConstructor<FPaint> for FPrepaint
where
    T: 'static,
    FPrepaint: 'static + for<'a, 'b> FnOnce(Bounds<Pixels>, &'a mut Window, &'b mut App) -> T,
    FPaint: 'static + for<'a, 'b> FnOnce(Bounds<Pixels>, T, &'a mut Window, &'b mut App),
{
    type Output = Canvas<T>;

    fn into_canvas(self, paint: FPaint) -> Self::Output {
        canvas_with_prepaint(self, paint)
    }
}

impl<T: 'static> IntoElement for Canvas<T> {
    type Element = Self;

    fn into_element(self) -> Self::Element {
        self
    }
}

impl<T: 'static> Element for Canvas<T> {
    type RequestLayoutState = Style;
    type PrepaintState = Option<T>;

    fn id(&self) -> Option<ElementId> {
        None
    }

    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (crate::LayoutId, Self::RequestLayoutState) {
        let mut style = Style::default();
        style.refine(&self.style);
        let layout_id = window.request_layout(style.clone(), [], cx);
        (layout_id, style)
    }

    fn prepaint(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        _request_layout: &mut Style,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<T> {
        Some(self.prepaint.take().unwrap()(bounds, window, cx))
    }

    fn paint(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        style: &mut Style,
        prepaint: &mut Self::PrepaintState,
        window: &mut Window,
        cx: &mut App,
    ) {
        let prepaint = prepaint.take().unwrap();
        style.paint(bounds, window, cx, |window, cx| {
            (self.paint.take().unwrap())(bounds, prepaint, window, cx)
        });
    }
}

impl<T> Styled for Canvas<T> {
    fn style(&mut self) -> &mut crate::StyleRefinement {
        &mut self.style
    }
}