Skip to main content

cranpose_ui/
draw.rs

1use std::rc::Rc;
2
3use cranpose_ui_graphics::{CommandRecording, DrawPrimitive, DrawScope, DrawScopeDefault};
4
5use crate::{BackgroundDraw, 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    /// A background modifier's fill, behind the node's content.
25    Background(BackgroundDraw),
26}
27
28impl DrawCommand {
29    /// Records the command's drawing into `scope`: the whole command, its
30    /// content marker included.
31    pub fn draw(&self, scope: &mut DrawScopeDefault) {
32        match self {
33            Self::Behind(f) | Self::WithContent(f) | Self::Overlay(f) => f(scope),
34            Self::Background(background) => background.draw(scope),
35        }
36    }
37
38    // Whether `other` is the same command: the same closure, or a background
39    // that draws the same.
40    pub(crate) fn same_as(&self, other: &Self) -> bool {
41        match (self, other) {
42            (Self::Behind(a), Self::Behind(b))
43            | (Self::WithContent(a), Self::WithContent(b))
44            | (Self::Overlay(a), Self::Overlay(b)) => Rc::ptr_eq(a, b),
45            (Self::Background(a), Self::Background(b)) => a.same_as(b),
46            _ => false,
47        }
48    }
49}
50
51/// The scope every framework-run draw command records into: sized by the
52/// consumer, measuring text with the app's fonts, recording into `recording`
53/// in place. A recording kept from an earlier frame keeps the capacity its
54/// buffers earned.
55pub fn command_draw_scope(size: Size, recording: &mut CommandRecording) -> DrawScopeDefault<'_> {
56    DrawScopeDefault::with_text_measurer(size, AppContextTextMeasurer::shared(), recording)
57}
58
59/// Collects the draw commands that a `Modifier::draw_with_cache` block records.
60#[derive(Default, Clone)]
61pub struct DrawCacheBuilder {
62    behind: Vec<DrawCommandFn>,
63    with_content: Vec<DrawCommandFn>,
64    overlay: Vec<DrawCommandFn>,
65}
66
67impl DrawCacheBuilder {
68    /// Records drawing that appears behind the node's content.
69    pub fn on_draw_behind(&mut self, f: impl Fn(&mut dyn DrawScope) + 'static) {
70        self.behind
71            .push(Rc::new(move |scope: &mut DrawScopeDefault| f(scope)));
72    }
73
74    /// Returns the recorded commands in the order behind, with-content, overlay.
75    pub fn finish(self) -> Vec<DrawCommand> {
76        let mut commands = Vec::new();
77        commands.extend(self.behind.into_iter().map(DrawCommand::Behind));
78        commands.extend(self.with_content.into_iter().map(DrawCommand::WithContent));
79        commands.extend(self.overlay.into_iter().map(DrawCommand::Overlay));
80        commands
81    }
82}
83
84/// Runs every command at `size` and returns the primitives they record, in command order.
85pub fn execute_draw_commands(commands: &[DrawCommand], size: Size) -> Vec<DrawPrimitive> {
86    let mut primitives = Vec::new();
87    let mut recording = CommandRecording::default();
88    for command in commands {
89        let mut scope = command_draw_scope(size, &mut recording);
90        command.draw(&mut scope);
91        primitives.extend(scope.into_primitives());
92    }
93    primitives
94}