1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::{
    messenger::MessageData,
    unpack_named_slots, widget,
    widget::{
        component::interactive::{
            button::{use_button, ButtonProps},
            navigation::{use_nav_item, use_nav_text_input, NavSignal, NavTextChange},
        },
        context::WidgetMountOrChangeContext,
        unit::area::AreaBoxNode,
        WidgetId, WidgetIdOrRef,
    },
    widget_component, widget_hook,
};
use serde::{Deserialize, Serialize};

fn is_false(v: &bool) -> bool {
    !*v
}

fn is_zero(v: &usize) -> bool {
    *v == 0
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TextInputProps {
    #[serde(default)]
    #[serde(skip_serializing_if = "is_false")]
    pub focused: bool,
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub cursor_position: usize,
    #[serde(default)]
    #[serde(skip_serializing_if = "is_false")]
    pub allow_new_line: bool,
    #[serde(default)]
    #[serde(skip_serializing_if = "String::is_empty")]
    pub text: String,
}
implement_props_data!(TextInputProps);

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TextInputNotifyProps(
    #[serde(default)]
    #[serde(skip_serializing_if = "WidgetIdOrRef::is_none")]
    pub WidgetIdOrRef,
);
implement_props_data!(TextInputNotifyProps);

#[derive(Debug, Clone)]
pub struct TextInputNotifyMessage {
    pub sender: WidgetId,
    pub state: TextInputProps,
}
implement_message_data!(TextInputNotifyMessage);

widget_hook! {
    pub use_text_input_notified_state(life_cycle) {
        life_cycle.change(|context| {
            for msg in context.messenger.messages {
                if let Some(msg) = msg.as_any().downcast_ref::<TextInputNotifyMessage>() {
                    drop(context.state.write_with(msg.state.to_owned()));
                }
            }
        });
    }
}

widget_hook! {
    pub use_text_input(life_cycle) [use_nav_text_input] {
        fn notify<T>(context: &WidgetMountOrChangeContext, data: T)
        where
            T: 'static + MessageData,
        {
            if let Ok(notify) = context.props.read::<TextInputNotifyProps>() {
                if let Some(to) = notify.0.read() {
                    context.messenger.write(to, data);
                }
            }
        }

        life_cycle.mount(|context| {
            let mut data = context.props.read_cloned_or_default::<TextInputProps>();
            data.focused = false;
            notify(&context, TextInputNotifyMessage {
                sender: context.id.to_owned(),
                state: data.to_owned(),
            });
            drop(context.state.write_with(data));
        });

        life_cycle.change(|context| {
            let mut data = context.state.read_cloned_or_default::<TextInputProps>();
            let mut dirty = false;
            for msg in context.messenger.messages {
                if let Some(msg) = msg.as_any().downcast_ref::<NavSignal>() {
                    match msg {
                        NavSignal::FocusTextInput(idref) => {
                            data.focused = idref.is_some();
                            dirty = true;
                        }
                        NavSignal::TextChange(change) => if data.focused {
                            match change {
                                NavTextChange::InsertCharacter(c) => if !c.is_control() {
                                    data.cursor_position = data.cursor_position.min(data.text.len());
                                    data.text.insert(data.cursor_position, *c);
                                    data.cursor_position += 1;
                                }
                                NavTextChange::MoveCursorLeft => if data.cursor_position > 0 {
                                    data.cursor_position -= 1;
                                }
                                NavTextChange::MoveCursorRight => {
                                    if data.cursor_position < data.text.len() {
                                        data.cursor_position += 1;
                                    }
                                }
                                NavTextChange::MoveCursorStart => data.cursor_position = 0,
                                NavTextChange::MoveCursorEnd => {
                                    data.cursor_position = data.text.len();
                                }
                                NavTextChange::DeleteLeft => {
                                    if data.cursor_position > 0 && data.cursor_position <= data.text.len() {
                                        data.cursor_position -= 1;
                                        data.text.remove(data.cursor_position);
                                    }
                                }
                                NavTextChange::DeleteRight => {
                                    if data.cursor_position < data.text.len() {
                                        data.text.remove(data.cursor_position);
                                    }
                                }
                                NavTextChange::NewLine => if data.allow_new_line {
                                    data.cursor_position = data.cursor_position.min(data.text.len());
                                    data.text.insert(data.cursor_position, '\n');
                                    data.cursor_position += 1;
                                }
                            }
                            data.cursor_position = data.cursor_position.min(data.text.len());
                            dirty = true;
                        }
                        _ => {}
                    }
                }
            }
            if dirty {
                notify(&context, TextInputNotifyMessage {
                    sender: context.id.to_owned(),
                    state: data.to_owned(),
                });
                drop(context.state.write_with(data));
            }
        });
    }
}

widget_hook! {
    pub use_input_field(life_cycle) [use_button, use_text_input] {
        life_cycle.change(|context| {
            let focused = context.state.map_or_default::<TextInputProps, _, _>(|s| s.focused);
            for msg in context.messenger.messages {
                if let Some(msg) = msg.as_any().downcast_ref::<NavSignal>() {
                    match msg {
                        NavSignal::Accept(true) => if !focused {
                            context.signals.write(NavSignal::FocusTextInput(
                                context.id.to_owned().into()
                            ));
                        }
                        NavSignal::Cancel(true) => if focused {
                            context.signals.write(NavSignal::FocusTextInput(().into()));
                        }
                        _ => {}
                    }
                }
            }
        });
    }
}

widget_component! {
    pub text_input(id, state, named_slots) [use_nav_item, use_text_input] {
        unpack_named_slots!(named_slots => content);

        if let Some(p) = content.props_mut() {
            p.write(state.read_cloned_or_default::<TextInputProps>());
        }

        widget! {{{
            AreaBoxNode {
                id: id.to_owned(),
                slot: Box::new(content),
                ..Default::default()
            }
        }}}
    }
}

widget_component! {
    pub input_field(id, state, named_slots) [use_nav_item, use_input_field] {
        unpack_named_slots!(named_slots => content);

        if let Some(p) = content.props_mut() {
            p.write(state.read_cloned_or_default::<ButtonProps>());
            p.write(state.read_cloned_or_default::<TextInputProps>());
        }

        widget! {{{
            AreaBoxNode {
                id: id.to_owned(),
                slot: Box::new(content),
                ..Default::default()
            }
        }}}
    }
}