From 74b30216910076243828e56b8583f66c45246030 Mon Sep 17 00:00:00 2001 From: Mateo Kuruk Miccino Date: Sun, 28 Feb 2021 17:41:11 -0300 Subject: [PATCH] LineEdit: Now double click to select a word, and triple click to select all the content --- scene/gui/line_edit.cpp | 34 ++++++++++++++++++++++++++++------ scene/gui/line_edit.h | 1 + 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 45c9b81737ff..5f72fe391e51 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -88,12 +88,34 @@ void LineEdit::_gui_input(Ref p_event) { } 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; + } else if (b->is_doubleclick()) { + // Double-click select word. + selection.enabled = true; + int beg = cursor_pos; + int end = beg; + bool symbol = beg < text.length() && is_symbol(text[beg]); + while (beg > 0 && text[beg - 1] > 32 && (symbol == is_symbol(text[beg - 1]))) { + beg--; + } + while (end < text.length() && text[end + 1] > 32 && (symbol == is_symbol(text[end + 1]))) { + end++; + } + if (end < text.length()) { + end += 1; + } + selection.begin = beg; + selection.end = end; + selection.doubleclick = true; + selection.last_dblclk = OS::get_singleton()->get_ticks_msec(); + } } selection.drag_attempt = false; diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index d1ebda9bf605..85df7cbb0359 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -104,6 +104,7 @@ class LineEdit : public Control { bool creating; bool doubleclick; bool drag_attempt; + uint64_t last_dblclk = 0; } selection; struct TextOperation {