Skip to main content

cotis_defaults/
render_commands.rs

1use crate::colors::Color;
2#[cfg(any(feature = "complex_color", feature = "complex_colored_text"))]
3use crate::colors::ColorLayer;
4use cotis::utils::OwnedOrRef;
5use cotis_utils::math::BoundingBox;
6use std::fmt::Debug;
7
8/// Fill command for rounded rectangles.
9/// Represents a rectangle with a specified color and corner radii.
10#[derive(Debug, Clone)]
11#[cfg_attr(
12    feature = "serialization",
13    derive(serde::Serialize, serde::Deserialize)
14)]
15#[cfg_attr(
16    feature = "serialization",
17    serde(bound(deserialize = "
18    ExtraData: serde::de::DeserializeOwned
19"))
20)]
21pub struct Rectangle<'a, ExtraData> {
22    /// Shared metadata for renderer processing and ordering.
23    pub info: RenderCommandInfo<'a, ExtraData>,
24    /// The fill color of the rectangle (solid or layered when `complex_color` is enabled).
25    #[cfg(not(feature = "complex_color"))]
26    pub color: Color,
27    #[cfg(feature = "complex_color")]
28    pub color: ColorLayer,
29    /// The corner radii for rounded edges.
30    pub corner_radii: CornerRadii,
31}
32
33/// Represents a text element with styling attributes.
34#[derive(Debug, Clone)]
35#[cfg_attr(
36    feature = "serialization",
37    derive(serde::Serialize, serde::Deserialize)
38)]
39#[cfg_attr(
40    feature = "serialization",
41    serde(bound(deserialize = "
42    ExtraData: serde::de::DeserializeOwned
43"))
44)]
45pub struct Text<'a, ExtraData> {
46    /// Shared metadata for renderer processing and ordering.
47    pub info: RenderCommandInfo<'a, ExtraData>,
48    /// The text content.
49    pub text: OwnedOrRef<'a, str>,
50    /// The color of the text (solid or layered when `complex_colored_text` is enabled).
51    #[cfg(not(feature = "complex_colored_text"))]
52    pub color: Color,
53    #[cfg(feature = "complex_colored_text")]
54    pub color: ColorLayer,
55    /// The ID of the font used.
56    pub font_id: u16,
57    /// The font size.
58    pub font_size: f32,
59    /// The spacing between letters.
60    pub letter_spacing: f32,
61    /// The line height.
62    pub line_height: f32,
63}
64
65/// Defines individual corner radii for an element.
66#[derive(Debug, Clone)]
67#[cfg_attr(
68    feature = "serialization",
69    derive(serde::Serialize, serde::Deserialize)
70)]
71pub struct CornerRadii {
72    /// The radius for the top-left corner.
73    pub top_left: f32,
74    /// The radius for the top-right corner.
75    pub top_right: f32,
76    /// The radius for the bottom-left corner.
77    pub bottom_left: f32,
78    /// The radius for the bottom-right corner.
79    pub bottom_right: f32,
80}
81
82/// Defines the border width for each side of an element.
83#[derive(Debug, Clone)]
84#[cfg_attr(
85    feature = "serialization",
86    derive(serde::Serialize, serde::Deserialize)
87)]
88pub struct BorderWidth {
89    /// Border width on the left side.
90    pub left: f32,
91    /// Border width on the right side.
92    pub right: f32,
93    /// Border width on the top side.
94    pub top: f32,
95    /// Border width on the bottom side.
96    pub bottom: f32,
97    /// Border width between child elements.
98    pub between_children: f32,
99}
100
101/// Represents a border with a specified color, width, and corner radii.
102#[derive(Debug, Clone)]
103#[cfg_attr(
104    feature = "serialization",
105    derive(serde::Serialize, serde::Deserialize)
106)]
107#[cfg_attr(
108    feature = "serialization",
109    serde(bound(deserialize = "
110    ExtraData: serde::de::DeserializeOwned
111"))
112)]
113pub struct Border<'a, ExtraData> {
114    /// Shared metadata for renderer processing and ordering.
115    pub info: RenderCommandInfo<'a, ExtraData>,
116    /// The border color.
117    pub color: Color,
118    /// The corner radii for rounded border edges.
119    pub corner_radii: CornerRadii,
120    /// The width of the border on each side.
121    pub width: BorderWidth,
122}
123
124/// Represents an image with defined dimensions and data.
125#[derive(Debug, Clone)]
126#[cfg_attr(
127    feature = "serialization",
128    derive(serde::Serialize, serde::Deserialize)
129)]
130#[cfg_attr(
131    feature = "serialization",
132    serde(bound(deserialize = "
133    ImageElementData: serde::de::DeserializeOwned,
134    ExtraData: serde::de::DeserializeOwned
135"))
136)]
137pub struct Image<'a, ImageElementData, ExtraData> {
138    /// Shared metadata for renderer processing and ordering.
139    pub info: RenderCommandInfo<'a, ExtraData>,
140    /// Background color (solid or layered when `complex_color` is enabled).
141    #[cfg(not(feature = "complex_color"))]
142    pub background_color: Color,
143    #[cfg(feature = "complex_color")]
144    pub background_color: ColorLayer,
145    /// The corner radii for rounded border edges.
146    pub corner_radii: CornerRadii,
147    /// A pointer to the image data.
148    pub data: OwnedOrRef<'a, ImageElementData>,
149}
150
151/// Represents a clip element with corner radii, clipping axis and position.
152#[derive(Debug, Clone)]
153#[cfg_attr(
154    feature = "serialization",
155    derive(serde::Serialize, serde::Deserialize)
156)]
157#[cfg_attr(
158    feature = "serialization",
159    serde(bound(deserialize = "
160    ExtraData: serde::de::DeserializeOwned
161"))
162)]
163pub struct ClipStart<'a, ExtraData> {
164    /// Shared metadata for renderer processing and ordering.
165    pub info: RenderCommandInfo<'a, ExtraData>,
166    /// Enables horizontal clipping.
167    pub horizontal: bool,
168    /// Enables vertical clipping.
169    pub vertical: bool,
170    /// The corner radii for rounded edges.
171    pub corner_radii: CornerRadii,
172}
173
174/// Ends the most recent active clip region.
175#[derive(Debug, Clone)]
176#[cfg_attr(
177    feature = "serialization",
178    derive(serde::Serialize, serde::Deserialize)
179)]
180pub struct ClipEnd;
181
182/// Shared command metadata available on every render command.
183#[derive(Debug, Clone)]
184#[cfg_attr(
185    feature = "serialization",
186    derive(serde::Serialize, serde::Deserialize)
187)]
188#[cfg_attr(
189    feature = "serialization",
190    serde(bound(deserialize = "
191    ExtraData: serde::de::DeserializeOwned
192"))
193)]
194pub struct RenderCommandInfo<'a, ExtraData> {
195    /// The bounding box defining the area occupied by the element.
196    pub bounding_box: BoundingBox,
197    /// A unique identifier for the render command.
198    pub id: u32,
199    /// The z-index determines the stacking order of elements.
200    /// Higher values are drawn above lower values.
201    pub z_index: i16,
202    /// Extra data
203    pub extra_data: Option<OwnedOrRef<'a, ExtraData>>,
204}
205
206/// Typed heterogeneous command-list helpers.
207pub mod render_t_list;
208
209#[cfg(test)]
210mod test {
211    use cotis::utils::OwnedOrRef;
212    use std::sync::Arc;
213
214    fn is_send<T: Send>() {}
215    #[test]
216    fn test_render_command() {
217        is_send::<OwnedOrRef<()>>();
218    }
219
220    fn str_owned_ref(_: OwnedOrRef<str>) {
221        // noting
222    }
223
224    #[test]
225    fn test_owned_ref() {
226        let res: String = String::new();
227        let res: &String = &res;
228        str_owned_ref(OwnedOrRef::Ref(res));
229
230        let s = Arc::new(String::from("hello"));
231
232        let by_ref: OwnedOrRef<'_, str> = OwnedOrRef::Ref("hello");
233        let by_arc: OwnedOrRef<'_, str> = OwnedOrRef::AsOwnedRef(s);
234        assert_eq!(by_ref.as_ref(), by_arc.as_ref());
235    }
236}