multiple messages from same author are under the same message block
This commit is contained in:
@@ -1,48 +1,59 @@
|
|||||||
#include "chatmessage.hpp"
|
#include "chatmessage.hpp"
|
||||||
|
|
||||||
ChatMessageTextItem::ChatMessageTextItem(const MessageData *data) {
|
ChatMessageTextItem::ChatMessageTextItem(const MessageData *data) {
|
||||||
auto *main_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
m_main_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
||||||
auto *sub_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
m_sub_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
|
||||||
auto *meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
m_meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
||||||
auto *author = Gtk::manage(new Gtk::Label);
|
m_author = Gtk::manage(new Gtk::Label);
|
||||||
auto *timestamp = Gtk::manage(new Gtk::Label);
|
m_timestamp = Gtk::manage(new Gtk::Label);
|
||||||
auto *text = Gtk::manage(new Gtk::TextView);
|
m_text = Gtk::manage(new Gtk::TextView);
|
||||||
|
|
||||||
text->set_can_focus(false);
|
m_text->set_can_focus(false);
|
||||||
text->set_editable(false);
|
m_text->set_editable(false);
|
||||||
text->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
|
m_text->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
|
||||||
text->set_halign(Gtk::ALIGN_FILL);
|
m_text->set_halign(Gtk::ALIGN_FILL);
|
||||||
text->set_hexpand(true);
|
m_text->set_hexpand(true);
|
||||||
text->get_buffer()->set_text(data->Content);
|
m_text->get_buffer()->set_text(data->Content);
|
||||||
text->show();
|
m_text->show();
|
||||||
|
|
||||||
author->set_markup("<span weight=\"bold\">" + Glib::Markup::escape_text(data->Author.Username) + "</span>");
|
m_author->set_markup("<span weight=\"bold\">" + Glib::Markup::escape_text(data->Author.Username) + "</span>");
|
||||||
author->set_single_line_mode(true);
|
m_author->set_single_line_mode(true);
|
||||||
author->set_line_wrap(false);
|
m_author->set_line_wrap(false);
|
||||||
author->set_ellipsize(Pango::ELLIPSIZE_END);
|
m_author->set_ellipsize(Pango::ELLIPSIZE_END);
|
||||||
author->set_xalign(0.f);
|
m_author->set_xalign(0.f);
|
||||||
author->show();
|
m_author->set_can_focus(false);
|
||||||
|
m_author->show();
|
||||||
|
|
||||||
timestamp->set_text(data->Timestamp);
|
m_timestamp->set_text(data->Timestamp);
|
||||||
timestamp->set_opacity(0.5);
|
m_timestamp->set_opacity(0.5);
|
||||||
timestamp->set_single_line_mode(true);
|
m_timestamp->set_single_line_mode(true);
|
||||||
timestamp->set_margin_start(12);
|
m_timestamp->set_margin_start(12);
|
||||||
timestamp->show();
|
m_timestamp->set_can_focus(false);
|
||||||
|
m_timestamp->show();
|
||||||
|
|
||||||
main_box->set_hexpand(true);
|
m_main_box->set_hexpand(true);
|
||||||
main_box->set_vexpand(true);
|
m_main_box->set_vexpand(true);
|
||||||
main_box->show();
|
m_main_box->set_can_focus(true);
|
||||||
|
m_main_box->show();
|
||||||
|
|
||||||
meta_box->show();
|
m_meta_box->set_can_focus(false);
|
||||||
sub_box->show();
|
m_meta_box->show();
|
||||||
|
|
||||||
meta_box->add(*author);
|
m_sub_box->set_can_focus(false);
|
||||||
meta_box->add(*timestamp);
|
m_sub_box->show();
|
||||||
sub_box->add(*meta_box);
|
|
||||||
sub_box->add(*text);
|
m_meta_box->add(*m_author);
|
||||||
main_box->add(*sub_box);
|
m_meta_box->add(*m_timestamp);
|
||||||
add(*main_box);
|
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);
|
set_margin_bottom(8);
|
||||||
|
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ChatMessageTextItem::AppendNewContent(std::string content) {
|
||||||
|
auto buf = m_text->get_buffer();
|
||||||
|
buf->set_text(buf->get_text() + "\n" + content);
|
||||||
|
}
|
||||||
|
@@ -2,12 +2,27 @@
|
|||||||
#include <gtkmm.h>
|
#include <gtkmm.h>
|
||||||
#include "../discord/discord.hpp"
|
#include "../discord/discord.hpp"
|
||||||
|
|
||||||
|
enum class ChatDisplayType {
|
||||||
|
Unknown,
|
||||||
|
Text,
|
||||||
|
};
|
||||||
|
|
||||||
class ChatMessageItem : public Gtk::ListBoxRow {
|
class ChatMessageItem : public Gtk::ListBoxRow {
|
||||||
public:
|
public:
|
||||||
Snowflake ID;
|
Snowflake ID;
|
||||||
|
ChatDisplayType MessageType;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ChatMessageTextItem : public ChatMessageItem {
|
class ChatMessageTextItem : public ChatMessageItem {
|
||||||
public:
|
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;
|
||||||
};
|
};
|
||||||
|
@@ -71,14 +71,24 @@ Snowflake ChatWindow::GetActiveChannel() const {
|
|||||||
return m_active_channel;
|
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) {
|
ChatMessageItem *ChatWindow::CreateChatEntryComponentText(const MessageData *data) {
|
||||||
return Gtk::manage(new ChatMessageTextItem(data));
|
return Gtk::manage(new ChatMessageTextItem(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
|
ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
|
||||||
ChatMessageItem *item = nullptr;
|
ChatMessageItem *item = nullptr;
|
||||||
if (data->Type == MessageType::DEFAULT && data->Content.size() > 0)
|
switch (GetMessageDisplayType(data)) {
|
||||||
item = CreateChatEntryComponentText(data);
|
case ChatDisplayType::Text:
|
||||||
|
item = CreateChatEntryComponentText(data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (item != nullptr)
|
if (item != nullptr)
|
||||||
item->ID = data->ID;
|
item->ID = data->ID;
|
||||||
@@ -86,6 +96,35 @@ ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
|
|||||||
return item;
|
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) {
|
bool ChatWindow::on_key_press_event(GdkEventKey *e) {
|
||||||
if (e->keyval == GDK_KEY_Return) {
|
if (e->keyval == GDK_KEY_Return) {
|
||||||
auto buffer = m_input->get_buffer();
|
auto buffer = m_input->get_buffer();
|
||||||
@@ -136,9 +175,7 @@ void ChatWindow::AddNewMessageInternal() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto data = m_abaddon->GetDiscordClient().GetMessage(id);
|
auto data = m_abaddon->GetDiscordClient().GetMessage(id);
|
||||||
auto *row = CreateChatEntryComponent(data);
|
ProcessMessage(data);
|
||||||
if (row != nullptr)
|
|
||||||
m_listbox->add(*row);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChatWindow::SetMessagesInternal() {
|
void ChatWindow::SetMessagesInternal() {
|
||||||
@@ -150,6 +187,8 @@ void ChatWindow::SetMessagesInternal() {
|
|||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_num_rows = 0;
|
||||||
|
|
||||||
std::unordered_set<const MessageData *> *msgs;
|
std::unordered_set<const MessageData *> *msgs;
|
||||||
{
|
{
|
||||||
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
std::scoped_lock<std::mutex> guard(m_update_mutex);
|
||||||
@@ -162,9 +201,7 @@ void ChatWindow::SetMessagesInternal() {
|
|||||||
sorted_messages[msg->ID] = msg;
|
sorted_messages[msg->ID] = msg;
|
||||||
|
|
||||||
for (const auto &[id, msg] : sorted_messages) {
|
for (const auto &[id, msg] : sorted_messages) {
|
||||||
auto *row = CreateChatEntryComponent(msg);
|
ProcessMessage(msg);
|
||||||
if (row != nullptr)
|
|
||||||
m_listbox->add(*row);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@@ -22,8 +22,11 @@ protected:
|
|||||||
void ScrollToBottom();
|
void ScrollToBottom();
|
||||||
void SetMessagesInternal();
|
void SetMessagesInternal();
|
||||||
void AddNewMessageInternal();
|
void AddNewMessageInternal();
|
||||||
|
ChatDisplayType GetMessageDisplayType(const MessageData *data);
|
||||||
ChatMessageItem *CreateChatEntryComponentText(const MessageData *data);
|
ChatMessageItem *CreateChatEntryComponentText(const MessageData *data);
|
||||||
ChatMessageItem *CreateChatEntryComponent(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);
|
bool on_key_press_event(GdkEventKey *e);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user