Hello,
I have extended the base object to enable managing widget children in a sorted manner. Kind of the emails in your inbox are kept sorted by some criteria even if new emails arrive and/or other emails are deleted.
For this, I’ve added new functions lv_XXX_create_sorted, which work similar to the existing functions, but insert at positions defined by user-defined sort-criteria instead of the end of the child list. If no sort-criteria is defined, those functions fall back to act like the old lv_XXX_create functions.
Here’s an example:
static int sort_compar (const void *btn, const void *new_obj_data)
{
char *user_data = lv_obj_get_user_data (btn);
return strcmp (user_data, new_obj_data);
}
static void add_new_item (lv_obj_t *parent, char *criteria)
{
lv_obj_t *b = lv_btn_create_sorted (parent,
NULL,
sort_compar,
criteria);
lv_obj_allocate_user_data(b, strlen(criteria)+1, criteria);
}
void create_sorted_page (void)
{
lv_obj_t *page = lv_page_create (it1010_container_main, NULL);
lv_obj_t *scrollable = lv_page_get_scrollable (evlist_page);
add_item (scrollable, "Orange");
add_item (scrollable, "Banana");
add_item (scrollable, "Pie");
add_item (scrollable, "Beer");
add_item (scrollable, "Apple");
}
The original lv_XXX_create functions still exist, but simply call the new functions with the last two arguments set to NULL.
As can be seen in this example, I’ve also added two functions to handle user_data, which are much simpler to use and also way more flexible than the currently existing user_data functions. The user_data could be anything. I prefer strings, as they are very flexible.
A drawback here is that one have to manually go to the container part of the parent widget. I’ve not yet figured how to modify the page, list (and maybe other) widgets to “forward” the create onto their proper container part.
For deleting obejects, I’ve also added a new function:
static int match_cb (const void *btn, const void *match_data)
{
char *user_data = lv_obj_get_user_data (btn);
return ! strcmp(user_data, match_data);
}
void delete_object(lv_obj_t* scrollable)
{
lv_obj_del_matching (scrollable, match_cb, "Banana", LV_DEL_MATCH_ALL);
}
Any suggestions or thoughts on this?