From f1b960e4b2a9bdacf6aa6971a4760fd8b305ef1e Mon Sep 17 00:00:00 2001 From: Robert Winkler Date: Wed, 2 Sep 2026 01:01:06 -0700 Subject: [PATCH] Fix the same bug for contextual menus, then a related bug for unnecessary scrollbars --- nuklear.h | 55 +++++++++++++++++++++++++--------------- src/nuklear_contextual.c | 31 +++++++++++++--------- src/nuklear_menu.c | 4 +-- src/nuklear_popup.c | 20 ++++++++++----- 4 files changed, 68 insertions(+), 42 deletions(-) diff --git a/nuklear.h b/nuklear.h index f51bdf1e..9e569cdd 100644 --- a/nuklear.h +++ b/nuklear.h @@ -21624,13 +21624,19 @@ nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, body.h = (space_below > 1.0f) ? space_below : 1.0f; } } else if (fit == NK_POPUP_FIT_SLIDE) { - if (body.y + need_h > d_bottom) - body.y = d_bottom - need_h; - if (body.y < d.y) - body.y = d.y; - if (body.y + body.h > d_bottom) - body.h = d_bottom - body.y; - if (body.h < 1.0f) body.h = 1.0f; + /* size.y is a maximum, not current content. First open (known_h == 0) + * stays on the click; later frames slide only if actual height + * does not fit below. Sliding with the max left the DYNAMIC-shrunk + * panel floating above the cursor. */ + if (known_h > 0.0f && known_h > space_below) { + body.y = d_bottom - known_h; + if (body.y < d.y) + body.y = d.y; + if (body.y + known_h > d_bottom) + body.h = d_bottom - body.y; + else body.h = known_h; + if (body.h < 1.0f) body.h = 1.0f; + } } else { /* tooltip: flip then slide; do not shrink (clipping is acceptable) */ if (body.y + need_h > d_bottom) { @@ -21950,36 +21956,43 @@ nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size, if ((!is_open && !is_clicked)) return 0; - /* calculate contextual position on click */ + /* calculate contextual position on click; keep the click as the + * anchor every frame (combo/menu re-fit from the trigger). */ win->popup.active_con = win->popup.con_count; + {struct nk_rect anchor; if (is_clicked) { - body.x = in->mouse.pos.x; - body.y = in->mouse.pos.y; + anchor.x = in->mouse.pos.x; + anchor.y = in->mouse.pos.y; } else { - body.x = popup->bounds.x; - body.y = popup->bounds.y; + anchor.x = win->popup.header.x; + anchor.y = win->popup.header.y; } - + anchor.w = 1; + anchor.h = 1; + body.x = anchor.x; + body.y = anchor.y; body.w = size.x; body.h = size.y; {float known_h = (!is_clicked) ? win->popup.last_h : 0; - float req_h = body.h; - body = nk_fit_popup_rect(ctx, body, body, NK_POPUP_FIT_SLIDE, known_h, nk_false); + body = nk_fit_popup_rect(ctx, body, anchor, NK_POPUP_FIT_SLIDE, known_h, nk_false); flags |= NK_WINDOW_NO_SCROLLBAR; - if (body.h + 0.5f < req_h) + /* scrollbar only if actual content does not fit in the display */ + if (known_h > 0.0f && body.h + 0.5f < known_h) flags &= ~(nk_flags)NK_WINDOW_NO_SCROLLBAR;} /* start nonblocking contextual popup */ ret = nk_nonblock_begin(ctx, flags, body, null_rect, NK_PANEL_CONTEXTUAL); - if (ret) win->popup.type = NK_PANEL_CONTEXTUAL; - else { + if (ret) { + win->popup.type = NK_PANEL_CONTEXTUAL; + win->popup.header = anchor; + } else { win->popup.active_con = 0; win->popup.type = NK_PANEL_NONE; if (win->popup.win) win->popup.win->flags = 0; - } + }} } return ret; } @@ -22244,11 +22257,11 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win, (!is_open && !is_active && !is_clicked)) return 0; {nk_flags flags = NK_WINDOW_NO_SCROLLBAR; float known_h = (is_active) ? win->popup.last_h : 0; - float req_h = body.h; body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_h, is_active && win->popup.pinned_up); win->popup.pinned_up = (body.y < header.y); - if (body.h + 0.5f < req_h) + /* scrollbar only if actual content does not fit in the display */ + if (known_h > 0.0f && body.h + 0.5f < known_h) flags = 0; if (!nk_nonblock_begin(ctx, flags, body, header, NK_PANEL_MENU)) return 0;} diff --git a/src/nuklear_contextual.c b/src/nuklear_contextual.c index 948998af..f01a6c2f 100644 --- a/src/nuklear_contextual.c +++ b/src/nuklear_contextual.c @@ -44,36 +44,43 @@ nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size, if ((!is_open && !is_clicked)) return 0; - /* calculate contextual position on click */ + /* calculate contextual position on click; keep the click as the + * anchor every frame (combo/menu re-fit from the trigger). */ win->popup.active_con = win->popup.con_count; + {struct nk_rect anchor; if (is_clicked) { - body.x = in->mouse.pos.x; - body.y = in->mouse.pos.y; + anchor.x = in->mouse.pos.x; + anchor.y = in->mouse.pos.y; } else { - body.x = popup->bounds.x; - body.y = popup->bounds.y; + anchor.x = win->popup.header.x; + anchor.y = win->popup.header.y; } - + anchor.w = 1; + anchor.h = 1; + body.x = anchor.x; + body.y = anchor.y; body.w = size.x; body.h = size.y; {float known_h = (!is_clicked) ? win->popup.last_h : 0; - float req_h = body.h; - body = nk_fit_popup_rect(ctx, body, body, NK_POPUP_FIT_SLIDE, known_h, nk_false); + body = nk_fit_popup_rect(ctx, body, anchor, NK_POPUP_FIT_SLIDE, known_h, nk_false); flags |= NK_WINDOW_NO_SCROLLBAR; - if (body.h + 0.5f < req_h) + /* scrollbar only if actual content does not fit in the display */ + if (known_h > 0.0f && body.h + 0.5f < known_h) flags &= ~(nk_flags)NK_WINDOW_NO_SCROLLBAR;} /* start nonblocking contextual popup */ ret = nk_nonblock_begin(ctx, flags, body, null_rect, NK_PANEL_CONTEXTUAL); - if (ret) win->popup.type = NK_PANEL_CONTEXTUAL; - else { + if (ret) { + win->popup.type = NK_PANEL_CONTEXTUAL; + win->popup.header = anchor; + } else { win->popup.active_con = 0; win->popup.type = NK_PANEL_NONE; if (win->popup.win) win->popup.win->flags = 0; - } + }} } return ret; } diff --git a/src/nuklear_menu.c b/src/nuklear_menu.c index c45bfd7b..f79f94ea 100644 --- a/src/nuklear_menu.c +++ b/src/nuklear_menu.c @@ -104,11 +104,11 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win, (!is_open && !is_active && !is_clicked)) return 0; {nk_flags flags = NK_WINDOW_NO_SCROLLBAR; float known_h = (is_active) ? win->popup.last_h : 0; - float req_h = body.h; body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_h, is_active && win->popup.pinned_up); win->popup.pinned_up = (body.y < header.y); - if (body.h + 0.5f < req_h) + /* scrollbar only if actual content does not fit in the display */ + if (known_h > 0.0f && body.h + 0.5f < known_h) flags = 0; if (!nk_nonblock_begin(ctx, flags, body, header, NK_PANEL_MENU)) return 0;} diff --git a/src/nuklear_popup.c b/src/nuklear_popup.c index 304985ad..6ec7bc7a 100644 --- a/src/nuklear_popup.c +++ b/src/nuklear_popup.c @@ -80,13 +80,19 @@ nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, body.h = (space_below > 1.0f) ? space_below : 1.0f; } } else if (fit == NK_POPUP_FIT_SLIDE) { - if (body.y + need_h > d_bottom) - body.y = d_bottom - need_h; - if (body.y < d.y) - body.y = d.y; - if (body.y + body.h > d_bottom) - body.h = d_bottom - body.y; - if (body.h < 1.0f) body.h = 1.0f; + /* size.y is a maximum, not current content. First open (known_h == 0) + * stays on the click; later frames slide only if actual height + * does not fit below. Sliding with the max left the DYNAMIC-shrunk + * panel floating above the cursor. */ + if (known_h > 0.0f && known_h > space_below) { + body.y = d_bottom - known_h; + if (body.y < d.y) + body.y = d.y; + if (body.y + known_h > d_bottom) + body.h = d_bottom - body.y; + else body.h = known_h; + if (body.h < 1.0f) body.h = 1.0f; + } } else { /* tooltip: flip then slide; do not shrink (clipping is acceptable) */ if (body.y + need_h > d_bottom) {