clay_layout/elements/containers/
floating.rs

1use crate::{
2    bindings::*,
3    elements::ElementConfigType,
4    math::{Dimensions, Vector2},
5    mem, TypedConfig,
6};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[repr(u8)]
10pub enum FloatingAttachPointType {
11    LeftTop = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_LEFT_TOP,
12    LeftCenter = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_LEFT_CENTER,
13    LeftBottom = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_LEFT_BOTTOM,
14    CenterTop = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_CENTER_TOP,
15    CenterCenter = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_CENTER_CENTER,
16    CenterBottom = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_CENTER_BOTTOM,
17    RightTop = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_RIGHT_TOP,
18    RightCenter = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_RIGHT_CENTER,
19    RightBottom = Clay_FloatingAttachPointType_CLAY_ATTACH_POINT_RIGHT_BOTTOM,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23#[repr(u32)]
24pub enum PointerCaptureMode {
25    Capture = Clay_PointerCaptureMode_CLAY_POINTER_CAPTURE_MODE_CAPTURE,
26    Passthrough = Clay_PointerCaptureMode_CLAY_POINTER_CAPTURE_MODE_PASSTHROUGH,
27}
28
29#[derive(Debug, Copy, Clone)]
30pub struct FloatingContainer {
31    /// Offset from the default attachment point
32    pub offset: Vector2,
33    /// Minimum dimensions of the container
34    pub expand: Dimensions,
35    pub z_index: u16,
36    /// Id of the parent layout
37    pub parent: u32,
38    /// The attachment point on the container
39    pub element_attachment: FloatingAttachPointType,
40    /// The point to which you attach [element_attachment](FloatingContainer::element_attachment)
41    /// on the parent
42    pub parent_attachment: FloatingAttachPointType,
43    pub pointer_capture_mode: PointerCaptureMode,
44}
45
46impl FloatingContainer {
47    pub fn new() -> Self {
48        Self::default()
49    }
50
51    pub fn offset(&mut self, offset: Vector2) -> &mut Self {
52        self.offset = offset;
53        self
54    }
55
56    pub fn dimensions(&mut self, dimensions: Dimensions) -> &mut Self {
57        self.expand = dimensions;
58        self
59    }
60
61    pub fn z_index(&mut self, z_index: u16) -> &mut Self {
62        self.z_index = z_index;
63        self
64    }
65
66    pub fn parent_id(&mut self, id: u32) -> &mut Self {
67        self.parent = id;
68        self
69    }
70
71    pub fn attachment(
72        &mut self,
73        element: FloatingAttachPointType,
74        parent: FloatingAttachPointType,
75    ) -> &mut Self {
76        self.element_attachment = element;
77        self.parent_attachment = parent;
78        self
79    }
80
81    pub fn pointer_capture_mode(&mut self, mode: PointerCaptureMode) -> &mut Self {
82        self.pointer_capture_mode = mode;
83        self
84    }
85
86    pub fn end(&self) -> TypedConfig {
87        let memory = unsafe { Clay__StoreFloatingElementConfig((*self).into()) };
88
89        TypedConfig {
90            config_memory: memory as _,
91            id: mem::zeroed_init(),
92            config_type: ElementConfigType::FloatingContainer as _,
93        }
94    }
95}
96
97impl Default for FloatingContainer {
98    fn default() -> Self {
99        Self {
100            offset: Vector2::default(),
101            expand: Dimensions::default(),
102            z_index: 0,
103            parent: 0,
104            parent_attachment: FloatingAttachPointType::LeftTop,
105            element_attachment: FloatingAttachPointType::LeftTop,
106            pointer_capture_mode: PointerCaptureMode::Capture,
107        }
108    }
109}
110
111impl From<Clay_FloatingElementConfig> for FloatingContainer {
112    fn from(value: Clay_FloatingElementConfig) -> Self {
113        Self {
114            offset: value.offset.into(),
115            expand: value.expand.into(),
116            z_index: value.zIndex,
117            parent: value.parentId,
118            element_attachment: unsafe {
119                core::mem::transmute::<u8, FloatingAttachPointType>(value.attachment.element)
120            },
121            parent_attachment: unsafe {
122                core::mem::transmute::<u8, FloatingAttachPointType>(value.attachment.parent)
123            },
124            pointer_capture_mode: unsafe {
125                core::mem::transmute::<u32, PointerCaptureMode>(value.pointerCaptureMode)
126            },
127        }
128    }
129}
130impl From<FloatingContainer> for Clay_FloatingElementConfig {
131    fn from(value: FloatingContainer) -> Self {
132        Self {
133            offset: value.offset.into(),
134            expand: value.expand.into(),
135            zIndex: value.z_index,
136            parentId: value.parent,
137            attachment: Clay_FloatingAttachPoints {
138                element: value.element_attachment as _,
139                parent: value.parent_attachment as _,
140            },
141            pointerCaptureMode: value.pointer_capture_mode as _,
142        }
143    }
144}