Lv_obj_update_layout does not what i expect

Hi There,
LVGL V9.3
I know that this topic is not new, but I am working already the whole day on it and can not get it to work :frowning:

I have a “smart icon grid”
Story short: I do set the width of objects all to the same size. When something in the layout changes i want to recalculate all of it. for that i need to icon with the max width. For that i set the icons to LV_SIZE_CONTENT and update the layout. I then read all the widths and find the widest one.

The Problem is that on the first run it finds the widest object (179 px) and rewrites all objects to (265 px). But on the next run the width is for all 265 px even if i make size content before that (as an reset). I think I tried lv_obj_update_layout on every position but it does not update the get_width values…
I thought maybe, for some reason a child got wide, but no. I added logging and attached the printout.

I am sure lt is a minor thing, but I can not proceed without your help :slight_smile:

static void RecalculateIconLayout(lv_obj_t* const container,
                                  icon_data_t* const iconData,
                                  uint8_t const iconCount,
                                  uint16_t const spacing,
                                  uint16_t const verticalSpacerThickness)
{
    uint16_t const kParentWidth = (uint16_t) lv_obj_get_width(container);
    uint16_t const kParentHeight = (uint16_t) lv_obj_get_height(container);

    uint32_t xMaxIconWidth = 0u;



    for (uint8_t i = 0u; i < iconCount; ++i)
    {
        lv_obj_set_width(iconData[i].icon_ptr, LV_SIZE_CONTENT);
        lv_obj_update_layout(iconData[i].icon_ptr);
    }
    lv_obj_update_layout(container);

    lv_obj_t* pWidestIcon = NULL;

    for (uint8_t i = 0u; i < iconCount; ++i)
    {
        lv_obj_t* const kIcon = iconData[i].icon_ptr;

        if (lv_obj_has_flag(kIcon, LV_OBJ_FLAG_HIDDEN) == false)
        {
            lv_obj_update_layout(kIcon);

            uint32_t const kWidth = (uint32_t)lv_obj_get_width(kIcon);

            if (kWidth > xMaxIconWidth)
            {
                xMaxIconWidth = kWidth;
                pWidestIcon = kIcon;
            }
        }
    }

    if (pWidestIcon != NULL)
    {
        LV_LOG_USER("Widest icon width: %u", (unsigned)xMaxIconWidth);
        uint32_t const kChildCount = lv_obj_get_child_count(pWidestIcon);

        for (uint32_t j = 0u; j < kChildCount; ++j)
        {
            lv_obj_t* const kChild = lv_obj_get_child(pWidestIcon, (int32_t)j);
            LV_LOG_USER("  Child[%u] width: %d", (unsigned)j, lv_obj_get_width(kChild));
        }
    }

    uint16_t xCalculatedIconWidth = 0u;
    uint16_t xCalculatedIconHeight = 0u;
    uint8_t xRows = 0u;
    uint8_t xColumns = 0u;
    uint8_t xActiveIcons = 0u;

    for (uint8_t i = 0u; i < iconCount; ++i)
    {
        if (lv_obj_has_flag(iconData[i].icon_ptr, LV_OBJ_FLAG_HIDDEN) == false)
        {
            xActiveIcons++;
        }
    }

    if (xActiveIcons == 0u)
    {
        return;
    }

    CalculateBestRowsCols(kParentWidth, kParentHeight,
                          &xCalculatedIconWidth, &xCalculatedIconHeight,
                          xActiveIcons, &xRows, &xColumns,
                          spacing + verticalSpacerThickness);

    uint32_t xIconWidth = LV_MAX(xCalculatedIconWidth, xMaxIconWidth);
    uint32_t const kOptimalIconWidth = (kParentWidth - ((xColumns + 1u) * spacing)) / xColumns;
    xIconWidth = LV_MAX(xIconWidth, kOptimalIconWidth);

    for (uint8_t i = 0u; i < iconCount; ++i)
    {
        lv_obj_t* const kIcon = iconData[i].icon_ptr;

        lv_obj_set_size(kIcon, xIconWidth, xCalculatedIconHeight);


    }
}

Logging output:

[User]  (101487.307, +116)       RecalculateIconLayout: Widest icon width: 179 HB_GUI_GenericFunctions.c:744
[User]  (101487.307, +0)         RecalculateIconLayout:   Child[0] width: 70 HB_GUI_GenericFunctions.c:750
[User]  (101487.307, +0)         RecalculateIconLayout:   Child[1] width: 4 HB_GUI_GenericFunctions.c:750
[User]  (101487.307, +0)         RecalculateIconLayout:   Child[2] width: 177 HB_GUI_GenericFunctions.c:750
[User]  (101487.310, +3)         RecalculateIconLayout: Widest icon width: 265 HB_GUI_GenericFunctions.c:744
[User]  (101487.311, +1)         RecalculateIconLayout:   Child[0] width: 70 HB_GUI_GenericFunctions.c:750
[User]  (101487.311, +0)         RecalculateIconLayout:   Child[1] width: 98 HB_GUI_GenericFunctions.c:750
[User]  (101487.311, +0)         RecalculateIconLayout:   Child[2] width: 55 HB_GUI_GenericFunctions.c:750