cotis-defaults 0.1.0-alpha.2

Modular Rust UI framework core
Documentation
use crate::colors::Color;
#[cfg(any(feature = "complex_color", feature = "complex_colored_text"))]
use crate::colors::ColorLayer;
use cotis::utils::OwnedOrRef;
use cotis_utils::math::BoundingBox;
use std::fmt::Debug;

/// Fill command for rounded rectangles.
/// Represents a rectangle with a specified color and corner radii.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
    feature = "serialization",
    serde(bound(deserialize = "
    ExtraData: serde::de::DeserializeOwned
"))
)]
pub struct Rectangle<'a, ExtraData> {
    /// Shared metadata for renderer processing and ordering.
    pub info: RenderCommandInfo<'a, ExtraData>,
    /// The fill color of the rectangle (solid or layered when `complex_color` is enabled).
    #[cfg(not(feature = "complex_color"))]
    pub color: Color,
    #[cfg(feature = "complex_color")]
    pub color: ColorLayer,
    /// The corner radii for rounded edges.
    pub corner_radii: CornerRadii,
}

/// Represents a text element with styling attributes.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
    feature = "serialization",
    serde(bound(deserialize = "
    ExtraData: serde::de::DeserializeOwned
"))
)]
pub struct Text<'a, ExtraData> {
    /// Shared metadata for renderer processing and ordering.
    pub info: RenderCommandInfo<'a, ExtraData>,
    /// The text content.
    pub text: OwnedOrRef<'a, str>,
    /// The color of the text (solid or layered when `complex_colored_text` is enabled).
    #[cfg(not(feature = "complex_colored_text"))]
    pub color: Color,
    #[cfg(feature = "complex_colored_text")]
    pub color: ColorLayer,
    /// The ID of the font used.
    pub font_id: u16,
    /// The font size.
    pub font_size: f32,
    /// The spacing between letters.
    pub letter_spacing: f32,
    /// The line height.
    pub line_height: f32,
}

/// Defines individual corner radii for an element.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct CornerRadii {
    /// The radius for the top-left corner.
    pub top_left: f32,
    /// The radius for the top-right corner.
    pub top_right: f32,
    /// The radius for the bottom-left corner.
    pub bottom_left: f32,
    /// The radius for the bottom-right corner.
    pub bottom_right: f32,
}

/// Defines the border width for each side of an element.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct BorderWidth {
    /// Border width on the left side.
    pub left: f32,
    /// Border width on the right side.
    pub right: f32,
    /// Border width on the top side.
    pub top: f32,
    /// Border width on the bottom side.
    pub bottom: f32,
    /// Border width between child elements.
    pub between_children: f32,
}

/// Represents a border with a specified color, width, and corner radii.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
    feature = "serialization",
    serde(bound(deserialize = "
    ExtraData: serde::de::DeserializeOwned
"))
)]
pub struct Border<'a, ExtraData> {
    /// Shared metadata for renderer processing and ordering.
    pub info: RenderCommandInfo<'a, ExtraData>,
    /// The border color.
    pub color: Color,
    /// The corner radii for rounded border edges.
    pub corner_radii: CornerRadii,
    /// The width of the border on each side.
    pub width: BorderWidth,
}

/// Represents an image with defined dimensions and data.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
    feature = "serialization",
    serde(bound(deserialize = "
    ImageElementData: serde::de::DeserializeOwned,
    ExtraData: serde::de::DeserializeOwned
"))
)]
pub struct Image<'a, ImageElementData, ExtraData> {
    /// Shared metadata for renderer processing and ordering.
    pub info: RenderCommandInfo<'a, ExtraData>,
    /// Background color (solid or layered when `complex_color` is enabled).
    #[cfg(not(feature = "complex_color"))]
    pub background_color: Color,
    #[cfg(feature = "complex_color")]
    pub background_color: ColorLayer,
    /// The corner radii for rounded border edges.
    pub corner_radii: CornerRadii,
    /// A pointer to the image data.
    pub data: OwnedOrRef<'a, ImageElementData>,
}

/// Represents a clip element with corner radii, clipping axis and position.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
    feature = "serialization",
    serde(bound(deserialize = "
    ExtraData: serde::de::DeserializeOwned
"))
)]
pub struct ClipStart<'a, ExtraData> {
    /// Shared metadata for renderer processing and ordering.
    pub info: RenderCommandInfo<'a, ExtraData>,
    /// Enables horizontal clipping.
    pub horizontal: bool,
    /// Enables vertical clipping.
    pub vertical: bool,
    /// The corner radii for rounded edges.
    pub corner_radii: CornerRadii,
}

/// Ends the most recent active clip region.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct ClipEnd;

/// Shared command metadata available on every render command.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
    feature = "serialization",
    serde(bound(deserialize = "
    ExtraData: serde::de::DeserializeOwned
"))
)]
pub struct RenderCommandInfo<'a, ExtraData> {
    /// The bounding box defining the area occupied by the element.
    pub bounding_box: BoundingBox,
    /// A unique identifier for the render command.
    pub id: u32,
    /// The z-index determines the stacking order of elements.
    /// Higher values are drawn above lower values.
    pub z_index: i16,
    /// Extra data
    pub extra_data: Option<OwnedOrRef<'a, ExtraData>>,
}

/// Typed heterogeneous command-list helpers.
pub mod render_t_list;

#[cfg(test)]
mod test {
    use cotis::utils::OwnedOrRef;
    use std::sync::Arc;

    fn is_send<T: Send>() {}
    #[test]
    fn test_render_command() {
        is_send::<OwnedOrRef<()>>();
    }

    fn str_owned_ref(_: OwnedOrRef<str>) {
        // noting
    }

    #[test]
    fn test_owned_ref() {
        let res: String = String::new();
        let res: &String = &res;
        str_owned_ref(OwnedOrRef::Ref(res));

        let s = Arc::new(String::from("hello"));

        let by_ref: OwnedOrRef<'_, str> = OwnedOrRef::Ref("hello");
        let by_arc: OwnedOrRef<'_, str> = OwnedOrRef::AsOwnedRef(s);
        assert_eq!(by_ref.as_ref(), by_arc.as_ref());
    }
}