From 369f7de279fff79bc017b8b821116ced8842a864 Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Wed, 10 Dec 2025 20:11:35 +0200 Subject: [PATCH] improvement of detection of active elements (not finished) --- src/main.c | 2 +- src/ui/modals/ask_server_addr.c | 19 ++++-- src/ui/modals/login.c | 4 +- src/ui/widget.h | 4 ++ src/ui/widget/dialogue.c | 107 +++++++++++++++++++++++++------- src/ui/widget/dialogue.h | 20 ++++-- src/ui/widget/group.c | 100 ++++++++++++++--------------- src/ui/widget/group.h | 7 +-- 8 files changed, 170 insertions(+), 93 deletions(-) diff --git a/src/main.c b/src/main.c index 0e855c6..b4f7dbc 100644 --- a/src/main.c +++ b/src/main.c @@ -110,7 +110,7 @@ void app_draw_modal(app_t *app) { default: break; } - draw_dialogue(NULL, &(app->modal.dialogue)); + draw_dialogue(&(app->modal.dialogue)); } } diff --git a/src/ui/modals/ask_server_addr.c b/src/ui/modals/ask_server_addr.c index 98d0956..48c0e21 100644 --- a/src/ui/modals/ask_server_addr.c +++ b/src/ui/modals/ask_server_addr.c @@ -30,17 +30,26 @@ void asa_modal_group_callback(callback_args_t *args) { dialogue_t *init_asa_modal(app_t *app) { if (app == NULL) return NULL; - group_el_init_t children[] = { + group_el_init_t content[] = { {.type = w_input, .label = "Address", .length = 15}, {.type = w_input, .label = "Port", .length = 5}, {.type = w_end}}; + group_el_init_t actions[] = {{.type = w_button, .label = "Connect"}, + {.type = w_button, .label = "Cancel"}, + {.type = w_end}}; + init_dialogue(&(app->modal.dialogue), "Connect to server", "There is information needed", app->cur_y, app->cur_x); - app->modal.dialogue.ch_group = - init_group(&(app->modal.dialogue.win), &(app->modal.dialogue.w), children, - horizontal); - app->modal.dialogue.ch_group->w.callback = asa_modal_group_callback; + dialogue_t *d = &(app->modal.dialogue); + + d->g_content = init_group(&(d->win), &(d->w), content, horizontal); + d->g_content->w.callback = asa_modal_group_callback; + + d->g_action = init_group(&(d->win), &(d->w), actions, horizontal); + d->g_action->w.callback = asa_modal_group_callback; + + dialogue_init_active_id(d); return NULL; } \ No newline at end of file diff --git a/src/ui/modals/login.c b/src/ui/modals/login.c index 7fc7a59..ca6ce10 100644 --- a/src/ui/modals/login.c +++ b/src/ui/modals/login.c @@ -40,10 +40,10 @@ dialogue_t *init_login_modal(app_t *app) { "Select your login " "option", app->cur_y, app->cur_x); - app->modal.dialogue.ch_group = + app->modal.dialogue.g_content = init_group(&(app->modal.dialogue.win), &(app->modal.dialogue.w), children, horizontal); - app->modal.dialogue.ch_group->w.callback = login_modal_group_callback; + app->modal.dialogue.g_content->w.callback = login_modal_group_callback; return NULL; } \ No newline at end of file diff --git a/src/ui/widget.h b/src/ui/widget.h index 99b18d5..c57111b 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -38,6 +38,10 @@ typedef struct widget_t { uint32_t y; uint32_t m_x; uint32_t m_y; + struct { + uint32_t y; + uint32_t x; + } cur; WINDOW *const *parent_win; struct widget_t *w_parent; enum rsize rsize; diff --git a/src/ui/widget/dialogue.c b/src/ui/widget/dialogue.c index 391cec1..a894645 100644 --- a/src/ui/widget/dialogue.c +++ b/src/ui/widget/dialogue.c @@ -3,9 +3,9 @@ #include #include +#include "../app.h" #include "dialogue.h" #include "group.h" -#include "../app.h" void dialogue_default_callback(callback_args_t *args) { dialogue_t *d = (void *)args->widget; @@ -14,25 +14,43 @@ void dialogue_default_callback(callback_args_t *args) { callback_args_t new_args; group_resp_t response; memcpy(&new_args, args, sizeof(callback_args_t)); - new_args.widget = d->ch_group; + new_args.widget = d->g_content; new_args.resp_data = &response; switch (key) { + case '\t': + // TODO: active task + if (d->active.type == g_content) { + if (d->active.id != d->g_content->last_id) { + d->active.id = d->active.id + 1; + } else { + d->active.id = d->g_content->first_id; + } + } + if (d->active.type == g_action) { + if (d->active.id != d->g_action->last_id) { + d->active.id = d->active.id + 1; + } else { + d->active.id = d->g_action->first_id; + } + } + draw_dialogue(d); + break; default: - d->ch_group->w.callback(&new_args); + d->g_content->w.callback(&new_args); if (response.value > -1) { d->is_initiated = 0; destroy_dialogue(d); app->modal.type = none; } else { - draw_dialogue(NULL, d); + draw_dialogue(d); } } } -void init_dialogue(dialogue_t *dialogue, const char title[], const char text[], uint32_t cur_y, - uint32_t cur_x) { +void init_dialogue(dialogue_t *dialogue, const char title[], const char text[], + uint32_t cur_y, uint32_t cur_x) { dialogue->win = 0; - dialogue->ch_group = NULL; + dialogue->g_content = NULL; dialogue->w.x = 0; dialogue->w.y = 0; dialogue->cur_y = cur_y; @@ -43,32 +61,70 @@ void init_dialogue(dialogue_t *dialogue, const char title[], const char text[], strcpy(dialogue->text, text); } -int32_t draw_dialogue(void *e, dialogue_t *d) { - if (!d->is_initiated) return -1; +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) { + 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) { + 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) \ + if (group) { \ + group->w.m_y = y; \ + if (line_max_len < group->w.x) { \ + line_max_len = group->w.x; \ + group->w.m_x = 1; /* when uses box */ \ + } else { \ + /* TODO: centering */ \ + } \ + y += group->w.y; \ + } + +int32_t draw_dialogue(dialogue_t *d) { + if (!d->is_initiated) + return -1; /* count dimensions */ uint32_t x = 1; /* when uses box */ uint32_t y = 1; /* when uses box */ + /* analyze text content */ uint32_t line_count = 0; uint32_t line_max_len = 0; line_max_len = get_max_line_len(d->text, &line_count); y += line_count; y += 2; /* margin for text */ - if (d->ch_group) { - d->ch_group->w.m_y = y; - if (line_max_len < d->ch_group->w.x) { - line_max_len = d->ch_group->w.x; - d->ch_group->w.m_x = 1; /* when uses box */ - } else { - /* TODO: centering */ - } - y += d->ch_group->w.y; - } + /* analyze groups */ + DETECT_GROUP_SIZE(d->g_content, line_max_len, y, x); + DETECT_GROUP_SIZE(d->g_action, line_max_len, y, x); x += line_max_len; x += 1; /* when uses box */ y += 1; /* when uses box */ + + /* space for action block */ + + y += 2; + d->w.x = x; d->w.y = y; d->w.m_y = (d->cur_y - y) / 2; @@ -96,16 +152,21 @@ int32_t draw_dialogue(void *e, dialogue_t *d) { /* text */ wattroff(d->win, A_BOLD); + /* 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); - draw_group(d->win, d->ch_group); - + if (d->g_content != NULL) { + draw_group(d->win, d->g_content, d->active.id); + } + if (d->g_action != NULL) { + draw_group(d->win, d->g_action, d->active.id); + } return 0; }; void destroy_dialogue(dialogue_t *d) { - if (d->ch_group) { - destroy_group(d->ch_group); + if (d->g_content) { + destroy_group(d->g_content); } delwin(d->win); d->win = NULL; diff --git a/src/ui/widget/dialogue.h b/src/ui/widget/dialogue.h index 8b6d45a..cf4d4ca 100644 --- a/src/ui/widget/dialogue.h +++ b/src/ui/widget/dialogue.h @@ -5,21 +5,31 @@ #include "button.h" #include "group.h" #include +#include + +enum g_type { g_content, g_action }; typedef struct { - WINDOW *win; widget_t w; - group_t *ch_group; + WINDOW *win; + group_t *g_content; + group_t *g_action; + struct { + int32_t id; + enum g_type type; + uint32_t y, x; + } active; uint32_t cur_y; uint32_t cur_x; char text[DIALOGUE_TEXT]; uint32_t is_initiated : 1; } dialogue_t; -void init_dialogue(dialogue_t *d, const char title[], const char text[], uint32_t cur_x, - uint32_t cur_y); -int32_t draw_dialogue(void *, dialogue_t *d); +void init_dialogue(dialogue_t *d, const char title[], const char text[], + uint32_t cur_x, uint32_t cur_y); +int32_t draw_dialogue(dialogue_t *d); void destroy_dialogue(dialogue_t *d); void dialogue_default_callback(callback_args_t *args); +void dialogue_init_active_id(dialogue_t *dialogue); #endif \ No newline at end of file diff --git a/src/ui/widget/group.c b/src/ui/widget/group.c index 06ae403..f09b660 100644 --- a/src/ui/widget/group.c +++ b/src/ui/widget/group.c @@ -5,67 +5,59 @@ #include #include +// TODO: refactor + +#define MAKE_RESPONSE_M1(args, resp_data, response) \ + if (args->resp_data != NULL) { \ + response->active_id = g->active_id; \ + response->value = -1; \ + } + +#define FIND_ELEMENT(args, element_ptr, element_idx) \ + if (args->resp_data != NULL) { \ + for (int i = 0; i < g->count; i++) { \ + if (g->elements[i].id == g->active_id) { \ + element_ptr = &(g->elements[i]); \ + element_idx = i; \ + break; \ + } \ + } \ + } + void group_default_callback(callback_args_t *args) { group_t *g = (group_t *)args->widget; char key = *((char *)args->data); group_resp_t *response = (group_resp_t *)args->resp_data; - input_t *inpt; + input_t *input; + group_el_t *element_ptr; + int32_t element_idx = -1; switch (key) { - case '\t': - if (g->active_id != g->last_id) { - g->active_id = g->active_id + 1; - } else { - g->active_id = g->first_id; - } - if (args->resp_data != NULL) { - response->active_id = g->active_id; - response->value = -1; - } + case '\n': /* Enter */ + FIND_ELEMENT(args, element_ptr, element_idx); + response->active_id = g->active_id; + response->value = element_idx; break; - case '\n': - if (args->resp_data != NULL) { - for (int i = 0; i < g->count; i++) { - if (g->elements[i].id == g->active_id) { - response->active_id = g->active_id; - response->value = i; - } + case '\177': /* DEL */ + FIND_ELEMENT(args, element_ptr, element_idx); + if (element_ptr->type == w_input) { + input = (input_t *)element_ptr->element; + if (input->value_len) { + input->value[--input->value_len] = '\0'; } } - break; - case '\177': - for (int i = 0; i < g->count; i++) { - if (g->elements[i].id == g->active_id) { - if (g->elements[i].type == w_input) { - inpt = (input_t *)g->elements[i].element; - if (inpt->value_len) { - inpt->value[--inpt->value_len] = '\0'; - } - break; - } - } - } - if (args->resp_data != NULL) { - response->active_id = g->active_id; - response->value = -1; - } + MAKE_RESPONSE_M1(args, resp_data, response); break; default: - for (int i = 0; i < g->count; i++) { - if (g->elements[i].id == g->active_id) { - if (g->elements[i].type == w_input) { - inpt = (input_t *)g->elements[i].element; - if (inpt->max_len > inpt->value_len) { - inpt->value[inpt->value_len++] = key; - inpt->value[inpt->value_len] = '\0'; - } - break; - } + FIND_ELEMENT(args, element_ptr, element_idx); + if (element_ptr->type == w_input) { + input = (input_t *)element_ptr->element; + if (input->max_len > input->value_len) { + input->value[input->value_len++] = key; + input->value[input->value_len] = '\0'; } } - if (args->resp_data != NULL) { - response->active_id = g->active_id; - response->value = -1; - } + MAKE_RESPONSE_M1(args, resp_data, response); + break; } } @@ -126,20 +118,20 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children, return group; } -void draw_group(WINDOW *win, group_t *group) { +void draw_group(WINDOW *win, group_t *group, int32_t active_id) { group_el_t *children = group->elements; for (int i = 0; i < group->count; i++) { group_el_t *el = &children[i]; switch (el->type) { case w_button: - draw_button((button_t *)el->element, group->active_id); + draw_button((button_t *)el->element, active_id); break; case w_box: case w_group: case w_end: break; case w_input: - draw_input((input_t *)el->element, group->active_id); + draw_input((input_t *)el->element, active_id); break; } } @@ -152,7 +144,9 @@ void draw_group(WINDOW *win, group_t *group) { if (input->max_len == input->value_len) { wmove(win, margin_y + 1, margin_x + input->value_len); } else { - wmove(win, margin_y + 1, margin_x + input->value_len + 1); // x: +1: margin left from the border + wmove(win, margin_y + 1, + margin_x + input->value_len + + 1); // x: +1: margin left from the border } curs_set(1); } diff --git a/src/ui/widget/group.h b/src/ui/widget/group.h index fd88aa1..4f89452 100644 --- a/src/ui/widget/group.h +++ b/src/ui/widget/group.h @@ -22,13 +22,12 @@ typedef struct { } group_el_init_t; typedef struct { - group_el_t *elements; widget_t w; - uint32_t active_id; + group_el_t *elements; + uint32_t active_id; // TODO: needs to be removed enum g_direction direction; uint32_t count; uint32_t first_id, last_id; - void *action_args; } group_t; typedef struct { @@ -38,7 +37,7 @@ typedef struct { group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children, enum g_direction dir); -void draw_group(WINDOW *win, group_t *group); +void draw_group(WINDOW *win, group_t *group, int32_t active_id); void destroy_group(group_t *group); void group_default_callback(callback_args_t *args);