ui: progress bar for file download
This commit is contained in:
+5
-2
@@ -1,9 +1,10 @@
|
||||
#include "app.h"
|
||||
#include "modals/ask_server_addr.h"
|
||||
#include "modals/download_pr.h"
|
||||
#include "modals/login_credentials.h"
|
||||
#include "modals/login_option.h"
|
||||
#include "modals/server_message.h"
|
||||
#include "widget/file_list.h"
|
||||
#include "widget/progress_bar.h"
|
||||
// #include "modals/server_message.h"
|
||||
// #include "action.h"
|
||||
#include <ncurses.h>
|
||||
@@ -151,6 +152,9 @@ void app_draw_modal(app_t *app) {
|
||||
case S_PRINT_SERVER_MESSAGE:
|
||||
init_server_message_modal(app);
|
||||
break;
|
||||
case S_FILE_DOWNLOAD:
|
||||
init_dwn_pr_modal(app);
|
||||
break;
|
||||
case WAIT_SERVER_INIT:
|
||||
case WAIT_SERVER:
|
||||
case WAIT_REGISTER:
|
||||
@@ -159,7 +163,6 @@ void app_draw_modal(app_t *app) {
|
||||
case UPLOAD_FILE:
|
||||
case S_FILE_LIST:
|
||||
case S_FILE_SELECT:
|
||||
case S_FILE_DOWNLOAD:
|
||||
case S_UPLOAD_PARAMS:
|
||||
case S_UPLOAD_FILE:
|
||||
case S_UPLOAD_REQUESTED:
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "../app.h"
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../widget/group.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void init_dwn_pr_modal_cb(callback_args_t *args) {
|
||||
}
|
||||
|
||||
dialogue_t *init_dwn_pr_modal(app_t *app) {
|
||||
if (app == NULL)
|
||||
return NULL;
|
||||
group_el_init_t content[] = {{.type = w_progress, .label = "", .length = 30}, /* length is ignored */
|
||||
{.type = w_end}};
|
||||
|
||||
char text[256];
|
||||
sprintf(text, "Downloading file: %s", app->file_args->f_selected.name);
|
||||
group_el_init_t actions[] = {
|
||||
{.type = w_button, .label = "Cancel", .is_default = 0}, {.type = w_end}};
|
||||
|
||||
app->modal.w.parent_win = &app->win;
|
||||
init_dialogue(&(app->modal), "Download", text, &(app->coordinates));
|
||||
dialogue_t *d = &(app->modal);
|
||||
|
||||
d->w.callback = init_dwn_pr_modal_cb;
|
||||
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
|
||||
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
|
||||
|
||||
app->query_args->progress_bar = d->g_content->elements[0].element;
|
||||
|
||||
dialogue_init_active_id(d);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef DOWNLOAD_PR_H
|
||||
#define DOWNLOAD_PR_H
|
||||
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../app.h"
|
||||
|
||||
dialogue_t *init_dwn_pr_modal(app_t *app);
|
||||
|
||||
#endif
|
||||
@@ -11,6 +11,7 @@ enum w_type {
|
||||
w_box,
|
||||
w_group,
|
||||
w_input,
|
||||
w_progress,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
|
||||
+18
-12
@@ -1,6 +1,7 @@
|
||||
#include "file_list.h"
|
||||
#include "../../file_processor.h"
|
||||
#include "../app.h"
|
||||
#include "progress_bar.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@@ -9,14 +10,16 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void reset_file_list(file_list_t *fl_ui);
|
||||
void reset_file_list(ui_file_list_t *fl_ui);
|
||||
|
||||
void file_list_cb(callback_args_t *args) {
|
||||
app_t *app = args->app;
|
||||
file_list_t *fui = app->query_args->file_list_ui;
|
||||
ui_file_list_t *fui = app->query_args->file_list_ui;
|
||||
int32_t key = *((int32_t *)args->data);
|
||||
char switch_page_query[10];
|
||||
int32_t spq_len = 0;
|
||||
char query[256];
|
||||
int32_t q_len = 0;
|
||||
fl_item_t *f_item;
|
||||
ui_progress_bar_t *pb;
|
||||
switch (key) {
|
||||
case KEY_DOWN:
|
||||
if (fui->current_idx < fui->current_count - 1) {
|
||||
@@ -25,8 +28,8 @@ void file_list_cb(callback_args_t *args) {
|
||||
} else if (fui->current_page < fui->pages) {
|
||||
fl_clear(fui->start, fui->current);
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
sprintf(switch_page_query, "p%u\n%n", fui->current_page + 1, &spq_len);
|
||||
write(app->query_args->sd, switch_page_query, spq_len);
|
||||
sprintf(query, "p%u\n%n", fui->current_page + 1, &q_len);
|
||||
write(app->query_args->sd, query, q_len);
|
||||
reset_file_list(fui);
|
||||
}
|
||||
break;
|
||||
@@ -37,17 +40,20 @@ void file_list_cb(callback_args_t *args) {
|
||||
} else if (fui->current_page > 1) {
|
||||
fl_clear(fui->start, fui->current);
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
sprintf(switch_page_query, "p%u\n%n", fui->current_page - 1, &spq_len);
|
||||
write(app->query_args->sd, switch_page_query, spq_len);
|
||||
sprintf(query, "p%u\n%n", fui->current_page - 1, &q_len);
|
||||
write(app->query_args->sd, query, q_len);
|
||||
reset_file_list(fui);
|
||||
fui->activate_last = true;
|
||||
}
|
||||
break;
|
||||
case '\n':
|
||||
ui_file_select(app->file_args, app->query_args, fui->current_idx+1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
file_list_t *init_file_list(WINDOW **win) {
|
||||
file_list_t *fl_ui = malloc(sizeof(file_list_t));
|
||||
ui_file_list_t *init_file_list(WINDOW **win) {
|
||||
ui_file_list_t *fl_ui = malloc(sizeof(ui_file_list_t));
|
||||
init_widget(&(fl_ui->w), NULL, win, "");
|
||||
fl_ui->current_idx = 0;
|
||||
fl_ui->current_page = 0;
|
||||
@@ -58,7 +64,7 @@ file_list_t *init_file_list(WINDOW **win) {
|
||||
return fl_ui;
|
||||
}
|
||||
|
||||
void reset_file_list(file_list_t *fl_ui) {
|
||||
void reset_file_list(ui_file_list_t *fl_ui) {
|
||||
fl_ui->current_idx = 0;
|
||||
fl_ui->current_page = 0;
|
||||
fl_ui->pages = 0;
|
||||
@@ -67,7 +73,7 @@ void reset_file_list(file_list_t *fl_ui) {
|
||||
fl_ui->activate_last = false;
|
||||
}
|
||||
|
||||
void draw_file_list(file_list_t *fl_ui) {
|
||||
void draw_file_list(ui_file_list_t *fl_ui) {
|
||||
int32_t sz_y, sz_x;
|
||||
int32_t p_y, p_x;
|
||||
getmaxyx(*(fl_ui->w.parent_win), sz_y, sz_x);
|
||||
|
||||
@@ -12,10 +12,10 @@ typedef struct {
|
||||
uint32_t pages;
|
||||
uint32_t current_page;
|
||||
uint32_t activate_last : 1;
|
||||
} file_list_t;
|
||||
} ui_file_list_t;
|
||||
|
||||
void draw_file_list(file_list_t *fl_ui);
|
||||
void redraw_file_list(file_list_t *fl_ui);
|
||||
file_list_t *init_file_list(WINDOW **win);
|
||||
void draw_file_list(ui_file_list_t *fl_ui);
|
||||
void redraw_file_list(ui_file_list_t *fl_ui);
|
||||
ui_file_list_t *init_file_list(WINDOW **win);
|
||||
void file_list_cb(callback_args_t *args);
|
||||
#endif
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "group.h"
|
||||
#include "button.h"
|
||||
#include "input.h"
|
||||
#include "progress_bar.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
@@ -87,6 +88,10 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
||||
init_input(win, &(group->w), children[i].label, children[i].length);
|
||||
w = &(((input_t *)elements[i].element)->w);
|
||||
break;
|
||||
case w_progress:
|
||||
elements[i].element = init_ui_progress_bar(win, &(group->w));
|
||||
w = &(((ui_progress_bar_t *)elements[i].element)->w);
|
||||
break;
|
||||
}
|
||||
// set dimensions
|
||||
elements[i].id = w->id;
|
||||
@@ -125,6 +130,9 @@ void draw_group(WINDOW *win, group_t *group, int32_t active_id,
|
||||
case w_input:
|
||||
draw_input((input_t *)el->element, active_id);
|
||||
break;
|
||||
case w_progress:
|
||||
draw_ui_progress_bar((ui_progress_bar_t *)el->element);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,6 +152,9 @@ void destroy_group(group_t *group) {
|
||||
case w_input:
|
||||
destroy_input(el->element);
|
||||
break;
|
||||
case w_progress:
|
||||
destroy_ui_progress_bar(el->element);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (group->elements != NULL) {
|
||||
|
||||
@@ -45,8 +45,6 @@ int32_t draw_input(input_t *input, uint32_t active_id) {
|
||||
mvwvline(win, margin_y + 1, margin_x + input->w.x - 1, 0, 1);
|
||||
mvwvline(win, margin_y + 2, margin_x + input->w.x - 1, ACS_LRCORNER, 1);
|
||||
|
||||
// TODO: correct sizes (x)
|
||||
|
||||
margin_x++;
|
||||
|
||||
if (input->w.id == active_id) {
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "progress_bar.h"
|
||||
#include "../app.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
ui_progress_bar_t *init_ui_progress_bar(WINDOW **win, widget_t *w_parent) {
|
||||
WINDOW *win_par = *(w_parent->w_parent->parent_win);
|
||||
ui_progress_bar_t *pb = malloc(sizeof(ui_progress_bar_t));
|
||||
init_widget(&pb->w, w_parent, win, "");
|
||||
pb->procent = 0;
|
||||
pb->text[0] = '\0';
|
||||
pb->w.y = 4;
|
||||
pb->w.x = getmaxx(win_par) / 10 * 9;
|
||||
return pb;
|
||||
}
|
||||
|
||||
void draw_ui_progress_bar(ui_progress_bar_t *pb) {
|
||||
WINDOW *win = *(pb->w.w_parent->parent_win);
|
||||
uint32_t margin_y = pb->w.m_y + pb->w.w_parent->m_y;
|
||||
uint32_t margin_x = pb->w.m_x + pb->w.w_parent->m_x + 1;
|
||||
|
||||
wattrset(win, COLOR_PAIR(modal_color_pair));
|
||||
|
||||
mvwhline(win, margin_y, margin_x, 0, pb->w.x - 1);
|
||||
|
||||
float procent = pb->procent;
|
||||
procent /= 100;
|
||||
uint32_t filled_num = procent * pb->w.x;
|
||||
if (filled_num > 2) {
|
||||
filled_num -= 2;
|
||||
}
|
||||
|
||||
for (int i = 1; i < 3; i++) {
|
||||
wattrset(win, COLOR_PAIR(1) | A_REVERSE);
|
||||
mvwprintw(win, margin_y + i, margin_x + 1, "%*s", filled_num, "");
|
||||
}
|
||||
|
||||
wattrset(win, COLOR_PAIR(modal_color_pair));
|
||||
|
||||
// bottom line
|
||||
mvwhline(win, margin_y + 3, margin_x, 0, pb->w.x - 1);
|
||||
// left
|
||||
mvwvline(win, margin_y, margin_x, ACS_ULCORNER, 1);
|
||||
mvwvline(win, margin_y + 1, margin_x, 0, 1);
|
||||
mvwvline(win, margin_y + 2, margin_x, 0, 1);
|
||||
mvwvline(win, margin_y + 3, margin_x, ACS_LLCORNER, 1);
|
||||
// right
|
||||
mvwvline(win, margin_y, margin_x + pb->w.x - 2, ACS_URCORNER, 1);
|
||||
mvwvline(win, margin_y + 1, margin_x + pb->w.x - 2, 0, 1);
|
||||
mvwvline(win, margin_y + 2, margin_x + pb->w.x - 2, 0, 1);
|
||||
mvwvline(win, margin_y + 3, margin_x + pb->w.x - 2, ACS_LRCORNER, 1);
|
||||
}
|
||||
|
||||
void destroy_ui_progress_bar(void *_pb) {
|
||||
ui_progress_bar_t *pb = (ui_progress_bar_t *)_pb;
|
||||
free(pb);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef PROGRESS_BAR_H
|
||||
#define PROGRESS_BAR_H
|
||||
#include "../widget.h"
|
||||
typedef struct {
|
||||
widget_t w;
|
||||
uint32_t procent;
|
||||
char text[24];
|
||||
} ui_progress_bar_t;
|
||||
void destroy_ui_progress_bar(void *_pb);
|
||||
void draw_ui_progress_bar(ui_progress_bar_t *pb);
|
||||
ui_progress_bar_t *init_ui_progress_bar(WINDOW **win, widget_t *w_parent);
|
||||
#endif
|
||||
Reference in New Issue
Block a user