Skip to main content

fui/controls/
text_editor_surface.rs

1use super::internal::text_input_presenter::TextInputTemplate;
2use super::TextInputColors;
3use crate::event::{SelectionChangedEventArgs, TextChangedEventArgs};
4use crate::FontFamily;
5use std::rc::Rc;
6
7/// Shared editable-text behavior implemented by [`TextInput`](super::TextInput)
8/// and [`TextArea`](super::TextArea).
9///
10/// Selection and caret positions use Unicode scalar-value indices. The
11/// explicitly named byte-offset getters expose the corresponding UTF-8 runtime
12/// offsets for diagnostics and low-level interop only.
13pub trait TextEditorSurface: Sized {
14    fn text(&self, value: impl Into<String>) -> &Self;
15    fn value(&self) -> String;
16    fn selection_start(&self) -> u32;
17    fn selection_end(&self) -> u32;
18    fn selection_start_byte_offset(&self) -> u32;
19    fn selection_end_byte_offset(&self) -> u32;
20    fn placeholder(&self, value: impl Into<String>) -> &Self;
21    fn max_chars(&self, limit: i32) -> &Self;
22    fn read_only(&self, flag: bool) -> &Self;
23    fn accepts_tab(&self, flag: bool) -> &Self;
24    fn selection_range(&self, start: u32, end: u32) -> &Self;
25    fn caret(&self, position: u32) -> &Self;
26    fn caret_to_end(&self) -> &Self;
27    fn colors(&self, colors: TextInputColors) -> &Self;
28    fn clear_colors(&self) -> &Self;
29    fn template(&self, template: Rc<dyn TextInputTemplate>) -> &Self;
30    fn clear_template(&self) -> &Self;
31    fn line_height(&self, value: f32) -> &Self;
32    fn font_family(&self, family: FontFamily) -> &Self;
33    fn font_size(&self, size: f32) -> &Self;
34    fn on_changed(&self, handler: impl Fn(TextChangedEventArgs) + 'static) -> &Self;
35    fn on_text_changed(&self, handler: impl Fn(TextChangedEventArgs) + 'static) -> &Self;
36    fn on_selection_changed(&self, handler: impl Fn(SelectionChangedEventArgs) + 'static) -> &Self;
37}
38
39macro_rules! impl_text_editor_surface {
40    ($control:ty) => {
41        impl crate::controls::TextEditorSurface for $control {
42            fn text(&self, value: impl Into<String>) -> &Self {
43                self.core.text(value);
44                self
45            }
46
47            fn value(&self) -> String {
48                self.core.value()
49            }
50
51            fn selection_start(&self) -> u32 {
52                self.core.selection_start()
53            }
54
55            fn selection_end(&self) -> u32 {
56                self.core.selection_end()
57            }
58
59            fn selection_start_byte_offset(&self) -> u32 {
60                self.core.selection_start_byte_offset()
61            }
62
63            fn selection_end_byte_offset(&self) -> u32 {
64                self.core.selection_end_byte_offset()
65            }
66
67            fn placeholder(&self, value: impl Into<String>) -> &Self {
68                self.core.placeholder(value);
69                self
70            }
71
72            fn max_chars(&self, limit: i32) -> &Self {
73                self.core.max_chars(limit);
74                self
75            }
76
77            fn read_only(&self, flag: bool) -> &Self {
78                self.core.read_only(flag);
79                self
80            }
81
82            fn accepts_tab(&self, flag: bool) -> &Self {
83                self.core.accepts_tab(flag);
84                self
85            }
86
87            fn selection_range(&self, start: u32, end: u32) -> &Self {
88                self.core.selection_range(start, end);
89                self
90            }
91
92            fn caret(&self, position: u32) -> &Self {
93                self.core.caret(position);
94                self
95            }
96
97            fn caret_to_end(&self) -> &Self {
98                self.core.caret_to_end();
99                self
100            }
101
102            fn colors(&self, colors: crate::controls::TextInputColors) -> &Self {
103                self.core.colors(colors);
104                self
105            }
106
107            fn clear_colors(&self) -> &Self {
108                self.core.clear_colors();
109                self
110            }
111
112            fn template(
113                &self,
114                template: std::rc::Rc<dyn crate::controls::TextInputTemplate>,
115            ) -> &Self {
116                self.core.template(template);
117                self
118            }
119
120            fn clear_template(&self) -> &Self {
121                self.core.clear_template();
122                self
123            }
124
125            fn line_height(&self, value: f32) -> &Self {
126                self.core.line_height(value);
127                self
128            }
129
130            fn font_family(&self, family: crate::FontFamily) -> &Self {
131                self.core.font_family(family);
132                self
133            }
134
135            fn font_size(&self, size: f32) -> &Self {
136                self.core.font_size(size);
137                self
138            }
139
140            fn on_changed(
141                &self,
142                handler: impl Fn(crate::event::TextChangedEventArgs) + 'static,
143            ) -> &Self {
144                self.core.on_changed(handler);
145                self
146            }
147
148            fn on_text_changed(
149                &self,
150                handler: impl Fn(crate::event::TextChangedEventArgs) + 'static,
151            ) -> &Self {
152                self.core.on_text_changed(handler);
153                self
154            }
155
156            fn on_selection_changed(
157                &self,
158                handler: impl Fn(crate::event::SelectionChangedEventArgs) + 'static,
159            ) -> &Self {
160                self.core.on_selection_changed(handler);
161                self
162            }
163        }
164    };
165}
166
167pub(crate) use impl_text_editor_surface;