Skip to content

Commit

Permalink
improv: all word boundaries for word selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Kneemund committed Nov 10, 2024
1 parent bcdc42c commit 959383c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
16 changes: 10 additions & 6 deletions crates/rnote-engine/src/pens/typewriter/penevents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
40 changes: 40 additions & 0 deletions crates/rnote-engine/src/strokes/textstroke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down

0 comments on commit 959383c

Please sign in to comment.