Skip to main content

freya_components/
input.rs

1use std::{
2    borrow::Cow,
3    cell::{
4        Ref,
5        RefCell,
6    },
7    rc::Rc,
8};
9
10use freya_core::prelude::*;
11use freya_edit::*;
12use torin::{
13    gaps::Gaps,
14    prelude::{
15        Alignment,
16        Area,
17        Content,
18        Direction,
19    },
20    size::Size,
21};
22
23use crate::{
24    cursor_blink::use_cursor_blink,
25    define_theme,
26    get_theme,
27    scrollviews::ScrollView,
28};
29
30define_theme! {
31    for = Input;
32    theme_field = theme_layout;
33
34    %[component]
35    pub InputLayout {
36        %[fields]
37        corner_radius: CornerRadius,
38        inner_margin: Gaps,
39    }
40}
41
42define_theme! {
43    for = Input;
44    theme_field = theme_colors;
45
46    %[component]
47    pub InputColors {
48        %[fields]
49        background: Color,
50        focus_background: Color,
51        border_fill: Color,
52        focus_border_fill: Color,
53        color: Color,
54        placeholder_color: Color,
55    }
56}
57
58#[derive(Clone, PartialEq)]
59pub enum InputStyleVariant {
60    Normal,
61    Filled,
62    Flat,
63}
64
65#[derive(Clone, PartialEq)]
66pub enum InputLayoutVariant {
67    Normal,
68    Compact,
69    Expanded,
70}
71
72#[derive(Default, Clone, PartialEq)]
73pub enum InputMode {
74    #[default]
75    Shown,
76    Hidden(char),
77}
78
79impl InputMode {
80    pub fn new_password() -> Self {
81        Self::Hidden('*')
82    }
83}
84
85#[derive(Debug, Default, PartialEq, Clone, Copy)]
86pub enum InputStatus {
87    /// Default state.
88    #[default]
89    Idle,
90    /// Pointer is hovering the input.
91    Hovering,
92}
93
94#[derive(Clone)]
95pub struct InputValidator {
96    valid: Rc<RefCell<bool>>,
97    text: Rc<RefCell<String>>,
98}
99
100impl InputValidator {
101    pub fn new(text: String) -> Self {
102        Self {
103            valid: Rc::new(RefCell::new(true)),
104            text: Rc::new(RefCell::new(text)),
105        }
106    }
107    pub fn text(&'_ self) -> Ref<'_, String> {
108        self.text.borrow()
109    }
110    pub fn set_valid(&self, is_valid: bool) {
111        *self.valid.borrow_mut() = is_valid;
112    }
113    pub fn is_valid(&self) -> bool {
114        *self.valid.borrow()
115    }
116}
117
118/// Small box to write some text.
119///
120/// ## **Normal**
121///
122/// ```rust
123/// # use freya::prelude::*;
124/// fn app() -> impl IntoElement {
125///     let value = use_state(String::new);
126///     Input::new(value).placeholder("Type here")
127/// }
128/// # use freya_testing::prelude::*;
129/// # launch_doc(|| {
130/// #   rect().center().expanded().child(app())
131/// # }, "./images/gallery_input.png").render();
132/// ```
133/// ## **Filled**
134///
135/// ```rust
136/// # use freya::prelude::*;
137/// fn app() -> impl IntoElement {
138///     let value = use_state(String::new);
139///     Input::new(value).placeholder("Type here").filled()
140/// }
141/// # use freya_testing::prelude::*;
142/// # launch_doc(|| {
143/// #   rect().center().expanded().child(app())
144/// # }, "./images/gallery_filled_input.png").render();
145/// ```
146/// ## **Flat**
147///
148/// ```rust
149/// # use freya::prelude::*;
150/// fn app() -> impl IntoElement {
151///     let value = use_state(String::new);
152///     Input::new(value).placeholder("Type here").flat()
153/// }
154/// # use freya_testing::prelude::*;
155/// # launch_doc(|| {
156/// #   rect().center().expanded().child(app())
157/// # }, "./images/gallery_flat_input.png").render();
158/// ```
159///
160/// # Preview
161/// ![Input Preview][input]
162/// ![Filled Input Preview][filled_input]
163/// ![Flat Input Preview][flat_input]
164#[cfg_attr(feature = "docs",
165    doc = embed_doc_image::embed_image!("input", "images/gallery_input.png"),
166    doc = embed_doc_image::embed_image!("filled_input", "images/gallery_filled_input.png"),
167    doc = embed_doc_image::embed_image!("flat_input", "images/gallery_flat_input.png"),
168)]
169#[derive(Clone, PartialEq)]
170pub struct Input {
171    pub(crate) theme_colors: Option<InputColorsThemePartial>,
172    pub(crate) theme_layout: Option<InputLayoutThemePartial>,
173    value: Writable<String>,
174    placeholder: Option<Cow<'static, str>>,
175    on_validate: Option<EventHandler<InputValidator>>,
176    on_submit: Option<EventHandler<String>>,
177    mode: InputMode,
178    auto_focus: bool,
179    width: Size,
180    enabled: bool,
181    key: DiffKey,
182    style_variant: InputStyleVariant,
183    layout_variant: InputLayoutVariant,
184    text_align: TextAlign,
185    a11y_id: Option<AccessibilityId>,
186    leading: Option<Element>,
187    trailing: Option<Element>,
188    on_pre_key_down: Callback<Event<KeyboardEventData>, bool>,
189}
190
191impl KeyExt for Input {
192    fn write_key(&mut self) -> &mut DiffKey {
193        &mut self.key
194    }
195}
196
197impl Input {
198    pub fn new(value: impl Into<Writable<String>>) -> Self {
199        Input {
200            theme_colors: None,
201            theme_layout: None,
202            value: value.into(),
203            placeholder: None,
204            on_validate: None,
205            on_submit: None,
206            mode: InputMode::default(),
207            auto_focus: false,
208            width: Size::px(150.),
209            enabled: true,
210            key: DiffKey::default(),
211            style_variant: InputStyleVariant::Normal,
212            layout_variant: InputLayoutVariant::Normal,
213            text_align: TextAlign::default(),
214            a11y_id: None,
215            leading: None,
216            trailing: None,
217            on_pre_key_down: Callback::new(|e: Event<KeyboardEventData>| match &e.key {
218                Key::Named(NamedKey::Enter) | Key::Named(NamedKey::Escape) => true,
219                Key::Named(NamedKey::Tab) => false,
220                _ => {
221                    e.stop_propagation();
222                    e.prevent_default();
223                    true
224                }
225            }),
226        }
227    }
228
229    pub fn enabled(mut self, enabled: impl Into<bool>) -> Self {
230        self.enabled = enabled.into();
231        self
232    }
233
234    pub fn placeholder(mut self, placeholder: impl Into<Cow<'static, str>>) -> Self {
235        self.placeholder = Some(placeholder.into());
236        self
237    }
238
239    pub fn on_validate(mut self, on_validate: impl Into<EventHandler<InputValidator>>) -> Self {
240        self.on_validate = Some(on_validate.into());
241        self
242    }
243
244    pub fn on_submit(mut self, on_submit: impl Into<EventHandler<String>>) -> Self {
245        self.on_submit = Some(on_submit.into());
246        self
247    }
248
249    pub fn mode(mut self, mode: InputMode) -> Self {
250        self.mode = mode;
251        self
252    }
253
254    pub fn auto_focus(mut self, auto_focus: impl Into<bool>) -> Self {
255        self.auto_focus = auto_focus.into();
256        self
257    }
258
259    pub fn width(mut self, width: impl Into<Size>) -> Self {
260        self.width = width.into();
261        self
262    }
263
264    pub fn theme_colors(mut self, theme: InputColorsThemePartial) -> Self {
265        self.theme_colors = Some(theme);
266        self
267    }
268
269    pub fn theme_layout(mut self, theme: InputLayoutThemePartial) -> Self {
270        self.theme_layout = Some(theme);
271        self
272    }
273
274    pub fn text_align(mut self, text_align: impl Into<TextAlign>) -> Self {
275        self.text_align = text_align.into();
276        self
277    }
278
279    pub fn style_variant(mut self, style_variant: impl Into<InputStyleVariant>) -> Self {
280        self.style_variant = style_variant.into();
281        self
282    }
283
284    pub fn layout_variant(mut self, layout_variant: impl Into<InputLayoutVariant>) -> Self {
285        self.layout_variant = layout_variant.into();
286        self
287    }
288
289    /// Shortcut for [Self::style_variant] with [InputStyleVariant::Filled].
290    pub fn filled(self) -> Self {
291        self.style_variant(InputStyleVariant::Filled)
292    }
293
294    /// Shortcut for [Self::style_variant] with [InputStyleVariant::Flat].
295    pub fn flat(self) -> Self {
296        self.style_variant(InputStyleVariant::Flat)
297    }
298
299    /// Shortcut for [Self::layout_variant] with [InputLayoutVariant::Compact].
300    pub fn compact(self) -> Self {
301        self.layout_variant(InputLayoutVariant::Compact)
302    }
303
304    /// Shortcut for [Self::layout_variant] with [InputLayoutVariant::Expanded].
305    pub fn expanded(self) -> Self {
306        self.layout_variant(InputLayoutVariant::Expanded)
307    }
308
309    pub fn a11y_id(mut self, a11y_id: impl Into<AccessibilityId>) -> Self {
310        self.a11y_id = Some(a11y_id.into());
311        self
312    }
313
314    /// Optional element rendered before the text input.
315    pub fn leading(mut self, leading: impl Into<Element>) -> Self {
316        self.leading = Some(leading.into());
317        self
318    }
319
320    /// Optional element rendered after the text input.
321    pub fn trailing(mut self, trailing: impl Into<Element>) -> Self {
322        self.trailing = Some(trailing.into());
323        self
324    }
325
326    /// Sets a pre-handler called for each key event. Return `true` to let the input process it,
327    /// `false` to skip. The callback may call `stop_propagation()` / `prevent_default()` directly.
328    pub fn on_pre_key_down(
329        mut self,
330        on_pre_key_down: impl Into<Callback<Event<KeyboardEventData>, bool>>,
331    ) -> Self {
332        self.on_pre_key_down = on_pre_key_down.into();
333        self
334    }
335}
336
337impl CornerRadiusExt for Input {
338    fn with_corner_radius(self, corner_radius: f32) -> Self {
339        self.corner_radius(corner_radius)
340    }
341}
342
343impl Component for Input {
344    fn render(&self) -> impl IntoElement {
345        let a11y_id = use_hook(|| self.a11y_id.unwrap_or_else(AccessibilityId::new_unique));
346        let focus = use_focus(a11y_id);
347        let holder = use_state(ParagraphHolder::default);
348        let mut area = use_state(Area::default);
349        let mut status = use_state(InputStatus::default);
350        let allow_write_clipboard = !matches!(self.mode, InputMode::Hidden(_));
351        let mut editable = use_editable(
352            || self.value.read().to_string(),
353            move || EditableConfig::new().with_allow_write_clipboard(allow_write_clipboard),
354        );
355        let mut is_dragging = use_state(|| false);
356        let mut value = self.value.clone();
357
358        let theme_colors = match self.style_variant {
359            InputStyleVariant::Normal => {
360                get_theme!(&self.theme_colors, InputColorsThemePreference, "input")
361            }
362            InputStyleVariant::Filled => get_theme!(
363                &self.theme_colors,
364                InputColorsThemePreference,
365                "filled_input"
366            ),
367            InputStyleVariant::Flat => {
368                get_theme!(&self.theme_colors, InputColorsThemePreference, "flat_input")
369            }
370        };
371        let theme_layout = match self.layout_variant {
372            InputLayoutVariant::Normal => get_theme!(
373                &self.theme_layout,
374                InputLayoutThemePreference,
375                "input_layout"
376            ),
377            InputLayoutVariant::Compact => get_theme!(
378                &self.theme_layout,
379                InputLayoutThemePreference,
380                "compact_input_layout"
381            ),
382            InputLayoutVariant::Expanded => get_theme!(
383                &self.theme_layout,
384                InputLayoutThemePreference,
385                "expanded_input_layout"
386            ),
387        };
388
389        let (mut movement_timeout, cursor_color) =
390            use_cursor_blink(focus() != Focus::Not, theme_colors.color);
391
392        let enabled = use_reactive(&self.enabled);
393        use_drop(move || {
394            if status() == InputStatus::Hovering && enabled() {
395                Cursor::set(CursorIcon::default());
396            }
397        });
398
399        let display_placeholder = value.read().is_empty()
400            && self.placeholder.is_some()
401            && !editable.editor().read().has_preedit();
402        let on_validate = self.on_validate.clone();
403        let on_submit = self.on_submit.clone();
404
405        if *value.read() != editable.editor().read().committed_text() {
406            let mut editor = editable.editor_mut().write();
407            editor.clear_preedit();
408            editor.set(&value.read());
409            editor.editor_history().clear();
410            editor.clear_selection();
411        }
412
413        let on_ime_preedit = move |e: Event<ImePreeditEventData>| {
414            let mut editor = editable.editor_mut().write();
415            if e.data().text.is_empty() {
416                editor.clear_preedit();
417            } else {
418                editor.set_preedit(&e.data().text);
419            }
420        };
421
422        let on_pre_key_down = self.on_pre_key_down.clone();
423        let on_key_down = move |e: Event<KeyboardEventData>| {
424            let key = e.key.clone();
425            let modifiers = e.modifiers;
426
427            if !on_pre_key_down.call(e) {
428                return;
429            }
430
431            match &key {
432                // On submit
433                Key::Named(NamedKey::Enter) => {
434                    if let Some(on_submit) = &on_submit {
435                        let text = editable.editor().peek().committed_text();
436                        on_submit.call(text);
437                    }
438                }
439                // On unfocus
440                Key::Named(NamedKey::Escape) => {
441                    a11y_id.request_unfocus();
442                    Cursor::set(CursorIcon::default());
443                }
444                // On change
445                _ => {
446                    movement_timeout.reset();
447                    editable.process_event(EditableEvent::KeyDown {
448                        key: &key,
449                        modifiers,
450                    });
451                    let text = editable.editor().read().committed_text();
452
453                    let apply_change = match &on_validate {
454                        Some(on_validate) => {
455                            let mut editor = editable.editor_mut().write();
456                            let validator = InputValidator::new(text.clone());
457                            on_validate.call(validator.clone());
458                            if !validator.is_valid() {
459                                if let Some(selection) = editor.undo() {
460                                    *editor.selection_mut() = selection;
461                                }
462                                editor.editor_history().clear_redos();
463                            }
464                            validator.is_valid()
465                        }
466                        None => true,
467                    };
468
469                    if apply_change {
470                        *value.write() = text;
471                    }
472                }
473            }
474        };
475
476        let on_key_up = move |e: Event<KeyboardEventData>| {
477            e.stop_propagation();
478            editable.process_event(EditableEvent::KeyUp { key: &e.key });
479        };
480
481        let on_input_focus_press = move |e: Event<FocusPressEventData>| {
482            e.stop_propagation();
483            e.prevent_default();
484            is_dragging.set(true);
485            movement_timeout.reset();
486            if !display_placeholder {
487                let area = area.read().to_f64();
488                let global_location = e.global_location().clamp(area.min(), area.max());
489                let location = (global_location - area.min()).to_point();
490                editable.process_event(EditableEvent::Down {
491                    location,
492                    editor_line: EditorLine::SingleParagraph,
493                    holder: &holder.read(),
494                });
495            }
496            a11y_id.request_focus();
497        };
498
499        let on_focus_press = move |e: Event<FocusPressEventData>| {
500            e.stop_propagation();
501            e.prevent_default();
502            is_dragging.set(true);
503            movement_timeout.reset();
504            if !display_placeholder {
505                editable.process_event(EditableEvent::Down {
506                    location: e.element_location(),
507                    editor_line: EditorLine::SingleParagraph,
508                    holder: &holder.read(),
509                });
510            }
511            a11y_id.request_focus();
512        };
513
514        let on_global_pointer_move = move |e: Event<PointerEventData>| {
515            if a11y_id.is_focused() && *is_dragging.read() {
516                let mut location = e.global_location();
517                location.x -= area.read().min_x() as f64;
518                location.y -= area.read().min_y() as f64;
519                editable.process_event(EditableEvent::Move {
520                    location,
521                    editor_line: EditorLine::SingleParagraph,
522                    holder: &holder.read(),
523                });
524            }
525        };
526
527        let on_pointer_enter = move |_| {
528            *status.write() = InputStatus::Hovering;
529            if enabled() {
530                Cursor::set(CursorIcon::Text);
531            } else {
532                Cursor::set(CursorIcon::NotAllowed);
533            }
534        };
535
536        let on_pointer_leave = move |_| {
537            if status() == InputStatus::Hovering {
538                Cursor::set(CursorIcon::default());
539                *status.write() = InputStatus::default();
540            }
541        };
542
543        let on_global_pointer_press = move |_: Event<PointerEventData>| {
544            match *status.read() {
545                InputStatus::Idle if a11y_id.is_focused() => {
546                    editable.process_event(EditableEvent::Release);
547                }
548                InputStatus::Hovering => {
549                    editable.process_event(EditableEvent::Release);
550                }
551                _ => {}
552            };
553
554            if a11y_id.is_focused() {
555                if *is_dragging.read() {
556                    // The input is focused and dragging, but it just clicked so we assume the dragging can stop
557                    is_dragging.set(false);
558                } else {
559                    // The input is focused but not dragging, so the click means it was clicked outside, therefore we can unfocus this input
560                    a11y_id.request_unfocus();
561                }
562            }
563        };
564
565        let on_pointer_press = move |e: Event<PointerEventData>| {
566            e.stop_propagation();
567            e.prevent_default();
568            match *status.read() {
569                InputStatus::Idle if a11y_id.is_focused() => {
570                    editable.process_event(EditableEvent::Release);
571                }
572                InputStatus::Hovering => {
573                    editable.process_event(EditableEvent::Release);
574                }
575                _ => {}
576            };
577
578            if a11y_id.is_focused() {
579                is_dragging.set_if_modified(false);
580            }
581        };
582
583        let (background, cursor_index, text_selection) = if enabled() && focus() != Focus::Not {
584            (
585                theme_colors.focus_background,
586                Some(editable.editor().read().cursor_pos()),
587                editable
588                    .editor()
589                    .read()
590                    .get_visible_selection(EditorLine::SingleParagraph),
591            )
592        } else {
593            (theme_colors.background, None, None)
594        };
595
596        let border = if focus().is_focused() {
597            Border::new()
598                .fill(theme_colors.focus_border_fill)
599                .width(2.)
600                .alignment(BorderAlignment::Inner)
601        } else {
602            Border::new()
603                .fill(theme_colors.border_fill.mul_if(!self.enabled, 0.85))
604                .width(1.)
605                .alignment(BorderAlignment::Inner)
606        };
607
608        let color = if display_placeholder {
609            theme_colors.placeholder_color
610        } else {
611            theme_colors.color
612        };
613
614        let value = self.value.read();
615        let a11y_text: Cow<str> = match (self.mode.clone(), &self.placeholder) {
616            (_, Some(ph)) if display_placeholder => Cow::Borrowed(ph.as_ref()),
617            (InputMode::Hidden(ch), _) => Cow::Owned(ch.to_string().repeat(value.len())),
618            (InputMode::Shown, _) => Cow::Borrowed(value.as_ref()),
619        };
620
621        let a11_role = match self.mode {
622            InputMode::Hidden(_) => AccessibilityRole::PasswordInput,
623            _ => AccessibilityRole::TextInput,
624        };
625
626        rect()
627            .a11y_id(a11y_id)
628            .a11y_focusable(self.enabled)
629            .a11y_auto_focus(self.auto_focus)
630            .a11y_alt(a11y_text)
631            .a11y_role(a11_role)
632            .maybe(self.enabled, |el| {
633                el.on_key_up(on_key_up)
634                    .on_key_down(on_key_down)
635                    .on_focus_press(on_input_focus_press)
636                    .on_ime_preedit(on_ime_preedit)
637                    .on_pointer_press(on_pointer_press)
638                    .on_global_pointer_press(on_global_pointer_press)
639                    .on_global_pointer_move(on_global_pointer_move)
640            })
641            .on_pointer_enter(on_pointer_enter)
642            .on_pointer_leave(on_pointer_leave)
643            .width(self.width.clone())
644            .background(background.mul_if(!self.enabled, 0.85))
645            .border(border)
646            .corner_radius(theme_layout.corner_radius)
647            .content(Content::Flex)
648            .direction(Direction::Horizontal)
649            .cross_align(Alignment::center())
650            .maybe_child(
651                self.leading
652                    .clone()
653                    .map(|leading| rect().padding(Gaps::new(0., 0., 0., 8.)).child(leading)),
654            )
655            .child(
656                ScrollView::new()
657                    .width(Size::flex(1.))
658                    .height(Size::Inner)
659                    .direction(Direction::Horizontal)
660                    .show_scrollbar(false)
661                    .child(
662                        paragraph()
663                            .holder(holder.read().clone())
664                            .on_sized(move |e: Event<SizedEventData>| area.set(e.visible_area))
665                            .min_width(Size::func(move |context| {
666                                Some(context.parent - theme_layout.inner_margin.horizontal())
667                            }))
668                            .maybe(self.enabled, |el| el.on_focus_press(on_focus_press))
669                            .margin(theme_layout.inner_margin)
670                            .cursor_index(cursor_index)
671                            .cursor_color(cursor_color)
672                            .color(color)
673                            .text_align(self.text_align)
674                            .max_lines(1)
675                            .highlights(text_selection.map(|h| vec![h]))
676                            .maybe(display_placeholder, |el| {
677                                el.span(self.placeholder.as_ref().unwrap().to_string())
678                            })
679                            .maybe(!display_placeholder, |el| {
680                                let editor = editable.editor().read();
681                                if editor.has_preedit() {
682                                    let (b, p, a) = editor.preedit_text_segments();
683                                    let (b, p, a) = match self.mode.clone() {
684                                        InputMode::Hidden(ch) => {
685                                            let ch = ch.to_string();
686                                            (
687                                                ch.repeat(b.chars().count()),
688                                                ch.repeat(p.chars().count()),
689                                                ch.repeat(a.chars().count()),
690                                            )
691                                        }
692                                        InputMode::Shown => (b, p, a),
693                                    };
694                                    el.span(b)
695                                        .span(
696                                            Span::new(p).text_decoration(TextDecoration::Underline),
697                                        )
698                                        .span(a)
699                                } else {
700                                    let text = match self.mode.clone() {
701                                        InputMode::Hidden(ch) => {
702                                            ch.to_string().repeat(editor.rope().len_chars())
703                                        }
704                                        InputMode::Shown => editor.rope().to_string(),
705                                    };
706                                    el.span(text)
707                                }
708                            }),
709                    ),
710            )
711            .maybe_child(
712                self.trailing
713                    .clone()
714                    .map(|trailing| rect().padding(Gaps::new(0., 8., 0., 0.)).child(trailing)),
715            )
716    }
717
718    fn render_key(&self) -> DiffKey {
719        self.key.clone().or(self.default_key())
720    }
721}