From d7b82c79668d7b31d52f6535360eff15396eade5 Mon Sep 17 00:00:00 2001 From: Fabian Blatz Date: Thu, 15 Aug 2024 09:41:22 +0200 Subject: [PATCH] feat(core): Add lv_group_get_obj_by_index method (#6589) --- src/core/lv_group.c | 20 +++++++++++++++++--- src/core/lv_group.h | 9 +++++++++ tests/src/test_cases/test_group.c | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/core/lv_group.c b/src/core/lv_group.c index 15ab1ce9b..d1cdb3176 100644 --- a/src/core/lv_group.c +++ b/src/core/lv_group.c @@ -377,6 +377,20 @@ uint32_t lv_group_get_obj_count(lv_group_t * group) return lv_ll_get_len(&group->obj_ll); } +lv_obj_t * lv_group_get_obj_by_index(lv_group_t * group, uint32_t index) +{ + uint32_t len = 0; + lv_obj_t ** obj; + + LV_LL_READ(&group->obj_ll, obj) { + if(len == index) { + return *obj; + } + len++; + } + return NULL; +} + uint32_t lv_group_get_count(void) { return lv_ll_get_len(group_ll_p); @@ -385,11 +399,11 @@ uint32_t lv_group_get_count(void) lv_group_t * lv_group_by_index(uint32_t index) { uint32_t len = 0; - void * node; + lv_group_t * group; - for(node = lv_ll_get_tail(group_ll_p); node != NULL; node = lv_ll_get_prev(group_ll_p, node)) { + LV_LL_READ_BACK(group_ll_p, group) { if(len == index) { - return (lv_group_t *) node; + return group; } len++; } diff --git a/src/core/lv_group.h b/src/core/lv_group.h index ac7d6501d..e5f267d00 100644 --- a/src/core/lv_group.h +++ b/src/core/lv_group.h @@ -213,6 +213,14 @@ bool lv_group_get_wrap(lv_group_t * group); */ uint32_t lv_group_get_obj_count(lv_group_t * group); +/** + * Get the nth object within a group + * @param group pointer to a group + * @param index index of object within the group + * @return pointer to the object + */ +lv_obj_t * lv_group_get_obj_by_index(lv_group_t * group, uint32_t index); + /** * Get the number of groups * @return number of groups @@ -221,6 +229,7 @@ uint32_t lv_group_get_count(void); /** * Get a group by its index + * @param index index of the group * @return pointer to the group */ lv_group_t * lv_group_by_index(uint32_t index); diff --git a/tests/src/test_cases/test_group.c b/tests/src/test_cases/test_group.c index 409cfc51b..2c4d55c8a 100644 --- a/tests/src/test_cases/test_group.c +++ b/tests/src/test_cases/test_group.c @@ -39,4 +39,23 @@ void test_group_by_index(void) lv_group_delete(group_2); } +void test_group_obj_by_index(void) +{ + lv_group_t * group = lv_group_create(); + + TEST_ASSERT_EQUAL_PTR(lv_group_get_obj_by_index(group, 0), NULL); + + lv_obj_t * obj_0 = lv_obj_create(NULL); + lv_obj_t * obj_1 = lv_obj_create(NULL); + lv_group_add_obj(group, obj_0); + lv_group_add_obj(group, obj_1); + + TEST_ASSERT_EQUAL_PTR(lv_group_get_obj_by_index(group, 0), obj_0); + TEST_ASSERT_EQUAL_PTR(lv_group_get_obj_by_index(group, 1), obj_1); + + lv_group_remove_obj(obj_0); + TEST_ASSERT_EQUAL_PTR(lv_group_get_obj_by_index(group, 0), obj_1); + TEST_ASSERT_EQUAL_PTR(lv_group_get_obj_by_index(group, 1), NULL); +} + #endif