From be2bba7be2529a256b4abf7130286f8541f9d850 Mon Sep 17 00:00:00 2001 From: nick black Date: Tue, 13 Oct 2020 23:25:41 -0400 Subject: [PATCH] implement ncmenu_item_set_status #1057 --- include/notcurses/notcurses.h | 4 ++++ src/lib/menu.c | 23 +++++++++++++++++++++++ src/poc/menu.c | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/include/notcurses/notcurses.h b/include/notcurses/notcurses.h index 4b880eeb1..7bebce8eb 100644 --- a/include/notcurses/notcurses.h +++ b/include/notcurses/notcurses.h @@ -2859,6 +2859,10 @@ API int ncmenu_prevsection(struct ncmenu* n); API int ncmenu_nextitem(struct ncmenu* n); API int ncmenu_previtem(struct ncmenu* n); +// Disable or enable a menu item. Returns 0 if the item was found. +API int ncmenu_item_set_status(struct ncmenu* n, const char* section, + const char* item, bool enabled); + // Return the selected item description, or NULL if no section is unrolled. If // 'ni' is not NULL, and the selected item has a shortcut, 'ni' will be filled // in with that shortcut--this can allow faster matching. diff --git a/src/lib/menu.c b/src/lib/menu.c index 34f968ea7..894d00e58 100644 --- a/src/lib/menu.c +++ b/src/lib/menu.c @@ -604,6 +604,29 @@ bool ncmenu_offer_input(ncmenu* n, const ncinput* nc){ return false; } +// FIXME we probably ought implement this with a trie or something +int ncmenu_item_set_status(ncmenu* n, const char* section, const char* item, + bool enabled){ + for(int si = 0 ; si < n->sectioncount ; ++si){ + const struct ncmenu_int_section* sec = &n->sections[si]; + if(strcmp(sec->name, section) == 0){ + for(int ii = 0 ; ii < sec->itemcount ; ++ii){ + struct ncmenu_int_item* i = &sec->items[ii]; + if(strcmp(i->desc, item) == 0){ + const bool changed = i->disabled != enabled; + i->disabled = !enabled; + if(changed && n->unrolledsection == si){ + ncmenu_unroll(n, n->unrolledsection); + } + return 0; + } + } + break; + } + } + return -1; +} + ncplane* ncmenu_plane(ncmenu* menu){ return menu->ncp; } diff --git a/src/poc/menu.c b/src/poc/menu.c index e4494f02d..bd5e8e2a0 100644 --- a/src/poc/menu.c +++ b/src/poc/menu.c @@ -88,6 +88,7 @@ int main(void){ notcurses_mouse_enable(nc); struct ncmenu_item demo_items[] = { { .desc = "Restart", .shortcut = { .id = 'r', .ctrl = true, }, }, + { .desc = "Disabled", .shortcut = { .id = 'd', .ctrl = false, }, }, }; struct ncmenu_item file_items[] = { { .desc = "New", .shortcut = { .id = 'n', .ctrl = true, }, }, @@ -125,6 +126,9 @@ int main(void){ if(top == NULL){ goto err; } + if(ncmenu_item_set_status(top, "Schwarzgerät", "Disabled", false)){ + goto err; + } uint64_t channels = 0; channels_set_fg_rgb(&channels, 0x88aa00); channels_set_bg_rgb(&channels, 0x000088);