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
//! `OtpInput` — a row of single-character boxes for one-time codes.
//!
//! Backed by a single `&mut String`; only the configured number of characters
//! is kept. Click to focus the group, then type — digits fill left to right
//! and `Backspace` removes the last one. The returned [`Response`] reports
//! `.changed()` on edits.
//!
//! ```ignore
//! ui.add(sc::OtpInput::new(&mut self.code).length(6));
//! ```
use egui::{vec2, FontId, Key, Rect, Response, Sense, Stroke, Ui, Widget};
use egui_components_theme::Theme;
pub struct OtpInput<'a> {
value: &'a mut String,
length: usize,
digits_only: bool,
box_size: f32,
gap: f32,
}
impl<'a> OtpInput<'a> {
pub fn new(value: &'a mut String) -> Self {
Self {
value,
length: 6,
digits_only: true,
box_size: 40.0,
gap: 8.0,
}
}
pub fn length(mut self, n: usize) -> Self {
self.length = n.max(1);
self
}
/// Allow any character (otherwise only ASCII digits are accepted).
pub fn any_char(mut self) -> Self {
self.digits_only = false;
self
}
pub fn box_size(mut self, s: f32) -> Self {
self.box_size = s;
self
}
}
impl<'a> Widget for OtpInput<'a> {
fn ui(self, ui: &mut Ui) -> Response {
let theme = Theme::get(ui.ctx());
let m = theme.metrics;
let c = theme.colors;
let total_w = self.length as f32 * self.box_size + (self.length - 1) as f32 * self.gap;
let desired = vec2(total_w, self.box_size);
let (rect, mut response) = ui.allocate_exact_size(desired, Sense::click());
if response.clicked() {
response.request_focus();
}
let has_focus = response.has_focus();
// Collect typed input while focused.
let mut changed = false;
if has_focus {
let mut chars: Vec<char> = self.value.chars().collect();
ui.input(|i| {
for ev in &i.events {
match ev {
egui::Event::Text(t) => {
for ch in t.chars() {
if chars.len() >= self.length {
break;
}
if self.digits_only && !ch.is_ascii_digit() {
continue;
}
if ch.is_control() {
continue;
}
chars.push(ch);
changed = true;
}
}
egui::Event::Key {
key: Key::Backspace,
pressed: true,
..
} => {
changed |= chars.pop().is_some();
}
_ => {}
}
}
});
if changed {
*self.value = chars.into_iter().take(self.length).collect();
}
}
if changed {
response.mark_changed();
}
if ui.is_rect_visible(rect) {
let radius = theme.corner();
let filled = self.value.chars().count();
let font = FontId::proportional(m.font_size_lg);
for idx in 0..self.length {
let x = rect.left() + idx as f32 * (self.box_size + self.gap);
let box_rect =
Rect::from_min_size(egui::pos2(x, rect.top()), vec2(self.box_size, self.box_size));
let painter = ui.painter();
painter.rect_filled(box_rect, radius, c.background);
let active = has_focus && idx == filled.min(self.length - 1);
let border = if active {
c.ring
} else {
c.input_border
};
painter.rect_stroke(
box_rect,
radius,
Stroke::new(if active { m.focus_ring_width } else { m.border_width }, border),
egui::StrokeKind::Inside,
);
if let Some(ch) = self.value.chars().nth(idx) {
painter.text(
box_rect.center(),
egui::Align2::CENTER_CENTER,
ch,
font.clone(),
c.foreground,
);
}
}
}
if response.hovered() {
ui.ctx().set_cursor_icon(egui::CursorIcon::Text);
}
response
}
}