From: Daniel Liu Date: Thu, 4 Feb 2021 05:13:59 +0000 (-0500) Subject: jump to similar position between menus X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=1231bcf06132e735fa69870b7de2898e44efba3b;p=taskasaur.git jump to similar position between menus --- diff --git a/headers/menu.h b/headers/menu.h index 9de9cb4..48e5bdd 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -26,6 +26,9 @@ extern Menu* create_menu(char* menu_name, MenuItem** item_list); extern int set_menu_win(Menu* menu, WINDOW* win); extern MenuItem* get_menu_items(Menu* menu); extern int set_menu_focus(Menu* menu, bool focus); +extern int get_selected_item(Menu* menu); +extern int set_selected_item(Menu* menu, int selected_item); +extern int get_menu_length(Menu* menu); extern int menu_driver(Menu* menu, MenuAction action); diff --git a/menu.c b/menu.c index d47c553..4d27ec4 100644 --- a/menu.c +++ b/menu.c @@ -98,6 +98,26 @@ set_menu_focus(Menu* menu, bool focus) return 0; } +int +get_selected_item(Menu* menu) +{ + return menu->selected_item; +} + +int +set_selected_item(Menu* menu, int selected_item) +{ + menu->selected_item = selected_item; + + return 0; +} + +int +get_menu_length(Menu* menu) +{ + return menu->menu_length; +} + int swap_item(Menu* menu, int src_index, int dest_index) { diff --git a/taskasaur.c b/taskasaur.c index 03c3ab4..793f0fa 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -17,7 +17,6 @@ int set_selected_menu(BoardMenu* boardmenu, int index); MenuItem** todolist_to_menuitem(TodoItem** item_list, int list_length); Menu** make_menus(Board* board, int todolist_length); - int main(int argc, char** argv) { @@ -74,7 +73,6 @@ main(int argc, char** argv) case BINDING_DELETE_ITEM: menu_driver(active_menu, MENU_DELETE); break; - } for (int i = 0; i < boardmenu->menu_count; i++) { @@ -106,6 +104,7 @@ set_selected_menu(BoardMenu* boardmenu, int index) { Menu* old_menu; Menu* new_menu; + int new_pos; old_menu = boardmenu->menu_list[boardmenu->selected]; new_menu = boardmenu->menu_list[index]; @@ -113,6 +112,12 @@ set_selected_menu(BoardMenu* boardmenu, int index) set_menu_focus(old_menu, false); set_menu_focus(new_menu, true); + /* also try to jump to a similar position if possible */ + /* rn theres a bug if old menu is empty */ + new_pos = (get_selected_item(old_menu) > get_menu_length(new_menu)-1) ? + get_menu_length(new_menu)-1 : get_selected_item(old_menu); + set_selected_item(new_menu, new_pos); + boardmenu->selected = index; return 0;