diff --git a/src/widgets/text_box.rs b/src/widgets/text_box.rs index f6a157f6da1750f8d0ec74e4c7bf08e11009da67..73df76cbbcb398c68840d75ef7ef562e75f62e8b 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>>,