ui integration into event loop; login modals
This commit is contained in:
+53
-3
@@ -1,7 +1,12 @@
|
||||
#include "app.h"
|
||||
#include "modals/ask_server_addr.h"
|
||||
#include "modals/login_credentials.h"
|
||||
#include "modals/login_option.h"
|
||||
// #include "action.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void ac_file(WINDOW *win, int is_action_w);
|
||||
|
||||
@@ -30,9 +35,8 @@ app_t *init_app() {
|
||||
_app->right_win = newwin(y_max - 4, action_w_x, 2, menu_w_x + 1);
|
||||
|
||||
// dialogue
|
||||
_app->modal.dialogue.win = NULL;
|
||||
_app->modal.dialogue.is_initiated = 0;
|
||||
_app->modal.type = none;
|
||||
_app->modal.win = NULL;
|
||||
_app->modal.is_initiated = 0;
|
||||
|
||||
// print decorative bars
|
||||
print_bars(_app);
|
||||
@@ -98,6 +102,52 @@ void print_bars(app_t *app) {
|
||||
|
||||
wattroff(app->win, A_REVERSE);
|
||||
}
|
||||
void app_refresh(app_t *app) {
|
||||
draw_borders(app);
|
||||
|
||||
wnoutrefresh(app->win);
|
||||
wnoutrefresh(app->left_win);
|
||||
wnoutrefresh(app->right_win);
|
||||
if (app->modal.win != NULL) {
|
||||
if (app->modal.is_initiated != 0) {
|
||||
wnoutrefresh(app->modal.win);
|
||||
}
|
||||
}
|
||||
doupdate();
|
||||
}
|
||||
|
||||
void app_draw_modal(app_t *app) {
|
||||
if (!app->modal.is_initiated) {
|
||||
switch (app->query_args->state) {
|
||||
case S_ASK_SEVER_IP:
|
||||
init_asa_modal(app);
|
||||
break;
|
||||
case S_ASK_LOGIN_TYPE:
|
||||
init_login_option_modal(app);
|
||||
break;
|
||||
case S_ASK_LOGIN_USER:
|
||||
init_login_credentials_modal(app);
|
||||
break;
|
||||
case WAIT_SERVER_INIT:
|
||||
case WAIT_SERVER:
|
||||
case WAIT_REGISTER:
|
||||
case WAIT_REGISTER_CONFIRMATION:
|
||||
case WAIT_CLIENT:
|
||||
case UPLOAD_FILE:
|
||||
case STATE_FILE_LIST:
|
||||
case STATE_FILE_SELECT:
|
||||
case STATE_FILE_DOWNLOAD:
|
||||
case STATE_UPLOAD_PARAMS:
|
||||
case STATE_UPLOAD_FILE:
|
||||
case STATE_UPLOAD_REQUESTED:
|
||||
case STATE_ASK_USER_BEFORE_LOGIN:
|
||||
case STATE_ERR:
|
||||
case S_N_D:
|
||||
return;
|
||||
}
|
||||
}
|
||||
draw_dialogue(&(app->modal));
|
||||
}
|
||||
|
||||
void destroy_app(app_t *app) {
|
||||
delwin(app->win);
|
||||
|
||||
+7
-5
@@ -1,8 +1,8 @@
|
||||
#ifndef N_APP_H
|
||||
#define N_APP_H
|
||||
|
||||
#include "widget/dialogue.h"
|
||||
#include "../common.h"
|
||||
#include "widget/dialogue.h"
|
||||
#include <ncurses.h>
|
||||
|
||||
typedef enum {
|
||||
@@ -20,12 +20,12 @@ typedef struct {
|
||||
WINDOW *win;
|
||||
WINDOW *left_win;
|
||||
WINDOW *right_win;
|
||||
struct {
|
||||
dialogue_e type;
|
||||
dialogue_t dialogue;
|
||||
} modal;
|
||||
dialogue_t modal;
|
||||
params_t *params;
|
||||
coordinates_t coordinates;
|
||||
query_args_t *query_args;
|
||||
file_args_t *file_args;
|
||||
int32_t read_fd;
|
||||
} app_t;
|
||||
|
||||
app_t *init_app();
|
||||
@@ -33,5 +33,7 @@ void init_nc();
|
||||
void destroy_app(app_t *app);
|
||||
void draw_borders(app_t *app);
|
||||
void print_bars(app_t *app);
|
||||
void app_refresh(app_t *app);
|
||||
void app_draw_modal(app_t *app);
|
||||
|
||||
#endif // N_APP_H
|
||||
@@ -1,12 +1,13 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
#include "../../connection.h"
|
||||
#include "../app.h"
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../widget/group.h"
|
||||
#include "../widget/input.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
button_t *element;
|
||||
@@ -28,8 +29,8 @@ void asa_modal_dialogue_callback(callback_args_t *args) {
|
||||
switch (response) {
|
||||
case 0:
|
||||
get_ip_port(app->params, in_ip->value, in_port->value);
|
||||
connect_to_server(app);
|
||||
destroy_dialogue(d);
|
||||
app->modal.type = none;
|
||||
break;
|
||||
case 1:
|
||||
destroy_app(app);
|
||||
@@ -51,9 +52,9 @@ dialogue_t *init_asa_modal(app_t *app) {
|
||||
{.type = w_button, .label = "Cancel", .is_default = 0},
|
||||
{.type = w_end}};
|
||||
|
||||
init_dialogue(&(app->modal.dialogue), "Connect to server",
|
||||
init_dialogue(&(app->modal), "Connect to server",
|
||||
"There is information needed", &(app->coordinates));
|
||||
dialogue_t *d = &(app->modal.dialogue);
|
||||
dialogue_t *d = &(app->modal);
|
||||
|
||||
d->w.callback = asa_modal_dialogue_callback;
|
||||
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#ifndef LOGIN_MODAL_H
|
||||
#define LOGIN_MODAL_H
|
||||
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../app.h"
|
||||
|
||||
dialogue_t *init_login_modal(app_t *app);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
#include "../app.h"
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../widget/group.h"
|
||||
#include "../widget/input.h"
|
||||
|
||||
typedef struct {
|
||||
button_t *element;
|
||||
enum w_type type;
|
||||
} group_bt_t;
|
||||
|
||||
void init_login_credentials_modal_cb(callback_args_t *args) {
|
||||
int32_t response;
|
||||
callback_args_t d_args;
|
||||
app_t *app = args->app;
|
||||
dialogue_t *d = (dialogue_t *)args->widget;
|
||||
memcpy(&d_args, args, sizeof(callback_args_t));
|
||||
d_args.app = NULL;
|
||||
d_args.resp_data = &response;
|
||||
input_t *in_ip = d->g_content->elements[0].element;
|
||||
input_t *in_port = d->g_content->elements[1].element;
|
||||
dialogue_default_callback(&d_args);
|
||||
if (response > -1) {
|
||||
switch (response) {
|
||||
case 0:
|
||||
app->query_args->state = S_ASK_LOGIN_USER;
|
||||
destroy_dialogue(d);
|
||||
break;
|
||||
case 1:
|
||||
destroy_app(app);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dialogue_t *init_login_credentials_modal(app_t *app) {
|
||||
if (app == NULL)
|
||||
return NULL;
|
||||
group_el_init_t content[] = {
|
||||
{.type = w_input, .label = "Username", .length = 10},
|
||||
{.type = w_input, .label = "Password", .length = 10},
|
||||
{.type = w_end}};
|
||||
|
||||
group_el_init_t actions[] = {
|
||||
{.type = w_button, .label = "Login", .is_default = 1},
|
||||
{.type = w_button, .label = "Cancel", .is_default = 0},
|
||||
{.type = w_end}};
|
||||
|
||||
init_dialogue(&(app->modal), "Credentials",
|
||||
"There is information needed", &(app->coordinates));
|
||||
dialogue_t *d = &(app->modal);
|
||||
|
||||
d->w.callback = init_login_credentials_modal_cb;
|
||||
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
|
||||
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
|
||||
|
||||
dialogue_init_active_id(d);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef LOGIN_CREDENTIALS_MODAL_H
|
||||
#define LOGIN_CREDENTIALS_MODAL_H
|
||||
|
||||
#include "../app.h"
|
||||
#include "../widget/dialogue.h"
|
||||
|
||||
dialogue_t *init_login_credentials_modal(app_t *app);
|
||||
|
||||
#endif
|
||||
@@ -1,16 +1,17 @@
|
||||
#include "../app.h"
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../widget/group.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../app.h"
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../widget/group.h"
|
||||
|
||||
typedef struct {
|
||||
button_t *element;
|
||||
enum w_type type;
|
||||
} group_bt_t;
|
||||
|
||||
void login_modal_group_callback(callback_args_t *args) {
|
||||
void login_modal_option_cb(callback_args_t *args) {
|
||||
int32_t response;
|
||||
callback_args_t d_args;
|
||||
app_t *app = args->app;
|
||||
@@ -24,13 +25,14 @@ void login_modal_group_callback(callback_args_t *args) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
// exit(0);
|
||||
destroy_dialogue(d);
|
||||
app->query_args->state = S_ASK_LOGIN_USER;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dialogue_t *init_login_modal(app_t *app) {
|
||||
dialogue_t *init_login_option_modal(app_t *app) {
|
||||
if (app == NULL)
|
||||
return NULL;
|
||||
group_el_init_t children[] = {{.type = w_button, .label = "Anonymous"},
|
||||
@@ -38,14 +40,14 @@ dialogue_t *init_login_modal(app_t *app) {
|
||||
{.type = w_button, .label = "Register"},
|
||||
{.type = w_end}};
|
||||
|
||||
init_dialogue(&(app->modal.dialogue), "Login",
|
||||
init_dialogue(&(app->modal), "Login",
|
||||
"Select your login "
|
||||
"option",
|
||||
&(app->coordinates));
|
||||
app->modal.dialogue.g_content =
|
||||
init_group(&(app->modal.dialogue.win), &(app->modal.dialogue.w), children,
|
||||
app->modal.g_content =
|
||||
init_group(&(app->modal.win), &(app->modal.w), children,
|
||||
horizontal);
|
||||
app->modal.dialogue.w.callback = login_modal_group_callback;
|
||||
app->modal.w.callback = login_modal_option_cb;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef LOGIN_OPTION_MODAL_H
|
||||
#define LOGIN_OPTION_MODAL_H
|
||||
|
||||
#include "../widget/dialogue.h"
|
||||
#include "../app.h"
|
||||
|
||||
dialogue_t *init_login_option_modal(app_t *app);
|
||||
|
||||
#endif
|
||||
@@ -75,13 +75,17 @@ void dialogue_default_callback(callback_args_t *args) {
|
||||
if (d->active.type == g_action) {
|
||||
new_args.widget = new_args.widget = d->g_action;
|
||||
group_default_callback(&new_args);
|
||||
} else {
|
||||
} else if (d->g_action != NULL) {
|
||||
for (int i = 0; i < d->g_action->count; i++) {
|
||||
if (d->g_action->elements[i].is_default) {
|
||||
*resp_value = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* TODO: temp solution, improve */
|
||||
*resp_value = d->active.id - d->g_content->first_id;
|
||||
return;
|
||||
}
|
||||
draw_dialogue(d);
|
||||
break;
|
||||
@@ -222,8 +226,10 @@ 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)
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user