Fix the same bug for contextual menus, then a related bug for unnecessary scrollbars

This commit is contained in:
Robert Winkler
2026-11-26 01:36:11 -05:00
committed by Rob Loach
parent bd7d582f4c
commit f1b960e4b2
4 changed files with 68 additions and 42 deletions
+34 -21
View File
@@ -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;}
+19 -12
View File
@@ -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;
}
+2 -2
View File
@@ -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;}
+13 -7
View File
@@ -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) {