clay_layout/elements/
text.rs

1use crate::{bindings::*, color::Color};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4#[repr(u32)]
5pub enum TextElementConfigWrapMode {
6    /// Wraps on whitespaces not breaking words
7    Words = Clay_TextElementConfigWrapMode_CLAY_TEXT_WRAP_WORDS,
8    /// Only wraps on new line characters
9    Newline = Clay_TextElementConfigWrapMode_CLAY_TEXT_WRAP_NEWLINES,
10    /// Never wraps, can overflow of parent layout
11    None = Clay_TextElementConfigWrapMode_CLAY_TEXT_WRAP_NONE,
12}
13
14pub struct TextElementConfig {
15    inner: *mut Clay_TextElementConfig,
16}
17
18impl From<TextElementConfig> for *mut Clay_TextElementConfig {
19    fn from(value: TextElementConfig) -> Self {
20        value.inner
21    }
22}
23impl From<*mut Clay_TextElementConfig> for TextElementConfig {
24    fn from(value: *mut Clay_TextElementConfig) -> Self {
25        Self { inner: value }
26    }
27}
28
29#[derive(Debug, Clone, Copy)]
30pub struct Text {
31    pub color: Color,
32    /// Clay doesn't manage font, it's up to you to assign an id to each font you are using, and
33    /// passing them to the [font_id](Text::font_id) field
34    pub font_id: u16,
35    pub font_size: u16,
36    pub letter_spacing: u16,
37    pub line_height: u16,
38    pub wrap_mode: TextElementConfigWrapMode,
39}
40
41impl Text {
42    pub fn new() -> Self {
43        Self::default()
44    }
45
46    pub fn color(&mut self, color: Color) -> &mut Self {
47        self.color = color;
48        self
49    }
50
51    pub fn font_id(&mut self, id: u16) -> &mut Self {
52        self.font_id = id;
53        self
54    }
55
56    pub fn font_size(&mut self, size: u16) -> &mut Self {
57        self.font_size = size;
58        self
59    }
60
61    pub fn letter_spacing(&mut self, spacing: u16) -> &mut Self {
62        self.letter_spacing = spacing;
63        self
64    }
65
66    pub fn line_height(&mut self, height: u16) -> &mut Self {
67        self.line_height = height;
68        self
69    }
70
71    pub fn wrap_mode(&mut self, mode: TextElementConfigWrapMode) -> &mut Self {
72        self.wrap_mode = mode;
73        self
74    }
75
76    pub fn end(&self) -> TextElementConfig {
77        let memory = unsafe { Clay__StoreTextElementConfig((*self).into()) };
78
79        TextElementConfig { inner: memory }
80    }
81}
82
83impl Default for Text {
84    fn default() -> Self {
85        Self {
86            color: Color::rgba(0., 0., 0., 0.),
87            font_id: 0,
88            font_size: 0,
89            letter_spacing: 0,
90            line_height: 0,
91            wrap_mode: TextElementConfigWrapMode::Words,
92        }
93    }
94}
95
96impl From<Clay_TextElementConfig> for Text {
97    fn from(value: Clay_TextElementConfig) -> Self {
98        Self {
99            color: value.textColor.into(),
100            font_id: value.fontId,
101            font_size: value.fontSize,
102            letter_spacing: value.letterSpacing,
103            line_height: value.lineHeight,
104            wrap_mode: unsafe {
105                core::mem::transmute::<u32, TextElementConfigWrapMode>(value.wrapMode)
106            },
107        }
108    }
109}
110impl From<Text> for Clay_TextElementConfig {
111    fn from(value: Text) -> Self {
112        Self {
113            textColor: value.color.into(),
114            fontId: value.font_id,
115            fontSize: value.font_size,
116            letterSpacing: value.letter_spacing,
117            lineHeight: value.line_height,
118            wrapMode: value.wrap_mode as _,
119        }
120    }
121}