Replace char *key_binding with an enum

This commit is contained in:
Luca Nimmrichter 2022-10-20 18:40:27 +02:00 committed by Jari Vetoniemi
parent a8c89c41a7
commit 0c2bc885a1
5 changed files with 31 additions and 21 deletions

View File

@ -482,7 +482,11 @@ do_getopt(struct client *client, int *argc, char **argv[])
client->colors[BM_COLOR_BORDER] = optarg;
break;
case 0x128:
client->key_binding = optarg;
if(strcmp(optarg, "vim") == 0){
client->key_binding = BM_KEY_BINDING_VIM;
} else {
client->key_binding = BM_KEY_BINDING_DEFAULT;
}
break;
case 0x116:

View File

@ -36,7 +36,7 @@ struct client {
bool force_fork, fork;
bool no_exec;
bool password;
char *key_binding;
enum bm_key_binding key_binding;
char *monitor_name;
};

View File

@ -353,6 +353,16 @@ enum bm_color {
BM_COLOR_LAST
};
/**
* Key bindings
*/
enum bm_key_binding {
BM_KEY_BINDING_DEFAULT,
BM_KEY_BINDING_VIM,
};
/**
* @name Menu Memory
* @{ */
@ -790,12 +800,11 @@ BM_PUBLIC bool bm_menu_get_password(struct bm_menu *menu);
/**
* Specify the key bindings that should be used.
* If an invalid key_binding is provided it will fall back to the default one.
*
* @param menu bm_menu instance to set the key binding on.
* @param key_binding binding name that should be used. (available: vim)
* @param key_binding binding that should be used.
*/
BM_PUBLIC void bm_menu_set_key_binding(struct bm_menu *menu, char *key_binding);
BM_PUBLIC void bm_menu_set_key_binding(struct bm_menu *menu, enum bm_key_binding);
/** @} Properties */

View File

@ -414,9 +414,8 @@ struct bm_menu {
/**
* Key binding that should be used.
* If empty or invalid the default bindings will be used. (available: vim)
*/
char *key_binding;
enum bm_key_binding key_binding;
/**
* Vim binding specific variables.

View File

@ -55,7 +55,7 @@ bm_menu_new(const char *renderer)
menu->dirty = true;
menu->key_binding = NULL;
menu->key_binding = BM_KEY_BINDING_DEFAULT;
menu->vim_mode = 'i';
menu->vim_last_key = 0;
@ -690,7 +690,7 @@ bm_menu_set_selected_items(struct bm_menu *menu, struct bm_item **items, uint32_
}
void
bm_menu_set_key_binding(struct bm_menu *menu, char *key_binding){
bm_menu_set_key_binding(struct bm_menu *menu, enum bm_key_binding key_binding){
menu->key_binding = key_binding;
}
@ -950,19 +950,17 @@ bm_menu_run_with_key(struct bm_menu *menu, enum bm_key key, uint32_t unicode)
if (key != BM_KEY_NONE)
menu->dirty = true;
if(menu->key_binding != NULL){
if(strcmp(menu->key_binding, "vim") == 0){
enum bm_vim_code code = bm_vim_key_press(menu, key, unicode, count, displayed);
if(menu->key_binding == BM_KEY_BINDING_VIM){
enum bm_vim_code code = bm_vim_key_press(menu, key, unicode, count, displayed);
switch(code){
case BM_VIM_CONSUME:
return BM_RUN_RESULT_RUNNING;
case BM_VIM_EXIT:
list_free_list(&menu->selection);
return BM_RUN_RESULT_CANCEL;
case BM_VIM_IGNORE:
break;
}
switch(code){
case BM_VIM_CONSUME:
return BM_RUN_RESULT_RUNNING;
case BM_VIM_EXIT:
list_free_list(&menu->selection);
return BM_RUN_RESULT_CANCEL;
case BM_VIM_IGNORE:
break;
}
}