#994

Removing spacing between widgets?

OpenTomJGoodingopened 16d ago

Honestly this is such a minor thing that I hesitated to create an issue. I'm trying to create a row of buttons without any spacing between them, but tiny gaps still appear depending on the window size. I might just be doing something wrong or maybe this is a known limitation.

MRE and screenshot below (using the SDL3 renderer):

static void buttons_no_spacing(struct nk_context *ctx) {
    struct nk_style_button button_style;

    nk_flags flags = NK_WINDOW_TITLE | NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE;
    if (nk_begin(ctx, "Buttons No Spacing", nk_rect(50, 50, 333, 333), flags)) {
        nk_style_push_vec2(ctx, &ctx->style.window.spacing, nk_vec2(0, 0));

        button_style = ctx->style.button;
        button_style.rounding = 0.0f;
        button_style.border = 0.0f;

        button_style.normal = nk_style_item_color(nk_rgb(0, 255, 255));
        button_style.hover = nk_style_item_color(nk_rgb(255, 255, 0));
        button_style.active = nk_style_item_color(nk_rgb(0, 255, 0));

        nk_layout_row_dynamic(ctx, 0, 4);
        nk_button_label_styled(ctx, &button_style, "Header1");
        nk_button_label_styled(ctx, &button_style, "Header2");
        nk_button_label_styled(ctx, &button_style, "Header3");
        nk_button_label_styled(ctx, &button_style, "Header4");

        nk_style_pop_vec2(ctx);
    }
    nk_end(ctx);
}

Activity

3
RobLoach13d ago

I think there was some discussion around margin/padding at some point. Not sure where that landed. I agree the spacing in general should get fixed, because there is confusion.

TomJGooding11d ago

Thanks @RobLoach - I was worried that I might just be doing something wrong, so I'm glad you didn't spot anything obvious at least.

I haven't dived into the code yet, but presuming something like:

window_width = 333
column_count = 4

// Assuming window spacing set to zero
widget_widthf = window_width / column_count  // 83.25
widget_width = floor(widget_widthf)
leftover = window_width - (widget_width * column count)  // 1

My guess is these remaining gaps is where nk_layout_row_dynamic tries to redistribute the leftover space?

TomJGooding2d ago

https://github.com/Immediate-Mode-UI/Nuklear/blob/8407f477c7cb0cc69236c0232a3a4967395e9eb1/src/nuklear_layout.c#L600-L613

Based on the example above:

panel_space = 311.0
columns = 4
w = panel_space / columns
item_offsets = [w * i for i in range(columns)]
# [0.0, 77.75, 155.5, 233.25]
item_widths = [w + off - int(off+0.5) for off in item_offsets]
# [77.75, 77.5, 77.25, 78.0]

Presumably somewhere (nk_widget?) these bounds are cast to ints:

xs = [int(x) for x in item_offsets]
# [0, 77, 155, 233]
ws = [int(w) for w in item_widths]
# [77, 77, 77, 78]

gaps = [xs[i+1] - (xs[i] + ws[i]) for i in range(len(xs)-1)]
# [0, 1, 1]