add blinking cursor in text input field
This commit is contained in:
parent
2ecb86d283
commit
2d961608e2
1 changed files with 14 additions and 7 deletions
21
src/ui.rs
21
src/ui.rs
|
@ -227,6 +227,7 @@ pub fn text_input(
|
||||||
editable: bool,
|
editable: bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let mut changed = false;
|
let mut changed = false;
|
||||||
|
let font_size = 20;
|
||||||
d.draw_rectangle_rec(bounds, widget_bg(*is_selected));
|
d.draw_rectangle_rec(bounds, widget_bg(*is_selected));
|
||||||
d.draw_rectangle_rec(
|
d.draw_rectangle_rec(
|
||||||
Rectangle::new(
|
Rectangle::new(
|
||||||
|
@ -237,18 +238,24 @@ pub fn text_input(
|
||||||
),
|
),
|
||||||
BG_DARK,
|
BG_DARK,
|
||||||
);
|
);
|
||||||
let drawn_text = if *is_selected {
|
|
||||||
&format!("{text}_")
|
|
||||||
} else {
|
|
||||||
text.as_str()
|
|
||||||
};
|
|
||||||
d.draw_text(
|
d.draw_text(
|
||||||
drawn_text,
|
text,
|
||||||
bounds.x as i32 + 4,
|
bounds.x as i32 + 4,
|
||||||
bounds.y as i32 + 4,
|
bounds.y as i32 + 4,
|
||||||
20,
|
font_size,
|
||||||
Color::WHITE,
|
Color::WHITE,
|
||||||
);
|
);
|
||||||
|
// blinking cursor
|
||||||
|
if *is_selected && d.get_time().fract() < 0.5 {
|
||||||
|
let width = d.measure_text(text, font_size);
|
||||||
|
d.draw_rectangle(
|
||||||
|
bounds.x as i32 + 6 + width,
|
||||||
|
bounds.y as i32 + 4,
|
||||||
|
2,
|
||||||
|
font_size,
|
||||||
|
Color::WHITE,
|
||||||
|
);
|
||||||
|
};
|
||||||
if editable && mouse.left_click() && (mouse.is_over(bounds) || *is_selected) {
|
if editable && mouse.left_click() && (mouse.is_over(bounds) || *is_selected) {
|
||||||
*is_selected = !*is_selected;
|
*is_selected = !*is_selected;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue