ui: refactoring. changes active id mechanism

This commit is contained in:
2026-02-24 16:36:00 +02:00
parent 57b858f98f
commit 86c11968da
17 changed files with 104 additions and 126 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ void notification(const char *title, const char *message,
d->w.callback = alert_cb;
d->g_content = NULL;
d->g_action = init_group(&(d->win), &(d->w), actions,&(d->id_map), horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions,&(d->id_map), horizontal, g_action);
dialogue_init_active_id(d);
d->color_scheme = color;
+3 -2
View File
@@ -10,6 +10,7 @@
#include <widget.h>
#include "../connection.h"
#include "group.h"
typedef struct {
button_t *element;
@@ -60,8 +61,8 @@ dialogue_t *init_asa_modal(app_t *app) {
dialogue_t *d = &(app->modal);
d->w.callback = asa_modal_dialogue_callback;
d->g_content = init_group(&(d->win), &(d->w), content,&(d->id_map), horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions,&(d->id_map), horizontal);
d->g_content = init_group(&(d->win), &(d->w), content,&(d->id_map), horizontal, g_content);
d->g_action = init_group(&(d->win), &(d->w), actions,&(d->id_map), horizontal, g_action);
dialogue_init_active_id(d);
+3 -2
View File
@@ -1,3 +1,4 @@
#include "group.h"
#include <widget.h>
#include <ncurses.h>
#include <stdint.h>
@@ -24,8 +25,8 @@ dialogue_t *init_dwn_pr_modal(app_t *app, bool is_upload) {
dialogue_t *d = &(app->modal);
d->w.callback = dwn_pr_modal_cb;
d->g_content = init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal);
d->g_content = init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal, g_content);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal, g_action);
app->query_args->progress_bar = d->g_content->elements[0].element;
app->query_args->active_dialogue = d;
+3 -2
View File
@@ -1,3 +1,4 @@
#include "group.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
@@ -65,8 +66,8 @@ dialogue_t *init_login_credentials_modal(app_t *app) {
dialogue_t *d = &(app->modal);
d->w.callback = init_login_credentials_modal_cb;
d->g_content = init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal);
d->g_content = init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal, g_content);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal, g_action);
dialogue_init_active_id(d);
+2 -1
View File
@@ -1,3 +1,4 @@
#include "group.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
@@ -56,7 +57,7 @@ dialogue_t *init_login_option_modal(app_t *app) {
"option",
&(app->coordinates));
app->modal.g_action = init_group(&(app->modal.win), &(app->modal.w), children,
&(app->modal.id_map), horizontal);
&(app->modal.id_map), horizontal, g_action);
app->modal.w.callback = login_modal_option_cb;
dialogue_init_active_id(&(app->modal));
+2 -1
View File
@@ -1,3 +1,4 @@
#include "group.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
@@ -43,7 +44,7 @@ dialogue_t *init_notification_modal(app_t *app) {
d->w.callback = init_notification_modal_cb;
d->g_content = NULL;
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal, g_action);
dialogue_init_active_id(d);
+2 -2
View File
@@ -94,9 +94,9 @@ dialogue_t *init_register_modal(app_t *app) {
d->w.callback = init_register_modal_cb;
d->g_content =
init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal);
init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal, g_content);
d->g_action =
init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal);
init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal, g_action);
dialogue_init_active_id(d);
+2 -1
View File
@@ -1,3 +1,4 @@
#include "group.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
@@ -44,7 +45,7 @@ void *init_server_message_modal(app_t *app) {
init_dialogue(&(app->modal), "Server response",
app->query_args->server_message.text, &(app->coordinates));
app->modal.g_action =
init_group(&(app->modal.win), &(app->modal.w), children, &(app->modal.id_map), horizontal);
init_group(&(app->modal.win), &(app->modal.w), children, &(app->modal.id_map), horizontal, g_action);
app->modal.w.callback = server_message_modal_cb;
dialogue_init_active_id(&(app->modal));
+3 -2
View File
@@ -1,4 +1,5 @@
#include "../file_processor.h"
#include "group.h"
#include <widget.h>
#include <ncurses.h>
#include <stdint.h>
@@ -49,8 +50,8 @@ dialogue_t *init_upload_dialogue_modal(app_t *app) {
dialogue_t *d = &(app->modal);
d->w.callback = upload_dialogue_modal_cb;
d->g_content = init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal);
d->g_content = init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal, g_content);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal, g_action);
app->query_args->active_dialogue = d;
+3 -2
View File
@@ -1,3 +1,4 @@
#include "group.h"
#include <widget.h>
#include <ncurses.h>
#include <stdint.h>
@@ -64,8 +65,8 @@ dialogue_t *init_upload_props_dialogue_modal(app_t *app) {
dialogue_t *d = &(app->modal);
d->w.callback = upload_props_dialogue_modal_cb;
d->g_content = init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal);
d->g_content = init_group(&(d->win), &(d->w), content, &(d->id_map), horizontal, g_content);
d->g_action = init_group(&(d->win), &(d->w), actions, &(d->id_map), horizontal, g_action);
app->query_args->active_dialogue = d;
+10 -2
View File
@@ -2,18 +2,26 @@
#include "d_array.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
void init_d_arr_ptr(d_array_ptr_t *arr, int64_t init_capacity) {
arr->capacity = init_capacity;
arr->length = 0;
arr->arr = malloc(sizeof(void *) * init_capacity);
arr->fist_el = NULL;
arr->last_el = NULL;
memset(arr->arr, 0, init_capacity);
}
void add_d_arr_ptr(d_array_ptr_t *arr, void *ptr, int64_t idx) {
int64_t new_capacity = idx * 2;
if (idx > arr->capacity - 1) {
arr->capacity = idx * 2;
arr->arr = realloc(arr->arr, arr->capacity);
arr->arr = realloc(arr->arr, new_capacity);
memset(arr->arr+arr->capacity, 0, arr->capacity - new_capacity);
arr->capacity = new_capacity;
}
if (arr->fist_el == NULL) arr->fist_el = ptr;
arr->last_el = ptr;
arr->arr[idx] = ptr;
arr->length = idx+1;
}
+2
View File
@@ -6,6 +6,8 @@ typedef struct d_array_ptr_t {
int64_t capacity;
int64_t length;
void **arr;
void *fist_el;
void *last_el;
} d_array_ptr_t;
void init_d_arr_ptr(d_array_ptr_t *arr, int64_t init_capacity);
+52 -89
View File
@@ -8,26 +8,42 @@
#include "file_list.h"
#include "group.h"
#include "input.h"
#include "widget_core.h"
#define INCR_ACTIVE_ID(d, current, next) \
if (d->active.id != d->current->last_id) { \
d->active.id = d->active.id + 1; \
} else if (d->next != NULL) { \
d->active.id = d->next->first_id; \
d->active.type = next; \
} else { \
d->active.id = d->current->first_id; \
void incr_active_id(dialogue_t *d) {
group_el_t *active_el = NULL;
while (true) {
for (int i = d->active_el->id + 1;
active_el == NULL && d->id_map.length > i; i++) {
active_el = d->id_map.arr[i];
}
d->active_el = active_el;
if (d->active_el == NULL) {
d->active_el = d->id_map.fist_el;
break;
} else if (d->active_el->type != w_box && d->active_el->type != w_group) {
break;
}
active_el = NULL;
}
}
#define DECR_ACTIVE_ID(d, current, next) \
if (d->active.id != d->current->first_id) { \
d->active.id = d->active.id - 1; \
} else if (d->next != NULL) { \
d->active.id = d->next->last_id; \
d->active.type = next; \
} else { \
d->active.id = d->current->last_id; \
void decr_active_id(dialogue_t *d) {
group_el_t *active_el = NULL;
while (true) {
for (int i = d->active_el->id - 1; active_el == NULL && i > 0; i--) {
active_el = d->id_map.arr[i];
}
d->active_el = active_el;
if (d->active_el == NULL) {
d->active_el = d->id_map.last_el;
break;
} else if (d->active_el->type != w_box && d->active_el->type != w_group) {
break;
}
active_el = NULL;
}
}
#define CH_GROUP(d, current, next) \
{ \
@@ -42,82 +58,43 @@
} \
}
widget_t *get_active_widget(dialogue_t *d) {
group_t *g;
if (d->active.type == g_action) {
g = d->g_action;
} else {
g = d->g_content;
}
widget_t *w = (widget_t *)g->elements[d->active.id - g->first_id].element;
return w;
}
void dialogue_default_callback(callback_args_t *args) {
dialogue_t *d = (void *)args->element;
int32_t key = *((int32_t *)args->data);
callback_args_t new_args;
new_args.active_el = d->active_el;
int32_t *resp_value = (int32_t *)args->resp_data;
memcpy(&new_args, args, sizeof(callback_args_t));
new_args.active_id = d->active.id;
new_args.active_id = d->active_el->id;
int32_t diff;
d->needs_update = true;
input_t *input;
widget_t *widget;
switch (key) {
case '\t':
if (d->active.type == g_content) {
INCR_ACTIVE_ID(d, g_content, g_action);
} else if (d->active.type == g_action) {
INCR_ACTIVE_ID(d, g_action, g_content);
}
incr_active_id(d);
*resp_value = -1;
break;
case '\33': /* Esc key */
*resp_value = -2;
break;
case KEY_RIGHT:
if (d->active.type == g_content) {
if (d->g_content->elements[d->active.id - d->g_content->first_id].type ==
w_input) {
input = d->g_content->elements[d->active.id - d->g_content->first_id]
.element;
if (input->cur_pos > 0) {
input->cur_pos -= 1;
}
} else {
INCR_ACTIVE_ID(d, g_content, g_action);
}
} else if (d->active.type == g_action) {
INCR_ACTIVE_ID(d, g_action, g_content);
}
incr_active_id(d);
*resp_value = -1;
break;
case KEY_LEFT:
if (d->active.type == g_content) {
if (d->g_content->elements[d->active.id - d->g_content->first_id].type ==
w_input) {
input = d->g_content->elements[d->active.id - d->g_content->first_id]
.element;
if (input->value_len > input->cur_pos) {
input->cur_pos += 1;
}
} else {
DECR_ACTIVE_ID(d, g_content, g_action);
}
} else if (d->active.type == g_action) {
DECR_ACTIVE_ID(d, g_action, g_content);
}
decr_active_id(d);
*resp_value = -1;
break;
case '\n':
widget = get_active_widget(d);
widget = (widget_t *)d->active_el->element;
if (widget->callback != NULL) { /* Existing callback case */
new_args.element = widget;
widget->callback(&new_args);
break;
} /* Default cases */
if (d->active.type == g_action) {
if (d->active_el->g_type == g_action) {
new_args.element = d->g_action;
group_default_callback(&new_args);
} else if (d->g_action != NULL) {
@@ -129,31 +106,29 @@ void dialogue_default_callback(callback_args_t *args) {
}
} else {
/* TODO: temp solution, improve */
*resp_value = d->active.id - d->g_content->first_id;
*resp_value = d->active_el->id - d->g_content->first_id;
return;
}
break;
case KEY_UP:
if (d->g_content->elements[d->active.id - d->g_content->first_id].type ==
w_fs_file_list) {
if (d->active_el->type == w_fs_file_list) {
new_args.element = d->g_content;
group_default_callback(&new_args);
} else {
CH_GROUP(d, g_action, g_content);
/* CH_GROUP(d, g_action, g_content); */
}
break;
case KEY_DOWN:
if (d->g_content->elements[d->active.id - d->g_content->first_id].type ==
w_fs_file_list) {
if (d->active_el->type == w_fs_file_list) {
new_args.element = d->g_content;
group_default_callback(&new_args);
} else {
CH_GROUP(d, g_content, g_action);
/* CH_GROUP(d, g_content, g_action); */
}
break;
default:
/* run callback function */
if (d->active.type == g_content) {
if (d->active_el->g_type == g_content) {
new_args.element = d->g_content;
} else {
new_args.element = d->g_action;
@@ -192,9 +167,7 @@ void dialogue_init_active_id(dialogue_t *dialogue) {
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;
dialogue->active_el = &(dialogue->g_content->elements[i]);
return;
}
}
@@ -204,9 +177,7 @@ void dialogue_init_active_id(dialogue_t *dialogue) {
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;
dialogue->active_el = &(dialogue->g_action->elements[i]);
return;
}
}
@@ -233,7 +204,6 @@ int32_t draw_dialogue(dialogue_t *d) {
}
group_el_t *ae_ptr = NULL; /* active element */
uint32_t ae_idx; /* active element */
/* count dimensions */
uint32_t x = 1; /* when uses box */
uint32_t y = 1; /* when uses box */
@@ -291,22 +261,15 @@ int32_t draw_dialogue(dialogue_t *d) {
wattroff(d->win, A_REVERSE);
if (d->g_content != NULL) {
draw_group(d->win, d->g_content, d->active.id);
draw_group(d->win, d->g_content, d->active_el->id);
}
if (d->g_action != NULL) {
draw_group(d->win, d->g_action, d->active.id);
draw_group(d->win, d->g_action, d->active_el->id);
}
/* move cursor */
if (d->g_content != NULL) {
FIND_ACTIVE_ELEMENT(d->g_content, d->active.id, ae_ptr, ae_idx);
}
if (ae_ptr == NULL && d->g_action != NULL) {
FIND_ACTIVE_ELEMENT(d->g_action, d->active.id, ae_ptr, ae_idx);
}
if (ae_ptr != NULL && ae_ptr->id == d->active.id && ae_ptr->type == w_input) {
input_t *input = ae_ptr->element;
if (d->active_el != NULL && d->active_el->type == w_input) {
input_t *input = d->active_el->element;
d->w.cur.y = input->w.cur.y;
d->w.cur.x = input->w.cur.x + input->value_len;
d->w.cur.x -= input->cur_pos;
+2 -5
View File
@@ -8,7 +8,7 @@
#include <widget_core.h>
#include <utils.h>
#define MAX_IDS 64
enum g_type { g_content, g_action };
enum d_color_scheme { dc_normal, dc_alert };
typedef struct {
@@ -16,10 +16,7 @@ typedef struct {
WINDOW *win;
group_t *g_content;
group_t *g_action;
struct {
int32_t id;
enum g_type type;
} active;
group_el_t *active_el;
char text[DIALOGUE_TEXT];
bool is_initiated;
bool needs_update;
+7 -8
View File
@@ -18,24 +18,23 @@
*response = -1; \
}
/* TODO: adapt to a new indexation method */
void group_default_callback(callback_args_t *args) {
group_t *g = (group_t *)args->element;
int32_t key = *((int32_t *)args->data);
int32_t *response = (int32_t *)args->resp_data;
input_t *input;
widget_t *widget;
group_el_t *element_ptr;
int32_t element_idx = -1;
group_el_t *element_ptr = args->active_el;
int32_t element_idx = element_ptr->id - g->first_id; ;
u_int32_t start_pos = 0;
callback_args_t new_args;
switch (key) {
case '\n': /* Enter */
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
*response = element_idx;
break;
case KEY_BACKSPACE:
case KEY_DL:
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
if (element_ptr->type == w_input) {
input = (input_t *)element_ptr->element;
if (input->value_len) {
@@ -51,7 +50,6 @@ void group_default_callback(callback_args_t *args) {
MAKE_RESPONSE_M1(args, resp_data, response);
break;
default:
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
if (element_ptr->type == w_input) {
input = (input_t *)element_ptr->element;
if (input->max_len > input->value_len) {
@@ -85,7 +83,7 @@ union current_element {
};
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
d_array_ptr_t *id_map, enum g_direction direction) {
d_array_ptr_t *id_map, enum g_direction direction, enum g_type g_type) {
group_t *group = malloc(sizeof(group_t));
union current_element current;
group->parent_group = NULL;
@@ -105,6 +103,7 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
for (int32_t i = 0; i < group->count; i++) {
elements[i].type = children[i].type;
elements[i].is_default = children[i].is_default;
elements[i].g_type = g_type;
}
/* init child elements */
@@ -120,7 +119,7 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
break;
case w_group:
elements[i].element = init_group(win, &(group->w), children[i].children,
id_map, children[i].direction);
id_map, children[i].direction, g_type);
w = &(((group_t *)elements[i].element)->w);
current.group = (group_t *)elements[i].element;
current.group->parent_group = group;
@@ -157,7 +156,7 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
}
/* set dimensions */
elements[i].id = w->id;
add_d_arr_ptr(id_map, &(elements[i]), w->id);
add_d_arr_ptr(id_map, elements + i, w->id);
if (direction == horizontal) {
w->m_x = group->w.m_x + 1 + group->w.x;
w->m_y = group->w.m_y;
+6 -6
View File
@@ -2,8 +2,10 @@
#define GROUP_H
#include <stdint.h>
#include <widget_core.h>
#include <utils.h>
#include <widget_core.h>
enum g_type { g_content, g_action };
enum g_direction {
horizontal,
@@ -14,6 +16,7 @@ typedef struct {
uint32_t id;
void *element;
enum w_type type;
enum g_type g_type;
bool is_default;
} group_el_t;
@@ -36,12 +39,9 @@ typedef struct group_t {
struct group_t *parent_group;
} group_t;
#define FIND_ACTIVE_ELEMENT(g, active_id, element_ptr, element_idx) \
element_idx = active_id - g->first_id; \
element_ptr = &(g->elements[element_idx]);
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
d_array_ptr_t *id_map, enum g_direction dir);
d_array_ptr_t *id_map, enum g_direction dir,
enum g_type g_type);
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);
+1
View File
@@ -21,6 +21,7 @@ typedef struct {
void *data;
void *resp_data;
uint32_t active_id;
void *active_el;
} callback_args_t;
enum rsize {