summaryrefslogtreecommitdiff
path: root/scene/gui
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2022-12-05 18:06:50 +0100
committerRémi Verschelde <rverschelde@gmail.com>2022-12-05 18:06:50 +0100
commit45c6950f1b45dfb2959b85b29078e699ec52ea6a (patch)
tree3d6ca261087ab6c45c93e1305e003dfbf6a56357 /scene/gui
parentfef6df489f117d629815f3f8fa1e290c3f5f46c9 (diff)
parent3f20108703321fcd951bcec243a6c10381677286 (diff)
Merge pull request #67123 from Rindbee/improve-caret-draw-conditions-for-LineEdit
Improve caret drawing conditions for `LineEdit`
Diffstat (limited to 'scene/gui')
-rw-r--r--scene/gui/line_edit.cpp69
-rw-r--r--scene/gui/line_edit.h3
2 files changed, 35 insertions, 37 deletions
diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp
index 8a77c39487..bd39ee3bb3 100644
--- a/scene/gui/line_edit.cpp
+++ b/scene/gui/line_edit.cpp
@@ -768,18 +768,18 @@ void LineEdit::_notification(int p_what) {
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
window_has_focus = true;
- draw_caret = true;
+ _validate_caret_can_draw();
queue_redraw();
} break;
case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
window_has_focus = false;
- draw_caret = false;
+ _validate_caret_can_draw();
queue_redraw();
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
- if (caret_blinking) {
+ if (caret_blink_enabled && caret_can_draw) {
caret_blink_timer += get_process_delta_time();
if (caret_blink_timer >= caret_blink_interval) {
@@ -790,10 +790,6 @@ void LineEdit::_notification(int p_what) {
} break;
case NOTIFICATION_DRAW: {
- if ((!has_focus() && !(menu && menu->has_focus()) && !caret_force_displayed) || !window_has_focus) {
- draw_caret = false;
- }
-
int width, height;
bool rtl = is_layout_rtl();
@@ -806,7 +802,6 @@ void LineEdit::_notification(int p_what) {
Ref<StyleBox> style = theme_cache.normal;
if (!is_editable()) {
style = theme_cache.read_only;
- draw_caret = false;
}
Ref<Font> font = theme_cache.font;
@@ -953,7 +948,7 @@ void LineEdit::_notification(int p_what) {
// Draw carets.
ofs.x = x_ofs + scroll_offset;
- if (draw_caret || drag_caret_force_displayed) {
+ if ((caret_can_draw && draw_caret) || drag_caret_force_displayed) {
// Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1).
const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale);
@@ -1063,16 +1058,7 @@ void LineEdit::_notification(int p_what) {
} break;
case NOTIFICATION_FOCUS_ENTER: {
- if (!caret_force_displayed) {
- if (caret_blink_enabled) {
- if (!caret_blinking) {
- caret_blinking = true;
- caret_blink_timer = 0.0;
- }
- } else {
- draw_caret = true;
- }
- }
+ _validate_caret_can_draw();
if (select_all_on_focus) {
if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
@@ -1093,9 +1079,7 @@ void LineEdit::_notification(int p_what) {
} break;
case NOTIFICATION_FOCUS_EXIT: {
- if (caret_blink_enabled && !caret_force_displayed) {
- caret_blinking = false;
- }
+ _validate_caret_can_draw();
if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
DisplayServer::get_singleton()->window_set_ime_position(Point2(), get_viewport()->get_window_id());
@@ -1401,21 +1385,18 @@ bool LineEdit::is_caret_blink_enabled() const {
}
void LineEdit::set_caret_blink_enabled(const bool p_enabled) {
+ if (caret_blink_enabled == p_enabled) {
+ return;
+ }
+
caret_blink_enabled = p_enabled;
set_process_internal(p_enabled);
- if (has_focus() || caret_force_displayed) {
- if (p_enabled) {
- if (!caret_blinking) {
- caret_blinking = true;
- caret_blink_timer = 0.0;
- }
- } else {
- caret_blinking = false;
- }
+ draw_caret = !caret_blink_enabled;
+ if (caret_blink_enabled) {
+ caret_blink_timer = 0.0;
}
-
- draw_caret = true;
+ queue_redraw();
notify_property_list_changed();
}
@@ -1425,8 +1406,13 @@ bool LineEdit::is_caret_force_displayed() const {
}
void LineEdit::set_caret_force_displayed(const bool p_enabled) {
+ if (caret_force_displayed == p_enabled) {
+ return;
+ }
+
caret_force_displayed = p_enabled;
- set_caret_blink_enabled(caret_blink_enabled);
+ _validate_caret_can_draw();
+
queue_redraw();
}
@@ -1442,7 +1428,7 @@ void LineEdit::set_caret_blink_interval(const float p_interval) {
void LineEdit::_reset_caret_blink_timer() {
if (caret_blink_enabled) {
draw_caret = true;
- if (has_focus()) {
+ if (caret_can_draw) {
caret_blink_timer = 0.0;
queue_redraw();
}
@@ -1451,11 +1437,19 @@ void LineEdit::_reset_caret_blink_timer() {
void LineEdit::_toggle_draw_caret() {
draw_caret = !draw_caret;
- if (is_visible_in_tree() && ((has_focus() && window_has_focus) || caret_force_displayed)) {
+ if (is_visible_in_tree() && caret_can_draw) {
queue_redraw();
}
}
+void LineEdit::_validate_caret_can_draw() {
+ if (caret_blink_enabled) {
+ draw_caret = true;
+ caret_blink_timer = 0.0;
+ }
+ caret_can_draw = editable && (window_has_focus || (menu && menu->has_focus())) && (has_focus() || caret_force_displayed);
+}
+
void LineEdit::delete_char() {
if ((text.length() <= 0) || (caret_column == 0)) {
return;
@@ -1846,6 +1840,7 @@ void LineEdit::set_editable(bool p_editable) {
}
editable = p_editable;
+ _validate_caret_can_draw();
update_minimum_size();
queue_redraw();
@@ -2523,6 +2518,8 @@ void LineEdit::_ensure_menu() {
menu->add_child(menu_ctl, false, INTERNAL_MODE_FRONT);
menu->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
+ menu->connect(SNAME("focus_entered"), callable_mp(this, &LineEdit::_validate_caret_can_draw));
+ menu->connect(SNAME("focus_exited"), callable_mp(this, &LineEdit::_validate_caret_can_draw));
menu_dir->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
menu_ctl->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
}
diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h
index e0a079b623..79db9dce21 100644
--- a/scene/gui/line_edit.h
+++ b/scene/gui/line_edit.h
@@ -172,7 +172,7 @@ private:
bool draw_caret = true;
float caret_blink_interval = 0.65;
double caret_blink_timer = 0.0;
- bool caret_blinking = false;
+ bool caret_can_draw = false;
bool pending_select_all_on_focus = false;
bool select_all_on_focus = false;
@@ -225,6 +225,7 @@ private:
void _reset_caret_blink_timer();
void _toggle_draw_caret();
+ void _validate_caret_can_draw();
void clear_internal();