implement ncmenu_item_set_status #1057

This commit is contained in:
nick black 2020-10-13 23:25:41 -04:00 committed by Nick Black
parent 7d528dcd6f
commit be2bba7be2
3 changed files with 31 additions and 0 deletions

View File

@ -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.

View File

@ -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;
}

View File

@ -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);