Skip to main content

fui/controls/
text_input.rs

1use super::internal::text_input_core::TextInputCore;
2use super::text_editor_surface::impl_text_editor_surface;
3use crate::event::FocusChangedEventArgs;
4use crate::node::{FlexBox, HasFlexBoxRoot, Node, NodeRef, TextNode};
5use std::any::Any;
6use std::rc::Rc;
7
8#[derive(Clone)]
9pub struct TextInput {
10    core: Rc<TextInputCore>,
11}
12
13impl Default for TextInput {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl TextInput {
20    pub fn new() -> Self {
21        let core = Rc::new(TextInputCore::new());
22        core.finish_init(Rc::downgrade(&core));
23        Self { core }
24    }
25
26    pub fn password(&self, flag: bool) -> &Self {
27        self.core.password(flag);
28        self
29    }
30
31    pub fn host_autofill(&self, hint: impl AsRef<str>) -> &Self {
32        self.core.host_autofill(Some(hint.as_ref()));
33        self
34    }
35
36    pub fn clear_host_autofill(&self) -> &Self {
37        self.core.host_autofill(None);
38        self
39    }
40
41    pub(crate) fn editor_node(&self) -> TextNode {
42        self.core.editor_node()
43    }
44}
45
46impl_text_editor_surface!(TextInput);
47
48impl HasFlexBoxRoot for TextInput {
49    fn flex_box_root(&self) -> &FlexBox {
50        self.core.flex_box_root()
51    }
52}
53
54impl crate::ThemeBindable for TextInput {
55    fn theme_binding_node(&self) -> NodeRef {
56        self.core.flex_box_root().retained_node_ref()
57    }
58
59    fn weak_theme_target(&self) -> Box<dyn Fn() -> Option<Self>> {
60        let weak_core = Rc::downgrade(&self.core);
61        Box::new(move || {
62            Some(TextInput {
63                core: weak_core.upgrade()?,
64            })
65        })
66    }
67}
68
69impl Node for TextInput {
70    fn apply_node_id(&self, node_id: String) {
71        self.core.node_id(node_id);
72    }
73
74    fn apply_semantic_label(&self, label: String) {
75        self.core.semantic_label(label);
76    }
77
78    fn apply_focusable(&self, enabled: bool, tab_index: i32) {
79        self.core.focusable(enabled, tab_index);
80    }
81
82    fn apply_focus_now(&self) {
83        self.core.focus_now();
84    }
85
86    fn apply_enabled(&self, enabled: bool) {
87        self.core.enabled(enabled);
88    }
89
90    fn apply_focus_changed_handler(&self, handler: Rc<dyn Fn(FocusChangedEventArgs)>) {
91        self.core.set_focus_changed_callback(handler);
92    }
93
94    fn retained_node_ref(&self) -> NodeRef {
95        let core = self.core.clone();
96        self.core
97            .flex_box_root()
98            .retained_node_ref()
99            .with_build_callback(move || core.build_control())
100    }
101
102    fn retained_owner_attachment(&self) -> Option<Rc<dyn Any>> {
103        Some(self.core.clone())
104    }
105
106    fn build_self(&self) {
107        self.core.build_control();
108    }
109}