multiple messages from same author are under the same message block

This commit is contained in:
ouwou 2020-08-27 18:50:47 -04:00
parent f3e8d1aff3
commit 34b572db40
4 changed files with 110 additions and 44 deletions

View File

@ -1,48 +1,59 @@
#include "chatmessage.hpp"
ChatMessageTextItem::ChatMessageTextItem(const MessageData *data) {
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);
m_main_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
m_sub_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
m_meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
m_author = Gtk::manage(new Gtk::Label);
m_timestamp = Gtk::manage(new Gtk::Label);
m_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();
m_text->set_can_focus(false);
m_text->set_editable(false);
m_text->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
m_text->set_halign(Gtk::ALIGN_FILL);
m_text->set_hexpand(true);
m_text->get_buffer()->set_text(data->Content);
m_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();
m_author->set_markup("<span weight=\"bold\">" + Glib::Markup::escape_text(data->Author.Username) + "</span>");
m_author->set_single_line_mode(true);
m_author->set_line_wrap(false);
m_author->set_ellipsize(Pango::ELLIPSIZE_END);
m_author->set_xalign(0.f);
m_author->set_can_focus(false);
m_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();
m_timestamp->set_text(data->Timestamp);
m_timestamp->set_opacity(0.5);
m_timestamp->set_single_line_mode(true);
m_timestamp->set_margin_start(12);
m_timestamp->set_can_focus(false);
m_timestamp->show();
main_box->set_hexpand(true);
main_box->set_vexpand(true);
main_box->show();
m_main_box->set_hexpand(true);
m_main_box->set_vexpand(true);
m_main_box->set_can_focus(true);
m_main_box->show();
meta_box->show();
sub_box->show();
m_meta_box->set_can_focus(false);
m_meta_box->show();
meta_box->add(*author);
meta_box->add(*timestamp);
sub_box->add(*meta_box);
sub_box->add(*text);
main_box->add(*sub_box);
add(*main_box);
m_sub_box->set_can_focus(false);
m_sub_box->show();
m_meta_box->add(*m_author);
m_meta_box->add(*m_timestamp);
m_sub_box->add(*m_meta_box);
m_sub_box->add(*m_text);
m_main_box->add(*m_sub_box);
add(*m_main_box);
set_margin_bottom(8);
show();
}
void ChatMessageTextItem::AppendNewContent(std::string content) {
auto buf = m_text->get_buffer();
buf->set_text(buf->get_text() + "\n" + content);
}

View File

@ -2,12 +2,27 @@
#include <gtkmm.h>
#include "../discord/discord.hpp"
enum class ChatDisplayType {
Unknown,
Text,
};
class ChatMessageItem : public Gtk::ListBoxRow {
public:
Snowflake ID;
ChatDisplayType MessageType;
};
class ChatMessageTextItem : public ChatMessageItem {
public:
ChatMessageTextItem(const MessageData* data);
ChatMessageTextItem(const MessageData *data);
void AppendNewContent(std::string content);
protected:
Gtk::Box *m_main_box;
Gtk::Box *m_sub_box;
Gtk::Box *m_meta_box;
Gtk::Label *m_author;
Gtk::Label *m_timestamp;
Gtk::TextView *m_text;
};

View File

@ -71,14 +71,24 @@ Snowflake ChatWindow::GetActiveChannel() const {
return m_active_channel;
}
ChatDisplayType ChatWindow::GetMessageDisplayType(const MessageData *data) {
if (data->Type == MessageType::DEFAULT && data->Content.size() > 0)
return ChatDisplayType::Text;
return ChatDisplayType::Unknown;
}
ChatMessageItem *ChatWindow::CreateChatEntryComponentText(const MessageData *data) {
return Gtk::manage(new ChatMessageTextItem(data));
}
ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
ChatMessageItem *item = nullptr;
if (data->Type == MessageType::DEFAULT && data->Content.size() > 0)
item = CreateChatEntryComponentText(data);
switch (GetMessageDisplayType(data)) {
case ChatDisplayType::Text:
item = CreateChatEntryComponentText(data);
break;
}
if (item != nullptr)
item->ID = data->ID;
@ -86,6 +96,35 @@ ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
return item;
}
void ChatWindow::ProcessMessage(const MessageData *data) {
auto create_new_row = [&]() {
auto *item = CreateChatEntryComponent(data);
if (item != nullptr) {
m_listbox->add(*item);
m_num_rows++;
}
};
// if the last row's message's author is the same as the new one's, then append the new message content to the last row
if (m_num_rows > 0) {
auto *item = dynamic_cast<ChatMessageItem *>(m_listbox->get_row_at_index(m_num_rows - 1));
assert(item != nullptr);
auto *previous_data = m_abaddon->GetDiscordClient().GetMessage(item->ID);
auto new_type = GetMessageDisplayType(data);
auto old_type = GetMessageDisplayType(previous_data);
if ((data->Author.ID == previous_data->Author.ID) && (new_type == old_type && new_type == ChatDisplayType::Text)) {
auto *text_item = dynamic_cast<ChatMessageTextItem *>(item);
text_item->AppendNewContent(data->Content);
} else {
create_new_row();
}
} else {
create_new_row();
}
}
bool ChatWindow::on_key_press_event(GdkEventKey *e) {
if (e->keyval == GDK_KEY_Return) {
auto buffer = m_input->get_buffer();
@ -136,9 +175,7 @@ void ChatWindow::AddNewMessageInternal() {
}
auto data = m_abaddon->GetDiscordClient().GetMessage(id);
auto *row = CreateChatEntryComponent(data);
if (row != nullptr)
m_listbox->add(*row);
ProcessMessage(data);
}
void ChatWindow::SetMessagesInternal() {
@ -150,6 +187,8 @@ void ChatWindow::SetMessagesInternal() {
it++;
}
m_num_rows = 0;
std::unordered_set<const MessageData *> *msgs;
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
@ -162,9 +201,7 @@ void ChatWindow::SetMessagesInternal() {
sorted_messages[msg->ID] = msg;
for (const auto &[id, msg] : sorted_messages) {
auto *row = CreateChatEntryComponent(msg);
if (row != nullptr)
m_listbox->add(*row);
ProcessMessage(msg);
}
{

View File

@ -22,8 +22,11 @@ protected:
void ScrollToBottom();
void SetMessagesInternal();
void AddNewMessageInternal();
ChatDisplayType GetMessageDisplayType(const MessageData *data);
ChatMessageItem *CreateChatEntryComponentText(const MessageData *data);
ChatMessageItem *CreateChatEntryComponent(const MessageData *data);
void ProcessMessage(const MessageData *data);
int m_num_rows = 0; // youd think thered be a Gtk::ListBox::get_row_count or something but nope
bool on_key_press_event(GdkEventKey *e);