ui: optimization of dialogue refresh

This commit is contained in:
2025-12-31 15:50:46 +02:00
parent f1757b4ca7
commit 48460e4aa2
7 changed files with 44 additions and 21 deletions
+12 -8
View File
@@ -1,9 +1,3 @@
#include "client.h"
#include "main.h"
#include "query.h"
#include "ui/widget/dialogue.h"
#include "ui/widget/file_list.h"
#include "ui/widget/progress_bar.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
@@ -20,6 +14,13 @@
#include <termios.h>
#include <unistd.h>
#include "client.h"
#include "main.h"
#include "query.h"
#include "ui/widget/dialogue.h"
#include "ui/widget/file_list.h"
#include "ui/widget/progress_bar.h"
static void fl_add(fl_item_t **cur, fl_item_t **start, char *fname);
fl_item_t *fl_select(fl_item_t *start, int num);
void fl_clear(fl_item_t **start, fl_item_t **current);
@@ -158,7 +159,7 @@ int32_t ui_file_select(file_args_t *f_args, query_args_t *q_args, int32_t idx) {
f_selected->name = NULL;
sprintf(file_path, "%s/%s", DOWNLOADS_DIR, l_selected->name);
f_selected->name = strdup(l_selected->name);
fl_clear(&f_args->l_start, &f_args->l_current);
/* fl_clear(&f_args->l_start, &f_args->l_current); */
f_args->file_d = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (f_args->file_d == -1) {
qlen = sprintf(send_buf, "error: %s\n", f_selected->name);
@@ -181,7 +182,7 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
static uint32_t it_count = 0;
static size_t it_interval = 0;
if (it_interval == 0) {
it_interval = (f_selected->size / INBUFSIZE / 100) * 1; /* every 1% */
it_interval = (f_selected->size / INBUFSIZE / 100) * 5; /* every 1% */
if (it_interval == 0)
it_interval = 1;
}
@@ -189,6 +190,7 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
static size_t size_rest = 0;
uint32_t progress = (f_selected->size - size_rest) * 100 / f_selected->size;
ui_progress_bar_t *pb = (ui_progress_bar_t *)q_args->progress_bar;
dialogue_t *d = (dialogue_t *)q_args->active_dialogue;
if (size_rest == 0)
size_rest = f_selected->size;
@@ -197,6 +199,7 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
it_count++;
if (!(it_count % it_interval)) {
pb->procent = progress;
d->needs_update = true;
}
if (it_count % it_interval)
q_args->buf_used = 0;
@@ -211,6 +214,7 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
f_selected->size = 0;
close(f_args->file_d);
free(f_selected->name);
d->needs_destroy = true;
q_args->state = WAIT_CLIENT;
}
}
+1
View File
@@ -86,6 +86,7 @@ typedef struct query_args {
} server_message;
void *file_list_ui; /* TODO: find solution to insert actual type without recursion */
void *progress_bar;
void *active_dialogue;
} query_args_t;
#endif
+1
View File
@@ -262,6 +262,7 @@ void init_query_args(query_args_t *q_args, params_t *params) {
q_args->server_message.size = 0;
q_args->next_server_command = NULL;
q_args->progress_bar = NULL;
q_args->active_dialogue = NULL;
}
void user_request_description(query_args_t *q_args) {
+8 -3
View File
@@ -4,9 +4,7 @@
#include "modals/login_credentials.h"
#include "modals/login_option.h"
#include "modals/server_message.h"
#include "widget/progress_bar.h"
// #include "modals/server_message.h"
// #include "action.h"
#include "widget/dialogue.h"
#include <ncurses.h>
#include <netinet/in.h>
#include <stdint.h>
@@ -84,6 +82,9 @@ void init_nc() {
}
void draw_borders(app_t *app) {
if (app->modal.win != NULL && !app->modal.needs_update) {
return;
}
clear();
box(app->win, 0, 0);
box(app->left_win, 0, 0);
@@ -178,8 +179,12 @@ void app_draw_modal(app_t *app) {
app->active_win_type = aw_modal;
app->active_widget = &(app->modal);
}
if (app->modal.needs_destroy) {
destroy_dialogue(&(app->modal), app);
} else {
draw_dialogue(&(app->modal));
}
}
void destroy_app(app_t *app) {
delwin(app->win);
+1
View File
@@ -28,6 +28,7 @@ dialogue_t *init_dwn_pr_modal(app_t *app) {
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
app->query_args->progress_bar = d->g_content->elements[0].element;
app->query_args->active_dialogue = d;
dialogue_init_active_id(d);
+18 -9
View File
@@ -52,6 +52,7 @@ void dialogue_default_callback(callback_args_t *args) {
memcpy(&new_args, args, sizeof(callback_args_t));
new_args.active_id = d->active.id;
int32_t diff;
d->needs_update = true;
switch (key) {
case KEY_RIGHT:
case '\t':
@@ -61,7 +62,7 @@ void dialogue_default_callback(callback_args_t *args) {
INCR_ACTIVE_ID(d, g_action, g_content);
}
*resp_value = -1;
draw_dialogue(d);
break;
case KEY_LEFT:
if (d->active.type == g_content) {
@@ -70,7 +71,7 @@ void dialogue_default_callback(callback_args_t *args) {
DECR_ACTIVE_ID(d, g_action, g_content);
}
*resp_value = -1;
draw_dialogue(d);
break;
case '\n':
if (d->active.type == g_action) {
@@ -88,15 +89,15 @@ void dialogue_default_callback(callback_args_t *args) {
*resp_value = d->active.id - d->g_content->first_id;
return;
}
draw_dialogue(d);
break;
case KEY_UP:
CH_GROUP(d, g_action, g_content);
draw_dialogue(d);
break;
case KEY_DOWN:
CH_GROUP(d, g_content, g_action);
draw_dialogue(d);
break;
default:
/* run callback function */
@@ -106,9 +107,6 @@ void dialogue_default_callback(callback_args_t *args) {
new_args.widget = new_args.widget = d->g_action;
}
group_default_callback(&new_args);
if (*resp_value == -1) {
draw_dialogue(d);
}
}
}
@@ -120,7 +118,9 @@ void init_dialogue(dialogue_t *dialogue, const char title[], const char text[],
dialogue->w.y = 0;
dialogue->p_coordinates = p_coordinates;
dialogue->w.callback = dialogue_default_callback;
dialogue->is_initiated = 1;
dialogue->is_initiated = true;
dialogue->needs_update = true;
dialogue->needs_destroy = false;
strcpy(dialogue->w.title, title);
strcpy(dialogue->text, text);
}
@@ -166,6 +166,10 @@ void dialogue_init_active_id(dialogue_t *dialogue) {
int32_t draw_dialogue(dialogue_t *d) {
if (!d->is_initiated)
return -1;
else if (!d->needs_update) {
return 0;
}
group_el_t *ae_ptr = NULL; /* active element */
uint32_t ae_idx; /* active element */
/* count dimensions */
@@ -196,6 +200,7 @@ int32_t draw_dialogue(dialogue_t *d) {
if (d->win == NULL) {
d->win = newwin(y, x, d->w.m_y, d->w.m_x);
}
wattrset(d->win, COLOR_PAIR(0) | A_BOLD | A_REVERSE);
/* background */
@@ -215,6 +220,7 @@ int32_t draw_dialogue(dialogue_t *d) {
/* mvwhline(d->win, d->w.y - 3, 1, 0, d->w.x - 2); */
print_multiline_text(d->win, d->text, d->w.x, 2, 1, PMT_ALIGN_CENTER);
wattroff(d->win, A_REVERSE);
if (d->g_content != NULL) {
draw_group(d->win, d->g_content, d->active.id, &(d->w));
}
@@ -247,6 +253,8 @@ int32_t draw_dialogue(dialogue_t *d) {
}
}
d->needs_update = false;
return 0;
};
@@ -264,4 +272,5 @@ void destroy_dialogue(dialogue_t *d, void *_app) {
app->active_win_type = aw_left;
app->active_win = app->left_win;
app->active_callback = file_list_cb;
app->query_args->active_dialogue = NULL;
}
+2
View File
@@ -21,6 +21,8 @@ typedef struct {
} active;
char text[DIALOGUE_TEXT];
uint32_t is_initiated : 1;
uint32_t needs_update : 1;
uint32_t needs_destroy: 1;
coordinates_t *p_coordinates;
} dialogue_t;