Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LineEdit: Now double click to select a word, and triple click to sele… #46524

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions scene/gui/line_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,30 @@ void LineEdit::_gui_input(Ref<InputEvent> p_event) {
selection.creating = true;

} else {
if (b->is_doubleclick() && selecting_enabled) {
selection.enabled = true;
selection.begin = 0;
selection.end = text.length();
selection.doubleclick = true;
if (selecting_enabled) {
if (!b->is_doubleclick() && (OS::get_singleton()->get_ticks_msec() - selection.last_dblclk) < 600) {
// Triple-click select all.
selection.enabled = true;
selection.begin = 0;
selection.end = text.length();
selection.doubleclick = true;
selection.last_dblclk = 0;
cursor_pos = selection.begin;
} else if (b->is_doubleclick()) {
// Double-click select word.
Vector<Vector2i> words = TS->shaped_text_get_word_breaks(text_rid);
for (int i = 0; i < words.size(); i++) {
if (words[i].x < cursor_pos && words[i].y > cursor_pos) {
selection.enabled = true;
selection.begin = words[i].x;
selection.end = words[i].y;
selection.doubleclick = true;
selection.last_dblclk = OS::get_singleton()->get_ticks_msec();
kuruk-mm marked this conversation as resolved.
Show resolved Hide resolved
cursor_pos = selection.end;
break;
}
}
}
}

selection.drag_attempt = false;
Expand Down
1 change: 1 addition & 0 deletions scene/gui/line_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class LineEdit : public Control {
bool creating = false;
bool doubleclick = false;
bool drag_attempt = false;
uint64_t last_dblclk = 0;
} selection;

struct TextOperation {
Expand Down
27 changes: 9 additions & 18 deletions scene/gui/text_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,25 +412,16 @@ void TextEdit::_update_selection_mode_word() {
_get_mouse_pos(Point2i(mp.x, mp.y), row, col);

String line = text[row];
int beg = CLAMP(col, 0, line.length());
// If its the first selection and on whitespace make sure we grab the word instead.
if (!selection.active) {
while (beg > 0 && line[beg] <= 32) {
beg--;
}
}
int cursor_pos = CLAMP(col, 0, line.length());
int beg = cursor_pos;
int end = beg;
bool symbol = beg < line.length() && _is_symbol(line[beg]);

// Get the word end and begin points.
while (beg > 0 && line[beg - 1] > 32 && (symbol == _is_symbol(line[beg - 1]))) {
beg--;
}
while (end < line.length() && line[end + 1] > 32 && (symbol == _is_symbol(line[end + 1]))) {
end++;
}
if (end < line.length()) {
end += 1;
Vector<Vector2i> words = TS->shaped_text_get_word_breaks(text.get_line_data(row)->get_rid());
for (int i = 0; i < words.size(); i++) {
if (words[i].x < cursor_pos && words[i].y > cursor_pos) {
beg = words[i].x;
end = words[i].y;
break;
}
}

// Initial selection.
Expand Down