mirror of
https://github.com/celisej567/abaddon.git
synced 2026-09-13 20:18:50 +03:00
rudimentary chat
This commit is contained in:
@@ -52,6 +52,10 @@ void ChannelList::on_row_activated(Gtk::ListBoxRow *row) {
|
||||
bool new_collapsed = !info.IsUserCollapsed;
|
||||
info.IsUserCollapsed = new_collapsed;
|
||||
|
||||
if (info.Type == ListItemInfo::ListItemType::Channel) {
|
||||
m_abaddon->ActionListChannelItemClick(info.ID);
|
||||
}
|
||||
|
||||
if (info.CatArrow != nullptr)
|
||||
info.CatArrow->set(new_collapsed ? Gtk::ARROW_RIGHT : Gtk::ARROW_DOWN, Gtk::SHADOW_NONE);
|
||||
|
||||
@@ -143,6 +147,7 @@ void ChannelList::SetListingFromGuildsInternal() {
|
||||
info.ID = id;
|
||||
info.IsUserCollapsed = false;
|
||||
info.IsHidden = false;
|
||||
info.Type = ListItemInfo::ListItemType::Channel;
|
||||
|
||||
m_infos[channel_row] = std::move(info);
|
||||
return channel_row;
|
||||
@@ -168,6 +173,7 @@ void ChannelList::SetListingFromGuildsInternal() {
|
||||
info.IsUserCollapsed = false;
|
||||
info.IsHidden = true;
|
||||
info.CatArrow = category_arrow;
|
||||
info.Type = ListItemInfo::ListItemType::Category;
|
||||
|
||||
if (cat_to_channels.find(id) != cat_to_channels.end()) {
|
||||
std::map<int, const ChannelData *> sorted_channels;
|
||||
@@ -206,6 +212,7 @@ void ChannelList::SetListingFromGuildsInternal() {
|
||||
info.IsUserCollapsed = true;
|
||||
info.IsHidden = false;
|
||||
info.GuildIndex = m_guild_count++;
|
||||
info.Type = ListItemInfo::ListItemType::Guild;
|
||||
|
||||
if (orphan_channels.find(id) != orphan_channels.end()) {
|
||||
std::map<int, const ChannelData *> sorted_orphans;
|
||||
|
||||
@@ -21,11 +21,17 @@ protected:
|
||||
Gtk::ScrolledWindow *m_main;
|
||||
|
||||
struct ListItemInfo {
|
||||
enum ListItemType {
|
||||
Guild,
|
||||
Category,
|
||||
Channel,
|
||||
};
|
||||
int GuildIndex;
|
||||
Snowflake ID;
|
||||
std::unordered_set<Gtk::ListBoxRow *> Children;
|
||||
bool IsUserCollapsed;
|
||||
bool IsHidden;
|
||||
ListItemType Type;
|
||||
// for categories
|
||||
Gtk::Arrow *CatArrow = nullptr;
|
||||
};
|
||||
|
||||
164
components/chatwindow.cpp
Normal file
164
components/chatwindow.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#include "chatwindow.hpp"
|
||||
#include <map>
|
||||
|
||||
ChatWindow::ChatWindow() {
|
||||
m_update_dispatcher.connect(sigc::mem_fun(*this, &ChatWindow::SetMessagesInternal));
|
||||
|
||||
m_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
||||
m_listbox = Gtk::manage(new Gtk::ListBox);
|
||||
m_viewport = Gtk::manage(new Gtk::Viewport(Gtk::Adjustment::create(0, 0, 0, 0, 0, 0), Gtk::Adjustment::create(0, 0, 0, 0, 0, 0)));
|
||||
m_scroll = Gtk::manage(new Gtk::ScrolledWindow);
|
||||
m_input = Gtk::manage(new Gtk::TextView);
|
||||
m_entry_scroll = Gtk::manage(new Gtk::ScrolledWindow);
|
||||
|
||||
m_main->set_hexpand(true);
|
||||
m_main->set_vexpand(true);
|
||||
m_main->show();
|
||||
|
||||
m_scroll->set_can_focus(false);
|
||||
m_scroll->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
|
||||
m_scroll->show();
|
||||
|
||||
m_listbox->signal_size_allocate().connect([this](Gtk::Allocation &) {
|
||||
ScrollToBottom();
|
||||
});
|
||||
|
||||
m_listbox->set_selection_mode(Gtk::SELECTION_NONE);
|
||||
m_listbox->set_hexpand(true);
|
||||
m_listbox->set_vexpand(true);
|
||||
m_listbox->set_focus_hadjustment(m_scroll->get_hadjustment());
|
||||
m_listbox->set_focus_vadjustment(m_scroll->get_vadjustment());
|
||||
m_listbox->show();
|
||||
|
||||
m_viewport->set_can_focus(false);
|
||||
m_viewport->set_valign(Gtk::ALIGN_END);
|
||||
m_viewport->set_vscroll_policy(Gtk::SCROLL_NATURAL);
|
||||
m_viewport->set_shadow_type(Gtk::SHADOW_NONE);
|
||||
m_viewport->show();
|
||||
|
||||
m_input->set_hexpand(true);
|
||||
m_input->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
|
||||
m_input->show();
|
||||
|
||||
m_entry_scroll->set_max_content_height(150);
|
||||
m_entry_scroll->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
|
||||
m_entry_scroll->add(*m_input);
|
||||
m_entry_scroll->show();
|
||||
|
||||
m_viewport->add(*m_listbox);
|
||||
m_scroll->add(*m_viewport);
|
||||
m_main->add(*m_scroll);
|
||||
m_main->add(*m_entry_scroll);
|
||||
}
|
||||
|
||||
Gtk::Widget *ChatWindow::GetRoot() const {
|
||||
return m_main;
|
||||
}
|
||||
|
||||
void ChatWindow::SetActiveChannel(Snowflake id) {
|
||||
m_active_channel = id;
|
||||
}
|
||||
|
||||
Snowflake ChatWindow::GetActiveChannel() const {
|
||||
return m_active_channel;
|
||||
}
|
||||
|
||||
Gtk::ListBoxRow *ChatWindow::CreateChatEntryComponentText(const MessageData *data) {
|
||||
auto *row = Gtk::manage(new Gtk::ListBoxRow);
|
||||
auto *main_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
||||
auto *sub_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
||||
auto *meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
||||
auto *author = Gtk::manage(new Gtk::Label);
|
||||
auto *timestamp = Gtk::manage(new Gtk::Label);
|
||||
auto *text = Gtk::manage(new Gtk::TextView);
|
||||
|
||||
text->set_can_focus(false);
|
||||
text->set_editable(false);
|
||||
text->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
|
||||
text->set_halign(Gtk::ALIGN_FILL);
|
||||
text->set_hexpand(true);
|
||||
text->get_buffer()->set_text(data->Content);
|
||||
text->show();
|
||||
|
||||
author->set_markup("<span weight=\"bold\">" + Glib::Markup::escape_text(data->Author.Username) + "</span>");
|
||||
author->set_single_line_mode(true);
|
||||
author->set_line_wrap(false);
|
||||
author->set_ellipsize(Pango::ELLIPSIZE_END);
|
||||
author->set_xalign(0.f);
|
||||
author->show();
|
||||
|
||||
timestamp->set_text(data->Timestamp);
|
||||
timestamp->set_opacity(0.5);
|
||||
timestamp->set_single_line_mode(true);
|
||||
timestamp->set_margin_start(12);
|
||||
timestamp->show();
|
||||
|
||||
main_box->set_hexpand(true);
|
||||
main_box->set_vexpand(true);
|
||||
main_box->show();
|
||||
|
||||
meta_box->show();
|
||||
sub_box->show();
|
||||
|
||||
meta_box->add(*author);
|
||||
meta_box->add(*timestamp);
|
||||
sub_box->add(*meta_box);
|
||||
sub_box->add(*text);
|
||||
main_box->add(*sub_box);
|
||||
row->add(*main_box);
|
||||
row->set_margin_bottom(8);
|
||||
|
||||
row->show();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
Gtk::ListBoxRow *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
|
||||
if (data->Type == MessageType::DEFAULT && data->Content.size() > 0)
|
||||
return CreateChatEntryComponentText(data);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ChatWindow::SetMessages(std::unordered_set<const MessageData *> msgs) {
|
||||
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
||||
m_update_queue.push(msgs);
|
||||
m_update_dispatcher.emit();
|
||||
}
|
||||
|
||||
void ChatWindow::ScrollToBottom() {
|
||||
auto x = m_scroll->get_vadjustment();
|
||||
x->set_value(x->get_upper());
|
||||
}
|
||||
|
||||
void ChatWindow::SetMessagesInternal() {
|
||||
auto children = m_listbox->get_children();
|
||||
auto it = children.begin();
|
||||
|
||||
while (it != children.end()) {
|
||||
delete *it;
|
||||
it++;
|
||||
}
|
||||
|
||||
std::unordered_set<const MessageData *> *msgs;
|
||||
{
|
||||
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
||||
msgs = &m_update_queue.front();
|
||||
}
|
||||
|
||||
// sort
|
||||
std::map<Snowflake, const MessageData *> sorted_messages;
|
||||
for (const auto msg : *msgs)
|
||||
sorted_messages[msg->ID] = msg;
|
||||
|
||||
for (const auto &[id, msg] : sorted_messages) {
|
||||
auto *row = CreateChatEntryComponent(msg);
|
||||
if (row != nullptr)
|
||||
m_listbox->add(*row);
|
||||
}
|
||||
|
||||
{
|
||||
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
||||
m_update_queue.pop();
|
||||
}
|
||||
}
|
||||
33
components/chatwindow.hpp
Normal file
33
components/chatwindow.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include <gtkmm.h>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include "../discord/discord.hpp"
|
||||
|
||||
class ChatWindow {
|
||||
public:
|
||||
ChatWindow();
|
||||
Gtk::Widget *GetRoot() const;
|
||||
void SetActiveChannel(Snowflake id);
|
||||
Snowflake GetActiveChannel() const;
|
||||
void SetMessages(std::unordered_set<const MessageData *> msgs);
|
||||
|
||||
protected:
|
||||
void ScrollToBottom();
|
||||
void SetMessagesInternal();
|
||||
Gtk::ListBoxRow *CreateChatEntryComponentText(const MessageData *data);
|
||||
Gtk::ListBoxRow *CreateChatEntryComponent(const MessageData *data);
|
||||
|
||||
Glib::Dispatcher m_update_dispatcher;
|
||||
std::queue<std::unordered_set<const MessageData *>> m_update_queue;
|
||||
std::mutex m_update_mutex;
|
||||
|
||||
Snowflake m_active_channel;
|
||||
|
||||
Gtk::Box *m_main;
|
||||
Gtk::ListBox *m_listbox;
|
||||
Gtk::Viewport *m_viewport;
|
||||
Gtk::ScrolledWindow *m_scroll;
|
||||
Gtk::ScrolledWindow *m_entry_scroll;
|
||||
Gtk::TextView *m_input;
|
||||
};
|
||||
Reference in New Issue
Block a user