Merge branch 'master' into msys

This commit is contained in:
ouwou
2022-03-31 03:21:26 -04:00
2 changed files with 29 additions and 20 deletions

View File

@@ -1,6 +1,6 @@
#include "chatmessage.hpp"
#include "chatlist.hpp"
#include "abaddon.hpp"
#include "chatmessage.hpp"
#include "constants.hpp"
ChatList::ChatList() {
@@ -8,17 +8,23 @@ ChatList::ChatList() {
set_can_focus(false);
set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
signal_edge_reached().connect(sigc::mem_fun(*this, &ChatList::OnScrollEdgeOvershot));
auto v = get_vadjustment();
v->signal_value_changed().connect([this, v] {
if (m_history_timer.elapsed() > 1 && v->get_value() < 500) {
m_history_timer.start();
m_signal_action_chat_load_history.emit(m_active_channel);
}
m_should_scroll_to_bottom = v->get_upper() - v->get_page_size() <= v->get_value();
});
m_list.signal_size_allocate().connect([this](Gtk::Allocation &) {
if (m_should_scroll_to_bottom)
ScrollToBottom();
});
v->property_upper().signal_changed().connect(sigc::mem_fun(*this, &ChatList::OnUpperAdjustmentChanged));
m_list.signal_size_allocate()
.connect([this](Gtk::Allocation &) {
if (m_should_scroll_to_bottom)
ScrollToBottom();
});
m_list.set_focus_hadjustment(get_hadjustment());
m_list.set_focus_vadjustment(get_vadjustment());
@@ -243,7 +249,7 @@ void ChatList::RefetchMessage(Snowflake id) {
}
Snowflake ChatList::GetOldestListedMessage() {
if (m_id_to_widget.size() > 0)
if (!m_id_to_widget.empty())
return m_id_to_widget.begin()->first;
else
return Snowflake::Invalid;
@@ -306,9 +312,14 @@ void ChatList::ActuallyRemoveMessage(Snowflake id) {
RemoveMessageAndHeader(it->second);
}
void ChatList::OnScrollEdgeOvershot(Gtk::PositionType pos) {
if (pos == Gtk::POS_TOP)
m_signal_action_chat_load_history.emit(m_active_channel);
void ChatList::OnUpperAdjustmentChanged() {
const auto v = get_vadjustment();
const auto upper = v->get_upper();
if (m_needs_upper_adjustment && m_old_upper > -1.0) {
const auto inc = upper - m_old_upper;
v->set_value(v->get_value() + inc);
}
m_old_upper = v->get_upper();
}
void ChatList::ScrollToBottom() {

View File

@@ -2,6 +2,7 @@
#include <gtkmm.h>
#include <map>
#include <vector>
#include "discord/message.hpp"
#include "discord/snowflake.hpp"
class ChatList : public Gtk::ScrolledWindow {
@@ -25,7 +26,7 @@ public:
void ActuallyRemoveMessage(Snowflake id); // perhaps not the best method name
private:
void OnScrollEdgeOvershot(Gtk::PositionType pos);
void OnUpperAdjustmentChanged();
void ScrollToBottom();
void RemoveMessageAndHeader(Gtk::Widget *widget);
@@ -52,6 +53,10 @@ private:
bool m_separate_all = false;
Glib::Timer m_history_timer;
bool m_needs_upper_adjustment = false;
double m_old_upper = -1.0;
public:
// these are all forwarded by the parent
using type_signal_action_message_edit = sigc::signal<void, Snowflake, Snowflake>;
@@ -88,6 +93,7 @@ private:
template<typename Iter>
inline void ChatList::SetMessages(Iter begin, Iter end) {
m_needs_upper_adjustment = false;
Clear();
m_num_rows = 0;
m_num_messages = 0;
@@ -101,15 +107,7 @@ inline void ChatList::SetMessages(Iter begin, Iter end) {
template<typename Iter>
inline void ChatList::PrependMessages(Iter begin, Iter end) {
const auto old_upper = get_vadjustment()->get_upper();
const auto old_value = get_vadjustment()->get_value();
m_needs_upper_adjustment = true;
for (Iter it = begin; it != end; it++)
ProcessNewMessage(*it, true);
// force everything to process before getting new values
while (Gtk::Main::events_pending())
Gtk::Main::iteration();
const auto new_upper = get_vadjustment()->get_upper();
if (old_value == 0.0 && (new_upper - old_upper) > 0.0)
get_vadjustment()->set_value(new_upper - old_upper);
// this isn't ideal
}