Skip to content

Commit ec7c6e2

Browse files
committed
First attempt on utf-8 support in editbox
1 parent 266c6b3 commit ec7c6e2

2 files changed

Lines changed: 83 additions & 71 deletions

File tree

‎src/ui/widgets/editbox.rs‎

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a> Editbox<'a> {
7272
&self,
7373
input_buffer: &mut Vec<InputCharacter>,
7474
clipboard: &mut dyn crate::ui::ClipboardObject,
75-
text: &mut String,
75+
text: &mut Vec<char>,
7676
state: &mut EditboxState,
7777
) {
7878
for character in input_buffer.drain(0..) {
@@ -139,7 +139,7 @@ impl<'a> Editbox<'a> {
139139
}
140140
}
141141
} else {
142-
state.insert_string(text, clipboard);
142+
state.insert_string(text, clipboard.chars().collect());
143143
}
144144
}
145145
}
@@ -251,6 +251,7 @@ impl<'a> Editbox<'a> {
251251
}
252252

253253
pub fn ui(self, ui: &mut Ui, text: &mut String) -> bool {
254+
let mut text_vec = text.chars().collect::<Vec<_>>();
254255
let time = ui.time;
255256

256257
let context = ui.get_active_window_context();
@@ -278,19 +279,19 @@ impl<'a> Editbox<'a> {
278279
.storage_any
279280
.get_or_default::<EditboxState>(hash!(self.id, "cursor"));
280281

281-
// if text changed outside the selection range should be clamped
282-
state.clamp_selection(text);
282+
// if text changed outside, than the selection range should be clamped
283+
state.clamp_selection(&text_vec);
283284

284285
if self.select_all {
285-
state.select_all(text);
286+
state.select_all(&text_vec);
286287
}
287288

288-
if let Some(selected) = state.selected_text(text) {
289-
*context.clipboard_selection = selected.to_owned();
289+
if let Some(selected) = state.selected_text(&text_vec) {
290+
*context.clipboard_selection = selected.iter().collect();
290291
}
291292
// in case the string was updated outside of editbox
292-
if state.cursor > text.len() as u32 {
293-
state.cursor = text.len() as u32;
293+
if state.cursor > text_vec.len() as u32 {
294+
state.cursor = text_vec.len() as u32;
294295
}
295296

296297
let input_focused =
@@ -318,7 +319,7 @@ impl<'a> Editbox<'a> {
318319
self.apply_keyboard_input(
319320
&mut context.input.input_buffer,
320321
&mut *context.clipboard,
321-
text,
322+
&mut text_vec,
322323
&mut state,
323324
);
324325
}
@@ -357,9 +358,12 @@ impl<'a> Editbox<'a> {
357358

358359
let line_height = context.style.editbox_style.font_size as f32;
359360

360-
let size = vec2(150., line_height * text.split('\n').count() as f32);
361+
let size = vec2(
362+
150.,
363+
line_height * text_vec.iter().filter(|c| **c == '\n').count() as f32,
364+
);
361365

362-
// TODO: this is very weird hardcoded text margin
366+
// TODO: this is very weird hardcoded text_vec margin
363367
let pos = context.window.cursor.fit(size, Layout::Free(vec2(2., 2.)));
364368

365369
context.window.painter.clip(parent_rect);
@@ -376,7 +380,12 @@ impl<'a> Editbox<'a> {
376380
let mut y = 0.;
377381
let mut clicked = false;
378382

379-
for (n, character) in text.chars().chain(std::iter::once(' ')).enumerate() {
383+
for (n, character) in text_vec
384+
.iter()
385+
.copied()
386+
.chain(std::iter::once(' '))
387+
.enumerate()
388+
{
380389
let character = if character != '\n' && self.password {
381390
'*'
382391
} else {
@@ -439,12 +448,12 @@ impl<'a> Editbox<'a> {
439448
let cursor_on_current_line =
440449
(context.input.mouse_position.y - (pos.y + y + line_height / 2.)).abs()
441450
< line_height / 2. + 0.1;
442-
let line_end = character == '\n' || n == text.len();
451+
let line_end = character == '\n' || n == text_vec.len();
443452
let cursor_after_line_end = context.input.mouse_position.x > (pos.x + x);
444453
let clickable_character = character != '\n';
445454
let cursor_on_character =
446455
(context.input.mouse_position.x - (pos.x + x)).abs() < advance / 2.;
447-
let last_character = n == text.len();
456+
let last_character = n == text_vec.len();
448457
let cursor_below_line =
449458
(context.input.mouse_position.y - (pos.y + y + line_height)) > 0.;
450459

@@ -455,9 +464,9 @@ impl<'a> Editbox<'a> {
455464
clicked = true;
456465

457466
if context.input.click_down() {
458-
state.click_down(time, text, n as u32);
467+
state.click_down(time, &text_vec, n as u32);
459468
} else {
460-
state.click_move(text, n as u32);
469+
state.click_move(&text_vec, n as u32);
461470
}
462471
}
463472
}
@@ -470,7 +479,7 @@ impl<'a> Editbox<'a> {
470479
}
471480

472481
if context.input.click_up() && input_focused {
473-
state.click_up(text);
482+
state.click_up(&text_vec);
474483
}
475484

476485
let context = ui.get_active_window_context();
@@ -479,6 +488,7 @@ impl<'a> Editbox<'a> {
479488

480489
ui.end_window();
481490

491+
*text = text_vec.iter().collect();
482492
edited
483493
}
484494
}

0 commit comments

Comments
 (0)