From 3e527b0e1eeb1ce61cda8251494a35b2a3ce872e Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Thu, 18 Aug 2022 23:47:38 +0200 Subject: Normalized list input via macros Signed-off-by: TheJackiMonster --- Makefile | 3 ++ src/ui/accounts.c | 71 ++++----------------------- src/ui/chats.c | 88 ++++++--------------------------- src/ui/list_input.h | 114 +++++++++++++++++++++++++++++++++++++++++++ src/ui/lobby_create_dialog.c | 53 ++++++++++++++++++++ src/ui/lobby_create_dialog.h | 49 +++++++++++++++++++ src/ui/members.c | 60 +++-------------------- src/ui/messages.c | 74 ++++------------------------ 8 files changed, 259 insertions(+), 253 deletions(-) create mode 100644 src/ui/list_input.h create mode 100644 src/ui/lobby_create_dialog.c create mode 100644 src/ui/lobby_create_dialog.h diff --git a/Makefile b/Makefile index 1713bff..6ab183d 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ SOURCES = messenger_cli.c\ ui/accounts.c\ ui/chat_open_dialog.c\ ui/chats.c\ + ui/lobby_create_dialog.c\ ui/lobby_enter_dialog.c\ ui/members.c\ ui/messages.c @@ -22,6 +23,8 @@ HEADERS = application.h\ ui/chat.h\ ui/chat_open_dialog.h\ ui/chats.h\ + ui/list_input.h\ + ui/lobby_create_dialog.h\ ui/lobby_enter_dialog.h\ ui/members.h\ ui/messages.h\ diff --git a/src/ui/accounts.c b/src/ui/accounts.c index 4936c82..380506d 100644 --- a/src/ui/accounts.c +++ b/src/ui/accounts.c @@ -24,6 +24,7 @@ #include "accounts.h" +#include "list_input.h" #include "../application.h" #include "../util.h" @@ -33,14 +34,7 @@ _accounts_iterate(void *cls, struct GNUNET_CHAT_Account *account) { UI_ACCOUNTS_Handle *accounts = cls; - - const bool selected = (accounts->line_selected == accounts->line_index); - - accounts->line_index++; - - if (selected) - accounts->selected = account; - + list_input_select(accounts, 1, account); return GNUNET_YES; } @@ -55,66 +49,34 @@ accounts_event(UI_ACCOUNTS_Handle *accounts, return; } - accounts->line_index = 0; - accounts->selected = NULL; + list_input_reset(accounts); - int count = GNUNET_CHAT_iterate_accounts( + GNUNET_CHAT_iterate_accounts( app->chat.handle, &_accounts_iterate, accounts - ) + 1; + ); + + list_input_select(accounts, 1, NULL); switch (key) { case 27: case KEY_EXIT: - { app->chat.quit = TRUE; break; - } - case KEY_UP: - { - accounts->line_selected--; - break; - } - case KEY_DOWN: - { - accounts->line_selected++; - break; - } case '\n': case KEY_ENTER: - { if (accounts->selected) GNUNET_CHAT_connect(app->chat.handle, accounts->selected); else accounts->create_dialog.window = &(accounts->window); break; - } default: break; } - if (accounts->line_selected < 0) - accounts->line_selected = 0; - else if (accounts->line_selected >= count) - accounts->line_selected = count - 1; - - if (!(accounts->window)) - return; - - const int height = getmaxy(accounts->window); - const int y = accounts->line_selected - accounts->line_offset; - - if (y < 0) - accounts->line_offset += y; - else if (y + 1 >= height) - accounts->line_offset += y + 1 - height; - - if (accounts->line_offset < 0) - accounts->line_offset = 0; - else if (accounts->line_offset >= count) - accounts->line_offset = count - 1; + list_input_event(accounts, key); } static int @@ -122,18 +84,7 @@ _accounts_print_entry(UI_ACCOUNTS_Handle *accounts, char type, const char *text) { - const bool selected = (accounts->line_selected == accounts->line_index); - const int y = accounts->line_index - accounts->line_offset; - - accounts->line_index++; - - if (y < 0) - return GNUNET_YES; - - const int height = getmaxy(accounts->window); - - if (y >= height) - return GNUNET_NO; + list_input_print_gnunet(accounts, 1); const int attrs_select = A_BOLD; @@ -153,9 +104,7 @@ _accounts_iterate_print(void *cls, struct GNUNET_CHAT_Account *account) { UI_ACCOUNTS_Handle *accounts = cls; - const char *name = GNUNET_CHAT_account_get_name(account); - return _accounts_print_entry(accounts, 'x', name); } @@ -172,7 +121,7 @@ accounts_print(UI_ACCOUNTS_Handle *accounts, if (!(accounts->window)) return; - accounts->line_index = 0; + list_input_reset(accounts); werase(accounts->window); GNUNET_CHAT_iterate_accounts( diff --git a/src/ui/chats.c b/src/ui/chats.c index 1a25927..5517541 100644 --- a/src/ui/chats.c +++ b/src/ui/chats.c @@ -24,6 +24,7 @@ #include "chats.h" +#include "list_input.h" #include "../application.h" #include "../util.h" @@ -33,14 +34,7 @@ _chats_iterate_group(void *cls, struct GNUNET_CHAT_Group *group) { UI_CHATS_Handle *chats = cls; - - const bool selected = (chats->line_selected == chats->line_index); - - chats->line_index++; - - if (selected) - chats->selected = GNUNET_CHAT_group_get_context(group); - + list_input_select(chats, 1, GNUNET_CHAT_group_get_context(group)); return GNUNET_YES; } @@ -50,14 +44,7 @@ _chats_iterate_contact(void *cls, struct GNUNET_CHAT_Contact *contact) { UI_CHATS_Handle *chats = cls; - - const bool selected = (chats->line_selected == chats->line_index); - - chats->line_index++; - - if (selected) - chats->selected = GNUNET_CHAT_contact_get_context(contact); - + list_input_select(chats, 1, GNUNET_CHAT_contact_get_context(contact)); return GNUNET_YES; } @@ -87,44 +74,34 @@ chats_event(UI_CHATS_Handle *chats, return; } - chats->line_index = 0; - chats->selected = NULL; + list_input_reset(chats); - int count = 3; - - count += GNUNET_CHAT_iterate_groups( + GNUNET_CHAT_iterate_groups( app->chat.handle, &_chats_iterate_group, chats ); - count += GNUNET_CHAT_iterate_contacts( + GNUNET_CHAT_iterate_contacts( app->chat.handle, &_chats_iterate_contact, chats ); + list_input_select(chats, 1, NULL); + list_input_select(chats, 1, NULL); + list_input_select(chats, 1, NULL); + + const int count = chats->line_index; + switch (key) { case 27: case KEY_EXIT: - { GNUNET_CHAT_disconnect(app->chat.handle); break; - } - case KEY_UP: - { - chats->line_selected--; - break; - } - case KEY_DOWN: - { - chats->line_selected++; - break; - } case '\n': case KEY_ENTER: - { if (chats->selected) { GNUNET_CHAT_context_request(chats->selected); @@ -150,31 +127,11 @@ chats_event(UI_CHATS_Handle *chats, else if (chats->line_selected == count - 1) chats->enter_dialog.window = &(chats->window); break; - } default: break; } - if (chats->line_selected < 0) - chats->line_selected = 0; - else if (chats->line_selected >= count) - chats->line_selected = count - 1; - - if (!(chats->window)) - return; - - const int height = getmaxy(chats->window); - const int y = chats->line_selected - chats->line_offset; - - if (y < 0) - chats->line_offset += y; - else if (y + 1 >= height) - chats->line_offset += y + 1 - height; - - if (chats->line_offset < 0) - chats->line_offset = 0; - else if (chats->line_offset >= count) - chats->line_offset = count - 1; + list_input_event(chats, key); } static int @@ -183,18 +140,7 @@ _chats_print_entry(UI_CHATS_Handle *chats, char chat_type, const char *text) { - const bool selected = (chats->line_selected == chats->line_index); - const int y = chats->line_index - chats->line_offset; - - chats->line_index++; - - if (y < 0) - return GNUNET_YES; - - const int height = getmaxy(chats->window); - - if (y >= height) - return GNUNET_NO; + list_input_print_gnunet(chats, 1); const int attrs_select = A_BOLD; @@ -218,9 +164,7 @@ _chats_iterate_print_group(void *cls, struct GNUNET_CHAT_Group *group) { UI_CHATS_Handle *chats = cls; - const char *name = GNUNET_CHAT_group_get_name(group); - return _chats_print_entry(chats, 'x', 'G', name); } @@ -230,9 +174,7 @@ _chats_iterate_print_contact(void *cls, struct GNUNET_CHAT_Contact *contact) { UI_CHATS_Handle *chats = cls; - const char *name = GNUNET_CHAT_contact_get_name(contact); - return _chats_print_entry(chats, 'x', 'C', name); } @@ -254,7 +196,7 @@ chats_print(UI_CHATS_Handle *chats, if (!(chats->window)) return; - chats->line_index = 0; + list_input_reset(chats); werase(chats->window); GNUNET_CHAT_iterate_groups( diff --git a/src/ui/list_input.h b/src/ui/list_input.h new file mode 100644 index 0000000..aad3ae4 --- /dev/null +++ b/src/ui/list_input.h @@ -0,0 +1,114 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/list_input.h + */ + +#ifndef UI_LIST_INPUT_H_ +#define UI_LIST_INPUT_H_ + +#include +#include + +#define list_input_reset(list) { \ + (list)->line_index = 0; \ + (list)->selected = NULL; \ +} + +#define list_input_select(list, line_width, item) { \ + const bool selected = ( \ + ((list)->line_selected >= (list)->line_index) && \ + ((list)->line_selected < (list)->line_index + line_width) \ + ); \ + \ + (list)->line_index += line_width; \ + \ + if (selected) \ + (list)->selected = item; \ +} + +#define list_input_event(list, key) { \ + int count = (list)->line_index; \ + \ + switch (key) \ + { \ + case KEY_UP: \ + { \ + (list)->line_selected--; \ + break; \ + } \ + case KEY_DOWN: \ + { \ + (list)->line_selected++; \ + break; \ + } \ + default: \ + break; \ + } \ + \ + if ((list)->line_selected < 0) \ + (list)->line_selected = 0; \ + else if ((list)->line_selected >= count) \ + (list)->line_selected = count - 1; \ + \ + if ((list)->window) \ + { \ + const int height = getmaxy((list)->window); \ + const int y = (list)->line_selected - (list)->line_offset; \ + \ + if (y < 0) \ + (list)->line_offset += y; \ + else if (y + 1 >= height) \ + (list)->line_offset += y + 1 - height; \ + \ + if ((list)->line_offset < 0) \ + (list)->line_offset = 0; \ + else if ((list)->line_offset >= count) \ + (list)->line_offset = count - 1; \ + } \ +} + +#define list_input_print_(list, line_width, yes_res, no_res) \ + const bool selected = ( \ + ((list)->line_selected >= (list)->line_index) && \ + ((list)->line_selected < (list)->line_index + line_width) \ + ); \ + \ + const int y = (list)->line_index - (list)->line_offset; { \ + \ + (list)->line_index += line_width; \ + \ + if (y + line_width < 1) \ + return yes_res; \ + \ + const int height = getmaxy((list)->window); \ + \ + if (y >= height) \ + return no_res; \ +} + +#define list_input_print_gnunet(list, line_width) \ + list_input_print_(list, line_width, GNUNET_YES, GNUNET_NO) + +#define list_input_print(list, line_width) \ + list_input_print_(list, line_width, , ) + +#endif /* UI_LIST_INPUT_H_ */ diff --git a/src/ui/lobby_create_dialog.c b/src/ui/lobby_create_dialog.c new file mode 100644 index 0000000..9c80929 --- /dev/null +++ b/src/ui/lobby_create_dialog.c @@ -0,0 +1,53 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/lobby_create_dialog.c + */ + +#include "lobby_create_dialog.h" + +void +lobby_create_dialog_event(UI_LOBBY_CREATE_DIALOG_Handle *create_dialog, + struct MESSENGER_Application *app, + int key) +{ + // TODO +} + +void +lobby_create_dialog_print(UI_LOBBY_CREATE_DIALOG_Handle *create_dialog, + struct MESSENGER_Application *app) +{ + /* + Delay until it expires: + + 30 seconds + 5 minutes + 1 hour + 8 hours + 1 day + 1 week + 4 weeks + Off + */ + + // TODO +} diff --git a/src/ui/lobby_create_dialog.h b/src/ui/lobby_create_dialog.h new file mode 100644 index 0000000..f780522 --- /dev/null +++ b/src/ui/lobby_create_dialog.h @@ -0,0 +1,49 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/lobby_create_dialog.h + */ + +#ifndef UI_LOBBY_CREATE_DIALOG_H_ +#define UI_LOBBY_CREATE_DIALOG_H_ + +#include +#include + +struct MESSENGER_Application; + +typedef struct UI_LOBBY_CREATE_DIALOG_Handle +{ + WINDOW **window; + + // TODO +} UI_LOBBY_CREATE_DIALOG_Handle; + +void +lobby_create_dialog_event(UI_LOBBY_CREATE_DIALOG_Handle *create_dialog, + struct MESSENGER_Application *app, + int key); + +void +lobby_create_dialog_print(UI_LOBBY_CREATE_DIALOG_Handle *create_dialog, + struct MESSENGER_Application *app); + +#endif /* UI_LOBBY_CREATE_DIALOG_H_ */ diff --git a/src/ui/members.c b/src/ui/members.c index 509e03b..b0abb7c 100644 --- a/src/ui/members.c +++ b/src/ui/members.c @@ -24,6 +24,7 @@ #include "members.h" +#include "list_input.h" #include "../application.h" #include "../util.h" @@ -32,16 +33,12 @@ members_event(UI_MEMBERS_Handle *members, struct MESSENGER_Application *app, int key) { - members->line_index = 0; - members->selected = NULL; - - int count = 0; + list_input_reset(members); UI_MEMBERS_List *element = members->head; while (element) { - count++; - + list_input_select(members, 1, element->contact); element = element->next; } @@ -50,72 +47,28 @@ members_event(UI_MEMBERS_Handle *members, case 27: case KEY_EXIT: case '\t': - { app->chat.show_members = FALSE; break; - } - case KEY_UP: - { - members->line_selected--; - break; - } - case KEY_DOWN: - { - members->line_selected++; - break; - } case '\n': case KEY_ENTER: - { if (members->selected) { // TODO } break; - } default: break; } - if (members->line_selected < 0) - members->line_selected = 0; - else if (members->line_selected >= count) - members->line_selected = count - 1; - - if (!(members->window)) - return; - - const int height = getmaxy(members->window); - const int y = members->line_selected - members->line_offset; - - if (y < 0) - members->line_offset += y; - else if (y + 1 >= height) - members->line_offset += y + 1 - height; - - if (members->line_offset < 0) - members->line_offset = 0; - else if (members->line_offset >= count) - members->line_offset = count - 1; + list_input_event(members, key); } static void _members_iterate_print(UI_MEMBERS_Handle *members, const struct GNUNET_CHAT_Contact *contact) { - const bool selected = (members->line_selected == members->line_index); - const int y = members->line_index - members->line_offset; - - members->line_index++; - - if (y < 0) - return; - - const int height = getmaxy(members->window); - - if (y >= height) - return; + list_input_print(members, 1); const int attrs_select = A_BOLD; @@ -142,14 +95,13 @@ members_print(UI_MEMBERS_Handle *members) if (!(members->window)) return; - members->line_index = 0; + list_input_reset(members); werase(members->window); UI_MEMBERS_List *element = members->head; while (element) { _members_iterate_print(members, element->contact); - element = element->next; } } diff --git a/src/ui/messages.c b/src/ui/messages.c index 211fe94..86df8e5 100644 --- a/src/ui/messages.c +++ b/src/ui/messages.c @@ -24,22 +24,11 @@ #include "messages.h" +#include "list_input.h" #include "text_input.h" #include "../application.h" #include "../util.h" -void -_messages_iterate(UI_MESSAGES_Handle *messages, - const struct GNUNET_CHAT_Message *message) -{ - const bool selected = (messages->line_selected == messages->line_index); - - messages->line_index++; - - if (selected) - messages->selected = message; -} - void _messages_handle_message(UI_MESSAGES_Handle *messages) { @@ -75,32 +64,23 @@ messages_event(UI_MESSAGES_Handle *messages, MESSENGER_Application *app, int key) { - messages->line_index = 0; - messages->selected = NULL; - - int count = 1; + list_input_reset(messages); UI_MESSAGES_List *element = messages->head; while (element) { - _messages_iterate(messages, element->message); - count++; - + list_input_select(messages, 1, element->message); element = element->next; } + list_input_select(messages, 1, NULL); + switch (key) { case 27: case KEY_EXIT: app->chat.context = NULL; break; - case KEY_UP: - messages->line_selected--; - break; - case KEY_DOWN: - messages->line_selected++; - break; case '\t': app->chat.show_members = TRUE; break; @@ -128,28 +108,7 @@ messages_event(UI_MESSAGES_Handle *messages, if (!(messages->selected)) text_input_event(messages->text, key); - if (messages->line_selected < 0) - messages->line_selected = 0; - else if (messages->line_selected >= count) - messages->line_selected = count - 1; - - if (!(messages->window)) - return; - - const int height = getmaxy(messages->window); - const int y = messages->line_selected - messages->line_offset; - - const int line_height = height - 2; - - if (y < 0) - messages->line_offset += y; - else if (y + 1 >= line_height) - messages->line_offset += y + 1 - line_height; - - if (messages->line_offset < 0) - messages->line_offset = 0; - else if (messages->line_offset >= count) - messages->line_offset = count - 1; + list_input_event(messages, key); } void @@ -158,19 +117,7 @@ _messages_iterate_print(UI_MESSAGES_Handle *messages, { enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(message); - const bool selected = (messages->line_selected == messages->line_index); - const int y = messages->line_index - messages->line_offset; - - messages->line_index++; - - if (y < 0) - return; - - const int height = getmaxy(messages->window); - const int line_height = height - 2; - - if (y >= line_height) - return; + list_input_print(messages, 1); struct GNUNET_CHAT_Contact *sender = GNUNET_CHAT_message_get_sender(message); @@ -270,20 +217,17 @@ messages_print(UI_MESSAGES_Handle *messages) if (!(messages->window)) return; - messages->line_index = 0; + list_input_reset(messages); werase(messages->window); - int count = 0; - UI_MESSAGES_List *element = messages->head; while (element) { _messages_iterate_print(messages, element->message); - count++; - element = element->next; } + const int count = messages->line_index; const bool selected = (count == messages->line_selected); const int width = getmaxx(messages->window); -- cgit v1.2.3