menu: switch to a hovered title while another menu is open

Once a menu is open, moving the pointer onto another title closes the
first and opens the second. Text, image, and symbol titles all do this.
This commit is contained in:
Robert Winkler
2026-11-29 16:13:24 -05:00
committed by Rob Loach
parent c8bfb4df12
commit 4a6686d746
2 changed files with 100 additions and 0 deletions
+50
View File
@@ -22260,6 +22260,31 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
popup = win->popup.win;
is_open = popup ? nk_true : nk_false;
is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
/* Another menu is already open and the pointer is on this title.
Take that popup over instead of waiting for a second click. */
if (is_open && !is_active &&
nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
unsigned int clicks = ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked = 0;
/* Drop the menu that was drawn earlier this frame so only this one shows. */
if (win->popup.buf.active) {
void *memory = ctx->memory.memory.ptr;
struct nk_command *parent_cmd = nk_ptr_add(struct nk_command, memory, win->popup.buf.parent);
struct nk_command *last_cmd = nk_ptr_add(struct nk_command, memory, win->popup.buf.last);
if (parent_cmd && last_cmd)
parent_cmd->next = last_cmd->next;
win->popup.buf.active = nk_false;
}
is_clicked = nk_true;
if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU)) {
ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked = clicks;
return 0;
}
ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked = clicks;
win->popup.type = NK_PANEL_MENU;
win->popup.name = hash;
return 1;
}
if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
(!is_open && !is_active && !is_clicked)) return 0;
{nk_flags flags = NK_WINDOW_NO_SCROLLBAR;
@@ -22277,6 +22302,26 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
win->popup.name = hash;
return 1;
}
/* An open menu marks the bar read-only. Clear that while the pointer is on
this title so it highlights and nk_menu_begin can take the popup over.
Call before nk_widget, which is what consumes the title's rectangle. */
NK_LIB void
nk_menu_title_hot(struct nk_context *ctx, struct nk_window *win)
{
struct nk_rect peek;
struct nk_panel *root;
if (!win->popup.win || win->popup.type != NK_PANEL_MENU)
return;
peek = nk_widget_bounds(ctx);
if (!nk_input_is_mouse_hovering_rect(&ctx->input, peek))
return;
win->flags &= ~(nk_flags)NK_WINDOW_ROM;
root = win->layout;
while (root) {
root->flags &= ~(nk_flags)NK_WINDOW_ROM;
root = root->parent;
}
}
NK_API nk_bool
nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
nk_flags align, struct nk_vec2 size)
@@ -22294,6 +22339,7 @@ nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
@@ -22324,6 +22370,7 @@ nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
@@ -22349,6 +22396,7 @@ nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
@@ -22374,6 +22422,7 @@ nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
@@ -22406,6 +22455,7 @@ nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
+50
View File
@@ -100,6 +100,31 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
popup = win->popup.win;
is_open = popup ? nk_true : nk_false;
is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
/* Another menu is already open and the pointer is on this title.
Take that popup over instead of waiting for a second click. */
if (is_open && !is_active &&
nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
unsigned int clicks = ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked = 0;
/* Drop the menu that was drawn earlier this frame so only this one shows. */
if (win->popup.buf.active) {
void *memory = ctx->memory.memory.ptr;
struct nk_command *parent_cmd = nk_ptr_add(struct nk_command, memory, win->popup.buf.parent);
struct nk_command *last_cmd = nk_ptr_add(struct nk_command, memory, win->popup.buf.last);
if (parent_cmd && last_cmd)
parent_cmd->next = last_cmd->next;
win->popup.buf.active = nk_false;
}
is_clicked = nk_true;
if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU)) {
ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked = clicks;
return 0;
}
ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked = clicks;
win->popup.type = NK_PANEL_MENU;
win->popup.name = hash;
return 1;
}
if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
(!is_open && !is_active && !is_clicked)) return 0;
{nk_flags flags = NK_WINDOW_NO_SCROLLBAR;
@@ -117,6 +142,26 @@ nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
win->popup.name = hash;
return 1;
}
/* An open menu marks the bar read-only. Clear that while the pointer is on
this title so it highlights and nk_menu_begin can take the popup over.
Call before nk_widget, which is what consumes the title's rectangle. */
NK_LIB void
nk_menu_title_hot(struct nk_context *ctx, struct nk_window *win)
{
struct nk_rect peek;
struct nk_panel *root;
if (!win->popup.win || win->popup.type != NK_PANEL_MENU)
return;
peek = nk_widget_bounds(ctx);
if (!nk_input_is_mouse_hovering_rect(&ctx->input, peek))
return;
win->flags &= ~(nk_flags)NK_WINDOW_ROM;
root = win->layout;
while (root) {
root->flags &= ~(nk_flags)NK_WINDOW_ROM;
root = root->parent;
}
}
NK_API nk_bool
nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
nk_flags align, struct nk_vec2 size)
@@ -134,6 +179,7 @@ nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
@@ -164,6 +210,7 @@ nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
@@ -189,6 +236,7 @@ nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
@@ -214,6 +262,7 @@ nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
@@ -246,6 +295,7 @@ nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
return 0;
win = ctx->current;
nk_menu_title_hot(ctx, win);
state = nk_widget(&header, ctx);
if (!state) return 0;