From 6018d99e0747c7175aabda27103fb636ea4cdc02 Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Fri, 12 Dec 2025 14:23:06 +0200 Subject: [PATCH] ui: correct reaction on arrow keys press --- src/main.c | 6 ++--- src/ui/app.c | 9 +++++-- src/ui/widget/dialogue.c | 54 +++++++++++++++++++++++++++++++++++----- src/ui/widget/group.c | 5 ++-- 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index b4f7dbc..e200c47 100644 --- a/src/main.c +++ b/src/main.c @@ -51,7 +51,7 @@ int main(int argc, char **argv) { void app_draw_modal(app_t *app); void event_loop(app_t *app) { - int c; + int32_t c; callback_args_t d_args = {.app = app, .win = NULL, @@ -63,10 +63,8 @@ void event_loop(app_t *app) { for (;;) { c = wgetch(app->win); switch (c) { - // 113 is q key. This exits the program - case 113: - return; case KEY_F(9): + destroy_app(app); return; default: if (app->modal.type != none) { diff --git a/src/ui/app.c b/src/ui/app.c index 50d04c7..d0ff3e3 100644 --- a/src/ui/app.c +++ b/src/ui/app.c @@ -1,5 +1,6 @@ #include "app.h" // #include "action.h" +#include #include void ac_file(WINDOW *win, int is_action_w); @@ -39,6 +40,8 @@ app_t *init_app() { // here goes box borders draw_borders(_app); + keypad(_app->win, TRUE); + // refresh the windows wnoutrefresh(_app->win); wnoutrefresh(_app->left_win); @@ -50,9 +53,10 @@ app_t *init_app() { void init_nc() { initscr(); + cbreak(); + keypad(stdscr, TRUE); noecho(); curs_set(0); - keypad(stdscr, TRUE); if (!has_colors()) { printf("Your terminal does not support color\n"); @@ -97,5 +101,6 @@ void print_bars(app_t *app) { void destroy_app(app_t *app) { delwin(app->win); - free(app); + // free(app); + endwin(); } diff --git a/src/ui/widget/dialogue.c b/src/ui/widget/dialogue.c index b9d9c21..e8c22d8 100644 --- a/src/ui/widget/dialogue.c +++ b/src/ui/widget/dialogue.c @@ -18,17 +18,41 @@ 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) { 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; callback_args_t new_args; int32_t *resp_value = (int32_t *)args->resp_data; group_el_t *default_element = NULL; memcpy(&new_args, args, sizeof(callback_args_t)); new_args.active_id = d->active.id; - + int32_t diff; switch (key) { + case KEY_RIGHT: case '\t': if (d->active.type == g_content) { INCR_ACTIVE_ID(d, g_content, g_action); @@ -38,6 +62,15 @@ void dialogue_default_callback(callback_args_t *args) { *resp_value = -1; draw_dialogue(d); 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': if (d->active.type == 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); 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: + /* run callback function */ if (d->active.type == g_content) { new_args.widget = new_args.widget = d->g_content; - group_default_callback(&new_args); } else { new_args.widget = new_args.widget = d->g_action; - group_default_callback(&new_args); } + group_default_callback(&new_args); if (*resp_value == -1) { draw_dialogue(d); } @@ -121,7 +162,7 @@ int32_t draw_dialogue(dialogue_t *d) { if (!d->is_initiated) return -1; group_el_t *ae_ptr = NULL; /* active element */ - uint32_t ae_idx; /* active element */ + uint32_t ae_idx; /* active element */ /* count dimensions */ uint32_t x = 1; /* when uses box */ uint32_t y = 1; /* when uses box */ @@ -182,7 +223,8 @@ int32_t draw_dialogue(dialogue_t *d) { /* move cursor */ 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->type == w_input) { diff --git a/src/ui/widget/group.c b/src/ui/widget/group.c index d13abef..ad4bb16 100644 --- a/src/ui/widget/group.c +++ b/src/ui/widget/group.c @@ -15,7 +15,7 @@ void group_default_callback(callback_args_t *args) { 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; input_t *input; 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); *response = element_idx; break; - case '\177': /* DEL */ + 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;