tessera_ui/renderer/drawer/command.rs
1//! Draw command traits and barrier requirements.
2//!
3//! This module defines the core traits and types for graphics rendering
4//! commands in the unified command system.
5
6use downcast_rs::{Downcast, impl_downcast};
7use dyn_clone::DynClone;
8
9use crate::{
10 px::{PxPosition, PxRect, PxSize},
11 render_scene::{DrawRegion, PaddingRect, SampleRegion},
12};
13
14/// Trait for graphics rendering commands that can be processed by draw
15/// pipelines.
16///
17/// Implement this trait for structs that represent graphics operations such as
18/// shape drawing, text rendering, image display, or custom visual effects.
19///
20/// # Example
21///
22/// ```
23/// use tessera_ui::DrawCommand;
24///
25/// #[derive(Clone, PartialEq)]
26/// struct RectangleCommand {
27/// color: [f32; 4],
28/// corner_radius: f32,
29/// }
30///
31/// impl DrawCommand for RectangleCommand {
32/// fn apply_opacity(&mut self, opacity: f32) {
33/// self.color[3] *= opacity.clamp(0.0, 1.0);
34/// }
35/// }
36/// ```
37pub trait DrawCommand: DynClone + Downcast + Send + Sync {
38 /// Specifies sample requirements for this draw operation.
39 ///
40 /// As a default implementation, this returns `None`, indicating that
41 /// the command does not need to sample from previously rendered content.
42 ///
43 /// Override this method if your command requires sampling from prior
44 /// contents.
45 fn sample_region(&self) -> Option<SampleRegion> {
46 None
47 }
48
49 /// Specifies the drawing region for this command.
50 ///
51 /// As a default implementation, this returns `DrawRegion::PaddedLocal` with
52 /// zero padding, indicating that the command draws within its own bounds.
53 ///
54 /// Override this method if your command draws to a different region but do
55 /// not want to affect layout calculations.
56 fn draw_region(&self) -> DrawRegion {
57 DrawRegion::PaddedLocal(PaddingRect::ZERO)
58 }
59
60 /// Applies an opacity multiplier to this command.
61 ///
62 /// In most cases you must implement this on your command to support
63 /// opacity changes in the UI.
64 fn apply_opacity(&mut self, opacity: f32);
65
66 /// Returns an absolute rectangle used for ordering decisions.
67 ///
68 /// The default implementation returns `None`, which falls back to the draw
69 /// region derived from the command size and position.
70 fn ordering_rect(&self, _position: PxPosition, _size: PxSize) -> Option<PxRect> {
71 None
72 }
73}
74
75impl_downcast!(DrawCommand);
76
77dyn_clone::clone_trait_object!(DrawCommand);