summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2022-03-30 01:48:38 +0200
committerTheJackiMonster <thejackimonster@gmail.com>2022-03-30 01:48:38 +0200
commit831daf8ab424dc0581fd448432fe1365ad1f3e4a (patch)
tree3b6c57a05b8efa9011896b691cc272e56ba8063c
parent9d1af57afca13c07e5f3612512513c72dcc32956 (diff)
Fixed accounts offset and selection handling
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--src/chat.c12
-rw-r--r--src/ui/accounts.c53
2 files changed, 55 insertions, 10 deletions
diff --git a/src/chat.c b/src/chat.c
index 1b4bd48..c4b67b2 100644
--- a/src/chat.c
+++ b/src/chat.c
@@ -25,6 +25,9 @@
#include "chat.h"
#include "application.h"
+#include "ui/accounts.h"
+
+UI_ACCOUNTS_Handle accounts;
int lc = 0;
@@ -33,7 +36,7 @@ _chat_message(UNUSED void *cls,
UNUSED struct GNUNET_CHAT_Context *context,
UNUSED const struct GNUNET_CHAT_Message *message)
{
- //MESSENGER_Application *app = cls;
+ // MESSENGER_Application *app = cls;
enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(
message
@@ -46,9 +49,12 @@ _chat_message(UNUSED void *cls,
}
static void
-_chat_refresh(UNUSED MESSENGER_Application *app)
+_chat_refresh(MESSENGER_Application *app)
{
// TODO
+
+ accounts.window = stdscr;
+ accounts_print(&accounts, app);
}
static int
@@ -64,6 +70,8 @@ _chat_event(MESSENGER_Application *app,
move(lc++, 0);
printw("KEY %d", key);
+ accounts_event(&accounts, app, key);
+
refresh:
_chat_refresh(app);
return 0;
diff --git a/src/ui/accounts.c b/src/ui/accounts.c
index 067e781..ef44d13 100644
--- a/src/ui/accounts.c
+++ b/src/ui/accounts.c
@@ -25,6 +25,8 @@
#include "accounts.h"
#include "../application.h"
+#include <stdbool.h>
+
int
_accounts_iterate(void *cls,
UNUSED const struct GNUNET_CHAT_Handle *handle,
@@ -32,10 +34,14 @@ _accounts_iterate(void *cls,
{
UI_ACCOUNTS_Handle *accounts = cls;
- if (accounts->line_index++ == accounts->line_selected)
+ const bool selected = (accounts->line_selected == accounts->line_index);
+
+ accounts->line_index++;
+
+ if (selected)
accounts->selected = account;
- return GNUNET_NO;
+ return GNUNET_YES;
}
void
@@ -64,6 +70,7 @@ accounts_event(UI_ACCOUNTS_Handle *accounts,
accounts->line_selected++;
break;
}
+ case '\n':
case KEY_ENTER:
{
if (accounts->selected)
@@ -72,12 +79,29 @@ accounts_event(UI_ACCOUNTS_Handle *accounts,
break;
}
default:
- if (accounts->line_selected < 0)
- accounts->line_selected = 0;
- else if (accounts->line_selected >= count)
- accounts->line_selected = count - 1;
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) - getbegy(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;
}
int
@@ -87,9 +111,12 @@ _accounts_iterate_print(void *cls,
{
UI_ACCOUNTS_Handle *accounts = cls;
+ const bool selected = (accounts->line_selected == accounts->line_index);
const int y = accounts->line_index - accounts->line_offset;
- if (accounts->line_index++ < accounts->line_offset)
+ accounts->line_index++;
+
+ if (y < 0)
return GNUNET_YES;
const int height = getmaxy(accounts->window) - getbegy(accounts->window);
@@ -99,8 +126,15 @@ _accounts_iterate_print(void *cls,
const char *name = GNUNET_CHAT_account_get_name(account);
- move(y, 0);
+ const int attrs_select = A_BOLD;
+
+ if (selected) wattron(accounts->window, attrs_select);
+
+ wmove(accounts->window, y, 0);
wprintw(accounts->window, "%s", name);
+
+ if (selected) wattroff(accounts->window, attrs_select);
+
return GNUNET_YES;
}
@@ -108,6 +142,9 @@ void
accounts_print(UI_ACCOUNTS_Handle *accounts,
struct MESSENGER_Application *app)
{
+ if (!(accounts->window))
+ return;
+
accounts->line_index = 0;
GNUNET_CHAT_iterate_accounts(