add views basics

This commit is contained in:
mrbesen 2023-11-20 21:24:51 +01:00
parent d5592d5ee3
commit 34404c7387
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
10 changed files with 136 additions and 20 deletions

View File

@ -55,6 +55,9 @@ public:
void registerChatFiltersCallback(ChatFiltersCallback cfclb); void registerChatFiltersCallback(ChatFiltersCallback cfclb);
//exposed apicalls //exposed apicalls
void openChat(int64_t chatid);
void closeChat(int64_t chatid);
void deleteMessage(int64_t chatid, int64_t messageid, bool forall = true); void deleteMessage(int64_t chatid, int64_t messageid, bool forall = true);
void screenShottaken(int64_t chatid); void screenShottaken(int64_t chatid);

View File

@ -6,6 +6,8 @@
#include "tgclient.h" #include "tgclient.h"
class View; class View;
class ViewChat;
class ViewChatList;
class TgTUI { class TgTUI {
public: public:
@ -19,16 +21,25 @@ public:
const std::vector<SlimChat>& getChats(); const std::vector<SlimChat>& getChats();
private: private:
enum class ViewMode {
ChatList = 0,
Chat = 1,
};
void initDoneCB(); void initDoneCB();
void threadLoop(); void threadLoop();
void handleNewChat(objptr<td_api::chat> chat); void handleNewChat(objptr<td_api::chat> chat);
void switchToView(ViewMode vm);
TGClient tgclient; TGClient tgclient;
ViewMode currentViewMode;
View* currentView = nullptr; View* currentView = nullptr;
std::vector<View*> views; ViewChatList* viewChatList = nullptr;
ViewChat* viewChat = nullptr;
std::vector<SlimChat> chats; std::vector<SlimChat> chats;
bool shouldRun = false; bool shouldRun = false;

View File

@ -12,8 +12,13 @@ public:
virtual void paint() = 0; virtual void paint() = 0;
virtual bool keyIn(int key); // return -2 -> exit
// return -1 -> keep current ViewMode
// return 0 <= -> switch to other ViewMode
virtual int keyIn(int key);
protected: protected:
TgTUI& tgtui; TgTUI& tgtui;
int maxRows, maxCols;
}; };

21
inc/viewchat.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "slimchat.h"
#include "view.h"
class ViewChat : public View {
public:
ViewChat(TgTUI& tgtui);
virtual ~ViewChat();
void setChat(const SlimChat* chat);
int64_t getChat() const;
virtual void paint() override;
virtual int keyIn(int key) override;
private:
const SlimChat* chat = nullptr;
};

View File

@ -15,7 +15,7 @@ public:
virtual void paint() override; virtual void paint() override;
virtual bool keyIn(int key) override; virtual int keyIn(int key) override;
int64_t getSelectedChatId(); int64_t getSelectedChatId();
@ -23,6 +23,4 @@ private:
std::vector<SlimChat> chats; std::vector<SlimChat> chats;
int32_t currentChatOffset = 0; int32_t currentChatOffset = 0;
int32_t selectedChatRow = 0; int32_t selectedChatRow = 0;
int maxRows, maxCols;
}; };

View File

@ -158,6 +158,14 @@ static auto createMessageInputText(const std::string& in) {
return createMessageInputTextEntities(in, entities); return createMessageInputTextEntities(in, entities);
} }
void TGClient::openChat(int64_t chatid) {
send_staticquery(td::make_tl_object<td_api::openChat>(chatid), HANDLER_NULL);
}
void TGClient::closeChat(int64_t chatid) {
send_staticquery(td::make_tl_object<td_api::closeChat>(chatid), HANDLER_NULL);
}
void TGClient::deleteMessage(int64_t chatid, int64_t messageid, bool forall) { void TGClient::deleteMessage(int64_t chatid, int64_t messageid, bool forall) {
send_staticquery(td::make_tl_object<td_api::deleteMessages>(chatid, td_api::array<int64_t>(1, messageid), forall), HANDLER_NULL); send_staticquery(td::make_tl_object<td_api::deleteMessages>(chatid, td_api::array<int64_t>(1, messageid), forall), HANDLER_NULL);
} }

View File

