ui: refactoring. changes active id mechanism
This commit is contained in:
+52
-89
@@ -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;
|
||||
|
||||
@@ -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
@@ -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
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user