cotis-defaults 0.1.0-alpha.2

Modular Rust UI framework core
Documentation
//! Style structs used by [`crate::element_configs::ElementConfig`].
use crate::colors::{Color, ColorLayer};
use cotis_utils::math::{Dimensions, Vector2};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Core style helper types such as alignment, padding and clip settings.
pub mod types;
use types::*;

/// Sizing enums used by layout integrations.
pub mod sizing;
use sizing::*;

/// Fully transparent solid background (`Color::rgba(0, 0, 0, 0)`).
///
/// Wrapped as [`ColorLayer::Solid`] for use with [`CotisStyle::background_color`]
/// and render-command fill fields when the `complex_color` feature is enabled.
/// Matches the default background applied by [`CotisStyle::default`].
#[cfg(feature = "complex_color")]
pub const TRANSPARENT_BACKGROUND: ColorLayer = ColorLayer::Solid(Color::rgba(0.0, 0.0, 0.0, 0.0));
#[cfg(not(feature = "complex_color"))]
pub const TRANSPARENT_BACKGROUND: Color = Color::rgba(0.0, 0.0, 0.0, 0.0);

/// Opaque black solid background (`Color::rgba(0, 0, 0, 255)`).
///
/// Wrapped as [`ColorLayer::Solid`] for use with [`CotisStyle::background_color`]
/// and render-command fill fields when the `complex_color` feature is enabled.
#[cfg(feature = "complex_color")]
pub const BLACK_BACKGROUND: ColorLayer = ColorLayer::Solid(Color::rgba(0.0, 0.0, 0.0, 255.0));
#[cfg(not(feature = "complex_color"))]
pub const BLACK_BACKGROUND: Color = Color::rgba(0.0, 0.0, 0.0, 255.0);

/// Full style payload attached to each configured element.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(not(feature = "complex_color"), derive(Copy))]
pub struct CotisStyle<Sizing> {
    /// Element sizing behavior used by the layout engine.
    pub sizing: Sizing,
    /// Child layout settings (direction, gap, padding, alignment).
    pub layout: LayoutStyle,
    /// Element background color.
    #[cfg(not(feature = "complex_color"))]
    pub background_color: Color,
    /// Element background paint (solid/gradient/layered).
    #[cfg(feature = "complex_color")]
    pub background_color: ColorLayer,
    /// Per-corner radius for rounded corners.
    pub corner_radius: CornerRadiiStyle,
    /// Optional floating behavior relative to parent bounds.
    pub floating: Option<FloatingElementStyle>,
    /// Clip behavior for element content.
    pub clip: ClipElementConfig,
    /// Border styling.
    pub border: BorderElementStyle,
}

impl Default for CotisStyle<Sizing> {
    /// Default style for `Sizing` enum-based integrations.
    fn default() -> Self {
        Self {
            sizing: Sizing::DoubleAxis(DoubleAxisSizing {
                width: AxisSizing::Fit(0.0, f32::MAX),
                height: AxisSizing::Fit(0.0, f32::MAX),
            }),
            layout: Default::default(),
            #[cfg(not(feature = "complex_color"))]
            background_color: Color::rgba(0.0, 0.0, 0.0, 0.0),
            #[cfg(feature = "complex_color")]
            background_color: ColorLayer::Solid(Color::rgba(0.0, 0.0, 0.0, 0.0)),
            corner_radius: CornerRadiiStyle {
                top_left: 0.0,
                top_right: 0.0,
                bottom_left: 0.0,
                bottom_right: 0.0,
            },
            floating: None,
            clip: ClipElementConfig {
                horizontal: false,
                vertical: false,
                child_offset: Default::default(),
            },
            border: BorderElementStyle {
                color: Color::rgba(0.0, 0.0, 0.0, 0.0),
                width: BorderWidthStyle {
                    left: 0.0,
                    right: 0.0,
                    top: 0.0,
                    bottom: 0.0,
                    between_children: 0.0,
                },
            },
        }
    }
}

impl Default for CotisStyle<DoubleAxisSizing> {
    /// Default style for direct `DoubleAxisSizing` integrations.
    fn default() -> Self {
        Self {
            sizing: DoubleAxisSizing {
                width: AxisSizing::Fit(0.0, f32::MAX),
                height: AxisSizing::Fit(0.0, f32::MAX),
            },
            layout: Default::default(),
            #[cfg(not(feature = "complex_color"))]
            background_color: Color::rgba(0.0, 0.0, 0.0, 0.0),
            #[cfg(feature = "complex_color")]
            background_color: ColorLayer::Solid(Color::rgba(0.0, 0.0, 0.0, 0.0)),
            corner_radius: CornerRadiiStyle {
                top_left: 0.0,
                top_right: 0.0,
                bottom_left: 0.0,
                bottom_right: 0.0,
            },
            floating: None,
            clip: ClipElementConfig {
                horizontal: false,
                vertical: false,
                child_offset: Default::default(),
            },
            border: BorderElementStyle {
                color: Color::rgba(0.0, 0.0, 0.0, 0.0),
                width: BorderWidthStyle {
                    left: 0.0,
                    right: 0.0,
                    top: 0.0,
                    bottom: 0.0,
                    between_children: 0.0,
                },
            },
        }
    }
}

/// Defines individual corner radii for an element.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CornerRadiiStyle {
    /// 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,
}

/// Border style payload.
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BorderElementStyle {
    /// Border color.
    pub color: Color,
    /// Per-side border widths.
    pub width: BorderWidthStyle,
}

/// Border width values for each side.
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BorderWidthStyle {
    /// Left border width.
    pub left: f32,
    /// Right border width.
    pub right: f32,
    /// Top border width.
    pub top: f32,
    /// Bottom border width.
    pub bottom: f32,
    /// Width used between sibling children.
    pub between_children: f32,
}

/// Floating behavior for overlays/tooltips/popups.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct FloatingElementStyle {
    /// Position offset relative to attachment point.
    pub offset: Vector2,
    /// Additional expansion of floating bounds.
    pub expand: Dimensions,
    /// Z-order offset used during rendering.
    pub z_index: i16,
    /// Anchor point on the parent.
    pub attach_point: FloatingAttachPointType,
    /// Pointer capture behavior.
    pub pointer_capture_mode: PointerCaptureMode,
}

/// Layout styling for children.
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LayoutStyle {
    /// Inner padding around child layout area.
    pub padding: Padding,
    /// Gap inserted between children.
    pub child_gap: f32,
    /// Child alignment along X and Y.
    pub child_alignment: Alignment,
    /// Primary direction used to place children.
    pub layout_direction: LayoutDirection,
    /// Experimental child-wrapping toggle.
    ///
    /// This field is feature-gated and currently not fully wired in all layout
    /// integrations.
    #[cfg(feature = "child_wrapping")]
    pub child_wrapping: bool,
}