Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix ItemList arrow keys doing nothing after incremental search

masonliiucc1f25c6d157
diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp
index 2d292df2d67..cba31034c4a 100644
--- a/scene/gui/item_list.cpp
+++ b/scene/gui/item_list.cpp
@@ -962,20 +962,20 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) {
uint64_t diff = now - search_time_msec;
if (diff < uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec")) * 2) {
+ const String search_lower = search_string.to_lower();
for (int i = current - 1; i >= 0; i--) {
- if (CAN_SELECT(i) && items[i].text.begins_with(search_string)) {
+ if (CAN_SELECT(i) && items[i].text.to_lower().begins_with(search_lower)) {
set_current(i);
ensure_current_is_visible();
if (select_mode == SELECT_SINGLE) {
emit_signal(SceneStringName(item_selected), current);
}
-
- break;
+ accept_event();
+ return;
}
}
- accept_event();
- return;
}
+ search_string = ""; // No more matches, navigate normally.
}
if (current >= current_columns) {
@@ -1010,19 +1010,20 @@ void ItemList::gui_input(const Ref<InputEvent> &p_event) {
uint64_t diff = now - search_time_msec;
if (diff < uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec")) * 2) {
+ const String search_lower = search_string.to_lower();
for (int i = current + 1; i < items.size(); i++) {
- if (CAN_SELECT(i) && items[i].text.begins_with(search_string)) {
+ if (CAN_SELECT(i) && items[i].text.to_lower().begins_with(search_lower)) {
set_current(i);
ensure_current_is_visible();
if (select_mode == SELECT_SINGLE) {
emit_signal(SceneStringName(item_selected), current);
}
- break;
+ accept_event();
+ return;
}
}
- accept_event();
- return;
}
+ search_string = ""; // No more matches, navigate normally.
}
if (current < items.size() - current_columns) {
diff --git a/tests/scene/test_item_list.cpp b/tests/scene/test_item_list.cpp
new file mode 100644
index 00000000000..3ac2f96f90d
--- /dev/null
+++ b/tests/scene/test_item_list.cpp
@@ -0,0 +1,106 @@
+/**************************************************************************/
+/* test_item_list.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#include "tests/test_macros.h"
+
+TEST_FORCE_LINK(test_item_list)
+
+#ifndef ADVANCED_GUI_DISABLED
+
+#include "scene/gui/item_list.h"
+#include "scene/main/scene_tree.h"
+#include "scene/main/window.h"
+#include "tests/display_server_mock.h"
+
+namespace TestItemList {
+
+TEST_CASE("[SceneTree][ItemList] Arrow keys after incremental search") {
+ ItemList *list = memnew(ItemList);
+ list->set_size(Size2(200, 200));
+ list->add_item("apple");
+ list->add_item("Banana");
+ list->add_item("avocado");
+ list->add_item("cherry");
+ SceneTree::get_singleton()->get_root()->add_child(list);
+ list->grab_focus();
+
+ SUBCASE("Arrow keys move to the next match, ignoring case") {
+ // Key::A types an uppercase "A", items are lowercase.
+ SEND_GUI_KEY_EVENT(Key::A);
+ CHECK_EQ(list->get_current(), 0);
+
+ SEND_GUI_KEY_EVENT(Key::DOWN);
+ SEND_GUI_KEY_UP_EVENT(Key::DOWN);
+ CHECK_EQ(list->get_current(), 2);
+
+ SEND_GUI_KEY_EVENT(Key::UP);
+ SEND_GUI_KEY_UP_EVENT(Key::UP);
+ CHECK_EQ(list->get_current(), 0);
+ }
+
+ SUBCASE("Down arrow still navigates when there is no next match") {
+ SEND_GUI_KEY_EVENT(Key::B);
+ CHECK_EQ(list->get_current(), 1);
+
+ SEND_GUI_KEY_EVENT(Key::DOWN);
+ SEND_GUI_KEY_UP_EVENT(Key::DOWN);
+ CHECK_EQ(list->get_current(), 2);
+ }
+
+ SUBCASE("Up arrow still navigates when there is no previous match") {
+ SEND_GUI_KEY_EVENT(Key::B);
+ CHECK_EQ(list->get_current(), 1);
+
+ SEND_GUI_KEY_EVENT(Key::UP);
+ SEND_GUI_KEY_UP_EVENT(Key::UP);
+ CHECK_EQ(list->get_current(), 0);
+ }
+
+ SUBCASE("Arrow keys navigate normally once the matches run out") {
+ SEND_GUI_KEY_EVENT(Key::A);
+ SEND_GUI_KEY_EVENT(Key::DOWN);
+ SEND_GUI_KEY_UP_EVENT(Key::DOWN);
+ SEND_GUI_KEY_EVENT(Key::DOWN);
+ SEND_GUI_KEY_UP_EVENT(Key::DOWN);
+ CHECK_EQ(list->get_current(), 3);
+
+ SEND_GUI_KEY_EVENT(Key::UP);
+ SEND_GUI_KEY_UP_EVENT(Key::UP);
+ SEND_GUI_KEY_EVENT(Key::UP);
+ SEND_GUI_KEY_UP_EVENT(Key::UP);
+ CHECK_EQ(list->get_current(), 1);
+ }
+
+ memdelete(list);
+}
+
+} // namespace TestItemList
+
+#endif // ADVANCED_GUI_DISABLED