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            if cfg!(target_os = "android") {
485                if a11y_id.is_focused() {
486                    // Require a second press to enabling dragging on Android
487                    is_dragging.set_if_modified(true);
488                }
489            } else {
490                is_dragging.set_if_modified(true);
491            }
492            movement_timeout.reset();
493            if !display_placeholder {
494                let area = area.read().to_f64();
495                let global_location = e.global_location().clamp(area.min(), area.max());
496                let location = (global_location - area.min()).to_point();
497                editable.process_event(EditableEvent::Down {
498                    location,
499                    editor_line: EditorLine::SingleParagraph,
500                    holder: &holder.read(),
501                });
502            }
503            a11y_id.request_focus();
504        };
505
506        let on_focus_press = move |e: Event<FocusPressEventData>| {
507            e.stop_propagation();
508            e.prevent_default();
509            if cfg!(target_os = "android") {
510                if a11y_id.is_focused() {
511                    // Require a second press to enabling dragging on Android
512                    is_dragging.set_if_modified(true);
513                }
514            } else {
515                is_dragging.set_if_modified(true);
516            }
517            movement_timeout.reset();
518            if !display_placeholder {
519                editable.process_event(EditableEvent::Down {
520                    location: e.element_location(),
521                    editor_line: EditorLine::SingleParagraph,
522                    holder: &holder.read(),
523                });
524            }
525            a11y_id.request_focus();
526        };
527
528        let on_global_pointer_move = move |e: Event<PointerEventData>| {
529            if a11y_id.is_focused() && *is_dragging.read() {
530                let mut location = e.global_location();
531                location.x -= area.read().min_x() as f64;
532                location.y -= area.read().min_y() as f64;
533                editable.process_event(EditableEvent::Move {
534                    location,
535                    editor_line: EditorLine::SingleParagraph,
536                    holder: &holder.read(),
537                });
538            }
539        };
540
541        let on_pointer_enter = move |_| {
542            *status.write() = InputStatus::Hovering;
543            if enabled() {
544                Cursor::set(CursorIcon::Text);
545            } else {
546                Cursor::set(CursorIcon::NotAllowed);
547            }
548        };
549
550        let on_pointer_leave = move |_| {
551            if status() == InputStatus::Hovering {
552                Cursor::set(CursorIcon::default());
553                *status.write() = InputStatus::default();
554            }
555        };
556
557        let on_global_pointer_press = move |_: Event<PointerEventData>| {
558            match *status.read() {
559                InputStatus::Idle if a11y_id.is_focused() => {
560                    editable.process_event(EditableEvent::Release);
561                }
562                InputStatus::Hovering => {
563                    editable.process_event(EditableEvent::Release);
564                }
565                _ => {}
566            };
567
568            if a11y_id.is_focused() {
569                if *is_dragging.read() {
570                    // The input is focused and dragging, but it just clicked so we assume the dragging can stop
571                    is_dragging.set(false);
572                } else {
573                    // The input is focused but not dragging, so the click means it was clicked outside, therefore we can unfocus this input
574                    a11y_id.request_unfocus();
575                }
576            }
577        };
578
579        let on_pointer_press = move |e: Event<PointerEventData>| {
580            e.stop_propagation();
581            e.prevent_default();
582            match *status.read() {
583                InputStatus::Idle if a11y_id.is_focused() => {
584                    editable.process_event(EditableEvent::Release);
585                }
586                InputStatus::Hovering => {
587                    editable.process_event(EditableEvent::Release);
588                }
589                _ => {}
590            };
591
592            if a11y_id.is_focused() {
593                is_dragging.set_if_modified(false);
594            }
595        };
596
597        let (background, cursor_index, text_selection) = if enabled() && focus() != Focus::Not {
598            (
599                theme_colors.focus_background,
600                Some(editable.editor().read().cursor_pos()),
601                editable
602                    .editor()
603                    .read()
604                    .get_visible_selection(EditorLine::SingleParagraph),
605            )
606        } else {
607            (theme_colors.background, None, None)
608        };
609
610        let border = if focus().is_focused() {
611            Border::new()
612                .fill(theme_colors.focus_border_fill)
613                .width(2.)
614                .alignment(BorderAlignment::Inner)
615        } else {
616            Border::new()
617                .fill(theme_colors.border_fill.mul_if(!self.enabled, 0.85))
618                .width(1.)
619                .alignment(BorderAlignment::Inner)
620        };
621
622        let color = if display_placeholder {
623            theme_colors.placeholder_color
624        } else {
625            theme_colors.color
626        };
627
628        let value = self.value.read();
629        let a11y_text: Cow<str> = match (self.mode.clone(), &self.placeholder) {
630            (_, Some(ph)) if display_placeholder => Cow::Borrowed(ph.as_ref()),
631            (InputMode::Hidden(ch), _) => Cow::Owned(ch.to_string().repeat(value.len())),
632            (InputMode::Shown, _) => Cow::Borrowed(value.as_ref()),
633        };
634
635        let a11_role = match self.mode {
636            InputMode::Hidden(_) => AccessibilityRole::PasswordInput,
637            _ => AccessibilityRole::TextInput,
638        };
639
640        rect()
641            .a11y_id(a11y_id)
642            .a11y_focusable(self.enabled)
643            .a11y_auto_focus(self.auto_focus)
644            .a11y_alt(a11y_text)
645            .a11y_role(a11_role)
646            .maybe(self.enabled, |el| {
647                el.on_key_up(on_key_up)
648                    .on_key_down(on_key_down)
649                    .on_focus_press(on_input_focus_press)
650                    .on_ime_preedit(on_ime_preedit)
651                    .on_pointer_press(on_pointer_press)
652                    .on_global_pointer_press(on_global_pointer_press)
653                    .on_global_pointer_move(on_global_pointer_move)
654            })
655            .on_pointer_enter(on_pointer_enter)
656            .on_pointer_leave(on_pointer_leave)
657            .width(self.width.clone())
658            .background(background.mul_if(!self.enabled, 0.85))
659            .border(border)
660            .corner_radius(theme_layout.corner_radius)
661            .content(Content::Flex)
662            .direction(Direction::Horizontal)
663            .cross_align(Alignment::center())
664            .maybe_child(
665                self.leading
666                    .clone()
667                    .map(|leading| rect().padding(Gaps::new(0., 0., 0., 8.)).child(leading)),
668            )
669            .child(
670                ScrollView::new()
671                    .width(Size::flex(1.))
672                    .height(Size::Inner)
673                    .direction(Direction::Horizontal)
674                    .show_scrollbar(false)
675                    .child(
676                        paragraph()
677                            .holder(holder.read().clone())
678                            .on_sized(move |e: Event<SizedEventData>| area.set(e.visible_area))
679                            .min_width(Size::func(move |context| {
680                                Some(context.parent - theme_layout.inner_margin.horizontal())
681                            }))
682                            .maybe(self.enabled, |el| el.on_focus_press(on_focus_press))
683                            .margin(theme_layout.inner_margin)
684                            .cursor_index(cursor_index)
685                            .cursor_color(cursor_color)
686                            .color(color)
687                            .text_align(self.text_align)
688                            .max_lines(1)
689                            .highlights(text_selection.map(|h| vec![h]))
690                            .maybe(display_placeholder, |el| {
691                                el.span(self.placeholder.as_ref().unwrap().to_string())
692                            })
693                            .maybe(!display_placeholder, |el| {
694                                let editor = editable.editor().read();
695                                if editor.has_preedit() {
696                                    let (b, p, a) = editor.preedit_text_segments();
697                                    let (b, p, a) = match self.mode.clone() {
698                                        InputMode::Hidden(ch) => {
699                                            let ch = ch.to_string();
700                                            (
701                                                ch.repeat(b.chars().count()),
702                                                ch.repeat(p.chars().count()),
703                                                ch.repeat(a.chars().count()),
704                                            )
705                                        }
706                                        InputMode::Shown => (b, p, a),
707                                    };
708                                    el.span(b)
709                                        .span(
710                                            Span::new(p).text_decoration(TextDecoration::Underline),
711                                        )
712                                        .span(a)
713                                } else {
714                                    let text = match self.mode.clone() {
715                                        InputMode::Hidden(ch) => {
716                                            ch.to_string().repeat(editor.rope().len_chars())
717                                        }
718                                        InputMode::Shown => editor.rope().to_string(),
719                                    };
720                                    el.span(text)
721                                }
722                            }),
723                    ),
724            )
725            .maybe_child(
726                self.trailing
727                    .clone()
728                    .map(|trailing| rect().padding(Gaps::new(0., 8., 0., 0.)).child(trailing)),
729            )
730    }
731
732    fn render_key(&self) -> DiffKey {
733        self.key.clone().or(self.default_key())
734    }
735}