scroll up to load more messages

This commit is contained in:
ouwou
2020-08-28 18:21:08 -04:00
parent 8699a547f3
commit 5b7d36bb74
10 changed files with 121 additions and 8 deletions

View File

@@ -5,6 +5,7 @@
ChatWindow::ChatWindow() {
m_message_set_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::SetMessagesInternal));
m_new_message_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::AddNewMessageInternal));
m_new_history_dispatch.connect(sigc::mem_fun(*this, &ChatWindow::AddNewHistoryInternal));
m_main = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
m_listbox = Gtk::manage(new Gtk::ListBox);
@@ -19,12 +20,20 @@ ChatWindow::ChatWindow() {
m_main->set_vexpand(true);
m_main->show();
m_scroll->signal_edge_reached().connect(sigc::mem_fun(*this, &ChatWindow::on_scroll_edge_overshot));
auto vadj = m_scroll->get_vadjustment();
vadj->signal_value_changed().connect([&, vadj]() {
m_scroll_to_bottom = vadj->get_upper() - vadj->get_page_size() <= vadj->get_value();
});
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();
if (m_scroll_to_bottom)
ScrollToBottom();
});
m_listbox->set_selection_mode(Gtk::SELECTION_NONE);
@@ -96,18 +105,25 @@ ChatMessageItem *ChatWindow::CreateChatEntryComponent(const MessageData *data) {
return item;
}
void ChatWindow::ProcessMessage(const MessageData *data) {
void ChatWindow::ProcessMessage(const MessageData *data, bool prepend) {
auto create_new_row = [&]() {
auto *item = CreateChatEntryComponent(data);
if (item != nullptr) {
m_listbox->add(*item);
if (prepend)
m_listbox->prepend(*item);
else
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));
ChatMessageItem *item;
if (prepend)
item = dynamic_cast<ChatMessageItem *>(m_listbox->get_row_at_index(0));
else
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);
@@ -116,7 +132,10 @@ void ChatWindow::ProcessMessage(const MessageData *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);
if (prepend)
text_item->PrependNewContent(data->Content);
else
text_item->AppendNewContent(data->Content);
} else {
create_new_row();
}
@@ -143,6 +162,11 @@ bool ChatWindow::on_key_press_event(GdkEventKey *e) {
return false;
}
void ChatWindow::on_scroll_edge_overshot(Gtk::PositionType pos) {
if (pos == Gtk::POS_TOP)
m_abaddon->ActionChatLoadHistory(m_active_channel);
}
void ChatWindow::SetMessages(std::unordered_set<const MessageData *> msgs) {
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_message_set_queue.push(msgs);
@@ -155,6 +179,15 @@ void ChatWindow::AddNewMessage(Snowflake id) {
m_new_message_dispatch.emit();
}
void ChatWindow::AddNewHistory(const std::vector<MessageData> &msgs) {
std::scoped_lock<std::mutex> guard(m_update_mutex);
std::vector<Snowflake> x;
for (const auto &msg : msgs)
x.push_back(msg.ID);
m_new_history_queue.push(x);
m_new_history_dispatch.emit();
}
void ChatWindow::ClearMessages() {
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_message_set_queue.push(std::unordered_set<const MessageData *>());
@@ -178,6 +211,25 @@ void ChatWindow::AddNewMessageInternal() {
ProcessMessage(data);
}
// todo this keeps the scrollbar at the top
void ChatWindow::AddNewHistoryInternal() {
std::set<Snowflake> msgs;
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
auto vec = m_new_history_queue.front();
msgs = std::set<Snowflake>(vec.begin(), vec.end());
}
for (auto it = msgs.rbegin(); it != msgs.rend(); it++) {
ProcessMessage(m_abaddon->GetDiscordClient().GetMessage(*it), true);
}
{
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_new_history_queue.pop();
}
}
void ChatWindow::SetMessagesInternal() {
auto children = m_listbox->get_children();
auto it = children.begin();