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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use base::basic_types::*;
use base::{BoolModifyMode, Cursor, StyleModifier, Window};
use input::{Editable, Navigatable, OperationResult, Writable};
use unicode_segmentation::UnicodeSegmentation;
use widget::{
count_grapheme_clusters, text_width, Blink, Demand, Demand2D, RenderingHints, Widget,
};
pub struct LineEdit {
text: String,
cursor_pos: usize,
cursor_style_active_blink_on: StyleModifier,
cursor_style_active_blink_off: StyleModifier,
cursor_style_inactive: StyleModifier,
}
impl LineEdit {
pub fn new() -> Self {
Self::with_cursor_styles(
StyleModifier::new().invert(BoolModifyMode::Toggle),
StyleModifier::new(),
StyleModifier::new().underline(true),
)
}
pub fn with_cursor_styles(
active_blink_on: StyleModifier,
active_blink_off: StyleModifier,
inactive: StyleModifier,
) -> Self {
LineEdit {
text: String::new(),
cursor_pos: 0,
cursor_style_active_blink_on: active_blink_on,
cursor_style_active_blink_off: active_blink_off,
cursor_style_inactive: inactive,
}
}
pub fn get(&self) -> &str {
&self.text
}
pub fn set(&mut self, text: &str) {
self.text = text.to_owned();
self.move_cursor_to_end_of_line();
}
pub fn move_cursor_to_end_of_line(&mut self) {
self.cursor_pos = count_grapheme_clusters(&self.text) as usize;
}
pub fn move_cursor_to_beginning_of_line(&mut self) {
self.cursor_pos = 0;
}
pub fn move_cursor_right(&mut self) -> Result<(), ()> {
let new_pos = self.cursor_pos + 1;
if new_pos <= count_grapheme_clusters(&self.text) as usize {
self.cursor_pos = new_pos;
Ok(())
} else {
Err(())
}
}
pub fn move_cursor_left(&mut self) -> Result<(), ()> {
if self.cursor_pos > 0 {
self.cursor_pos -= 1;
Ok(())
} else {
Err(())
}
}
pub fn insert(&mut self, text: &str) {
self.text = {
let grapheme_iter = self.text.graphemes(true);
grapheme_iter
.clone()
.take(self.cursor_pos)
.chain(Some(text))
.chain(grapheme_iter.skip(self.cursor_pos))
.collect()
};
}
fn erase_symbol_at(&mut self, pos: usize) -> Result<(), ()> {
if pos < self.text.len() {
self.text = self
.text
.graphemes(true)
.enumerate()
.filter_map(|(i, s)| if i != pos { Some(s) } else { None })
.collect();
Ok(())
} else {
Err(())
}
}
}
impl Widget for LineEdit {
fn space_demand(&self) -> Demand2D {
Demand2D {
width: Demand::at_least(text_width(&self.text) + 1),
height: Demand::exact(1),
}
}
fn draw(&self, mut window: Window, hints: RenderingHints) {
let (maybe_cursor_pos_offset, maybe_after_cursor_offset) = {
let mut grapheme_indices = self.text.grapheme_indices(true);
let cursor_cluster = grapheme_indices.nth(self.cursor_pos as usize);
let next_cluster = grapheme_indices.next();
(
cursor_cluster.map(|c: (usize, &str)| c.0),
next_cluster.map(|c: (usize, &str)| c.0),
)
};
let right_padding = 1;
let text_width_before_cursor =
text_width(&self.text[0..maybe_after_cursor_offset.unwrap_or(self.text.len())]);
let draw_cursor_start_pos = ::std::cmp::min(
ColIndex::new(0),
(window.get_width() - text_width_before_cursor - right_padding).from_origin(),
);
let cursor_style = match (hints.active, hints.blink) {
(true, Blink::On) => self.cursor_style_active_blink_on,
(true, Blink::Off) => self.cursor_style_active_blink_off,
(false, _) => self.cursor_style_inactive,
};
let mut cursor = Cursor::new(&mut window).position(draw_cursor_start_pos, RowIndex::new(0));
if let Some(cursor_pos_offset) = maybe_cursor_pos_offset {
let (until_cursor, from_cursor) = self.text.split_at(cursor_pos_offset);
cursor.write(until_cursor);
if let Some(after_cursor_offset) = maybe_after_cursor_offset {
let (cursor_str, after_cursor) =
from_cursor.split_at(after_cursor_offset - cursor_pos_offset);
{
let mut cursor = cursor.save().style_modifier();
cursor.apply_style_modifier(cursor_style);
cursor.write(cursor_str);
}
cursor.write(after_cursor);
} else {
let mut cursor = cursor.save().style_modifier();
cursor.apply_style_modifier(cursor_style);
cursor.write(from_cursor);
}
} else {
cursor.write(&self.text);
{
let mut cursor = cursor.save().style_modifier();
cursor.apply_style_modifier(cursor_style);
cursor.write(" ");
}
}
}
}
impl Navigatable for LineEdit {
fn move_up(&mut self) -> OperationResult {
Err(())
}
fn move_down(&mut self) -> OperationResult {
Err(())
}
fn move_left(&mut self) -> OperationResult {
self.move_cursor_left()
}
fn move_right(&mut self) -> OperationResult {
self.move_cursor_right()
}
}
impl Writable for LineEdit {
fn write(&mut self, c: char) -> OperationResult {
if c == '\n' {
Err(())
} else {
self.insert(&c.to_string());
self.move_cursor_right()
}
}
}
impl Editable for LineEdit {
fn delete_forwards(&mut self) -> OperationResult {
let to_erase = self.cursor_pos;
self.erase_symbol_at(to_erase)
}
fn delete_backwards(&mut self) -> OperationResult {
if self.cursor_pos > 0 {
let to_erase = self.cursor_pos - 1;
let _ = self.erase_symbol_at(to_erase);
let _ = self.move_cursor_left();
Ok(())
} else {
Err(())
}
}
fn go_to_beginning_of_line(&mut self) -> OperationResult {
self.move_cursor_to_beginning_of_line();
Ok(())
}
fn go_to_end_of_line(&mut self) -> OperationResult {
self.move_cursor_to_end_of_line();
Ok(())
}
fn clear(&mut self) -> OperationResult {
if self.text.is_empty() {
Err(())
} else {
self.text.clear();
self.cursor_pos = 0;
Ok(())
}
}
}