iris_ui/
text_input.rs

1use crate::geom::Bounds;
2use crate::gfx::TextStyle;
3use crate::view::{Align, View, ViewId};
4use crate::{Action, DrawEvent, EventType, GuiEvent, KeyboardAction};
5use log::info;
6
7fn draw_text_input(e: &mut DrawEvent) {
8    e.ctx.fill_rect(&e.view.bounds, &e.theme.bg);
9    e.ctx.stroke_rect(&e.view.bounds, &e.theme.fg);
10    let style = TextStyle::new(&e.theme.font, &e.theme.fg).with_halign(Align::Start);
11    e.ctx.fill_text(&e.view.bounds, &e.view.title, &style);
12
13    if let Some(focused) = e.focused {
14        if focused == &e.view.name {
15            e.ctx.stroke_rect(&e.view.bounds.contract(2), &e.theme.fg);
16            let n = e.view.title.len() as i32;
17            let w = e.theme.font.character_size.width as i32;
18            let h = e.theme.font.character_size.height as i32;
19            e.ctx.fill_rect(
20                &Bounds::new(
21                    e.view.bounds.position.x + n * w + 5,
22                    e.view.bounds.position.y + 5,
23                    2,
24                    h + 4,
25                ),
26                &e.theme.fg,
27            );
28        }
29    }
30}
31
32fn input_text_input(event: &mut GuiEvent) -> Option<Action> {
33    info!("text input got event {:?}", event.event_type);
34    match &event.event_type {
35        EventType::Keyboard(key) => {
36            if let Some(view) = event.scene.get_view_mut(event.target) {
37                match *key {
38                    8 => {
39                        if view.title.len() > 0 {
40                            view.title.remove(view.title.len() - 1);
41                        }
42                    }
43                    13 => {
44                        info!("doing return");
45                        return Some(Action::Command("Completed".into()));
46                    }
47                    _ => {
48                        view.title.push(*key as char);
49                    }
50                }
51                info!("done");
52            }
53            event.scene.mark_dirty_view(event.target);
54        }
55        EventType::KeyboardAction(act) => match act {
56            KeyboardAction::Left => {}
57            KeyboardAction::Right => {}
58            KeyboardAction::Up => {}
59            KeyboardAction::Down => {}
60            KeyboardAction::Backspace => {
61                if let Some(view) = event.scene.get_view_mut(event.target) {
62                    if view.title.len() > 0 {
63                        view.title.remove(view.title.len() - 1);
64                        event.scene.mark_dirty_view(event.target);
65                    }
66                }
67            }
68            KeyboardAction::Return => {}
69        },
70        EventType::Tap(_pt) => {
71            event.scene.set_focused(event.target);
72        }
73        _ => {}
74    }
75    None
76}
77
78pub fn make_text_input(name: &'static str, title: &str) -> View {
79    View {
80        name: ViewId::new(name),
81        title: title.into(),
82        bounds: Bounds::new(0, 0, 100, 30),
83        visible: true,
84        state: None,
85        input: Some(input_text_input),
86        layout: Some(|e| {
87            // if let Some(view) = e.scene.get_view_mut(e.target) {
88            //     view.bounds = util::calc_bounds(view.bounds, e.theme.bold_font, &view.title);
89            // }
90        }),
91        draw: Some(draw_text_input),
92        ..Default::default()
93    }
94}