improvement of detection of active elements (not finished)

This commit is contained in:
2025-12-10 20:11:35 +02:00
parent ad1fa9e973
commit 369f7de279
8 changed files with 170 additions and 93 deletions
+14 -5
View File
@@ -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;
}
+2 -2
View File
@@ -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;
}
+4
View File
@@ -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;
+84 -23
View File
@@ -3,9 +3,9 @@
#include <string.h>
#include <sys/types.h>
#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;
+15 -5
View File
@@ -5,21 +5,31 @@
#include "button.h"
#include "group.h"
#include <ncurses.h>
#include <stdint.h>
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
+47 -53
View File
@@ -5,67 +5,59 @@
#include <stdlib.h>
#include <sys/types.h>
// 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);
}
+3 -4
View File
@@ -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);