diff --git a/demo/common/overview.c b/demo/common/overview.c index 143f72e4..0d032bc7 100644 --- a/demo/common/overview.c +++ b/demo/common/overview.c @@ -102,10 +102,11 @@ overview(struct nk_context *ctx) const float values[]={26.0f,13.0f,30.0f,15.0f,25.0f,10.0f,20.0f,40.0f,12.0f,8.0f,22.0f,28.0f}; menu_state = MENU_CHART; nk_layout_row_dynamic(ctx, 150, 1); - nk_chart_begin(ctx, NK_CHART_COLUMN, NK_LEN(values), 0, 50); - for (i = 0; i < NK_LEN(values); ++i) - nk_chart_push(ctx, values[i]); - nk_chart_end(ctx); + if (nk_chart_begin(ctx, NK_CHART_COLUMN, NK_LEN(values), 0, 50)) { + for (i = 0; i < NK_LEN(values); ++i) + nk_chart_push(ctx, values[i]); + nk_chart_end(ctx); + } nk_tree_pop(ctx); } else menu_state = (menu_state == MENU_CHART) ? MENU_NONE: menu_state; nk_menu_end(ctx); @@ -513,15 +514,16 @@ overview(struct nk_context *ctx) size_t i = 0; static const float values[]={26.0f,13.0f,30.0f,15.0f,25.0f,10.0f,20.0f,40.0f, 12.0f, 8.0f, 22.0f, 28.0f, 5.0f}; nk_layout_row_dynamic(ctx, 150, 1); - nk_chart_begin(ctx, NK_CHART_COLUMN, NK_LEN(values), 0, 50); - for (i = 0; i < NK_LEN(values); ++i) { - nk_flags res = nk_chart_push(ctx, values[i]); - if (res & NK_CHART_CLICKED) { - chart_selection = values[i]; - nk_combo_close(ctx); + if (nk_chart_begin(ctx, NK_CHART_COLUMN, NK_LEN(values), 0, 50)) { + for (i = 0; i < NK_LEN(values); ++i) { + nk_flags res = nk_chart_push(ctx, values[i]); + if (res & NK_CHART_CLICKED) { + chart_selection = values[i]; + nk_combo_close(ctx); + } } + nk_chart_end(ctx); } - nk_chart_end(ctx); nk_combo_end(ctx); } diff --git a/nuklear.h b/nuklear.h index 5f5815b1..f51bdf1e 100644 --- a/nuklear.h +++ b/nuklear.h @@ -5830,6 +5830,7 @@ struct nk_popup_state { unsigned active_con; struct nk_rect header; float last_h; /**< last frame's content height; used to flip without a gap */ + nk_bool pinned_up; /**< keep drop-up once chosen so expand/collapse does not jump */ }; struct nk_edit_state { @@ -6412,7 +6413,7 @@ enum nk_popup_fit { NK_POPUP_FIT_SLIDE, /* contextual: slide to stay on-screen */ NK_POPUP_FIT_TOOLTIP /* tooltip: flip around cursor, then slide */ }; -NK_LIB struct nk_rect nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, struct nk_rect anchor, enum nk_popup_fit fit, float known_h); +NK_LIB struct nk_rect nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, struct nk_rect anchor, enum nk_popup_fit fit, float known_h, nk_bool stay_up); NK_LIB nk_bool nk_nonblock_begin(struct nk_context *ctx, nk_flags flags, struct nk_rect body, struct nk_rect header, enum nk_panel_type panel_type); /* text */ @@ -21551,7 +21552,8 @@ nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, nk_bool roundi * ===============================================================*/ NK_LIB struct nk_rect nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, - struct nk_rect anchor, enum nk_popup_fit fit, float known_h) + struct nk_rect anchor, enum nk_popup_fit fit, float known_h, + nk_bool stay_up) { struct nk_rect d; float overlap; @@ -21597,23 +21599,28 @@ nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, if (fit == NK_POPUP_FIT_FLIP) { int flip = 0; - if (req_h > space_below) { - /* flip only when the real content fits above. using the - * clamped window height here caused a flip/restore flicker. */ - if (need_h <= space_above) + /* Caller size.y is a maximum (ADVANCED is 600px for expanded trees), + * not the current content. Flip only when last frame's actual height + * does not fit below. First open (known_h == 0) always drops down. + * stay_up keeps a drop-up for the rest of this open so collapsing + * a tree does not jump back to a dropdown. */ + if (stay_up && space_above > 0.0f) + flip = 1; + else if (known_h > 0.0f && known_h > space_below) { + if (known_h <= space_above) flip = 1; - else if (known_h > 0.0f && space_above > space_below) + else if (space_above > space_below) flip = 1; } if (flip) { float use_h = req_h; if (use_h > space_above) use_h = space_above; - if (known_h > 0.0f && known_h < use_h) use_h = known_h; + if (known_h < use_h) use_h = known_h; if (use_h < 1.0f) use_h = 1.0f; body.y = anchor.y - use_h; if (overlap > 0.0f) body.y += overlap; body.h = use_h; - } else if (req_h > space_below) { + } else if (known_h > space_below) { body.h = (space_below > 1.0f) ? space_below : 1.0f; } } else if (fit == NK_POPUP_FIT_SLIDE) { @@ -21787,6 +21794,7 @@ nk_nonblock_begin(struct nk_context *ctx, } win->popup.buf.active = 0; win->popup.last_h = 0; + win->popup.pinned_up = nk_false; return is_active; } popup->bounds = body; @@ -21957,7 +21965,7 @@ nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size, {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); + body = nk_fit_popup_rect(ctx, body, body, NK_POPUP_FIT_SLIDE, known_h, nk_false); flags |= NK_WINDOW_NO_SCROLLBAR; if (body.h + 0.5f < req_h) flags &= ~(nk_flags)NK_WINDOW_NO_SCROLLBAR;} @@ -22237,7 +22245,9 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win, {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); + 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) flags = 0; if (!nk_nonblock_begin(ctx, flags, body, header, NK_PANEL_MENU)) @@ -30217,8 +30227,10 @@ nk_chart_push_slot(struct nk_context *ctx, float value, int slot) NK_ASSERT(ctx); NK_ASSERT(ctx->current); NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT); - NK_ASSERT(slot < ctx->current->layout->chart.slot); if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false; + /* nk_chart_begin zeros the chart and returns 0 when the widget is + * clipped (e.g. a menu drop-up sized from last frame while a tree + * expands). Pushing in that case is a no-op, not a programmer error. */ if (slot >= ctx->current->layout->chart.slot) return nk_false; win = ctx->current; @@ -30561,7 +30573,9 @@ nk_combo_begin(struct nk_context *ctx, struct nk_window *win, if ((is_clicked && is_open && !is_active) || (is_open && !is_active) || (!is_open && !is_active && !is_clicked)) return 0; {float known_h = (is_active) ? win->popup.last_h : 0; - body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_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 (!nk_nonblock_begin(ctx, 0, body, (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0; @@ -31466,7 +31480,7 @@ nk_tooltip_begin_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos anchor.y = in->mouse.pos.y; anchor.w = 1; anchor.h = 1; - screen = nk_fit_popup_rect(ctx, screen, anchor, NK_POPUP_FIT_TOOLTIP, known_h); + screen = nk_fit_popup_rect(ctx, screen, anchor, NK_POPUP_FIT_TOOLTIP, known_h, nk_false); bounds.x = screen.x - clip_x; bounds.y = screen.y - clip_y; diff --git a/src/nuklear.h b/src/nuklear.h index 52d2a123..db78f893 100644 --- a/src/nuklear.h +++ b/src/nuklear.h @@ -5640,6 +5640,7 @@ struct nk_popup_state { unsigned active_con; struct nk_rect header; float last_h; /**< last frame's content height; used to flip without a gap */ + nk_bool pinned_up; /**< keep drop-up once chosen so expand/collapse does not jump */ }; struct nk_edit_state { diff --git a/src/nuklear_chart.c b/src/nuklear_chart.c index 87301faa..0ca3e171 100644 --- a/src/nuklear_chart.c +++ b/src/nuklear_chart.c @@ -244,8 +244,10 @@ nk_chart_push_slot(struct nk_context *ctx, float value, int slot) NK_ASSERT(ctx); NK_ASSERT(ctx->current); NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT); - NK_ASSERT(slot < ctx->current->layout->chart.slot); if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false; + /* nk_chart_begin zeros the chart and returns 0 when the widget is + * clipped (e.g. a menu drop-up sized from last frame while a tree + * expands). Pushing in that case is a no-op, not a programmer error. */ if (slot >= ctx->current->layout->chart.slot) return nk_false; win = ctx->current; diff --git a/src/nuklear_combo.c b/src/nuklear_combo.c index 8c36b83d..1a6357fb 100644 --- a/src/nuklear_combo.c +++ b/src/nuklear_combo.c @@ -50,7 +50,9 @@ nk_combo_begin(struct nk_context *ctx, struct nk_window *win, if ((is_clicked && is_open && !is_active) || (is_open && !is_active) || (!is_open && !is_active && !is_clicked)) return 0; {float known_h = (is_active) ? win->popup.last_h : 0; - body = nk_fit_popup_rect(ctx, body, header, NK_POPUP_FIT_FLIP, known_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 (!nk_nonblock_begin(ctx, 0, body, (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0; diff --git a/src/nuklear_contextual.c b/src/nuklear_contextual.c index 454c9151..948998af 100644 --- a/src/nuklear_contextual.c +++ b/src/nuklear_contextual.c @@ -59,7 +59,7 @@ nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size, {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); + body = nk_fit_popup_rect(ctx, body, body, NK_POPUP_FIT_SLIDE, known_h, nk_false); flags |= NK_WINDOW_NO_SCROLLBAR; if (body.h + 0.5f < req_h) flags &= ~(nk_flags)NK_WINDOW_NO_SCROLLBAR;} diff --git a/src/nuklear_internal.h b/src/nuklear_internal.h index 31f91ab4..c2beaad9 100644 --- a/src/nuklear_internal.h +++ b/src/nuklear_internal.h @@ -251,7 +251,7 @@ enum nk_popup_fit { NK_POPUP_FIT_SLIDE, /* contextual: slide to stay on-screen */ NK_POPUP_FIT_TOOLTIP /* tooltip: flip around cursor, then slide */ }; -NK_LIB struct nk_rect nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, struct nk_rect anchor, enum nk_popup_fit fit, float known_h); +NK_LIB struct nk_rect nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, struct nk_rect anchor, enum nk_popup_fit fit, float known_h, nk_bool stay_up); NK_LIB nk_bool nk_nonblock_begin(struct nk_context *ctx, nk_flags flags, struct nk_rect body, struct nk_rect header, enum nk_panel_type panel_type); /* text */ diff --git a/src/nuklear_menu.c b/src/nuklear_menu.c index 49567ada..c45bfd7b 100644 --- a/src/nuklear_menu.c +++ b/src/nuklear_menu.c @@ -105,7 +105,9 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win, {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); + 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) flags = 0; if (!nk_nonblock_begin(ctx, flags, body, header, NK_PANEL_MENU)) diff --git a/src/nuklear_popup.c b/src/nuklear_popup.c index 933d4933..304985ad 100644 --- a/src/nuklear_popup.c +++ b/src/nuklear_popup.c @@ -8,7 +8,8 @@ * ===============================================================*/ NK_LIB struct nk_rect nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, - struct nk_rect anchor, enum nk_popup_fit fit, float known_h) + struct nk_rect anchor, enum nk_popup_fit fit, float known_h, + nk_bool stay_up) { struct nk_rect d; float overlap; @@ -54,23 +55,28 @@ nk_fit_popup_rect(const struct nk_context *ctx, struct nk_rect body, if (fit == NK_POPUP_FIT_FLIP) { int flip = 0; - if (req_h > space_below) { - /* flip only when the real content fits above. using the - * clamped window height here caused a flip/restore flicker. */ - if (need_h <= space_above) + /* Caller size.y is a maximum (ADVANCED is 600px for expanded trees), + * not the current content. Flip only when last frame's actual height + * does not fit below. First open (known_h == 0) always drops down. + * stay_up keeps a drop-up for the rest of this open so collapsing + * a tree does not jump back to a dropdown. */ + if (stay_up && space_above > 0.0f) + flip = 1; + else if (known_h > 0.0f && known_h > space_below) { + if (known_h <= space_above) flip = 1; - else if (known_h > 0.0f && space_above > space_below) + else if (space_above > space_below) flip = 1; } if (flip) { float use_h = req_h; if (use_h > space_above) use_h = space_above; - if (known_h > 0.0f && known_h < use_h) use_h = known_h; + if (known_h < use_h) use_h = known_h; if (use_h < 1.0f) use_h = 1.0f; body.y = anchor.y - use_h; if (overlap > 0.0f) body.y += overlap; body.h = use_h; - } else if (req_h > space_below) { + } else if (known_h > space_below) { body.h = (space_below > 1.0f) ? space_below : 1.0f; } } else if (fit == NK_POPUP_FIT_SLIDE) { @@ -244,6 +250,7 @@ nk_nonblock_begin(struct nk_context *ctx, } win->popup.buf.active = 0; win->popup.last_h = 0; + win->popup.pinned_up = nk_false; return is_active; } popup->bounds = body; diff --git a/src/nuklear_tooltip.c b/src/nuklear_tooltip.c index 25684b32..63dc2ab4 100644 --- a/src/nuklear_tooltip.c +++ b/src/nuklear_tooltip.c @@ -98,7 +98,7 @@ nk_tooltip_begin_offset(struct nk_context *ctx, float width, enum nk_tooltip_pos anchor.y = in->mouse.pos.y; anchor.w = 1; anchor.h = 1; - screen = nk_fit_popup_rect(ctx, screen, anchor, NK_POPUP_FIT_TOOLTIP, known_h); + screen = nk_fit_popup_rect(ctx, screen, anchor, NK_POPUP_FIT_TOOLTIP, known_h, nk_false); bounds.x = screen.x - clip_x; bounds.y = screen.y - clip_y;