ui: correct reaction on arrow keys press
This commit is contained in:
+2
-4
@@ -51,7 +51,7 @@ int main(int argc, char **argv) {
|
|||||||
void app_draw_modal(app_t *app);
|
void app_draw_modal(app_t *app);
|
||||||
|
|
||||||
void event_loop(app_t *app) {
|
void event_loop(app_t *app) {
|
||||||
int c;
|
int32_t c;
|
||||||
|
|
||||||
callback_args_t d_args = {.app = app,
|
callback_args_t d_args = {.app = app,
|
||||||
.win = NULL,
|
.win = NULL,
|
||||||
@@ -63,10 +63,8 @@ void event_loop(app_t *app) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
c = wgetch(app->win);
|
c = wgetch(app->win);
|
||||||
switch (c) {
|
switch (c) {
|
||||||
// 113 is q key. This exits the program
|
|
||||||
case 113:
|
|
||||||
return;
|
|
||||||
case KEY_F(9):
|
case KEY_F(9):
|
||||||
|
destroy_app(app);
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
if (app->modal.type != none) {
|
if (app->modal.type != none) {
|
||||||
|
|||||||
+7
-2
@@ -1,5 +1,6 @@
|
|||||||
#include "app.h"
|
#include "app.h"
|
||||||
// #include "action.h"
|
// #include "action.h"
|
||||||
|
#include <ncurses.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void ac_file(WINDOW *win, int is_action_w);
|
void ac_file(WINDOW *win, int is_action_w);
|
||||||
@@ -39,6 +40,8 @@ app_t *init_app() {
|
|||||||
// here goes box borders
|
// here goes box borders
|
||||||
draw_borders(_app);
|
draw_borders(_app);
|
||||||
|
|
||||||
|
keypad(_app->win, TRUE);
|
||||||
|
|
||||||
// refresh the windows
|
// refresh the windows
|
||||||
wnoutrefresh(_app->win);
|
wnoutrefresh(_app->win);
|
||||||
wnoutrefresh(_app->left_win);
|
wnoutrefresh(_app->left_win);
|
||||||
@@ -50,9 +53,10 @@ app_t *init_app() {
|
|||||||
|
|
||||||
void init_nc() {
|
void init_nc() {
|
||||||
initscr();
|
initscr();
|
||||||
|
cbreak();
|
||||||
|
keypad(stdscr, TRUE);
|
||||||
noecho();
|
noecho();
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
keypad(stdscr, TRUE);
|
|
||||||
|
|
||||||
if (!has_colors()) {
|
if (!has_colors()) {
|
||||||
printf("Your terminal does not support color\n");
|
printf("Your terminal does not support color\n");
|
||||||
@@ -97,5 +101,6 @@ void print_bars(app_t *app) {
|
|||||||
|
|
||||||
void destroy_app(app_t *app) {
|
void destroy_app(app_t *app) {
|
||||||
delwin(app->win);
|
delwin(app->win);
|
||||||
free(app);
|
// free(app);
|
||||||
|
endwin();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,17 +18,41 @@
|
|||||||
d->active.id = d->current->first_id; \
|
d->active.id = d->current->first_id; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CH_GROUP(d, current, next) \
|
||||||
|
{ \
|
||||||
|
if (d->active.type == current) { \
|
||||||
|
d->active.type = next; \
|
||||||
|
diff = d->active.id - d->current->first_id; \
|
||||||
|
if (diff > d->next->last_id - d->next->first_id) { \
|
||||||
|
d->active.id = d->next->last_id; \
|
||||||
|
} else { \
|
||||||
|
d->active.id = d->next->first_id + diff; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
void dialogue_default_callback(callback_args_t *args) {
|
void dialogue_default_callback(callback_args_t *args) {
|
||||||
dialogue_t *d = (void *)args->widget;
|
dialogue_t *d = (void *)args->widget;
|
||||||
char key = *((char *)args->data);
|
int32_t key = *((int32_t *)args->data);
|
||||||
app_t *app = (app_t *)args->app;
|
app_t *app = (app_t *)args->app;
|
||||||
callback_args_t new_args;
|
callback_args_t new_args;
|
||||||
int32_t *resp_value = (int32_t *)args->resp_data;
|
int32_t *resp_value = (int32_t *)args->resp_data;
|
||||||
group_el_t *default_element = NULL;
|
group_el_t *default_element = NULL;
|
||||||
memcpy(&new_args, args, sizeof(callback_args_t));
|
memcpy(&new_args, args, sizeof(callback_args_t));
|
||||||
new_args.active_id = d->active.id;
|
new_args.active_id = d->active.id;
|
||||||
|
int32_t diff;
|
||||||
switch (key) {
|
switch (key) {
|
||||||
|
case KEY_RIGHT:
|
||||||
case '\t':
|
case '\t':
|
||||||
if (d->active.type == g_content) {
|
if (d->active.type == g_content) {
|
||||||
INCR_ACTIVE_ID(d, g_content, g_action);
|
INCR_ACTIVE_ID(d, g_content, g_action);
|
||||||
@@ -38,6 +62,15 @@ void dialogue_default_callback(callback_args_t *args) {
|
|||||||
*resp_value = -1;
|
*resp_value = -1;
|
||||||
draw_dialogue(d);
|
draw_dialogue(d);
|
||||||
break;
|
break;
|
||||||
|
case KEY_LEFT:
|
||||||
|
if (d->active.type == g_content) {
|
||||||
|
DECR_ACTIVE_ID(d, g_content, g_action);
|
||||||
|
} else if (d->active.type == g_action) {
|
||||||
|
DECR_ACTIVE_ID(d, g_action, g_content);
|
||||||
|
}
|
||||||
|
*resp_value = -1;
|
||||||
|
draw_dialogue(d);
|
||||||
|
break;
|
||||||
case '\n':
|
case '\n':
|
||||||
if (d->active.type == g_action) {
|
if (d->active.type == g_action) {
|
||||||
new_args.widget = new_args.widget = d->g_action;
|
new_args.widget = new_args.widget = d->g_action;
|
||||||
@@ -52,14 +85,22 @@ void dialogue_default_callback(callback_args_t *args) {
|
|||||||
}
|
}
|
||||||
draw_dialogue(d);
|
draw_dialogue(d);
|
||||||
break;
|
break;
|
||||||
|
case KEY_UP:
|
||||||
|
CH_GROUP(d, g_action, g_content);
|
||||||
|
draw_dialogue(d);
|
||||||
|
break;
|
||||||
|
case KEY_DOWN:
|
||||||
|
CH_GROUP(d, g_content, g_action);
|
||||||
|
draw_dialogue(d);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
|
/* run callback function */
|
||||||
if (d->active.type == g_content) {
|
if (d->active.type == g_content) {
|
||||||
new_args.widget = new_args.widget = d->g_content;
|
new_args.widget = new_args.widget = d->g_content;
|
||||||
group_default_callback(&new_args);
|
|
||||||
} else {
|
} else {
|
||||||
new_args.widget = new_args.widget = d->g_action;
|
new_args.widget = new_args.widget = d->g_action;
|
||||||
group_default_callback(&new_args);
|
|
||||||
}
|
}
|
||||||
|
group_default_callback(&new_args);
|
||||||
if (*resp_value == -1) {
|
if (*resp_value == -1) {
|
||||||
draw_dialogue(d);
|
draw_dialogue(d);
|
||||||
}
|
}
|
||||||
@@ -121,7 +162,7 @@ int32_t draw_dialogue(dialogue_t *d) {
|
|||||||
if (!d->is_initiated)
|
if (!d->is_initiated)
|
||||||
return -1;
|
return -1;
|
||||||
group_el_t *ae_ptr = NULL; /* active element */
|
group_el_t *ae_ptr = NULL; /* active element */
|
||||||
uint32_t ae_idx; /* active element */
|
uint32_t ae_idx; /* active element */
|
||||||
/* count dimensions */
|
/* count dimensions */
|
||||||
uint32_t x = 1; /* when uses box */
|
uint32_t x = 1; /* when uses box */
|
||||||
uint32_t y = 1; /* when uses box */
|
uint32_t y = 1; /* when uses box */
|
||||||
@@ -182,7 +223,8 @@ int32_t draw_dialogue(dialogue_t *d) {
|
|||||||
|
|
||||||
/* move cursor */
|
/* move cursor */
|
||||||
FIND_ACTIVE_ELEMENT(d->g_content, d->active.id, ae_ptr, ae_idx);
|
FIND_ACTIVE_ELEMENT(d->g_content, d->active.id, ae_ptr, ae_idx);
|
||||||
if (ae_ptr == NULL) FIND_ACTIVE_ELEMENT(d->g_action, d->active.id, ae_ptr, ae_idx);
|
if (ae_ptr == NULL)
|
||||||
|
FIND_ACTIVE_ELEMENT(d->g_action, d->active.id, ae_ptr, ae_idx);
|
||||||
|
|
||||||
if (ae_ptr != NULL && ae_ptr->id == d->active.id) {
|
if (ae_ptr != NULL && ae_ptr->id == d->active.id) {
|
||||||
if (ae_ptr->type == w_input) {
|
if (ae_ptr->type == w_input) {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
void group_default_callback(callback_args_t *args) {
|
void group_default_callback(callback_args_t *args) {
|
||||||
group_t *g = (group_t *)args->widget;
|
group_t *g = (group_t *)args->widget;
|
||||||
char key = *((char *)args->data);
|
int32_t key = *((int32_t *)args->data);
|
||||||
int32_t *response = (int32_t *)args->resp_data;
|
int32_t *response = (int32_t *)args->resp_data;
|
||||||
input_t *input;
|
input_t *input;
|
||||||
group_el_t *element_ptr;
|
group_el_t *element_ptr;
|
||||||
@@ -25,7 +25,8 @@ void group_default_callback(callback_args_t *args) {
|
|||||||
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
|
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
|
||||||
*response = element_idx;
|
*response = element_idx;
|
||||||
break;
|
break;
|
||||||
case '\177': /* DEL */
|
case KEY_BACKSPACE:
|
||||||
|
case KEY_DL:
|
||||||
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
|
FIND_ACTIVE_ELEMENT(g, args->active_id, element_ptr, element_idx);
|
||||||
if (element_ptr->type == w_input) {
|
if (element_ptr->type == w_input) {
|
||||||
input = (input_t *)element_ptr->element;
|
input = (input_t *)element_ptr->element;
|
||||||
|
|||||||
Reference in New Issue
Block a user