Skip to main content

cranpose_ui/
draw.rs

1use std::rc::Rc;
2
3use cranpose_ui_graphics::{CommandRecording, DrawPrimitive, DrawScope, DrawScopeDefault};
4
5use crate::{modifier::Size, text::AppContextTextMeasurer};
6
7/// A draw command records into a scope the CONSUMER provides, instead of
8/// returning a freshly allocated primitive vector. This is the boundary that
9/// lets recording storage live with the command's stable identity and be
10/// reused frame over frame (the retained draw-command recorder), rather than
11/// being reallocated, moved, and dropped on every execution.
12pub type DrawCommandFn = Rc<dyn Fn(&mut DrawScopeDefault)>;
13
14/// A draw command and the phase of the node's drawing in which it runs.
15#[derive(Clone)]
16pub enum DrawCommand {
17    /// Records drawing that appears behind the node's content.
18    Behind(DrawCommandFn),
19    /// Records drawing around the node's content. A `draw_content` call in it marks where
20    /// the content draws, as in Compose's `drawWithContent`.
21    WithContent(DrawCommandFn),
22    /// Records drawing that appears in front of the node's content.
23    Overlay(DrawCommandFn),
24}
25
26/// The scope every framework-run draw command records into: sized by the
27/// consumer, measuring text with the app's fonts, recording into `recording`
28/// in place. A recording kept from an earlier frame keeps the capacity its
29/// buffers earned.
30pub fn command_draw_scope(size: Size, recording: &mut CommandRecording) -> DrawScopeDefault<'_> {
31    DrawScopeDefault::with_text_measurer(size, AppContextTextMeasurer::shared(), recording)
32}
33
34/// Collects the draw commands that a `Modifier::draw_with_cache` block records.
35#[derive(Default, Clone)]
36pub struct DrawCacheBuilder {
37    behind: Vec<DrawCommandFn>,
38    with_content: Vec<DrawCommandFn>,
39    overlay: Vec<DrawCommandFn>,
40}
41
42impl DrawCacheBuilder {
43    /// Records drawing that appears behind the node's content.
44    pub fn on_draw_behind(&mut self, f: impl Fn(&mut dyn DrawScope) + 'static) {
45        self.behind
46            .push(Rc::new(move |scope: &mut DrawScopeDefault| f(scope)));
47    }
48
49    /// Returns the recorded commands in the order behind, with-content, overlay.
50    pub fn finish(self) -> Vec<DrawCommand> {
51        let mut commands = Vec::new();
52        commands.extend(self.behind.into_iter().map(DrawCommand::Behind));
53        commands.extend(self.with_content.into_iter().map(DrawCommand::WithContent));
54        commands.extend(self.overlay.into_iter().map(DrawCommand::Overlay));
55        commands
56    }
57}
58
59/// Runs every command at `size` and returns the primitives they record, in command order.
60pub fn execute_draw_commands(commands: &[DrawCommand], size: Size) -> Vec<DrawPrimitive> {
61    let mut primitives = Vec::new();
62    let mut recording = CommandRecording::default();
63    for command in commands {
64        match command {
65            DrawCommand::Behind(f) | DrawCommand::WithContent(f) | DrawCommand::Overlay(f) => {
66                let mut scope = command_draw_scope(size, &mut recording);
67                f(&mut scope);
68                primitives.extend(scope.into_primitives());
69            }
70        }
71    }
72    primitives
73}