From ba9631a51345e710b2482489e5f2a7232b30e6f1 Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Tue, 19 May 2026 16:08:39 +0300 Subject: [PATCH] ui: cancel option fo file upload --- TODO.md | 2 + src/modals/load_pr.c | 10 ++++- src/widget/alert.c | 98 ++++++++++++++++++++++++++++++++++++++++++-- src/widget/alert.h | 6 ++- 4 files changed, 110 insertions(+), 6 deletions(-) diff --git a/TODO.md b/TODO.md index d5a1f62..92535be 100644 --- a/TODO.md +++ b/TODO.md @@ -5,6 +5,8 @@ - [ ] Separate common structures in the common.h - [ ] Sorting in the file list - [ ] Cancel upload/download +- [ ] Break line automatically in modal widgets +- [ ] Show download/upload progress in percentages - [x] Input: fix non-Latin letters - [x] Get rid of unsigned integers - [x] Check registration / enter server name again diff --git a/src/modals/load_pr.c b/src/modals/load_pr.c index 14019ec..49a72f8 100644 --- a/src/modals/load_pr.c +++ b/src/modals/load_pr.c @@ -1,3 +1,5 @@ +#include "alert.h" +#include "dialogue.h" #include "group.h" #include #include @@ -11,7 +13,13 @@ void dwn_pr_modal_cb(w_cb_args_t *args) { return; } app_t *app = args->app; - app->query_args->file->signal = sig_cancel; + /* TODO: forward actual file name */ + bool response = w_bool_ask("Cancel file load", "Yes", "No", false, + "You're going to cancel download\n Are you sure?", + app->query_args->file->name); + if (response) { + app->query_args->file->signal = sig_cancel; + } } w_dialogue_t *m_load_pr_init(app_t *app, bool is_upload) { diff --git a/src/widget/alert.c b/src/widget/alert.c index 64d254a..ffb6d53 100644 --- a/src/widget/alert.c +++ b/src/widget/alert.c @@ -1,4 +1,6 @@ #include "alert.h" +#include "dialogue.h" +#include "widget_core.h" #include #include #include @@ -15,10 +17,12 @@ void alert_cb(w_cb_args_t *args) { void w_alert_init(app_t *_app) { app = _app; } -void w_alert(const char *message) { w_notification("Alert", dc_alert, message); } +void w_alert(const char *message) { + w_notification("Alert", dc_alert, message); +} void w_notification(const char *title, enum w_dialogue_color_scheme color, - const char *f_message, ...) { + const char *f_message, ...) { if (app == NULL) return; @@ -36,7 +40,7 @@ void w_notification(const char *title, enum w_dialogue_color_scheme color, d->w.callback = alert_cb; d->g_content = NULL; d->g_action = w_group_init(&(d->win), &(d->w), actions, &(d->id_map), - horizontal, g_action); + horizontal, g_action); w_dialogue_init_active_id(d); d->color_scheme = color; @@ -58,4 +62,92 @@ void w_notification(const char *title, enum w_dialogue_color_scheme color, if (app->modal.win != NULL) { app->modal.needs_update = true; } +} + +int w_bool_callback(app_t *app, w_dialogue_t *dialogue, int32_t key) { + w_cb_args_t args; + args.app = app; + args.active_el = dialogue; + args.element = dialogue; + int _key = key; + args.data = (void *)&_key; + w_dialogue_callback_default(&args); + if (args.resp_data.code == cbrp_val) { + switch (args.resp_data.val.val.num) { + case 1: + return 1; + case 2: + return 2; + default: + return -2; + } + } + return 0; +} + +int w_bool_ask(const char *title, const char *text_y_btn, + const char *text_n_btn, bool yes_default, const char *f_message, + ...) { + + va_list args; + va_start(args, f_message); + + if (app == NULL) + return -1; + + void *callback = app->active_callback; + + w_group_el_init_t actions[] = {{.type = w_button, .val.num = 1}, /* YES */ + {.type = w_button, .val.num = 2}, /* NO */ + {.type = w_end}}; + + strcpy(actions[0].label, text_y_btn); + strcpy(actions[1].label, text_n_btn); + + w_dialogue_t *d = malloc(sizeof(w_dialogue_t)); + + w_dialogue_vinit(d, title, &(app->coordinates), f_message, &args); + + d->w.callback = alert_cb; + d->g_content = NULL; + d->g_action = w_group_init(&(d->win), &(d->w), actions, &(d->id_map), + horizontal, g_action); + + if (yes_default) { + d->active_el = d->g_action->elements; + } else { + d->active_el = d->g_action->elements + 1; + } + + w_dialogue_draw(d); + wrefresh(d->win); + int c = 0; + + bool response = false; + + while ((c = wgetch(app->win))) { + int ret = w_bool_callback(app, d, c); + switch (ret) { + case 1: + response = true; + goto break_while; + case 2: + goto break_while; + } + w_dialogue_draw(d); + wrefresh(d->win); + } + +break_while: + + w_group_destroy(d->g_action); + delwin(d->win); + free(d); + app->active_callback = callback; + app_refresh(app); + if (app->modal.win != NULL) { + app->modal.needs_update = true; + } + + return response; } \ No newline at end of file diff --git a/src/widget/alert.h b/src/widget/alert.h index 3c66d15..9a20b65 100644 --- a/src/widget/alert.h +++ b/src/widget/alert.h @@ -7,6 +7,8 @@ void w_alert_init(app_t *app); void w_alert(const char *message); void w_notification(const char *title, enum w_dialogue_color_scheme color, - const char *f_message, ...); - + const char *f_message, ...); +int w_bool_ask(const char *title, const char *yes_button, + const char *no_button, bool yes_default, const char *f_message, ...); + #endif \ No newline at end of file