Skip to main content

cranpose_ui/modifier/
draw_cache.rs

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