summaryrefslogtreecommitdiff
path: root/components/chatwindow.cpp
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2020-08-27 18:50:47 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2020-08-27 18:50:47 -0400
commit34b572db40a096aa7f411f837d7ae6884209a3f6 (patch)
tree67c4f48b6d3ffc0af288725be78cd4b50a7fb8a1 /components/chatwindow.cpp
parentf3e8d1aff329b65f171e9f6f41681eb179b58d4f (diff)
downloadabaddon-portaudio-34b572db40a096aa7f411f837d7ae6884209a3f6.tar.gz
abaddon-portaudio-34b572db40a096aa7f411f837d7ae6884209a3f6.zip
multiple messages from same author are under the same message block
Diffstat (limited to 'components/chatwindow.cpp')
-rw-r--r--components/chatwindow.cpp53
1 files changed, 45 insertions, 8 deletions
diff --git a/components/chatwindow.cpp b/components/chatwindow.cpp
index 539d6b6..36df514 100644
--- a/components/chatwindow.cpp
+++ b/components/chatwindow.cpp
@@ -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);
}
{