TelegramTUI/src/viewchatlist.cpp

65 lines
1.2 KiB
C++

#include "viewchatlist.h"
#include <curses.h>
#include "tgtui.h"
ViewChatList::ViewChatList(TgTUI& tgtui) : View(tgtui) {}
void ViewChatList::open() {
chats = tgtui.getChats();
currentChatOffset = 0;
selectedChatRow = 0;
}
void ViewChatList::close() {
chats.clear();
}
void ViewChatList::paint() {
::printw("Chats:\n");
getmaxyx(stdscr, maxRows, maxCols);
for(int row = 1; row < maxRows-1 && currentChatOffset + row < chats.size(); ++row) {
int selection = ' ';
const bool currentRowSelected = (row == selectedChatRow + currentChatOffset +1);
if(currentRowSelected) {
selection = '#';
attron(A_REVERSE);
}
::mvprintw(row, 0, "%c %s\n", selection, chats.at(currentChatOffset + row -1).name.c_str());
if(currentRowSelected) {
attroff(A_REVERSE);
}
}
}
int ViewChatList::keyIn(int key) {
switch (key) {
case KEY_UP:
if (selectedChatRow > 0) {
selectedChatRow--;
}
break;
case KEY_DOWN:
if (selectedChatRow < maxRows - 2) {
selectedChatRow++;
}
break;
case KEY_RIGHT:
case KEY_ENTER:
return 1;
case KEY_LEFT:
case 'q':
return -2;
}
return -1;
}
int64_t ViewChatList::getSelectedChatId() {
return chats.at(selectedChatRow + currentChatOffset).chatId;
}