Skip to content

Commit

Permalink
Merge pull request #85801 from HolonProduction/select-whole-words-lin…
Browse files Browse the repository at this point in the history
…e-edit

Allow dragging selection when selecting whole words in `LineEdit`
  • Loading branch information
akien-mga committed Mar 1, 2024
2 parents 9538372 + da6bacb commit 8e45506
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions scene/gui/line_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
selection.begin = words[i];
selection.end = words[i + 1];
selection.double_click = true;
selection.creating = true;
selection.start_column = caret_column;
set_caret_column(selection.end);
break;
}
Expand Down Expand Up @@ -406,6 +408,27 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
if (selection.creating) {
set_caret_at_pixel_pos(m->get_position().x);
selection_fill_at_caret();

if (selection.double_click) {
// Expand selection to whole words.

PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
for (int i = 0; i < words.size(); i = i + 2) {
if ((words[i] < selection.begin && words[i + 1] > selection.begin) || (i == words.size() - 2 && selection.begin == words[i + 1])) {
selection.begin = words[i];
}
if ((words[i] < selection.end && words[i + 1] > selection.end) || (i == words.size() - 2 && selection.end == words[i + 1])) {
selection.end = words[i + 1];
selection.enabled = true;
break;
}
}
if (caret_column < selection.start_column) {
set_caret_column(selection.begin);
} else {
set_caret_column(selection.end);
}
}
}
}

Expand Down

0 comments on commit 8e45506

Please sign in to comment.