make avatar loading a bit better

This commit is contained in:
ouwou
2020-09-12 03:17:34 -04:00
parent d6ed75c339
commit e68f8ef8f4
9 changed files with 87 additions and 15 deletions

View File

@@ -14,18 +14,16 @@ ChatMessageContainer::ChatMessageContainer(const Message *data) {
m_author = Gtk::manage(new Gtk::Label);
m_timestamp = Gtk::manage(new Gtk::Label);
static Glib::RefPtr<Gdk::Pixbuf> test = Gdk::Pixbuf::create_from_file("res/decamarks.png", 32, 32);
m_avatar = Gtk::manage(new Gtk::Image(test));
static auto placeholder = Gdk::Pixbuf::create_from_file("res/decamarks.png", 32, 32);
auto buf = Abaddon::Get().GetImageManager().GetFromURLIfCached(data->Author.GetAvatarURL());
if (buf)
m_avatar = Gtk::manage(new Gtk::Image(buf));
else
m_avatar = Gtk::manage(new Gtk::Image(placeholder));
m_avatar->set_valign(Gtk::ALIGN_START);
m_avatar->set_margin_right(10);
if (data->Author.HasAvatar()) {
Abaddon::Get().GetCache().GetFileFromURL(data->Author.GetAvatarURL(), [this](std::string filepath) {
auto buf = Gdk::Pixbuf::create_from_file(filepath, 32, 32);
m_avatar->property_pixbuf() = buf;
});
}
get_style_context()->add_class("message-container");
m_author->get_style_context()->add_class("message-container-author");
m_timestamp->get_style_context()->add_class("message-container-timestamp");
@@ -67,6 +65,10 @@ ChatMessageContainer::ChatMessageContainer(const Message *data) {
show();
}
void ChatMessageContainer::SetAvatarFromPixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf) {
m_avatar->property_pixbuf() = pixbuf;
}
void ChatMessageContainer::Update() {
auto &discord = Abaddon::Get().GetDiscordClient();
auto guild_id = discord.GetChannel(ChannelID)->GuildID;

View File

@@ -19,6 +19,7 @@ public:
ChatMessageContainer(const Message *data);
void AddNewContent(Gtk::Widget *widget, bool prepend = false);
void SetAvatarFromPixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf);
void Update();
protected:

View File

@@ -115,6 +115,25 @@ void ChatWindow::ProcessMessage(const Message *data, bool prepend) {
} else {
container = Gtk::manage(new ChatMessageContainer(data)); // only accesses timestamp and user
container->Update();
auto user_id = data->Author.ID;
const auto *user = Abaddon::Get().GetDiscordClient().GetUser(user_id);
if (user == nullptr) return;
Abaddon::Get().GetImageManager().LoadFromURL(user->GetAvatarURL(), [this, user_id](Glib::RefPtr<Gdk::Pixbuf> buf) {
// am i retarded?
Glib::signal_idle().connect([this, buf, user_id]() -> bool {
auto children = m_listbox->get_children();
for (auto child : children) {
auto *row = dynamic_cast<ChatMessageContainer *>(child);
if (row == nullptr) continue;
if (row->UserID == user_id) {
row->SetAvatarFromPixbuf(buf);
}
}
return false;
});
});
m_num_rows++;
}