Skip to main content

fui/controls/
text_area.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, ScrollBarVisibility};
5use std::any::Any;
6use std::rc::Rc;
7
8#[derive(Clone)]
9pub struct TextArea {
10    core: Rc<TextInputCore>,
11}
12
13impl Default for TextArea {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl TextArea {
20    pub fn new() -> Self {
21        let core = Rc::new(TextInputCore::multiline());
22        core.finish_init(Rc::downgrade(&core));
23        Self { core }
24    }
25
26    pub fn wrapping(&self, flag: bool) -> &Self {
27        self.core.wrapping(flag);
28        self
29    }
30
31    pub fn vertical_scrollbar_visibility(&self, mode: ScrollBarVisibility) -> &Self {
32        self.core.vertical_scrollbar_visibility(mode);
33        self
34    }
35
36    pub fn horizontal_scrollbar_visibility(&self, mode: ScrollBarVisibility) -> &Self {
37        self.core.horizontal_scrollbar_visibility(mode);
38        self
39    }
40
41    pub fn scroll_offset_x(&self) -> f32 {
42        self.core.scroll_offset_x()
43    }
44
45    pub fn scroll_offset_y(&self) -> f32 {
46        self.core.scroll_offset_y()
47    }
48
49    pub fn scroll_to(&self, x: f32, y: f32) -> &Self {
50        self.core.scroll_to(x, y);
51        self
52    }
53}
54
55impl_text_editor_surface!(TextArea);
56
57impl HasFlexBoxRoot for TextArea {
58    fn flex_box_root(&self) -> &FlexBox {
59        self.core.flex_box_root()
60    }
61}
62
63impl crate::ThemeBindable for TextArea {
64    fn theme_binding_node(&self) -> NodeRef {
65        self.core.flex_box_root().retained_node_ref()
66    }
67
68    fn weak_theme_target(&self) -> Box<dyn Fn() -> Option<Self>> {
69        let weak_core = Rc::downgrade(&self.core);
70        Box::new(move || {
71            Some(TextArea {
72                core: weak_core.upgrade()?,
73            })
74        })
75    }
76}
77
78impl Node for TextArea {
79    fn apply_node_id(&self, node_id: String) {
80        self.core.node_id(node_id);
81    }
82
83    fn apply_semantic_label(&self, label: String) {
84        self.core.semantic_label(label);
85    }
86
87    fn apply_focusable(&self, enabled: bool, tab_index: i32) {
88        self.core.focusable(enabled, tab_index);
89    }
90
91    fn apply_focus_now(&self) {
92        self.core.focus_now();
93    }
94
95    fn apply_enabled(&self, enabled: bool) {
96        self.core.enabled(enabled);
97    }
98
99    fn apply_focus_changed_handler(&self, handler: Rc<dyn Fn(FocusChangedEventArgs)>) {
100        self.core.set_focus_changed_callback(handler);
101    }
102
103    fn retained_node_ref(&self) -> NodeRef {
104        let core = self.core.clone();
105        self.core
106            .flex_box_root()
107            .retained_node_ref()
108            .with_build_callback(move || core.build_control())
109    }
110
111    fn retained_owner_attachment(&self) -> Option<Rc<dyn Any>> {
112        Some(self.core.clone())
113    }
114
115    fn build_self(&self) {
116        self.core.build_control();
117    }
118}