menu: implement menu accelerators

Menu accelerators are one-letter mnemonics to quickly select/exec
items from the current menu. For each menu item, the accelerator is
defined as the first character of the item label, converted to
lowercase. A different accelerator can be explicitly defined in
menu.xml with the special '_' character before the target letter.

- Add a field `accelerator` to the `menuitem` struct
- Implement `menu_item_select_by_accelerator()`

Example:
The accelerator for an item with the label "e_macs" is 'm'.
This commit is contained in:
Alex Chernika 2026-04-10 14:52:24 +02:00 committed by Johan Malm
parent 07a0a4e59b
commit 3632c6703a
4 changed files with 190 additions and 8 deletions

View file

@ -23,9 +23,11 @@ struct menuitem {
char *text;
char *icon_name;
const char *arrow;
uint32_t accelerator;
struct menu *parent;
struct menu *submenu;
bool selectable;
bool use_markup;
enum menuitem_type type;
int native_width;
struct wlr_scene_tree *tree;
@ -66,6 +68,19 @@ struct menu {
/* For keyboard support */
void menu_item_select_next(void);
void menu_item_select_previous(void);
/**
* menu_item_select_by_accelerator - selects the next menu item with
* a matching accelerator, starting after the current selection
*
* @accelerator a shortcut to quickly select/open an item, defined in menu.xml
* with an underscore in the item label before the target letter.
*
* Return: a boolean value that represents whether the newly selected item
* needs to be executed.
*/
bool menu_item_select_by_accelerator(uint32_t accelerator);
void menu_submenu_enter(void);
void menu_submenu_leave(void);
bool menu_call_selected_actions(void);
@ -100,7 +115,7 @@ void menu_open_root(struct menu *menu, int x, int y);
void menu_process_cursor_motion(struct wlr_scene_node *node);
/**
* menu_close_root- close root menu
* menu_close_root - close root menu
*
* This function will close server.menu_current and set it to NULL.
* Asserts that server.input_mode is set to LAB_INPUT_STATE_MENU.