layout icon indicating copy to clipboard operation
layout copied to clipboard

Preset/fixed item ID numbers

Open zzo38 opened this issue 3 years ago • 2 comments

I would think what might sometimes be good to have is the mode for preset (fixed) item ID numbers, you will specify how many they are and then you will call the function to allocate that many, instead of calling lay_item for each one.

This could be done to check first if ctx->count is zero; if not, then it is an error, but if it is zero then it will set its capacity and count to the specified number, allocate them, and then initialize each item in a loop, similarly than lay_item already does.

(My own "smallxrm" library, which is an implementation of the X resource manager, has a xrm_init_quarks function with a similar use. You will allocate many items with fixed preset ID numbers from the beginning, which can be compile-time constants, rather than having to pass strings always or allocating them and assigning them to variables. (You can still add additional quarks afterward which do not have fixed numbers.))

zzo38 avatar Nov 21 '22 00:11 zzo38

I wrote a code below. I have not tested this and it may be wrong.

void lay_preset_items(lay_context *ctx, lay_id total)
{
    lay_id idx;
    ASSERT(ctx && !ctx->count);
    ctx->count = total;

    if (total > ctx->capacity) {
        ctx->capacity = total;
        const size_t item_size = sizeof(lay_item_t) + sizeof(lay_vec4);
        ctx->items = (lay_item_t*)LAY_REALLOC(ctx->items, ctx->capacity * item_size);
        const lay_item_t *past_last = ctx->items + ctx->capacity;
        ctx->rects = (lay_vec4*)past_last;
    }

    for(idx=0;idx<total;idx++) {
        lay_item_t *item = ctx->items + idx;
        LAY_MEMSET(item, 0, sizeof(lay_item_t));
        item->first_child = LAY_INVALID_ID;
        item->next_sibling = LAY_INVALID_ID;
        LAY_MEMSET(&ctx->rects[idx], 0, sizeof(lay_vec4));
    }
}

(I agree that this code is public domain, and you are permitted to include it in the code which is licensed by the license of this project.)

zzo38 avatar Nov 21 '22 00:11 zzo38

I think a good way might be to change the default way to allocate items to allocate them as an array instead of a single, and make the lay_item function call lay_items or something with a value of 1. Maybe I'll do that in a future version.

randrew avatar Aug 14 '23 10:08 randrew