@ -6,19 +6,19 @@
#include "config.h" #include "config.h"
#include "view.h" #include "view.h"
#include "viewchat.h"
#include "viewchatlist.h" #include "viewchatlist.h"
namespace pl = std::placeholders; namespace pl = std::placeholders;
TgTUI::TgTUI() : tgclient(std::bind(&TgTUI::initDoneCB, this)) { TgTUI::TgTUI() : tgclient(std::bind(&TgTUI::initDoneCB, this)) {
views.push_back(new ViewChatList(*this)); viewChatList = new ViewChatList(*this);
viewChat = new ViewChat(*this);
} }
TgTUI::~TgTUI() { TgTUI::~TgTUI() {
for(View* v : views) { delete viewChatList;
delete v; delete viewChat;
}
views.clear();
} }
void TgTUI::run() { void TgTUI::run() {
@ -70,7 +70,8 @@ const std::vector<SlimChat>& TgTUI::getChats() {
void TgTUI::initDoneCB() { void TgTUI::initDoneCB() {
shouldRun = true; shouldRun = true;
currentView = views.at(0); currentView = viewChatList;
currentViewMode = ViewMode::ChatList;
tuiThread = std::thread(&TgTUI::threadLoop, this); tuiThread = std::thread(&TgTUI::threadLoop, this);
} }
@ -84,8 +85,7 @@ void TgTUI::threadLoop() {
currentView->open(); currentView->open();
bool keep = true; while(shouldRun) {
while(shouldRun && keep) {
::clear(); ::clear();
currentView->paint(); currentView->paint();
@ -93,18 +93,56 @@ void TgTUI::threadLoop() {
::refresh(); ::refresh();
int ch = getch(); int ch = getch();
int newMode = currentView->keyIn(ch);
keep = currentView->keyIn(ch); if(newMode == -2) {
// exit
shouldRun = false;
} else if(newMode == -1) {
continue;
} else {
switchToView((ViewMode) newMode);
}
} }
currentView->close(); currentView->close();
tgclient.stop();
// deinit ncurses // deinit ncurses
::nocbreak(); ::nocbreak();
::echo(); ::echo();
::endwin(); ::endwin();
} }
void TgTUI::handleNewChat(objptr<td_api::chat> chat) { void TgTUI::handleNewChat(objptr<td_api::chat> chat) {
chats.push_back({chat->id_, chat->title_}); chats.push_back({chat->id_, chat->title_});
} }
void TgTUI::switchToView(ViewMode to) {
// close old mode
if(currentViewMode == ViewMode::Chat) {
tgclient.closeChat(viewChat->getChat());
}
// prepare new mode
if(to == ViewMode::Chat) {
int64_t chatid = viewChatList->getSelectedChatId();
auto it = std::find_if(chats.begin(), chats.end(), [chatid](const SlimChat& sc){
return sc.chatId == chatid;
});
if(it != chats.end()) {
viewChat->setChat(&*it);
tgclient.openChat(chatid);
}
}
switch(to) {
case ViewMode::ChatList: currentView = viewChatList; break;
case ViewMode::Chat: currentView = viewChat; break;
}
currentViewMode = to;
}

View File

@ -8,6 +8,6 @@ void View::open() {}
void View::close() {} void View::close() {}
bool View::keyIn(int) { int View::keyIn(int) {
return true; return -1;
} }

29
src/viewchat.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "viewchat.h"
#include <ncurses.h>
ViewChat::ViewChat(TgTUI& tgtui) : View(tgtui) {}
ViewChat::~ViewChat() {}
void ViewChat::setChat(const SlimChat* chat) {
this->chat = chat;
}
int64_t ViewChat::getChat() const {
return this->chat->chatId;
}
void ViewChat::paint() {
::printw("Chat: %s (%li)\n", chat->name.c_str(), chat->chatId);
}
int ViewChat::keyIn(int key) {
switch (key) {
case KEY_LEFT:
case 'q':
return 0;
}
return -1;
}

View File

@ -36,7 +36,7 @@ void ViewChatList::paint() {
} }
} }
bool ViewChatList::keyIn(int key) { int ViewChatList::keyIn(int key) {
switch (key) { switch (key) {
case KEY_UP: case KEY_UP:
if (selectedChatRow > 0) { if (selectedChatRow > 0) {
@ -50,10 +50,13 @@ bool ViewChatList::keyIn(int key) {
break; break;
case KEY_RIGHT: case KEY_RIGHT:
case KEY_ENTER: case KEY_ENTER:
return false; return 1;
case KEY_LEFT:
case 'q':
return -2;
} }
return true; return -1;
} }
int64_t ViewChatList::getSelectedChatId() { int64_t ViewChatList::getSelectedChatId() {