Dropdown dynamic options [Enhancement]

Most of my drop down lists are created dynamically, and it does not make sense to allocate static memory for the storage of the options. I have to guess the maximum size of the options and have a list sitting in memory for each drop down, even it is it not currently used. I could handle dynamic memory allocation outside the lv functions but that would just be duplication.

I propose adding functionality to the dropdown list to allow dynamical option storage in a similar way that label uses the static_txt flag.

The first step will be to add another function to set the options with a string which is stored in allocated memory:

void lv_dropdown_set_options(lv_obj_t * ddlist, const char * options);
void lv_dropdown_set_options_dynamic(lv_obj_t * ddlist, const char * options);

The first decision is whether we should follow the convention of the label and default to dynamic storage or keep the current default of static storage

void lv_dropdown_set_options(lv_obj_t * ddlist, const char * options);
void lv_dropdown_set_static_options(lv_obj_t * ddlist, const char * options);

Then the next logical thing would be to add a function which adds another option to the list. That would simplify the loop I use to create the options string. Not that efficient, since it would realloc for even added option, but I don’t think that is a big concern.

void lv_dropdown_add_option(lv_obj_t * ddlist, const char * option);

Also this would not work for static option lists, so depending on the previous decision the function might be

void lv_dropdown_add_option_dynamic(lv_obj_t * ddlist, const char * option);

My preference is to default to dynamic storage.

Any comments?

Done with the default set to dynamic. The only other change is that the drop down options default to Option1\nOption2\Option3, which needed to be removed for lv_dropdown_add_option().

void lv_dropdown_set_options(lv_obj_t * ddlist, const char * options);
void lv_dropdown_set_static_options(lv_obj_t * ddlist, const char * options);
void lv_dropdown_add_option(lv_obj_t * ddlist, const char * option, int pos);

Untested with bidi text.

The API looks good! Can you send a pull request?

1 Like

Ok. Someone else will have to look at the bidi handling because I cannot test that.

I would love to test it out also!

It should be merged soon otherwise this fork has the code

The PR is merged so I mark this topic as solved.

Thank you for the update!

It looks good! It would be great to have a lv_dropdown_remove_option function as well.

Hmm, also when changing the options, it should trigger a redraw of the dropdown.

I am using a button for testing, when the button is clicked, the options are changed. The options aren’t redrawn until I click on the dropdown list itself.

static void btn_event_handler(lv_obj_t * obj, lv_event_t event)
{
if(event == LV_EVENT_PRESSED) {
    printf("Clicked\n");
    lv_dropdown_set_options(ddlist, "Click0\nClick1\n");
    lv_dropdown_set_selected(ddlist, 0);
}
else if(event == LV_EVENT_RELEASED) {
    printf("Released\n");
    lv_dropdown_set_options(ddlist, "Release0\nRelease1\n");
    lv_dropdown_set_selected(ddlist, 1);
}
else if(event == LV_EVENT_VALUE_CHANGED) {
    printf("Toggled\n");

}
}

EDIT: I added lv_obj_invalidate(ddlist); to the functions for add_option and set_options so it redraws.

I agree. A Pull request would be welcome if you have time for it.

Thank you, I’ve added the invalidation