cranpose_ui/widgets/
text.rs1use std::rc::Rc;
9
10use cranpose_core::{MutableState, NodeId, State};
11use cranpose_foundation::modifier_element;
12
13use crate::{
14 composable,
15 layout::policies::EmptyMeasurePolicy,
16 modifier::Modifier,
17 text::{TextLayoutOptions, TextOptions, TextOverflow, TextStyle},
18 text_modifier_node::TextModifierElement,
19 widgets::layout::compose_layout,
20};
21
22#[derive(Clone)]
23pub struct DynamicTextSource(Rc<dyn Fn() -> Rc<crate::text::AnnotatedString>>);
24
25impl DynamicTextSource {
26 pub fn new<F>(resolver: F) -> Self
27 where
28 F: Fn() -> Rc<crate::text::AnnotatedString> + 'static,
29 {
30 Self(Rc::new(resolver))
31 }
32
33 fn resolve(&self) -> Rc<crate::text::AnnotatedString> {
34 (self.0)()
35 }
36}
37
38impl PartialEq for DynamicTextSource {
39 fn eq(&self, other: &Self) -> bool {
40 Rc::ptr_eq(&self.0, &other.0)
41 }
42}
43
44#[derive(Clone, PartialEq)]
45pub enum TextSource {
46 Static(Rc<crate::text::AnnotatedString>),
47 Dynamic(DynamicTextSource),
48}
49
50impl TextSource {
51 fn resolve(&self) -> Rc<crate::text::AnnotatedString> {
52 match self {
53 TextSource::Static(text) => text.clone(),
54 TextSource::Dynamic(dynamic) => dynamic.resolve(),
55 }
56 }
57}
58
59#[doc(hidden)]
60pub trait IntoTextSource {
61 fn into_text_source(self) -> TextSource;
62}
63
64impl IntoTextSource for String {
65 fn into_text_source(self) -> TextSource {
66 TextSource::Static(Rc::new(crate::text::AnnotatedString::from(self)))
67 }
68}
69
70impl IntoTextSource for &str {
71 fn into_text_source(self) -> TextSource {
72 TextSource::Static(Rc::new(crate::text::AnnotatedString::from(self)))
73 }
74}
75
76impl IntoTextSource for crate::text::AnnotatedString {
77 fn into_text_source(self) -> TextSource {
78 TextSource::Static(Rc::new(self))
79 }
80}
81
82impl IntoTextSource for Rc<crate::text::AnnotatedString> {
83 fn into_text_source(self) -> TextSource {
84 TextSource::Static(self)
85 }
86}
87
88impl<T> IntoTextSource for State<T>
89where
90 T: ToString + Clone + 'static,
91{
92 fn into_text_source(self) -> TextSource {
93 let state = self;
94 TextSource::Dynamic(DynamicTextSource::new(move || {
95 Rc::new(crate::text::AnnotatedString::from(
96 state.value().to_string(),
97 ))
98 }))
99 }
100}
101
102impl<T> IntoTextSource for MutableState<T>
103where
104 T: ToString + Clone + 'static,
105{
106 fn into_text_source(self) -> TextSource {
107 let state = self;
108 TextSource::Dynamic(DynamicTextSource::new(move || {
109 Rc::new(crate::text::AnnotatedString::from(
110 state.value().to_string(),
111 ))
112 }))
113 }
114}
115
116impl<F> IntoTextSource for F
117where
118 F: Fn() -> String + 'static,
119{
120 fn into_text_source(self) -> TextSource {
121 TextSource::Dynamic(DynamicTextSource::new(move || {
122 Rc::new(crate::text::AnnotatedString::from(self()))
123 }))
124 }
125}
126
127impl IntoTextSource for DynamicTextSource {
128 fn into_text_source(self) -> TextSource {
129 TextSource::Dynamic(self)
130 }
131}
132
133fn compose_basic_text_group(
134 text: TextSource,
135 modifier: Modifier,
136 style: TextStyle,
137 options: TextLayoutOptions,
138) -> NodeId {
139 let current = text.resolve();
140
141 let options = options.normalized();
142
143 let modifier = match crate::widgets::local_selection_registrar().current() {
144 Some(registrar) => modifier.then(crate::widgets::selection_container::selectable_text(
145 registrar,
146 Rc::clone(¤t),
147 style.clone(),
148 options,
149 )),
150 None => modifier,
151 };
152 let text_element = modifier_element(TextModifierElement::new(
153 current,
154 style,
155 options,
156 crate::density::density(),
157 ));
158 let final_modifier = Modifier::from_parts(vec![text_element]);
159 let combined_modifier = modifier.then(final_modifier);
160
161 compose_layout(combined_modifier, EmptyMeasurePolicy, || {})
162}
163
164#[composable]
165pub fn BasicTextWithOptions<S>(
166 text: S,
167 modifier: Modifier,
168 style: TextStyle,
169 options: TextLayoutOptions,
170) -> NodeId
171where
172 S: IntoTextSource + Clone + PartialEq + 'static,
173{
174 compose_basic_text_group(text.into_text_source(), modifier, style, options)
175}
176
177#[composable]
178pub fn BasicText<S>(
179 text: S,
180 modifier: Modifier,
181 style: TextStyle,
182 overflow: TextOverflow,
183 soft_wrap: bool,
184 max_lines: usize,
185 min_lines: usize,
186) -> NodeId
187where
188 S: IntoTextSource + Clone + PartialEq + 'static,
189{
190 compose_basic_text_group(
191 text.into_text_source(),
192 modifier,
193 style,
194 TextLayoutOptions {
195 overflow,
196 soft_wrap,
197 max_lines,
198 min_lines,
199 },
200 )
201}
202
203#[composable]
204pub fn TextWithOptions<S>(
205 value: S,
206 modifier: Modifier,
207 style: TextStyle,
208 options: TextOptions,
209) -> NodeId
210where
211 S: IntoTextSource + Clone + PartialEq + 'static,
212{
213 compose_basic_text_group(
214 value.into_text_source(),
215 modifier,
216 style,
217 TextLayoutOptions::from(options),
218 )
219}
220
221#[composable]
239pub fn Text<S>(value: S, modifier: Modifier, style: TextStyle) -> NodeId
240where
241 S: IntoTextSource + Clone + PartialEq + 'static,
242{
243 compose_basic_text_group(
244 value.into_text_source(),
245 modifier,
246 style,
247 TextLayoutOptions::from(TextOptions::default()),
248 )
249}
250
251#[cfg(test)]
252#[path = "tests/text_tests.rs"]
253mod tests;