[dynui] Allow for multiple flags on a user interface item

Signed-off-by: Michael Brown <mcb30@ipxe.org>
pull/1243/head
Michael Brown 2024-06-20 16:20:05 -07:00
parent c8e50bb0fd
commit 039019039e
4 changed files with 17 additions and 10 deletions

View File

@ -96,13 +96,13 @@ struct dynamic_ui * create_dynui ( const char *name, const char *title ) {
* @v dynui Dynamic user interface * @v dynui Dynamic user interface
* @v name Name, or NULL * @v name Name, or NULL
* @v text Text, or NULL * @v text Text, or NULL
* @v flags Flags
* @v shortcut Shortcut key * @v shortcut Shortcut key
* @v is_default Item is the default item
* @ret item User interface item, or NULL on failure * @ret item User interface item, or NULL on failure
*/ */
struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui, struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui,
const char *name, const char *text, const char *name, const char *text,
int shortcut, int is_default ) { unsigned int flags, int shortcut ) {
struct dynamic_item *item; struct dynamic_item *item;
size_t name_len; size_t name_len;
size_t text_len; size_t text_len;
@ -132,8 +132,8 @@ struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui,
strcpy ( text_copy, text ); strcpy ( text_copy, text );
item->text = text_copy; item->text = text_copy;
item->index = dynui->count++; item->index = dynui->count++;
item->flags = flags;
item->shortcut = shortcut; item->shortcut = shortcut;
item->is_default = is_default;
/* Add to list of items */ /* Add to list of items */
list_add_tail ( &item->list, &dynui->items ); list_add_tail ( &item->list, &dynui->items );

View File

@ -148,6 +148,7 @@ static int item_exec ( int argc, char **argv ) {
struct item_options opts; struct item_options opts;
struct dynamic_ui *dynui; struct dynamic_ui *dynui;
struct dynamic_item *item; struct dynamic_item *item;
unsigned int flags = 0;
char *name = NULL; char *name = NULL;
char *text = NULL; char *text = NULL;
int rc; int rc;
@ -174,8 +175,10 @@ static int item_exec ( int argc, char **argv ) {
goto err_parse_dynui; goto err_parse_dynui;
/* Add dynamic user interface item */ /* Add dynamic user interface item */
item = add_dynui_item ( dynui, name, ( text ? text : "" ), if ( opts.is_default )
opts.key, opts.is_default ); flags |= DYNUI_DEFAULT;
item = add_dynui_item ( dynui, name, ( text ? text : "" ), flags,
opts.key );
if ( ! item ) { if ( ! item ) {
rc = -ENOMEM; rc = -ENOMEM;
goto err_add_dynui_item; goto err_add_dynui_item;

View File

@ -267,7 +267,7 @@ int show_menu ( struct dynamic_ui *dynui, unsigned long timeout,
if ( strcmp ( select, item->name ) == 0 ) if ( strcmp ( select, item->name ) == 0 )
ui.scroll.current = ui.scroll.count; ui.scroll.current = ui.scroll.count;
} else { } else {
if ( item->is_default ) if ( item->flags & DYNUI_DEFAULT )
ui.scroll.current = ui.scroll.count; ui.scroll.current = ui.scroll.count;
} }
} }

View File

@ -35,17 +35,21 @@ struct dynamic_item {
const char *text; const char *text;
/** Index */ /** Index */
unsigned int index; unsigned int index;
/** Flags */
unsigned int flags;
/** Shortcut key */ /** Shortcut key */
int shortcut; int shortcut;
/** Is default item */
int is_default;
}; };
/** Dynamic user interface item is default selection */
#define DYNUI_DEFAULT 0x0001
extern struct dynamic_ui * create_dynui ( const char *name, const char *title ); extern struct dynamic_ui * create_dynui ( const char *name, const char *title );
extern struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui, extern struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui,
const char *name, const char *name,
const char *text, int shortcut, const char *text,
int is_default ); unsigned int flags,
int shortcut );
extern void destroy_dynui ( struct dynamic_ui *dynui ); extern void destroy_dynui ( struct dynamic_ui *dynui );
extern struct dynamic_ui * find_dynui ( const char *name ); extern struct dynamic_ui * find_dynui ( const char *name );
extern struct dynamic_item * dynui_item ( struct dynamic_ui *dynui, extern struct dynamic_item * dynui_item ( struct dynamic_ui *dynui,