Skip to main content

cotis_defaults/
element_configs.rs

1//! Element configuration data passed into Cotis layout.
2//!
3//! This module defines the configuration payload attached to each element before
4//! layout runs, including style, optional image data, and optional custom payloads.
5use crate::colors::Color;
6#[cfg(feature = "complex_color")]
7use crate::colors::ColorLayer;
8use crate::element_configs::image_config::ImageConfig;
9use crate::element_configs::style::types::{
10    Alignment, ClipElementConfig, FloatingAttachPointType, LayoutAlignmentX, LayoutAlignmentY,
11    LayoutDirection, Padding, PointerCaptureMode,
12};
13use crate::element_configs::style::{
14    BorderElementStyle, BorderWidthStyle, CornerRadiiStyle, CotisStyle, FloatingElementStyle,
15    LayoutStyle,
16};
17use cotis::utils::ElementIdConfig;
18use cotis::utils::OwnedOrRef;
19
20/// Image-specific element payload wrappers.
21pub mod image_config;
22/// Convenient type aliases for common element config signatures.
23pub mod premade_configs;
24/// Styling structures and layout-related style primitives.
25pub mod style;
26/// Text and rich-text element configuration types.
27pub mod text_config;
28
29/// Generic element configuration consumed by layout crates.
30///
31/// Type parameters:
32/// - `'render`: lifetime of borrowed config data.
33/// - `Sizing`: sizing model (`Sizing` or `DoubleAxisSizing` in most integrations).
34/// - `ImageElementData`: payload type used when [`image`](Self::image) is present.
35/// - `CustomElementData`: custom renderer-specific payload.
36/// - `ExtraData`: metadata propagated to render command info.
37#[derive(Debug, Clone)]
38#[cfg_attr(
39    feature = "serialization",
40    derive(serde::Serialize, serde::Deserialize)
41)]
42#[cfg_attr(
43    feature = "serialization",
44    serde(bound(deserialize = "
45    Sizing: serde::de::DeserializeOwned,
46    ImageElementData: serde::de::DeserializeOwned,
47    CustomElementData: serde::de::DeserializeOwned,
48    ExtraData: serde::de::DeserializeOwned
49"))
50)]
51pub struct ElementConfig<
52    'render,
53    Sizing: 'render,
54    ImageElementData: 'render,
55    CustomElementData: 'render,
56    ExtraData: 'render,
57> {
58    /// ID and identity behavior configuration for the element.
59    pub id_config: ElementIdConfig<'render>,
60    /// Style applied to this element.
61    pub style: CotisStyle<Sizing>,
62    /// Optional image payload.
63    pub image: Option<ImageConfig<'render, ImageElementData>>,
64    /// Optional custom payload consumed by renderer-specific logic.
65    pub custom: Option<OwnedOrRef<'render, CustomElementData>>,
66    /// Optional extra metadata forwarded to command-level info structs.
67    pub extra_data: Option<OwnedOrRef<'render, ExtraData>>,
68}
69
70impl<
71    'render,
72    Sizing: 'render,
73    ImageElementData: 'render,
74    CustomElementData: 'render,
75    ExtraData: 'render,
76> Default for ElementConfig<'render, Sizing, ImageElementData, CustomElementData, ExtraData>
77where
78    Sizing: Default,
79{
80    fn default() -> Self {
81        Self {
82            id_config: ElementIdConfig::new_empty(),
83            style: Default::default(),
84            image: None,
85            custom: None,
86            extra_data: None,
87        }
88    }
89}
90
91impl<Sizing: Default> Default for CotisStyle<Sizing> {
92    /// Builds a style with transparent background, no borders, no floating, and
93    /// default layout/sizing values.
94    fn default() -> Self {
95        Self {
96            sizing: Sizing::default(),
97            layout: Default::default(),
98            #[cfg(not(feature = "complex_color"))]
99            background_color: Color {
100                r: 0.0,
101                g: 0.0,
102                b: 0.0,
103                a: 0.0,
104            },
105            #[cfg(feature = "complex_color")]
106            background_color: ColorLayer::Solid(Color {
107                r: 0.0,
108                g: 0.0,
109                b: 0.0,
110                a: 0.0,
111            }),
112            corner_radius: CornerRadiiStyle {
113                top_left: 0.0,
114                top_right: 0.0,
115                bottom_left: 0.0,
116                bottom_right: 0.0,
117            },
118            floating: Default::default(),
119            clip: ClipElementConfig {
120                horizontal: false,
121                vertical: false,
122                child_offset: Default::default(),
123            },
124            border: BorderElementStyle {
125                color: Color {
126                    r: 0.0,
127                    g: 0.0,
128                    b: 0.0,
129                    a: 255.0,
130                },
131                width: BorderWidthStyle {
132                    left: 0.0,
133                    right: 0.0,
134                    top: 0.0,
135                    bottom: 0.0,
136                    between_children: 0.0,
137                },
138            },
139        }
140    }
141}
142
143impl Default for LayoutStyle {
144    /// Creates a left-to-right layout with zero padding and gap.
145    fn default() -> Self {
146        Self {
147            padding: Padding {
148                top: 0.0,
149                bottom: 0.0,
150                right: 0.0,
151                left: 0.0,
152            },
153            child_gap: 0.0,
154            child_alignment: Alignment {
155                x: LayoutAlignmentX::Left,
156                y: LayoutAlignmentY::Top,
157            },
158            layout_direction: LayoutDirection::LeftToRight,
159            #[cfg(feature = "child_wrapping")]
160            child_wrapping: false,
161        }
162    }
163}
164
165impl Default for FloatingElementStyle {
166    /// Creates a non-offset floating style attached to the parent top-left.
167    fn default() -> Self {
168        Self {
169            offset: Default::default(),
170            expand: Default::default(),
171            z_index: 0,
172            attach_point: FloatingAttachPointType::LeftTop,
173            pointer_capture_mode: PointerCaptureMode::Capture,
174        }
175    }
176}