remy_widgets/widgets/text_input/
state.rs1use crate::widgets::core::RemyWidgetState;
2use crate::widgets::text_field::TextFieldState;
3use crate::widgets::text_input::input::TextInputAction;
4use crate::widgets::util::cursor::TypingBehaviour;
5
6#[derive(Debug, Default, Clone)]
7pub struct TextInputState<T: TypingBehaviour>(pub(super) TextFieldState<T>);
8
9pub enum TextInputEvent {
10 Submitted(String),
11 Cancelled,
12 Typing
13}
14
15
16impl<T: TypingBehaviour> TextInputState<T> {
17 pub fn new(behaviour: T) -> Self {
18 Self(TextFieldState::new(behaviour))
19 }
20
21 pub fn get_cursor_location(&mut self, width: usize) -> usize {
22 self.0.get_cursor_location(width)
23 }
24
25 pub fn get_visible_text(&mut self, width: usize) -> (String, Option<(usize, usize)>) {
26 self.0.get_visible_text(width)
27 }
28
29 fn text(&self) -> &str {
30 self.0.text()
31 }
32}
33
34
35impl<B: TypingBehaviour> RemyWidgetState for TextInputState<B> {
36 type Command = TextInputAction;
37 type EventOutput = TextInputEvent;
38
39 fn handle_native_event(&mut self, event: Option<Self::Command>) -> Self::EventOutput {
40 match event {
41 Some(inner) => match inner {
42 TextInputAction::Esc => TextInputEvent::Cancelled,
43 TextInputAction::Enter => TextInputEvent::Submitted(self.0.text().to_string()),
44 TextInputAction::Other(a) => {
45 let _ = self.0.handle_native_event(Some(a));
46 TextInputEvent::Typing
47 }
48 TextInputAction::Null => TextInputEvent::Typing,
49 }
50 None => TextInputEvent::Typing
51 }
52 }
53}