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
+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;