floui/widgets/
text.rs

1use crate::enums::Color;
2use crate::prelude::*;
3use floui_sys;
4use std::sync::Arc;
5
6/// A label
7#[derive(Clone)]
8pub struct Text {
9    inner: Arc<*mut floui_sys::CText>,
10}
11
12unsafe impl Sync for Text {}
13unsafe impl Send for Text {}
14
15unsafe impl WidgetExt for Text {
16    fn inner(&self) -> *mut floui_sys::CWidget {
17        *self.inner as _
18    }
19    fn from_widget_ptr(ptr: *mut floui_sys::CWidget) -> Self {
20        Self {
21            inner: Arc::new(ptr as _),
22        }
23    }
24}
25
26impl Text {
27    /// Creates a new label
28    pub fn new(label: &str) -> Self {
29        let label = std::ffi::CString::new(label).unwrap();
30        let inner = unsafe { Arc::new(floui_sys::CText_new(label.as_ptr())) };
31        Self { inner }
32    }
33
34    /// Sets the text
35    pub fn text(self, label: &str) -> Text {
36        let label = std::ffi::CString::new(label).unwrap();
37        unsafe { floui_sys::CText_text(*self.inner, label.as_ptr()) };
38        self
39    }
40
41    /// Centers the text
42    pub fn center(self) -> Text {
43        unsafe { floui_sys::CText_center(*self.inner) };
44        self
45    }
46
47    /// Adjusts to the left
48    pub fn left(self) -> Text {
49        unsafe { floui_sys::CText_left(*self.inner) };
50        self
51    }
52
53    /// Adjusts to the right
54    pub fn right(self) -> Text {
55        unsafe { floui_sys::CText_right(*self.inner) };
56        self
57    }
58
59    /// Sets the text to be bold
60    pub fn bold(self) -> Text {
61        unsafe { floui_sys::CText_bold(*self.inner) };
62        self
63    }
64
65    /// Sets the text to be italic
66    pub fn italic(self) -> Text {
67        unsafe { floui_sys::CText_italic(*self.inner) };
68        self
69    }
70
71    /// Sets the text to be normal, the default
72    pub fn normal(self) -> Text {
73        unsafe { floui_sys::CText_normal(*self.inner) };
74        self
75    }
76
77    /// Sets the font size
78    pub fn fontsize(self, size: i32) -> Text {
79        unsafe { floui_sys::CText_fontsize(*self.inner, size) };
80        self
81    }
82
83    /// Sets the foreground color
84    pub fn foreground(self, col: Color) -> Text {
85        unsafe { floui_sys::CText_foreground(*self.inner, col.0) }
86        self
87    }
88}
89
90/// A text field
91#[derive(Clone)]
92pub struct TextField {
93    inner: Arc<*mut floui_sys::CTextField>,
94}
95
96unsafe impl Sync for TextField {}
97unsafe impl Send for TextField {}
98
99unsafe impl WidgetExt for TextField {
100    fn inner(&self) -> *mut floui_sys::CWidget {
101        *self.inner as _
102    }
103    fn from_widget_ptr(ptr: *mut floui_sys::CWidget) -> Self {
104        Self {
105            inner: Arc::new(ptr as _),
106        }
107    }
108}
109
110impl TextField {
111    /// Creates a new TextField
112    pub fn new() -> Self {
113        let inner = unsafe { Arc::new(floui_sys::CTextField_new()) };
114        Self { inner }
115    }
116
117    /// Sets the text
118    pub fn text(self, label: &str) -> TextField {
119        let label = std::ffi::CString::new(label).unwrap();
120        unsafe { floui_sys::CTextField_text(*self.inner, label.as_ptr()) };
121        self
122    }
123
124    /// Centers the text
125    pub fn center(self) -> TextField {
126        unsafe { floui_sys::CTextField_center(*self.inner) };
127        self
128    }
129
130    /// Adjusts to the left
131    pub fn left(self) -> TextField {
132        unsafe { floui_sys::CTextField_left(*self.inner) };
133        self
134    }
135
136    /// Adjusts to the right
137    pub fn right(self) -> TextField {
138        unsafe { floui_sys::CTextField_right(*self.inner) };
139        self
140    }
141
142    /// Sets the font size
143    pub fn fontsize(self, size: i32) -> TextField {
144        unsafe { floui_sys::CTextField_fontsize(*self.inner, size) };
145        self
146    }
147
148    /// Sets the foreground color
149    pub fn foreground(self, col: Color) -> TextField {
150        unsafe { floui_sys::CTextField_foreground(*self.inner, col.0) }
151        self
152    }
153}