Skip to main content

ferroui_base/input/text_input/
text_input_options.rs

1use super::{TextInputContentType, TextInputReturnKeyType};
2use crate::{ferro_property, AttachedProperty, FerroProperty, StyledElement, StyledPropertyOptions};
3use std::rc::Rc;
4
5/// Describes how a text input wants the input method of the platform to
6/// behave: the properties are attached to a text editing control (they are
7/// inherited) and gathered into a `TextInputOptions` value for the input
8/// method.
9#[derive(Clone, Debug, Default, PartialEq)]
10pub struct TextInputOptions {
11    /// The content type (mostly for determining the shape of the virtual keyboard).
12    pub content_type: TextInputContentType,
13
14    /// Determines what the Return key says and how it behaves.
15    pub return_key_type: TextInputReturnKeyType,
16
17    /// Text is multiline.
18    pub multiline: bool,
19
20    /// Text is in lower case.
21    pub lowercase: bool,
22
23    /// Text is in upper case.
24    pub uppercase: bool,
25
26    /// Automatically capitalize letters at the start of the sentence.
27    pub auto_capitalization: bool,
28
29    /// Text contains sensitive data like card numbers and should not be stored.
30    pub is_sensitive: bool,
31
32    /// Determines whether text suggestions are shown; `None` leaves the decision to the platform.
33    pub show_suggestions: Option<bool>,
34
35    /// Hints about the locales (BCP 47 language tags) the user is expected to type in.
36    pub locale_hints: Option<Rc<[String]>>,
37}
38
39crate::ferro_static_type!(TextInputOptions);
40
41crate::ferro_properties! {
42    impl TextInputOptions, also [
43        TextInputOptions::content_type_property,
44        TextInputOptions::return_key_type_property,
45        TextInputOptions::multiline_property,
46        TextInputOptions::lowercase_property,
47        TextInputOptions::uppercase_property,
48        TextInputOptions::auto_capitalization_property,
49        TextInputOptions::is_sensitive_property,
50        TextInputOptions::show_suggestions_property,
51        TextInputOptions::locale_hints_property,
52    ] {}
53}
54
55impl TextInputOptions {
56    /// Creates the options from the values of the attached properties of a
57    /// control.
58    pub fn from_styled_element(ferro_object: &StyledElement) -> TextInputOptions {
59        TextInputOptions {
60            content_type: Self::get_content_type(ferro_object),
61            return_key_type: Self::get_return_key_type(ferro_object),
62            multiline: Self::get_multiline(ferro_object),
63            auto_capitalization: Self::get_auto_capitalization(ferro_object),
64            is_sensitive: Self::get_is_sensitive(ferro_object),
65            lowercase: Self::get_lowercase(ferro_object),
66            uppercase: Self::get_uppercase(ferro_object),
67            show_suggestions: Self::get_show_suggestions(ferro_object),
68            locale_hints: Self::get_locale_hints(ferro_object),
69        }
70    }
71
72    /// The default options.
73    pub fn default_options() -> TextInputOptions {
74        TextInputOptions::default()
75    }
76
77    ferro_property!(for TextInputOptions;
78        /// Defines the `ContentType` attached property.
79        pub fn content_type_property() -> AttachedProperty<TextInputContentType> {
80            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
81                "ContentType",
82                StyledPropertyOptions::new(TextInputContentType::Normal).inherits(true),
83            )
84        }
85    );
86
87    /// Sets the value of the attached `ContentType` property on a control.
88    pub fn set_content_type(ferro_object: &StyledElement, value: TextInputContentType) {
89        ferro_object.set_value(Self::content_type_property(), value)
90    }
91
92    /// Gets the value of the attached `ContentType` property of a control.
93    pub fn get_content_type(ferro_object: &StyledElement) -> TextInputContentType {
94        ferro_object.get_value(Self::content_type_property())
95    }
96
97    ferro_property!(for TextInputOptions;
98        /// Defines the `ReturnKeyType` attached property.
99        pub fn return_key_type_property() -> AttachedProperty<TextInputReturnKeyType> {
100            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
101                "ReturnKeyType",
102                StyledPropertyOptions::new(TextInputReturnKeyType::Default).inherits(true),
103            )
104        }
105    );
106
107    /// Sets the value of the attached `ReturnKeyType` property on a control.
108    pub fn set_return_key_type(ferro_object: &StyledElement, value: TextInputReturnKeyType) {
109        ferro_object.set_value(Self::return_key_type_property(), value)
110    }
111
112    /// Gets the value of the attached `ReturnKeyType` property of a control.
113    pub fn get_return_key_type(ferro_object: &StyledElement) -> TextInputReturnKeyType {
114        ferro_object.get_value(Self::return_key_type_property())
115    }
116
117    ferro_property!(for TextInputOptions;
118        /// Defines the `Multiline` attached property.
119        pub fn multiline_property() -> AttachedProperty<bool> {
120            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
121                "Multiline",
122                StyledPropertyOptions::new(false).inherits(true),
123            )
124        }
125    );
126
127    /// Sets the value of the attached `Multiline` property on a control.
128    pub fn set_multiline(ferro_object: &StyledElement, value: bool) {
129        ferro_object.set_value(Self::multiline_property(), value)
130    }
131
132    /// Gets the value of the attached `Multiline` property of a control.
133    pub fn get_multiline(ferro_object: &StyledElement) -> bool {
134        ferro_object.get_value(Self::multiline_property())
135    }
136
137    ferro_property!(for TextInputOptions;
138        /// Defines the `Lowercase` attached property.
139        pub fn lowercase_property() -> AttachedProperty<bool> {
140            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
141                "Lowercase",
142                StyledPropertyOptions::new(false).inherits(true),
143            )
144        }
145    );
146
147    /// Sets the value of the attached `Lowercase` property on a control.
148    pub fn set_lowercase(ferro_object: &StyledElement, value: bool) {
149        ferro_object.set_value(Self::lowercase_property(), value)
150    }
151
152    /// Gets the value of the attached `Lowercase` property of a control.
153    pub fn get_lowercase(ferro_object: &StyledElement) -> bool {
154        ferro_object.get_value(Self::lowercase_property())
155    }
156
157    ferro_property!(for TextInputOptions;
158        /// Defines the `Uppercase` attached property.
159        pub fn uppercase_property() -> AttachedProperty<bool> {
160            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
161                "Uppercase",
162                StyledPropertyOptions::new(false).inherits(true),
163            )
164        }
165    );
166
167    /// Sets the value of the attached `Uppercase` property on a control.
168    pub fn set_uppercase(ferro_object: &StyledElement, value: bool) {
169        ferro_object.set_value(Self::uppercase_property(), value)
170    }
171
172    /// Gets the value of the attached `Uppercase` property of a control.
173    pub fn get_uppercase(ferro_object: &StyledElement) -> bool {
174        ferro_object.get_value(Self::uppercase_property())
175    }
176
177    ferro_property!(for TextInputOptions;
178        /// Defines the `AutoCapitalization` attached property.
179        pub fn auto_capitalization_property() -> AttachedProperty<bool> {
180            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
181                "AutoCapitalization",
182                StyledPropertyOptions::new(false).inherits(true),
183            )
184        }
185    );
186
187    /// Sets the value of the attached `AutoCapitalization` property on a control.
188    pub fn set_auto_capitalization(ferro_object: &StyledElement, value: bool) {
189        ferro_object.set_value(Self::auto_capitalization_property(), value)
190    }
191
192    /// Gets the value of the attached `AutoCapitalization` property of a control.
193    pub fn get_auto_capitalization(ferro_object: &StyledElement) -> bool {
194        ferro_object.get_value(Self::auto_capitalization_property())
195    }
196
197    ferro_property!(for TextInputOptions;
198        /// Defines the `IsSensitive` attached property.
199        pub fn is_sensitive_property() -> AttachedProperty<bool> {
200            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
201                "IsSensitive",
202                StyledPropertyOptions::new(false).inherits(true),
203            )
204        }
205    );
206
207    /// Sets the value of the attached `IsSensitive` property on a control.
208    pub fn set_is_sensitive(ferro_object: &StyledElement, value: bool) {
209        ferro_object.set_value(Self::is_sensitive_property(), value)
210    }
211
212    /// Gets the value of the attached `IsSensitive` property of a control.
213    pub fn get_is_sensitive(ferro_object: &StyledElement) -> bool {
214        ferro_object.get_value(Self::is_sensitive_property())
215    }
216
217    ferro_property!(for TextInputOptions;
218        /// Defines the `ShowSuggestions` attached property.
219        pub fn show_suggestions_property() -> AttachedProperty<Option<bool>> {
220            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
221                "ShowSuggestions",
222                StyledPropertyOptions::new(None).inherits(true),
223            )
224        }
225    );
226
227    /// Sets the value of the attached `ShowSuggestions` property on a control.
228    pub fn set_show_suggestions(ferro_object: &StyledElement, value: Option<bool>) {
229        ferro_object.set_value(Self::show_suggestions_property(), value)
230    }
231
232    /// Gets the value of the attached `ShowSuggestions` property of a control.
233    pub fn get_show_suggestions(ferro_object: &StyledElement) -> Option<bool> {
234        ferro_object.get_value(Self::show_suggestions_property())
235    }
236
237    ferro_property!(for TextInputOptions;
238        /// Defines the `LocaleHints` attached property.
239        pub fn locale_hints_property() -> AttachedProperty<Option<Rc<[String]>>> {
240            FerroProperty::register_attached_with::<TextInputOptions, StyledElement, _>(
241                "LocaleHints",
242                StyledPropertyOptions::new(None).inherits(true),
243            )
244        }
245    );
246
247    /// Sets the value of the attached `LocaleHints` property on a control.
248    pub fn set_locale_hints(ferro_object: &StyledElement, value: Option<Rc<[String]>>) {
249        ferro_object.set_value(Self::locale_hints_property(), value)
250    }
251
252    /// Gets the value of the attached `LocaleHints` property of a control.
253    pub fn get_locale_hints(ferro_object: &StyledElement) -> Option<Rc<[String]>> {
254        ferro_object.get_value(Self::locale_hints_property())
255    }
256}