clay_layout/
elements.rs

1use crate::{bindings::*, color::Color, Declaration, Dimensions, Vector2};
2
3/// Builder for configuring border properties of a `Declaration`.
4pub struct BorderBuilder<
5    'declaration,
6    'render,
7    ImageElementData: 'render,
8    CustomElementData: 'render,
9> {
10    parent: &'declaration mut Declaration<'render, ImageElementData, CustomElementData>,
11}
12
13impl<'declaration, 'render, ImageElementData: 'render, CustomElementData: 'render>
14    BorderBuilder<'declaration, 'render, ImageElementData, CustomElementData>
15{
16    /// Creates a new `BorderBuilder` with the given parent `Declaration`.
17    #[inline]
18    pub fn new(
19        parent: &'declaration mut Declaration<'render, ImageElementData, CustomElementData>,
20    ) -> Self {
21        BorderBuilder { parent }
22    }
23
24    /// Set the same border width for all sides.
25    #[inline]
26    pub fn all_directions(&mut self, width: u16) -> &mut Self {
27        self.parent.inner.border.width.left = width;
28        self.parent.inner.border.width.right = width;
29        self.parent.inner.border.width.top = width;
30        self.parent.inner.border.width.bottom = width;
31        self
32    }
33
34    /// Sets the left border width.
35    #[inline]
36    pub fn left(&mut self, width: u16) -> &mut Self {
37        self.parent.inner.border.width.left = width;
38        self
39    }
40
41    /// Sets the right border width.
42    #[inline]
43    pub fn right(&mut self, width: u16) -> &mut Self {
44        self.parent.inner.border.width.right = width;
45        self
46    }
47
48    /// Sets the top border width.
49    #[inline]
50    pub fn top(&mut self, width: u16) -> &mut Self {
51        self.parent.inner.border.width.top = width;
52        self
53    }
54
55    /// Sets the bottom border width.
56    #[inline]
57    pub fn bottom(&mut self, width: u16) -> &mut Self {
58        self.parent.inner.border.width.bottom = width;
59        self
60    }
61
62    /// Sets the spacing between child elements.
63    #[inline]
64    pub fn between_children(&mut self, width: u16) -> &mut Self {
65        self.parent.inner.border.width.betweenChildren = width;
66        self
67    }
68
69    /// Sets the border color.
70    #[inline]
71    pub fn color(&mut self, color: Color) -> &mut Self {
72        self.parent.inner.border.color = color.into();
73        self
74    }
75
76    /// Returns the modified `Declaration`.
77    #[inline]
78    pub fn end(&mut self) -> &mut Declaration<'render, ImageElementData, CustomElementData> {
79        self.parent
80    }
81}
82
83/// Builder for configuring image properties in a `Declaration`.
84pub struct ImageBuilder<
85    'declaration,
86    'render,
87    ImageElementData: 'render,
88    CustomElementData: 'render,
89> {
90    parent: &'declaration mut Declaration<'render, ImageElementData, CustomElementData>,
91}
92
93impl<'declaration, 'render, ImageElementData: 'render, CustomElementData: 'render>
94    ImageBuilder<'declaration, 'render, ImageElementData, CustomElementData>
95{
96    /// Creates a new `ImageBuilder` with the given parent `Declaration`.
97    #[inline]
98    pub fn new(
99        parent: &'declaration mut Declaration<'render, ImageElementData, CustomElementData>,
100    ) -> Self {
101        ImageBuilder { parent }
102    }
103
104    /// Sets the image data.
105    /// The data must be created using [`Clay::data`].
106    #[inline]
107    pub fn data(&mut self, data: &'render ImageElementData) -> &mut Self {
108        self.parent.inner.image.imageData = data as *const ImageElementData as _;
109        self
110    }
111    /// Returns the modified `Declaration`.
112    #[inline]
113    pub fn end(&mut self) -> &mut Declaration<'render, ImageElementData, CustomElementData> {
114        self.parent
115    }
116}
117
118/// Represents different attachment points for floating elements.
119#[derive(Debug, Clone, Copy)]
120#[repr(u8)]
121pub enum FloatingAttachPointType {
122    /// Attaches to the top-left of the parent.
123    LeftTop = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_LEFT_TOP,
124    /// Attaches to the center-left of the parent.
125    LeftCenter = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_LEFT_CENTER,
126    /// Attaches to the bottom-left of the parent.
127    LeftBottom = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_LEFT_BOTTOM,
128    /// Attaches to the top-center of the parent.
129    CenterTop = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_CENTER_TOP,
130    /// Attaches to the center of the parent.
131    CenterCenter = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_CENTER_CENTER,
132    /// Attaches to the bottom-center of the parent.
133    CenterBottom = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_CENTER_BOTTOM,
134    /// Attaches to the top-right of the parent.
135    RightTop = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_RIGHT_TOP,
136    /// Attaches to the center-right of the parent.
137    RightCenter = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_RIGHT_CENTER,
138    /// Attaches to the bottom-right of the parent.
139    RightBottom = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_RIGHT_BOTTOM,
140}
141
142/// Specifies how pointer capture should behave for floating elements.
143#[derive(Debug, Clone, Copy)]
144#[repr(u8)]
145pub enum PointerCaptureMode {
146    /// Captures all pointer input.
147    Capture = Clay_PointerCaptureMode_CLAY_POINTER_CAPTURE_MODE_CAPTURE,
148    /// Allows pointer input to pass through.
149    Passthrough = Clay_PointerCaptureMode_CLAY_POINTER_CAPTURE_MODE_PASSTHROUGH,
150}
151
152/// Defines how a floating element is attached to other elements.
153#[derive(Debug, Clone)]
154#[repr(u8)]
155pub enum FloatingAttachToElement {
156    /// The floating element is not attached to any other element.
157    None = Clay_FloatingAttachToElement_CLAY_ATTACH_TO_NONE,
158    /// The floating element is attached to its parent element.
159    Parent = Clay_FloatingAttachToElement_CLAY_ATTACH_TO_PARENT,
160    /// The floating element is attached to a specific element identified by an ID.
161    ElementWithId = Clay_FloatingAttachToElement_CLAY_ATTACH_TO_ELEMENT_WITH_ID,
162    /// The floating element is attached to the root of the layout.
163    Root = Clay_FloatingAttachToElement_CLAY_ATTACH_TO_ROOT,
164}
165
166/// Builder for configuring floating element properties in a `Declaration`.
167pub struct FloatingBuilder<
168    'declaration,
169    'render,
170    ImageElementData: 'render,
171    CustomElementData: 'render,
172> {
173    parent: &'declaration mut Declaration<'render, ImageElementData, CustomElementData>,
174}
175
176impl<'declaration, 'render, ImageElementData: 'render, CustomElementData: 'render>
177    FloatingBuilder<'declaration, 'render, ImageElementData, CustomElementData>
178{
179    /// Creates a new `FloatingBuilder` with the given parent `Declaration`.
180    #[inline]
181    pub fn new(
182        parent: &'declaration mut Declaration<'render, ImageElementData, CustomElementData>,
183    ) -> Self {
184        FloatingBuilder { parent }
185    }
186
187    /// Sets the floating element's offset.
188    #[inline]
189    pub fn offset(&mut self, offset: Vector2) -> &mut Self {
190        self.parent.inner.floating.offset = offset.into();
191        self
192    }
193
194    /// Sets the floating element's dimensions.
195    #[inline]
196    pub fn dimensions(&mut self, dimensions: Dimensions) -> &mut Self {
197        self.parent.inner.floating.expand = dimensions.into();
198        self
199    }
200
201    /// Sets the floating element's Z-index.
202    #[inline]
203    pub fn z_index(&mut self, z_index: i16) -> &mut Self {
204        self.parent.inner.floating.zIndex = z_index;
205        self
206    }
207
208    /// Sets the parent element ID.
209    #[inline]
210    pub fn parent_id(&mut self, id: u32) -> &mut Self {
211        self.parent.inner.floating.parentId = id;
212        self
213    }
214
215    /// Sets the attachment points of the floating element and its parent.
216    #[inline]
217    pub fn attach_points(
218        &mut self,
219        element: FloatingAttachPointType,
220        parent: FloatingAttachPointType,
221    ) -> &mut Self {
222        self.parent.inner.floating.attachPoints.element = element as _;
223        self.parent.inner.floating.attachPoints.parent = parent as _;
224        self
225    }
226
227    /// Sets how the floating element is attached to other elements.
228    ///
229    /// - [`FloatingAttachToElement::None`] - The element is not attached to anything.
230    /// - [`FloatingAttachToElement::Parent`] - The element is attached to its parent.
231    /// - [`FloatingAttachToElement::ElementWithId`] - The element is attached to a specific element by ID.
232    /// - [`FloatingAttachToElement::Root`] - The element is attached to the root of the layout.
233    #[inline]
234    pub fn attach_to(&mut self, attach: FloatingAttachToElement) -> &mut Self {
235        self.parent.inner.floating.attachTo = attach as _;
236        self
237    }
238
239    /// Sets the pointer capture mode.
240    #[inline]
241    pub fn pointer_capture_mode(&mut self, mode: PointerCaptureMode) -> &mut Self {
242        self.parent.inner.floating.pointerCaptureMode = mode as _;
243        self
244    }
245
246    /// Returns the modified `Declaration`.
247    #[inline]
248    pub fn end(&mut self) -> &mut Declaration<'render, ImageElementData, CustomElementData> {
249        self.parent
250    }
251}
252
253/// Builder for configuring corner radius properties in a `Declaration`.
254pub struct CornerRadiusBuilder<
255    'declaration,
256    'render,
257    ImageElementData: 'render,
258    CustomElementData: 'render,
259> {
260    parent: &'declaration mut Declaration<'render, ImageElementData, CustomElementData>,
261}
262
263impl<'declaration, 'render, ImageElementData: 'render, CustomElementData: 'render>
264    CornerRadiusBuilder<'declaration, 'render, ImageElementData, CustomElementData>
265{
266    /// Creates a new `CornerRadiusBuilder` with the given parent `Declaration`.
267    #[inline]
268    pub fn new(
269        parent: &'declaration mut Declaration<'render, ImageElementData, CustomElementData>,
270    ) -> Self {
271        CornerRadiusBuilder { parent }
272    }
273
274    /// Sets the top-left corner radius.
275    #[inline]
276    pub fn top_left(&mut self, radius: f32) -> &mut Self {
277        self.parent.inner.cornerRadius.topLeft = radius;
278        self
279    }
280
281    /// Sets the top-right corner radius.
282    #[inline]
283    pub fn top_right(&mut self, radius: f32) -> &mut Self {
284        self.parent.inner.cornerRadius.topRight = radius;
285        self
286    }
287
288    /// Sets the bottom-left corner radius.
289    #[inline]
290    pub fn bottom_left(&mut self, radius: f32) -> &mut Self {
291        self.parent.inner.cornerRadius.bottomLeft = radius;
292        self
293    }
294
295    /// Sets the bottom-right corner radius.
296    #[inline]
297    pub fn bottom_right(&mut self, radius: f32) -> &mut Self {
298        self.parent.inner.cornerRadius.bottomRight = radius;
299        self
300    }
301
302    /// Sets all four corner radii to the same value.
303    #[inline]
304    pub fn all(&mut self, radius: f32) -> &mut Self {
305        self.parent.inner.cornerRadius.topLeft = radius;
306        self.parent.inner.cornerRadius.topRight = radius;
307        self.parent.inner.cornerRadius.bottomLeft = radius;
308        self.parent.inner.cornerRadius.bottomRight = radius;
309        self
310    }
311
312    /// Returns the modified `Declaration`.
313    #[inline]
314    pub fn end(&mut self) -> &mut Declaration<'render, ImageElementData, CustomElementData> {
315        self.parent
316    }
317}