From 3d1f68f2632e4c39a703f1d49b1f8e613f996eee Mon Sep 17 00:00:00 2001 From: John Mitchell <startoaster23@gmail.com> Date: Thu, 17 Nov 2022 13:15:37 -0500 Subject: [PATCH] Fixed issue where graphemes weren't respected by the text box editing. --- src/widgets/text_box.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/widgets/text_box.rs b/src/widgets/text_box.rs index f6a157f..73df76c 100644 --- a/src/widgets/text_box.rs +++ b/src/widgets/text_box.rs @@ -218,13 +218,14 @@ pub fn text_box_render( let cursor_pos = state.cursor_position; if is_backspace(c) { if !state.current_value.is_empty() { - // TODO: This doesn't respect graphemes! - state.current_value.remove(cursor_pos - 1); + let char_pos: usize = state.graphemes[0..cursor_pos - 1].iter().map(|g| g.len()).sum(); + state.current_value.remove(char_pos); state.cursor_position -= 1; } } else if !c.is_control() { - // TODO: This doesn't respect graphemes! - state.current_value.insert(cursor_pos, c); + let char_pos: usize = state.graphemes[0..cursor_pos].iter().map(|g| g.len()).sum(); + state.current_value.insert(char_pos, c); + state.cursor_position += 1; } @@ -387,6 +388,25 @@ fn set_graphemes( } } +fn get_single_grapheme_length( + font_assets: &Res<Assets<KayakFont>>, + font_mapping: &FontMapping, + style_font: &StyleProp<String>, + text: &String, +) -> usize { + let font_handle = match style_font { + StyleProp::Value(font) => font_mapping.get_handle(font.clone()).unwrap(), + _ => font_mapping.get_handle(DEFAULT_FONT.into()).unwrap(), + }; + + if let Some(font) = font_assets.get(&font_handle) { + let graphemes = font.get_graphemes(&text); + return graphemes[0].len(); + } + + 0 +} + fn set_new_cursor_position( state: &mut TextBoxState, font_assets: &Res<Assets<KayakFont>>, -- GitLab