UI: nested groups. not finished

This commit is contained in:
2026-02-20 15:19:52 +02:00
parent 48c2c5946d
commit 8c765191fb
8 changed files with 102 additions and 77 deletions
+1
View File
@@ -44,6 +44,7 @@ int main(int argc, char **argv) {
q_args->buf = malloc(INBUFSIZE); q_args->buf = malloc(INBUFSIZE);
app->query_args = q_args; app->query_args = q_args;
q_args->file_list_ui = init_file_list(&(app->left_win), &(app->right_win)); q_args->file_list_ui = init_file_list(&(app->left_win), &(app->right_win));
draw_file_list(q_args->file_list_ui);
/* init client to connect to the server */ /* init client to connect to the server */
app->params->sd = init_client(); app->params->sd = init_client();
+17 -7
View File
@@ -8,6 +8,8 @@
#include <widget.h> #include <widget.h>
#include "alert.h" #include "alert.h"
#include "group.h"
#include "widget_core.h"
typedef struct { typedef struct {
button_t *element; button_t *element;
@@ -46,8 +48,8 @@ void init_register_modal_cb(callback_args_t *args) {
app->params->uname[in_name->value_len] = 0; app->params->uname[in_name->value_len] = 0;
strncpy(app->params->pass, in_pass->value, in_pass->value_len); strncpy(app->params->pass, in_pass->value, in_pass->value_len);
app->params->pass[in_pass->value_len] = 0; app->params->pass[in_pass->value_len] = 0;
sprintf(query, "register %s %s\n%n", app->params->uname, app->params->pass, sprintf(query, "register %s %s\n%n", app->params->uname,
&qlen); app->params->pass, &qlen);
write(app->params->sd, query, qlen); write(app->params->sd, query, qlen);
app->query_args->state = S_WAIT_REGISTER_CONFIRMATION; app->query_args->state = S_WAIT_REGISTER_CONFIRMATION;
break; break;
@@ -61,18 +63,26 @@ void init_register_modal_cb(callback_args_t *args) {
dialogue_t *init_register_modal(app_t *app) { dialogue_t *init_register_modal(app_t *app) {
if (app == NULL) if (app == NULL)
return NULL; return NULL;
group_el_init_t content[] = { group_el_init_t content_name_email[] = {
{.type = w_input, .label = "Username", .length = 15}, {.type = w_input, .label = "Username", .length = 18},
{.type = w_input, {.type = w_input, .label = "Email", .length = 18},
{.type = w_end}};
group_el_init_t content_pass[] = {{.type = w_input,
.label = "Password", .label = "Password",
.length = 15, .length = 18,
.is_hidden_value = true}, .is_hidden_value = true},
{.type = w_input, {.type = w_input,
.label = "Password (repeat)", .label = "Password (repeat)",
.length = 15, .length = 18,
.is_hidden_value = true}, .is_hidden_value = true},
{.type = w_end}}; {.type = w_end}};
group_el_init_t content[] = {
{.type = w_group, .direction = vertical, .children = content_name_email},
{.type = w_group, .direction = vertical, .children = content_pass},
{.type = w_end}};
group_el_init_t actions[] = { group_el_init_t actions[] = {
{.type = w_button, .label = "Register", .is_default = true}, {.type = w_button, .label = "Register", .is_default = true},
{.type = w_button, .label = "Cancel", .is_default = false}, {.type = w_button, .label = "Cancel", .is_default = false},
+10 -2
View File
@@ -21,8 +21,16 @@ button_t *init_button(WINDOW **win, widget_t *w_parent, char *label) {
int32_t draw_button(button_t *btn, uint32_t active_id) { int32_t draw_button(button_t *btn, uint32_t active_id) {
char title[DIALOGUE_TITLE + 6]; char title[DIALOGUE_TITLE + 6];
WINDOW *win = *(btn->w.parent_win); WINDOW *win = *(btn->w.parent_win);
uint32_t margin_y = btn->w.m_y + btn->w.w_parent->m_y; uint32_t margin_y = btn->w.m_y;
uint32_t margin_x = btn->w.m_x + btn->w.w_parent->m_x; uint32_t margin_x = btn->w.m_x;
widget_t *w_par = btn->w.w_parent;
while (w_par->parent_win == btn->w.parent_win) {
margin_y += w_par->m_y;
margin_x += w_par->m_x;
w_par = w_par->w_parent;
}
if (btn->w.id == active_id) { if (btn->w.id == active_id) {
sprintf(title, "[< %s >]", btn->w.title); sprintf(title, "[< %s >]", btn->w.title);
+6 -34
View File
@@ -179,7 +179,8 @@ void init_dialogue(dialogue_t *dialogue, const char title[], const char text[],
t_size = strlen(text); t_size = strlen(text);
strcpy(dialogue->text, text); strcpy(dialogue->text, text);
/* trim the last new line symbol */ /* trim the last new line symbol */
if (t_size && dialogue->text[t_size-1] == '\n') dialogue->text[t_size-1] = '\0'; if (t_size && dialogue->text[t_size - 1] == '\n')
dialogue->text[t_size - 1] = '\0';
} }
void dialogue_init_active_id(dialogue_t *dialogue) { void dialogue_init_active_id(dialogue_t *dialogue) {
@@ -211,34 +212,6 @@ void dialogue_init_active_id(dialogue_t *dialogue) {
} }
} }
// void dialogue_init_active_id(dialogue_t *dialogue) {
// enum w_type widget_type;
// if (dialogue->g_content != NULL) {
// for (int i = 0; i < dialogue->g_content->count; i++) {
// widget_type = dialogue->g_content->elements[i].type;
// if (widget_type == w_button || widget_type == w_input ||
// widget_type == w_fs_file_list) { /* Add here new types */
// dialogue->active.type = g_content;
// widget_t *w = (widget_t *)dialogue->g_content->elements[i].element;
// dialogue->active.id = w->id;
// break;
// }
// }
// } else if (dialogue->g_action != NULL) {
// for (int i = 0; i < dialogue->g_action->count; i++) {
// widget_type = dialogue->g_action->elements[i].type;
// if (widget_type == w_button || widget_type == w_input ||
// widget_type == w_fs_file_list) {
// dialogue->active.type = g_action;
// widget_t *w = (widget_t *)dialogue->g_action->elements[i].element;
// dialogue->active.id = w->id;
// break;
// }
// }
// }
// }
#define DETECT_GROUP_SIZE(group, line_max_len, y, x) \ #define DETECT_GROUP_SIZE(group, line_max_len, y, x) \
if (group) { \ if (group) { \
group->w.m_y = y; \ group->w.m_y = y; \
@@ -317,10 +290,10 @@ int32_t draw_dialogue(dialogue_t *d) {
wattroff(d->win, A_REVERSE); wattroff(d->win, A_REVERSE);
if (d->g_content != NULL) { if (d->g_content != NULL) {
draw_group(d->win, d->g_content, d->active.id, &(d->w)); draw_group(d->win, d->g_content, d->active.id);
} }
if (d->g_action != NULL) { if (d->g_action != NULL) {
draw_group(d->win, d->g_action, d->active.id, &(d->w)); draw_group(d->win, d->g_action, d->active.id);
} }
/* move cursor */ /* move cursor */
@@ -331,8 +304,7 @@ int32_t draw_dialogue(dialogue_t *d) {
FIND_ACTIVE_ELEMENT(d->g_action, d->active.id, ae_ptr, ae_idx); FIND_ACTIVE_ELEMENT(d->g_action, d->active.id, ae_ptr, ae_idx);
} }
if (ae_ptr != NULL && ae_ptr->id == d->active.id) { if (ae_ptr != NULL && ae_ptr->id == d->active.id && ae_ptr->type == w_input) {
if (ae_ptr->type == w_input) {
input_t *input = ae_ptr->element; input_t *input = ae_ptr->element;
d->w.cur.y = input->w.cur.y; d->w.cur.y = input->w.cur.y;
d->w.cur.x = input->w.cur.x + input->value_len; d->w.cur.x = input->w.cur.x + input->value_len;
@@ -341,10 +313,10 @@ int32_t draw_dialogue(dialogue_t *d) {
wmove(d->win, d->w.cur.y, d->w.cur.x); wmove(d->win, d->w.cur.y, d->w.cur.x);
curs_set(true); curs_set(true);
} }
} else { } else {
curs_set(false); curs_set(false);
} }
}
d->needs_update = false; d->needs_update = false;
+9 -2
View File
@@ -114,6 +114,8 @@ ui_file_list_t *init_file_list(WINDOW **win, WINDOW *const *info_win) {
fui->max_lines = getmaxy(parent_win) - 3; /* 2+1 (bottom info line) */ fui->max_lines = getmaxy(parent_win) - 3; /* 2+1 (bottom info line) */
fui->active_search = false; fui->active_search = false;
fui->search_key = bfromcstrrangealloc(12, 64, ""); fui->search_key = bfromcstrrangealloc(12, 64, "");
fui->start = NULL;
fui->current = NULL;
return fui; return fui;
} }
@@ -135,11 +137,16 @@ void draw_file_list(ui_file_list_t *fui) {
int32_t sz_y_f = sz_y - 2; /* TODO: можно заменить на поле max_lines */ int32_t sz_y_f = sz_y - 2; /* TODO: можно заменить на поле max_lines */
p_y = 1; p_y = 1;
p_x = 1; p_x = 1;
fl_item_t *el = *(fui->start); fl_item_t *el = NULL;
fl_item_t *active_el = *(fui->start); fl_item_t *active_el = NULL;
int32_t cur_el_idx = 0; int32_t cur_el_idx = 0;
curs_set(false); curs_set(false);
if (fui->start != NULL) {
el = *(fui->start);
active_el = el;
}
if (fui->activate_last) { if (fui->activate_last) {
fui->current_idx = fui->current_count - 1; fui->current_idx = fui->current_count - 1;
fui->activate_last = false; fui->activate_last = false;
+23 -5
View File
@@ -3,12 +3,14 @@
#include "fs_file_list.h" #include "fs_file_list.h"
#include "input.h" #include "input.h"
#include "progress_bar.h" #include "progress_bar.h"
#include "widget_core.h"
#include <ncurses.h> #include <ncurses.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
/* make response -1 */
#define MAKE_RESPONSE_M1(args, resp_data, response) \ #define MAKE_RESPONSE_M1(args, resp_data, response) \
if (args->resp_data != NULL) { \ if (args->resp_data != NULL) { \
*response = -1; \ *response = -1; \
@@ -104,9 +106,13 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
w = &(((button_t *)elements[i].element)->w); w = &(((button_t *)elements[i].element)->w);
break; break;
case w_box: case w_box:
case w_group:
case w_end: case w_end:
break; break;
case w_group:
elements[i].element = init_group(win, &(group->w), children[i].children,
children[i].direction);
w = &(((input_t *)elements[i].element)->w);
break;
case w_input: case w_input:
elements[i].element = elements[i].element =
init_input(win, &(group->w), children[i].label, children[i].length, init_input(win, &(group->w), children[i].label, children[i].length,
@@ -130,6 +136,12 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
group->w.x += w->x + 1; group->w.x += w->x + 1;
if (group->w.y < w->y) if (group->w.y < w->y)
group->w.y = w->y; group->w.y = w->y;
} else {
w->m_x = group->w.m_x;
w->m_y = group->w.m_y + group->w.y;
group->w.y += w->y;
if (group->w.x < w->x)
group->w.x = w->x;
} }
if (i == 0) { if (i == 0) {
group->first_id = w->id; group->first_id = w->id;
@@ -139,11 +151,13 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
} }
} }
if (direction == vertical)
group->w.y++;
return group; return group;
} }
void draw_group(WINDOW *win, group_t *group, int32_t active_id, void draw_group(WINDOW *win, group_t *group, int32_t active_id) {
widget_t *dialog_w) {
group_el_t *children = group->elements; group_el_t *children = group->elements;
for (int i = 0; i < group->count; i++) { for (int i = 0; i < group->count; i++) {
group_el_t *el = &children[i]; group_el_t *el = &children[i];
@@ -151,8 +165,10 @@ void draw_group(WINDOW *win, group_t *group, int32_t active_id,
case w_button: case w_button:
draw_button((button_t *)el->element, active_id); draw_button((button_t *)el->element, active_id);
break; break;
case w_box:
case w_group: case w_group:
draw_group(win, (group_t *)el->element, active_id);
break;
case w_box:
case w_end: case w_end:
break; break;
case w_input: case w_input:
@@ -176,8 +192,10 @@ void destroy_group(group_t *group) {
case w_button: case w_button:
destroy_button(el->element); destroy_button(el->element);
break; break;
case w_box:
case w_group: case w_group:
destroy_group(el->element);
break;
case w_box:
case w_end: case w_end:
break; break;
case w_input: case w_input:
+6 -5
View File
@@ -16,12 +16,14 @@ typedef struct {
uint32_t is_default : 1; uint32_t is_default : 1;
} group_el_t; } group_el_t;
typedef struct { typedef struct group_el_init_t {
enum w_type type; enum w_type type;
uint32_t is_default : 1; bool is_default;
uint32_t is_hidden_value : 1; bool is_hidden_value;
char label[DIALOGUE_TITLE]; char label[DIALOGUE_TITLE];
uint32_t length; uint32_t length;
enum g_direction direction;
struct group_el_init_t *children;
} group_el_init_t; } group_el_init_t;
typedef struct { typedef struct {
@@ -38,8 +40,7 @@ typedef struct {
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children, group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
enum g_direction dir); enum g_direction dir);
void draw_group(WINDOW *win, group_t *group, int32_t active_id, void draw_group(WINDOW *win, group_t *group, int32_t active_id);
widget_t *dialog_w);
void destroy_group(group_t *group); void destroy_group(group_t *group);
void group_default_callback(callback_args_t *args); void group_default_callback(callback_args_t *args);
+13 -5
View File
@@ -4,8 +4,8 @@
#include <string.h> #include <string.h>
#include "app.h" #include "app.h"
#include <widget_core.h>
#include "input.h" #include "input.h"
#include <widget_core.h>
input_t *init_input(WINDOW **win, widget_t *w_parent, char *label, input_t *init_input(WINDOW **win, widget_t *w_parent, char *label,
uint32_t length, uint32_t is_hidden_value) { uint32_t length, uint32_t is_hidden_value) {
@@ -29,8 +29,16 @@ input_t *init_input(WINDOW **win, widget_t *w_parent, char *label,
int32_t draw_input(input_t *input, uint32_t active_id) { int32_t draw_input(input_t *input, uint32_t active_id) {
const char stars[] = "*******************"; const char stars[] = "*******************";
WINDOW *win = *(input->w.parent_win); WINDOW *win = *(input->w.parent_win);
uint32_t margin_y = input->w.m_y + input->w.w_parent->m_y; uint32_t margin_y = input->w.m_y;
uint32_t margin_x = input->w.m_x + input->w.w_parent->m_x; uint32_t margin_x = input->w.m_x;
/* counts margins of the ancestors of the same window */
widget_t *w_par = input->w.w_parent;
while (w_par->parent_win == input->w.parent_win) {
margin_y += w_par->m_y;
margin_x += w_par->m_x;
w_par = w_par->w_parent;
}
wattrset(win, COLOR_PAIR(modal_color_pair)); wattrset(win, COLOR_PAIR(modal_color_pair));
@@ -57,8 +65,8 @@ int32_t draw_input(input_t *input, uint32_t active_id) {
} }
if (input->is_hidden) { if (input->is_hidden) {
mvwprintw(win, margin_y + 1, margin_x, "%.*s%*s", (int) input->value_len, stars, mvwprintw(win, margin_y + 1, margin_x, "%.*s%*s", (int)input->value_len,
input->w.x - input->value_len - 2, ""); stars, input->w.x - input->value_len - 2, "");
} else { } else {
mvwprintw(win, margin_y + 1, margin_x, "%s%*s", input->value, mvwprintw(win, margin_y + 1, margin_x, "%s%*s", input->value,
input->w.x - input->value_len - 2, ""); input->w.x - input->value_len - 2, "");