Skip to main content

cranpose_ui/modifier/
draw_cache.rs

1use std::rc::Rc;
2
3use cranpose_ui_graphics::{DrawScope, DrawScopeDefault};
4
5use super::{DrawCacheBuilder, DrawCommand, Modifier};
6use crate::modifier_nodes::DrawCommandElement;
7
8impl Modifier {
9    /// Draw around content.
10    ///
11    /// `draw_content()` splits drawing into behind (before) and overlay (after)
12    /// phases. If `draw_content()` is never called, primitives are treated as
13    /// overlay content.
14    ///
15    /// Example: `Modifier::empty().draw_with_content(|scope| { ... })`
16    pub fn draw_with_content(self, f: impl Fn(&mut dyn DrawScope) + 'static) -> Self {
17        let func = Rc::new(move |scope: &mut DrawScopeDefault| f(scope));
18        let modifier = Self::with_element(DrawCommandElement::new(DrawCommand::WithContent(func)));
19        self.then(modifier)
20    }
21
22    /// Draw content behind.
23    ///
24    /// Example: `Modifier::empty().draw_behind(|scope| { ... })`
25    pub fn draw_behind(self, f: impl Fn(&mut dyn DrawScope) + 'static) -> Self {
26        let func = Rc::new(move |scope: &mut DrawScopeDefault| f(scope));
27        let modifier = Self::with_element(DrawCommandElement::new(DrawCommand::Behind(func)));
28        self.then(modifier)
29    }
30
31    /// Draw with cache.
32    ///
33    /// Example: `Modifier::empty().draw_with_cache(|builder| { ... })`
34    pub fn draw_with_cache(self, build: impl FnOnce(&mut DrawCacheBuilder)) -> Self {
35        let mut builder = DrawCacheBuilder::default();
36        build(&mut builder);
37        let commands = builder.finish();
38        let modifier = Self::with_element(DrawCommandElement::from_commands(commands));
39        self.then(modifier)
40    }
41}