ui: key press callbacks (not finished)

This commit is contained in:
2026-04-09 15:44:30 +03:00
parent 48b24fdd46
commit 173bd3000d
9 changed files with 132 additions and 63 deletions
+17
View File
@@ -3,9 +3,26 @@
#ifndef COMMON_H
#define COMMON_H
#include <stdint.h>
typedef struct {
uint32_t cur_y, cur_x;
uint32_t max_y, max_x;
} coordinates_t;
#define MAIN_UI_RESET(app) \
app->main_ui.ui = NULL; \
app->main_ui.cb_refresh = NULL; \
app->main_ui.cb_b_press = NULL; \
app->main_ui.b_keys = NULL;
struct action_key {
char *const key;
char *const title;
int const code;
int (*callback)(void *app, void *d_args);
};
#endif
+2 -51
View File
@@ -17,16 +17,14 @@
#include "app.h"
#include "connection.h"
#include "file_list.h"
#include "fs_file_list.h"
#include "main_window.h"
#include "main.h"
#include "query.h"
uint32_t m_id = 0;
int32_t process_user_input(w_app_t *app, w_cb_args_t *d_args);
void main_window_set(w_app_t *app, enum main_window_type type);
void main_window_draw(w_app_t *app);
void main_window_destroy(w_app_t *app);
int main(int argc, char **argv) {
w_app_t *app;
@@ -103,51 +101,4 @@ int32_t process_user_input(w_app_t *app, w_cb_args_t *d_args) {
break;
}
return OK;
}
void main_window_set(w_app_t *app, enum main_window_type type) {
if (app->main_ui.ui != NULL) {
main_window_destroy(app);
}
app->main_ui.type = type;
switch (type) {
case mw_fl_server:
app->main_ui.ui = w_fl_init(app);
app->active_callback = w_fl_cb;
break;
case mw_fl_local:
app->main_ui.ui = w_lfl_init_win(app);
app->active_callback = w_lfl_cb;
break;
case mw_f_desc:
break;
default:
break;
}
}
void main_window_draw(w_app_t *app) {
switch (app->main_ui.type) {
case mw_fl_server:
w_fl_draw((w_ui_file_list_t *)app->main_ui.ui);
break;
case mw_fl_local:
w_lfl_draw((w_lfl_ui_t *)app->main_ui.ui);
break;
case mw_f_desc:
break;
}
}
void main_window_destroy(w_app_t *app) {
switch (app->main_ui.type) {
case mw_fl_server:
w_fl_destroy((w_ui_file_list_t **)&(app->main_ui.ui));
app->main_ui.cb_refresh = NULL;
break;
case mw_fl_local:
/* TODO: delete fs_file_list */
case mw_f_desc:
break;
}
}
+3
View File
@@ -4,6 +4,7 @@
#ifndef MAIN_H
#define MAIN_H
#include "common.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
@@ -91,7 +92,9 @@ enum main_window_type {
typedef struct main_window {
enum main_window_type type;
void *ui; /* structure which describes window UI */
int32_t (*cb_b_press) (void *app, void *args); /* callback on button press */
void (*cb_refresh) (void *app); /* refresh callback */
struct action_key *b_keys;
} main_window_t;
typedef struct query_args {
+49
View File
@@ -0,0 +1,49 @@
#include "main_window.h"
#include "fs_file_list.h"
void main_window_set(w_app_t *app, enum main_window_type type) {
if (app->main_ui.ui != NULL) {
main_window_destroy(app);
}
app->main_ui.type = type;
switch (type) {
case mw_fl_server:
app->main_ui.ui = w_fl_init(app);
app->active_callback = w_fl_cb;
break;
case mw_fl_local:
app->main_ui.ui = w_lfl_init_win(app);
app->active_callback = w_lfl_cb;
break;
case mw_f_desc:
break;
default:
break;
}
}
void main_window_draw(w_app_t *app) {
switch (app->main_ui.type) {
case mw_fl_server:
w_fl_draw((w_ui_file_list_t *)app->main_ui.ui);
break;
case mw_fl_local:
w_lfl_draw((w_lfl_ui_t *)app->main_ui.ui);
break;
case mw_f_desc:
break;
}
}
void main_window_destroy(w_app_t *app) {
switch (app->main_ui.type) {
case mw_fl_server:
w_fl_destroy((w_ui_file_list_t **)&(app->main_ui.ui));
MAIN_UI_RESET(app);
break;
case mw_fl_local:
/* TODO: delete fs_file_list */
case mw_f_desc:
break;
}
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H
#include <widget.h>
void main_window_set(w_app_t *app, enum main_window_type type);
void main_window_draw(w_app_t *app);
void main_window_destroy(w_app_t *app);
#endif
+1 -7
View File
@@ -43,8 +43,7 @@ w_app_t *w_app_init() {
w_app_draw_borders(_app);
/* NULL to main_ui */
_app->main_ui.ui = NULL;
_app->main_ui.cb_refresh = NULL;
MAIN_UI_RESET(_app);
keypad(_app->win, TRUE);
@@ -83,11 +82,6 @@ void w_app_draw_borders(w_app_t *app) {
box(app->win, 0, 0);
}
struct action_key {
char *const key;
char *const title;
};
int64_t print_bottom_menu_option(WINDOW *win, char *key, char *title, int64_t y,
int64_t x, int32_t size) {
int32_t key_len = 0;
+43 -1
View File
@@ -15,6 +15,7 @@
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "../main_window.h"
void w_fl_reset(w_ui_file_list_t *fl_ui);
@@ -125,11 +126,53 @@ static void w_fl_cb_refresh(void *data) {
wnoutrefresh(fui->win_info);
}
static struct action_key action_keys[] = {
{.key = "F1", .title = "Help", .code = KEY_F(1)},
{.key = "U", .title = "Upload", .code = 'u'},
{.key = "S", .title = "Search", .code = 's'},
{.key = "F9", .title = "Quit", .code = KEY_F(9)},
};
int32_t process_user_input(w_app_t *app, w_cb_args_t *d_args) {
int32_t c;
w_ui_file_list_t *fui = (w_ui_file_list_t *)app->query_args->main_ui->ui;
c = wgetch(app->win);
switch (c) {
case KEY_F(9):
w_app_destroy(app, 0);
return OK;
case 'U':
case 'u':
if (!app->modal.is_initiated && !fui->active_search &&
app->main_ui.type != mw_fl_local) {
/* app->query_args->state = S_UPLOAD_FILE_SELECT; */
main_window_set(app, mw_fl_local);
main_window_draw(app);
d_args->element = app->main_ui.ui;
break;
}
case 's':
case 'S':
if (!app->modal.is_initiated && !fui->active_search) {
fui->active_search = true;
w_fl_draw(fui);
return OK;
}
default:
d_args->data = (void *)&c;
app->active_callback(d_args);
break;
}
return OK;
}
w_ui_file_list_t *w_fl_init(w_app_t *app) {
w_ui_file_list_t *fui = malloc(sizeof(w_ui_file_list_t));
app->main_ui.ui = fui;
app->main_ui.type = mw_fl_server;
app->main_ui.cb_refresh = w_fl_cb_refresh;
app->main_ui.b_keys = action_keys;
app->main_ui.cb_b_press = process_user_input;
w_init(&(fui->w), NULL, &(app->win), "");
WINDOW *win_parent = app->win;
fui->current_idx = 0;
@@ -158,7 +201,6 @@ w_ui_file_list_t *w_fl_init(w_app_t *app) {
fui->win_info =
newwin(app->coordinates.max_y - 4, right_w_x, 2, left_w_x + 1);
fui->max_lines = getmaxy(fui->win_list) - 3; /* 2+1 (bottom info line) */
return fui;
}
+3 -3
View File
@@ -72,6 +72,8 @@ typedef struct {
void *active_el;
} w_cb_args_t;
typedef void (*w_cb_t)(w_cb_args_t *args);
typedef struct w_t {
int64_t id;
char title[DIALOGUE_TITLE];
@@ -93,11 +95,9 @@ typedef struct w_t {
} ps; /* position (relative to the window) */
WINDOW *const *parent_win;
struct w_t *w_parent;
void (*callback)(w_cb_args_t *args);
w_cb_t callback;
} w_t;
typedef void (*w_cb_t)(w_cb_args_t *args);
enum w_pmt_attrs {
PMT_POS_CENTER = 01,
PMT_ALIGN_CENTER = 03,