Hello all,
I am trying to create a popup with a context-menu. For this, I use a container which contains
-
a button (acts as menu-header and also to withdraw the popup)
-
a page, which contains the menu entries.
This works fine as long as I have a fixed menu size: when I have more entries than fit into the size, scrollbar of the page appears. But a fixed size is unfortunate, since there can be plenty of empty space if there are less entries than fit into the menu.
So I’d like to “shrink” the menu if there are less entries than fit into the menu. But when I use auto-fit, then the page will grow as large as it needs to cover all the children. The result is that the menu gets much bigger than the display and the page won’t show the scroll bar.
So how do I have a dynamic size of the popup but also ensure it won’t grow bigger than a given size?
Here’s the code:
void create_menu (int entries)
{
lv_obj_t *menu_popup = lv_cont_create (lv_scr_act (), NULL);
lv_cont_set_layout (menu_popup, LV_LAYOUT_COLUMN_MID);
lv_obj_t *menu_header = lv_btn_create (menu_popup, NULL);
lv_obj_t *menu_page = lv_page_create (menu_popup, NULL);
lv_page_set_scrl_layout (menu_page, LV_LAYOUT_COLUMN_MID);
lv_obj_t *scrollable = lv_page_get_scrollable (menu_page);
for (int i=0; i<entries; i++) {
lv_obj_t *chld = NULL;
lv_obj_t *menu_btn = lv_btn_create (menu_page, NULL);
}
/* Omit auto-fit if there are many entries */
if (entries < 6)
lv_cont_set_fit2 (menu_page, LV_FIT_NONE, LV_FIT_TIGHT);
/* Try to reduce the size if it got bigger for whatever reason */
if (lv_obj_get_height (menu_page) > 200)
lv_obj_set_height (menu_page, 200);
/* Also omit auto-fit for the container if there are many entries */
if (entries < 6)
lv_cont_set_fit2 (menu_popup, LV_FIT_NONE, LV_FIT_TIGHT);
}