diff --git a/TODO.md b/TODO.md index 4ac545b..0a873d9 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,10 @@ # TODO: ## Current: -- [ ] Sorting in file list +- [ ] Separate common structures in the common.h +- [ ] Bottom panel attach to the windows +- [ ] Testing +- [ ] Sorting in the file list - [ ] File upload multiline description (window) - [ ] More intuitive navigation between nested groups (by pressing left and right buttons) - [ ] Cancel upload/download diff --git a/src/common.h b/src/common.h index c06bded..99c5c77 100644 --- a/src/common.h +++ b/src/common.h @@ -3,9 +3,26 @@ #ifndef COMMON_H #define COMMON_H + #include + typedef struct { uint32_t cur_y, cur_x; uint32_t max_y, max_x; } coordinates_t; + +#define MAIN_UI_RESET(app) \ + app->main_ui.ui = NULL; \ + app->main_ui.cb_refresh = NULL; \ + app->main_ui.cb_b_press = NULL; \ + app->main_ui.b_keys = NULL; + + +struct action_key { + char *const key; + char *const title; + int const code; + int (*callback)(void *app, void *d_args); +}; + #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 562a818..b6f77d1 100644 --- a/src/main.c +++ b/src/main.c @@ -17,16 +17,14 @@ #include "app.h" #include "connection.h" #include "file_list.h" -#include "fs_file_list.h" +#include "main_window.h" + #include "main.h" #include "query.h" uint32_t m_id = 0; int32_t process_user_input(w_app_t *app, w_cb_args_t *d_args); -void main_window_set(w_app_t *app, enum main_window_type type); -void main_window_draw(w_app_t *app); -void main_window_destroy(w_app_t *app); int main(int argc, char **argv) { w_app_t *app; @@ -103,51 +101,4 @@ int32_t process_user_input(w_app_t *app, w_cb_args_t *d_args) { break; } return OK; -} - -void main_window_set(w_app_t *app, enum main_window_type type) { - if (app->main_ui.ui != NULL) { - main_window_destroy(app); - } - app->main_ui.type = type; - switch (type) { - case mw_fl_server: - app->main_ui.ui = w_fl_init(app); - app->active_callback = w_fl_cb; - break; - case mw_fl_local: - app->main_ui.ui = w_lfl_init_win(app); - app->active_callback = w_lfl_cb; - break; - case mw_f_desc: - break; - default: - break; - } -} - -void main_window_draw(w_app_t *app) { - switch (app->main_ui.type) { - case mw_fl_server: - w_fl_draw((w_ui_file_list_t *)app->main_ui.ui); - break; - case mw_fl_local: - w_lfl_draw((w_lfl_ui_t *)app->main_ui.ui); - break; - case mw_f_desc: - break; - } -} - -void main_window_destroy(w_app_t *app) { - switch (app->main_ui.type) { - case mw_fl_server: - w_fl_destroy((w_ui_file_list_t **)&(app->main_ui.ui)); - app->main_ui.cb_refresh = NULL; - break; - case mw_fl_local: - /* TODO: delete fs_file_list */ - case mw_f_desc: - break; - } } \ No newline at end of file diff --git a/src/main.h b/src/main.h index bad12b6..1c07d3d 100644 --- a/src/main.h +++ b/src/main.h @@ -4,6 +4,7 @@ #ifndef MAIN_H #define MAIN_H +#include "common.h" #include #include #include @@ -91,7 +92,9 @@ enum main_window_type { typedef struct main_window { enum main_window_type type; void *ui; /* structure which describes window UI */ + int32_t (*cb_b_press) (void *app, void *args); /* callback on button press */ void (*cb_refresh) (void *app); /* refresh callback */ + struct action_key *b_keys; } main_window_t; typedef struct query_args { diff --git a/src/main_window.c b/src/main_window.c new file mode 100644 index 0000000..370aa03 --- /dev/null +++ b/src/main_window.c @@ -0,0 +1,49 @@ +#include "main_window.h" +#include "fs_file_list.h" + +void main_window_set(w_app_t *app, enum main_window_type type) { + if (app->main_ui.ui != NULL) { + main_window_destroy(app); + } + app->main_ui.type = type; + switch (type) { + case mw_fl_server: + app->main_ui.ui = w_fl_init(app); + app->active_callback = w_fl_cb; + break; + case mw_fl_local: + app->main_ui.ui = w_lfl_init_win(app); + app->active_callback = w_lfl_cb; + break; + case mw_f_desc: + break; + default: + break; + } +} + +void main_window_draw(w_app_t *app) { + switch (app->main_ui.type) { + case mw_fl_server: + w_fl_draw((w_ui_file_list_t *)app->main_ui.ui); + break; + case mw_fl_local: + w_lfl_draw((w_lfl_ui_t *)app->main_ui.ui); + break; + case mw_f_desc: + break; + } +} + +void main_window_destroy(w_app_t *app) { + switch (app->main_ui.type) { + case mw_fl_server: + w_fl_destroy((w_ui_file_list_t **)&(app->main_ui.ui)); + MAIN_UI_RESET(app); + break; + case mw_fl_local: + /* TODO: delete fs_file_list */ + case mw_f_desc: + break; + } +} \ No newline at end of file diff --git a/src/main_window.h b/src/main_window.h new file mode 100644 index 0000000..4707cdf --- /dev/null +++ b/src/main_window.h @@ -0,0 +1,10 @@ +#ifndef MAIN_WINDOW_H +#define MAIN_WINDOW_H + +#include + +void main_window_set(w_app_t *app, enum main_window_type type); +void main_window_draw(w_app_t *app); +void main_window_destroy(w_app_t *app); + +#endif \ No newline at end of file diff --git a/src/widget/app.c b/src/widget/app.c index 2891ddf..1b73dab 100644 --- a/src/widget/app.c +++ b/src/widget/app.c @@ -43,8 +43,7 @@ w_app_t *w_app_init() { w_app_draw_borders(_app); /* NULL to main_ui */ - _app->main_ui.ui = NULL; - _app->main_ui.cb_refresh = NULL; + MAIN_UI_RESET(_app); keypad(_app->win, TRUE); @@ -83,11 +82,6 @@ void w_app_draw_borders(w_app_t *app) { box(app->win, 0, 0); } -struct action_key { - char *const key; - char *const title; -}; - int64_t print_bottom_menu_option(WINDOW *win, char *key, char *title, int64_t y, int64_t x, int32_t size) { int32_t key_len = 0; diff --git a/src/widget/file_list.c b/src/widget/file_list.c index 08cb831..53ccfa7 100644 --- a/src/widget/file_list.c +++ b/src/widget/file_list.c @@ -15,6 +15,7 @@ #include #include #include +#include "../main_window.h" void w_fl_reset(w_ui_file_list_t *fl_ui); @@ -125,11 +126,53 @@ static void w_fl_cb_refresh(void *data) { wnoutrefresh(fui->win_info); } +static struct action_key action_keys[] = { + {.key = "F1", .title = "Help", .code = KEY_F(1)}, + {.key = "U", .title = "Upload", .code = 'u'}, + {.key = "S", .title = "Search", .code = 's'}, + {.key = "F9", .title = "Quit", .code = KEY_F(9)}, +}; + +int32_t process_user_input(w_app_t *app, w_cb_args_t *d_args) { + int32_t c; + w_ui_file_list_t *fui = (w_ui_file_list_t *)app->query_args->main_ui->ui; + c = wgetch(app->win); + switch (c) { + case KEY_F(9): + w_app_destroy(app, 0); + return OK; + case 'U': + case 'u': + if (!app->modal.is_initiated && !fui->active_search && + app->main_ui.type != mw_fl_local) { + /* app->query_args->state = S_UPLOAD_FILE_SELECT; */ + main_window_set(app, mw_fl_local); + main_window_draw(app); + d_args->element = app->main_ui.ui; + break; + } + case 's': + case 'S': + if (!app->modal.is_initiated && !fui->active_search) { + fui->active_search = true; + w_fl_draw(fui); + return OK; + } + default: + d_args->data = (void *)&c; + app->active_callback(d_args); + break; + } + return OK; +} + w_ui_file_list_t *w_fl_init(w_app_t *app) { w_ui_file_list_t *fui = malloc(sizeof(w_ui_file_list_t)); app->main_ui.ui = fui; app->main_ui.type = mw_fl_server; app->main_ui.cb_refresh = w_fl_cb_refresh; + app->main_ui.b_keys = action_keys; + app->main_ui.cb_b_press = process_user_input; w_init(&(fui->w), NULL, &(app->win), ""); WINDOW *win_parent = app->win; fui->current_idx = 0; @@ -158,7 +201,6 @@ w_ui_file_list_t *w_fl_init(w_app_t *app) { fui->win_info = newwin(app->coordinates.max_y - 4, right_w_x, 2, left_w_x + 1); fui->max_lines = getmaxy(fui->win_list) - 3; /* 2+1 (bottom info line) */ - return fui; } diff --git a/src/widget_core/widget_core.h b/src/widget_core/widget_core.h index d7c788e..77ee704 100644 --- a/src/widget_core/widget_core.h +++ b/src/widget_core/widget_core.h @@ -72,6 +72,8 @@ typedef struct { void *active_el; } w_cb_args_t; +typedef void (*w_cb_t)(w_cb_args_t *args); + typedef struct w_t { int64_t id; char title[DIALOGUE_TITLE]; @@ -93,11 +95,9 @@ typedef struct w_t { } ps; /* position (relative to the window) */ WINDOW *const *parent_win; struct w_t *w_parent; - void (*callback)(w_cb_args_t *args); + w_cb_t callback; } w_t; -typedef void (*w_cb_t)(w_cb_args_t *args); - enum w_pmt_attrs { PMT_POS_CENTER = 01, PMT_ALIGN_CENTER = 03,