From 959383cef69b07289e04a43f154db379f226d269 Mon Sep 17 00:00:00 2001 From: Kneemund Date: Sun, 10 Nov 2024 01:05:23 +0100 Subject: [PATCH] improv: all word boundaries for word selection --- .../src/pens/typewriter/penevents.rs | 16 +++++--- crates/rnote-engine/src/strokes/textstroke.rs | 40 +++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/crates/rnote-engine/src/pens/typewriter/penevents.rs b/crates/rnote-engine/src/pens/typewriter/penevents.rs index 5ed299e223..379f6ebea0 100644 --- a/crates/rnote-engine/src/pens/typewriter/penevents.rs +++ b/crates/rnote-engine/src/pens/typewriter/penevents.rs @@ -226,12 +226,16 @@ impl Typewriter { SelectionMode::Word(start, end) => { let mouse_position = cursor.cur_cursor(); - if mouse_position < *start { + if mouse_position <= *start { selection_cursor.set_cursor(*end); - textstroke.move_cursor_word_back(cursor); - } else if mouse_position > *end { + textstroke + .move_cursor_word_boundary_back(cursor); + } else if mouse_position >= *end { selection_cursor.set_cursor(*start); - textstroke.move_cursor_word_forward(cursor); + textstroke + .move_cursor_word_boundary_forward( + cursor, + ); } else { selection_cursor.set_cursor(*start); cursor.set_cursor(*end); @@ -1291,10 +1295,10 @@ impl Typewriter { if let Some(Stroke::TextStroke(ref mut textstroke)) = engine_view.store.get_stroke_mut(*stroke_key) { - textstroke.move_cursor_word_forward(cursor); + textstroke.move_cursor_word_boundary_forward(cursor); let mut selection_cursor = cursor.clone(); - textstroke.move_cursor_word_back(&mut selection_cursor); + textstroke.move_cursor_word_boundary_back(&mut selection_cursor); *modify_state = ModifyState::Selecting { mode: SelectionMode::Word( diff --git a/crates/rnote-engine/src/strokes/textstroke.rs b/crates/rnote-engine/src/strokes/textstroke.rs index 83816854d5..09d1e910ad 100644 --- a/crates/rnote-engine/src/strokes/textstroke.rs +++ b/crates/rnote-engine/src/strokes/textstroke.rs @@ -840,6 +840,22 @@ impl TextStroke { current_char_index } + fn get_prev_word_boundary_index(&self, current_char_index: usize) -> usize { + for (start_index, word) in self.text.unicode_word_indices().rev() { + let end_index = start_index + word.len(); + + if end_index < current_char_index { + return end_index; + } + + if start_index < current_char_index { + return start_index; + } + } + + current_char_index + } + fn get_next_word_end_index(&self, current_char_index: usize) -> usize { for (start_index, word) in self.text.unicode_word_indices() { let end_index = start_index + word.len(); @@ -852,6 +868,22 @@ impl TextStroke { current_char_index } + fn get_next_word_boundary_index(&self, current_char_index: usize) -> usize { + for (start_index, word) in self.text.unicode_word_indices() { + if start_index >= current_char_index { + return start_index; + } + + let end_index = start_index + word.len(); + + if end_index >= current_char_index { + return end_index; + } + } + + current_char_index + } + pub fn move_cursor_back(&self, cursor: &mut GraphemeCursor) { // Cant fail, we are providing the entire text cursor.prev_boundary(&self.text, 0).unwrap(); @@ -866,10 +898,18 @@ impl TextStroke { cursor.set_cursor(self.get_prev_word_start_index(cursor.cur_cursor())); } + pub fn move_cursor_word_boundary_back(&self, cursor: &mut GraphemeCursor) { + cursor.set_cursor(self.get_prev_word_boundary_index(cursor.cur_cursor())); + } + pub fn move_cursor_word_forward(&self, cursor: &mut GraphemeCursor) { cursor.set_cursor(self.get_next_word_end_index(cursor.cur_cursor())); } + pub fn move_cursor_word_boundary_forward(&self, cursor: &mut GraphemeCursor) { + cursor.set_cursor(self.get_next_word_boundary_index(cursor.cur_cursor())); + } + pub fn move_cursor_text_start(&self, cursor: &mut GraphemeCursor) { cursor.set_cursor(0); }