From: Daniel Liu Date: Wed, 3 Feb 2021 04:49:06 +0000 (-0500) Subject: menu focus X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=30e6d7ecd064b2425f438293787a26d89781548b;p=taskasaur.git menu focus --- diff --git a/config.h b/config.h index a38ea94..d805395 100644 --- a/config.h +++ b/config.h @@ -19,8 +19,10 @@ static char font[] = "Source Code Pro:size=12"; * COLOR_WHITE */ -static int selected_color = COLOR_CYAN; +static int selected_color = COLOR_MAGENTA; static int non_selected_color = COLOR_WHITE; +static int menu_selected_color = COLOR_CYAN; +static int menu_non_selected_color = COLOR_GREEN; /* KEYS */ #define BINDING_QUIT 'q' diff --git a/headers/menu.h b/headers/menu.h index b1ef7ee..7db3c07 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -2,6 +2,8 @@ #ifndef __MENU_H__ #define __MENU_H__ +#include + /* ncurses doesn't support multiline menu items, * so i wrote my own menu lib */ @@ -23,6 +25,7 @@ extern MenuItem* create_menuitem(char* contents); extern Menu* create_menu(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 menu_driver(Menu* menu, MenuAction action); diff --git a/headers/render.h b/headers/render.h index 4232f92..ac094aa 100644 --- a/headers/render.h +++ b/headers/render.h @@ -6,7 +6,9 @@ enum TaskasaurColors { TS_SELECTED = 1, - TS_NONSELECTED + TS_NONSELECTED, + TS_MENU_SELECTED, + TS_MENU_NONSELECTED, }; /* the ts is used to not conflict with builtin names */ diff --git a/makefile b/makefile index eaa0b02..62337a7 100644 --- a/makefile +++ b/makefile @@ -18,7 +18,7 @@ utils.o: utils.c headers/utils.h # include config.h as depend -taskasaur: taskasaur.c parser.o render.o menu.o utils.o +taskasaur: taskasaur.c parser.o render.o menu.o utils.o config.h $(CC) -o $@ $^ -lncurses -lmd4c clean: diff --git a/menu.c b/menu.c index 997fc15..5c79587 100644 --- a/menu.c +++ b/menu.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "headers/menu.h" @@ -21,6 +22,8 @@ typedef struct Menu { MenuItem** menu_items; int menu_length; int selected_item; + int scroll_offset; + bool focused; WINDOW* menu_win; WINDOW* sub_win; int max_height; @@ -50,6 +53,8 @@ create_menu(MenuItem** item_list) new_menu->menu_items = item_list; new_menu->menu_length = array_length(MenuItem*, item_list); new_menu->selected_item = 0; + new_menu->scroll_offset = 0; + new_menu->focused = false; set_menu_win(new_menu, stdscr); return new_menu; @@ -83,6 +88,14 @@ get_menu_items(Menu* menu) return NULL; } +int +set_menu_focus(Menu* menu, bool focus) +{ + menu->focused = focus; + + return 0; +} + int swap_item(Menu* menu, int src_index, int dest_index) { @@ -155,7 +168,7 @@ menu_driver(Menu* menu, MenuAction action) case MENU_DELETE: delete_item(menu, menu->selected_item); - wclear(menu->menu_win); + wclear(menu->sub_win); break; default: // This is here for debug, disable later @@ -169,25 +182,33 @@ int render_menu(Menu* menu) { /* draw outer menu (prob dont need this every render) */ + int menu_header_color; + + /* menu_header_color = */ + wattron(menu->menu_win, COLOR_PAIR( + (menu->focused == true) ? + TS_MENU_SELECTED: TS_MENU_NONSELECTED + )); mvwprintw(menu->menu_win, 0, MENU_PAD_LEFT, "TODO"); + wattroff(menu->menu_win, COLOR_PAIR(0)); /* draw inner menu */ int cur_line = 0; - for (int i = 0; i < menu->menu_length; i++) { + for (int i = 0; i < menu->menu_length-menu->scroll_offset; i++) { int wrapped_lines; char* wrapped_text; - int text_color; /* wrap text by inserting newlines (maxwidth-1 for newline char)*/ wrapped_text = wrap_text(menu->menu_items[i]->contents, menu->max_width-1, &wrapped_lines); /* color selected item */ - text_color = (i == menu->selected_item) ? TS_SELECTED : TS_NONSELECTED; - - wattron(menu->sub_win, COLOR_PAIR(text_color)); + wattron(menu->sub_win, COLOR_PAIR( + (i == menu->selected_item && menu->focused == true) ? + TS_SELECTED : TS_NONSELECTED + )); mvwprintw(menu->sub_win, cur_line, 0, wrapped_text); - wattroff(menu->sub_win, COLOR_PAIR(text_color)); + wattroff(menu->sub_win, COLOR_PAIR(0)); cur_line += wrapped_lines; diff --git a/render.c b/render.c index de22b1d..c4812af 100644 --- a/render.c +++ b/render.c @@ -44,6 +44,8 @@ init_tscolors(void) { init_pair(TS_SELECTED, selected_color, COLOR_BLACK); init_pair(TS_NONSELECTED, non_selected_color, COLOR_BLACK); + init_pair(TS_MENU_SELECTED, menu_selected_color, COLOR_BLACK); + init_pair(TS_MENU_NONSELECTED, menu_non_selected_color, COLOR_BLACK); return 0; } diff --git a/taskasaur.c b/taskasaur.c index e201d86..d8aa46d 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -26,6 +26,7 @@ main(int argc, char** argv) Menu* menu = create_menu(item_list); WINDOW* win = newwin(20, 40, 5, 5); set_menu_win(menu, win); + set_menu_focus(menu, true); box(win, 0, 0); refresh(); wrefresh(win);