Skip to content

Commit

Permalink
First attempt on utf-8 support in editbox
Browse files Browse the repository at this point in the history
  • Loading branch information
not-fl3 committed Sep 2, 2024
1 parent 266c6b3 commit f074650
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 71 deletions.
41 changes: 23 additions & 18 deletions src/ui/widgets/editbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a> Editbox<'a> {
&self,
input_buffer: &mut Vec<InputCharacter>,
clipboard: &mut dyn crate::ui::ClipboardObject,
text: &mut String,
text: &mut Vec<char>,
state: &mut EditboxState,
) {
for character in input_buffer.drain(0..) {
Expand Down Expand Up @@ -139,7 +139,7 @@ impl<'a> Editbox<'a> {
}
}
} else {
state.insert_string(text, clipboard);
state.insert_string(text, clipboard.chars().collect());
}
}
}
Expand Down Expand Up @@ -251,6 +251,7 @@ impl<'a> Editbox<'a> {
}

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

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

// if text changed outside the selection range should be clamped
state.clamp_selection(text);
// if text changed outside, than the selection range should be clamped
state.clamp_selection(&text_vec);

if self.select_all {
state.select_all(text);
state.select_all(&text_vec);
}

if let Some(selected) = state.selected_text(text) {
*context.clipboard_selection = selected.to_owned();
if let Some(selected) = state.selected_text(&text_vec) {
*context.clipboard_selection = selected.iter().collect();
}
// in case the string was updated outside of editbox
if state.cursor > text.len() as u32 {
state.cursor = text.len() as u32;
if state.cursor > text_vec.len() as u32 {
state.cursor = text_vec.len() as u32;
}

let input_focused =
Expand Down Expand Up @@ -318,7 +319,7 @@ impl<'a> Editbox<'a> {
self.apply_keyboard_input(
&mut context.input.input_buffer,
&mut *context.clipboard,
text,
&mut text_vec,
&mut state,
);
}
Expand Down Expand Up @@ -357,9 +358,12 @@ impl<'a> Editbox<'a> {

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

let size = vec2(150., line_height * text.split('\n').count() as f32);
let size = vec2(
150.,
line_height * text_vec.iter().filter(|c| **c == '\n').count() as f32,
);

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

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

for (n, character) in text.chars().chain(std::iter::once(' ')).enumerate() {
for (n, character) in text_vec.iter().copied().chain(std::iter::once(' ')).enumerate() {
let character = if character != '\n' && self.password {
'*'
} else {
Expand Down Expand Up @@ -439,12 +443,12 @@ impl<'a> Editbox<'a> {
let cursor_on_current_line =
(context.input.mouse_position.y - (pos.y + y + line_height / 2.)).abs()
< line_height / 2. + 0.1;
let line_end = character == '\n' || n == text.len();
let line_end = character == '\n' || n == text_vec.len();
let cursor_after_line_end = context.input.mouse_position.x > (pos.x + x);
let clickable_character = character != '\n';
let cursor_on_character =
(context.input.mouse_position.x - (pos.x + x)).abs() < advance / 2.;
let last_character = n == text.len();
let last_character = n == text_vec.len();
let cursor_below_line =
(context.input.mouse_position.y - (pos.y + y + line_height)) > 0.;

Expand All @@ -455,9 +459,9 @@ impl<'a> Editbox<'a> {
clicked = true;

if context.input.click_down() {
state.click_down(time, text, n as u32);
state.click_down(time, &text_vec, n as u32);
} else {
state.click_move(text, n as u32);
state.click_move(&text_vec, n as u32);
}
}
}
Expand All @@ -470,7 +474,7 @@ impl<'a> Editbox<'a> {
}

if context.input.click_up() && input_focused {
state.click_up(text);
state.click_up(&text_vec);
}

let context = ui.get_active_window_context();
Expand All @@ -479,6 +483,7 @@ impl<'a> Editbox<'a> {

ui.end_window();

*text = text_vec.iter().collect();
edited
}
}
Expand Down
Loading

0 comments on commit f074650

Please sign in to comment.