From 56c44bfdc412ccac6e291e5d035c48696c96be2c Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Thu, 13 Feb 2025 12:26:22 +0100 Subject: [PATCH] refactor(objid): rename lv_obj_get_child_by_id to lv_obj_find_by_id for consistency --- docs/src/details/auxiliary-modules/obj_id.rst | 6 +-- src/core/lv_obj.c | 4 +- src/core/lv_obj.h | 2 +- src/lv_api_map_v9_1.h | 2 + src/others/xml/parsers/lv_xml_obj_parser.c | 4 +- src/widgets/dropdown/lv_dropdown.c | 4 +- tests/src/test_cases/widgets/test_obj_tree.c | 48 +++++++++---------- tests/src/test_cases/widgets/test_objid.c | 4 +- tests/src/test_cases/xml/test_xml_general.c | 6 ++- 9 files changed, 44 insertions(+), 36 deletions(-) diff --git a/docs/src/details/auxiliary-modules/obj_id.rst b/docs/src/details/auxiliary-modules/obj_id.rst index 2306af349..df9626bc7 100644 --- a/docs/src/details/auxiliary-modules/obj_id.rst +++ b/docs/src/details/auxiliary-modules/obj_id.rst @@ -24,7 +24,7 @@ Once enabled, several things change: - these two API functions become available: - :cpp:expr:`lv_obj_get_id(widget)`, - - :cpp:expr:`lv_obj_get_child_by_id(widget, id)`; + - :cpp:expr:`lv_obj_find_by_id(widget, id)`; - several more Widget-ID-related API functions become available if :c:macro:`LV_USE_OBJ_ID_BUILTIN` is non-zero (more on this below); @@ -182,7 +182,7 @@ state of the Widget Tree when :cpp:expr:`lv_obj_dump_tree(widget)` was called. For example, if a pointer to a deleted Widget is stored in a Timer's :cpp:expr:`timer->user_data` field when the timer event callback is called, attempted -use of that pointer will likly cause a crash because the pointer is not valid any +use of that pointer will likely cause a crash because the pointer is not valid any more. However, a timely dump of the Widget Tree right before that point will show that the Widget no longer exists. @@ -190,7 +190,7 @@ that the Widget no longer exists. Find child by ID ---------------- -:cpp:expr:`lv_obj_get_child_by_id(widget, id)` will perform a recursive walk through +:cpp:expr:`lv_obj_find_by_id(widget, id)` will perform a recursive walk through ``widget``\ 's children and return the first child encountered having the given ID. diff --git a/src/core/lv_obj.c b/src/core/lv_obj.c index da4c1decd..ac9eb8e67 100644 --- a/src/core/lv_obj.c +++ b/src/core/lv_obj.c @@ -436,7 +436,7 @@ void * lv_obj_get_id(const lv_obj_t * obj) return obj->id; } -lv_obj_t * lv_obj_get_child_by_id(const lv_obj_t * obj, const void * id) +lv_obj_t * lv_obj_find_by_id(const lv_obj_t * obj, const void * id) { LV_LOG_WARN("DEPRECATED: IDs are used only to print the widget trees. To find a widget use obj_name"); @@ -453,7 +453,7 @@ lv_obj_t * lv_obj_get_child_by_id(const lv_obj_t * obj, const void * id) /*Search children*/ for(i = 0; i < child_cnt; i++) { lv_obj_t * child = obj->spec_attr->children[i]; - lv_obj_t * found = lv_obj_get_child_by_id(child, id); + lv_obj_t * found = lv_obj_find_by_id(child, id); if(found != NULL) return found; } diff --git a/src/core/lv_obj.h b/src/core/lv_obj.h index 698748383..b63f22ef9 100644 --- a/src/core/lv_obj.h +++ b/src/core/lv_obj.h @@ -407,7 +407,7 @@ void * lv_obj_get_id(const lv_obj_t * obj); * @param id the id of the child object * @return pointer to the child object or NULL if not found */ -lv_obj_t * lv_obj_get_child_by_id(const lv_obj_t * obj, const void * id); +lv_obj_t * lv_obj_find_by_id(const lv_obj_t * obj, const void * id); /** * Assign id to object if not previously assigned. diff --git a/src/lv_api_map_v9_1.h b/src/lv_api_map_v9_1.h index ec1f4188e..217c86cab 100644 --- a/src/lv_api_map_v9_1.h +++ b/src/lv_api_map_v9_1.h @@ -77,6 +77,8 @@ extern "C" { #define _lv_disp_refr_timer lv_disp_refr_timer #define _lv_disp_get_refr_timer lv_disp_get_refr_timer +#define lv_obj_get_child_by_id lv_obj_find_by_id + #define _lv_inv_area lv_inv_area #define lv_chart_set_all_value lv_chart_set_all_values #define lv_calendar_set_showed_date lv_calendar_set_month_shown diff --git a/src/others/xml/parsers/lv_xml_obj_parser.c b/src/others/xml/parsers/lv_xml_obj_parser.c index 13b27a886..851e92fb3 100644 --- a/src/others/xml/parsers/lv_xml_obj_parser.c +++ b/src/others/xml/parsers/lv_xml_obj_parser.c @@ -57,7 +57,9 @@ void lv_xml_obj_apply(lv_xml_parser_state_t * state, const char ** attrs) for(int i = 0; attrs[i]; i += 2) { const char * name = attrs[i]; const char * value = attrs[i + 1]; - +#if LV_USE_OBJ_NAME + if(lv_streq("name", name)) lv_obj_set_name(item, value); +#endif if(lv_streq("x", name)) lv_obj_set_x(item, lv_xml_to_size(value)); else if(lv_streq("y", name)) lv_obj_set_y(item, lv_xml_to_size(value)); else if(lv_streq("width", name)) lv_obj_set_width(item, lv_xml_to_size(value)); diff --git a/src/widgets/dropdown/lv_dropdown.c b/src/widgets/dropdown/lv_dropdown.c index 83e1d6120..c91367006 100644 --- a/src/widgets/dropdown/lv_dropdown.c +++ b/src/widgets/dropdown/lv_dropdown.c @@ -122,7 +122,7 @@ const lv_obj_class_t lv_dropdown_class = { .editable = LV_OBJ_CLASS_EDITABLE_TRUE, .group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE, .base_class = &lv_obj_class, - .name = "dropdown", + .name = "lv_dropdown", #if LV_USE_OBJ_PROPERTY .prop_index_start = LV_PROPERTY_DROPDOWN_START, .prop_index_end = LV_PROPERTY_DROPDOWN_END, @@ -142,7 +142,7 @@ const lv_obj_class_t lv_dropdownlist_class = { .event_cb = lv_dropdown_list_event, .instance_size = sizeof(lv_dropdown_list_t), .base_class = &lv_obj_class, - .name = "dropdown-list", + .name = "lv_dropdown-list", }; /********************** diff --git a/tests/src/test_cases/widgets/test_obj_tree.c b/tests/src/test_cases/widgets/test_obj_tree.c index 0e5279619..115da157c 100644 --- a/tests/src/test_cases/widgets/test_obj_tree.c +++ b/tests/src/test_cases/widgets/test_obj_tree.c @@ -196,28 +196,28 @@ void test_obj_get_by_name(void) lv_obj_set_flex_flow(lv_screen_active(), LV_FLEX_FLOW_ROW); - lv_obj_t * cont1 = lv_obj_create(lv_screen_active()); - lv_obj_set_name_static(cont1, "first_static"); - lv_obj_set_name(cont1, "first_non_static"); - lv_obj_set_name_static(cont1, "first"); + lv_obj_t * cont0 = lv_obj_create(lv_screen_active()); + lv_obj_set_name_static(cont0, "zero_static"); + lv_obj_set_name(cont0, "zero_non_static"); + lv_obj_set_name_static(cont0, "zero"); + lv_obj_t * cont1 = lv_obj_create(lv_screen_active()); + lv_obj_set_flex_flow(cont1, LV_FLEX_FLOW_COLUMN); + lv_obj_set_name(cont1, "first_non_static"); + lv_obj_set_name(cont1, "first"); lv_obj_t * cont2 = lv_obj_create(lv_screen_active()); - lv_obj_set_flex_flow(cont2, LV_FLEX_FLOW_COLUMN); - lv_obj_set_name(cont2, "second_non_static"); - lv_obj_set_name(cont2, "second"); lv_obj_t * cont3 = lv_obj_create(lv_screen_active()); - lv_obj_t * cont4 = lv_obj_create(lv_screen_active()); - lv_obj_set_name_static(cont4, "forth_static"); - lv_obj_set_name_static(cont4, "forth"); + lv_obj_set_name_static(cont3, "third_static"); + lv_obj_set_name_static(cont3, "third"); lv_obj_t * root_label = lv_label_create(lv_screen_active()); lv_label_set_text(root_label, "Root"); lv_obj_set_name(root_label, "my_label"); - lv_slider_create(cont2); + lv_slider_create(cont1); - lv_obj_t * btn = lv_button_create(cont2); - lv_switch_create(cont2); + lv_obj_t * btn = lv_button_create(cont1); + lv_switch_create(cont1); lv_obj_t * hello_label = lv_label_create(btn); lv_label_set_text(hello_label, "Hello"); @@ -230,28 +230,28 @@ void test_obj_get_by_name(void) * Get by name *------------*/ - found_obj = lv_obj_get_child_by_name(lv_screen_active(), "second"); - TEST_ASSERT_EQUAL(cont2, found_obj); + found_obj = lv_obj_get_child_by_name(lv_screen_active(), "first"); + TEST_ASSERT_EQUAL(cont1, found_obj); - found_obj = lv_obj_get_child_by_name(lv_screen_active(), "lv_obj_3"); - TEST_ASSERT_EQUAL(cont3, found_obj); + found_obj = lv_obj_get_child_by_name(lv_screen_active(), "lv_obj_2"); + TEST_ASSERT_EQUAL(cont2, found_obj); found_obj = lv_obj_get_child_by_name(lv_screen_active(), "fifth"); TEST_ASSERT_EQUAL(NULL, found_obj); - found_obj = lv_obj_get_child_by_name(lv_screen_active(), "second/lv_button_1/my_label"); + found_obj = lv_obj_get_child_by_name(lv_screen_active(), "first/lv_button_0/my_label"); TEST_ASSERT_EQUAL(hello_label, found_obj); /*"hello" label doesn't have children*/ - found_obj = lv_obj_get_child_by_name(lv_screen_active(), "second/lv_button_1/my_label/no_child"); + found_obj = lv_obj_get_child_by_name(lv_screen_active(), "first/lv_button_0/my_label/no_child"); TEST_ASSERT_EQUAL(NULL, found_obj); /*Non existing child*/ - found_obj = lv_obj_get_child_by_name(lv_screen_active(), "second/lv_button_1/other_label"); + found_obj = lv_obj_get_child_by_name(lv_screen_active(), "first/lv_button_0/other_label"); TEST_ASSERT_EQUAL(NULL, found_obj); /*Extra slash*/ - found_obj = lv_obj_get_child_by_name(lv_screen_active(), "second//lv_button_1/other_label"); + found_obj = lv_obj_get_child_by_name(lv_screen_active(), "first//lv_button_0/other_label"); TEST_ASSERT_EQUAL(NULL, found_obj); /*Empty*/ @@ -262,13 +262,13 @@ void test_obj_get_by_name(void) * Find by name *------------*/ - found_obj = lv_obj_find_by_name(lv_screen_active(), "lv_obj_3"); - TEST_ASSERT_EQUAL(cont3, found_obj); + found_obj = lv_obj_find_by_name(lv_screen_active(), "lv_obj_2"); + TEST_ASSERT_EQUAL(cont2, found_obj); found_obj = lv_obj_find_by_name(lv_screen_active(), "my_label"); TEST_ASSERT_EQUAL(root_label, found_obj); - found_obj = lv_obj_find_by_name(cont2, "my_label"); + found_obj = lv_obj_find_by_name(cont1, "my_label"); TEST_ASSERT_EQUAL(hello_label, found_obj); } diff --git a/tests/src/test_cases/widgets/test_objid.c b/tests/src/test_cases/widgets/test_objid.c index adc980b1b..50637c3c3 100644 --- a/tests/src/test_cases/widgets/test_objid.c +++ b/tests/src/test_cases/widgets/test_objid.c @@ -37,8 +37,8 @@ void test_obj_id_get_child(void) lv_obj_set_id(child, (void *)(lv_uintptr_t)1); lv_obj_set_id(grandchild, (void *)(lv_uintptr_t)2); - TEST_ASSERT_EQUAL_PTR(child, lv_obj_get_child_by_id(NULL, (void *)(lv_uintptr_t)1)); - TEST_ASSERT_EQUAL_PTR(grandchild, lv_obj_get_child_by_id(NULL, (void *)(lv_uintptr_t)2)); + TEST_ASSERT_EQUAL_PTR(child, lv_obj_find_by_id(NULL, (void *)(lv_uintptr_t)1)); + TEST_ASSERT_EQUAL_PTR(grandchild, lv_obj_find_by_id(NULL, (void *)(lv_uintptr_t)2)); } #endif diff --git a/tests/src/test_cases/xml/test_xml_general.c b/tests/src/test_cases/xml/test_xml_general.c index 3a5b47f73..e0286200b 100644 --- a/tests/src/test_cases/xml/test_xml_general.c +++ b/tests/src/test_cases/xml/test_xml_general.c @@ -35,12 +35,16 @@ void test_xml_widget_direct_create(void) "range_max", "100", "mode", "symmetrical", "value", "50", + "name", "my_slider", NULL, NULL, }; slider = lv_xml_create(lv_screen_active(), "lv_slider", attrs); lv_obj_set_pos(slider, 10, 200); - +#if LV_USE_OBJ_NAME + lv_obj_t * same_slider = lv_obj_get_child_by_name(lv_screen_active(), "my_slider"); + TEST_ASSERT_EQUAL_PTR(slider, same_slider); +#endif TEST_ASSERT_EQUAL_SCREENSHOT("xml/widget_create_1.png"); }