forked from OpenGamers/abaddon
some refactorage and add avatars to member list
This commit is contained in:
parent
e68f8ef8f4
commit
2822add5fe
@ -14,12 +14,11 @@ ChatMessageContainer::ChatMessageContainer(const Message *data) {
|
||||
m_author = Gtk::manage(new Gtk::Label);
|
||||
m_timestamp = Gtk::manage(new Gtk::Label);
|
||||
|
||||
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 = Gtk::manage(new Gtk::Image(Abaddon::Get().GetImageManager().GetPlaceholder(32)));
|
||||
|
||||
m_avatar->set_valign(Gtk::ALIGN_START);
|
||||
m_avatar->set_margin_right(10);
|
||||
|
@ -2,6 +2,44 @@
|
||||
#include "../abaddon.hpp"
|
||||
#include "../util.hpp"
|
||||
|
||||
MemberListUserRow::MemberListUserRow(Snowflake guild_id, const User *data) {
|
||||
ID = data->ID;
|
||||
m_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
|
||||
m_label = Gtk::manage(new Gtk::Label);
|
||||
m_avatar = Gtk::manage(new Gtk::Image(Abaddon::Get().GetImageManager().GetPlaceholder(16)));
|
||||
|
||||
get_style_context()->add_class("members-row");
|
||||
get_style_context()->add_class("members-row-member");
|
||||
m_label->get_style_context()->add_class("members-row-label");
|
||||
|
||||
m_label->set_single_line_mode(true);
|
||||
m_label->set_ellipsize(Pango::ELLIPSIZE_END);
|
||||
if (data != nullptr) {
|
||||
std::string display = data->Username + "#" + data->Discriminator;
|
||||
auto col_id = data->GetHoistedRole(guild_id, true);
|
||||
if (col_id.IsValid()) {
|
||||
auto color = Abaddon::Get().GetDiscordClient().GetRole(col_id)->Color;
|
||||
m_label->set_use_markup(true);
|
||||
m_label->set_markup("<span color='#" + IntToCSSColor(color) + "'>" + Glib::Markup::escape_text(display) + "</span>");
|
||||
|
||||
} else {
|
||||
m_label->set_text(display);
|
||||
}
|
||||
} else {
|
||||
m_label->set_use_markup(true);
|
||||
m_label->set_markup("<i>[unknown user]</i>");
|
||||
}
|
||||
m_label->set_halign(Gtk::ALIGN_START);
|
||||
m_box->add(*m_avatar);
|
||||
m_box->add(*m_label);
|
||||
add(*m_box);
|
||||
show_all();
|
||||
}
|
||||
|
||||
void MemberListUserRow::SetAvatarFromPixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf) {
|
||||
m_avatar->property_pixbuf() = pixbuf;
|
||||
}
|
||||
|
||||
MemberList::MemberList() {
|
||||
m_update_member_list_dispatcher.connect(sigc::mem_fun(*this, &MemberList::UpdateMemberListInternal));
|
||||
|
||||
@ -52,6 +90,8 @@ void MemberList::UpdateMemberList() {
|
||||
}
|
||||
|
||||
void MemberList::UpdateMemberListInternal() {
|
||||
m_id_to_row.clear();
|
||||
|
||||
auto children = m_listbox->get_children();
|
||||
auto it = children.begin();
|
||||
while (it != children.end()) {
|
||||
@ -107,38 +147,31 @@ void MemberList::UpdateMemberListInternal() {
|
||||
}
|
||||
|
||||
auto add_user = [this, &user_to_color](const User *data) {
|
||||
auto *user_row = Gtk::manage(new MemberListUserRow);
|
||||
user_row->ID = data->ID;
|
||||
auto *user_ev = Gtk::manage(new Gtk::EventBox);
|
||||
auto *user_lbl = Gtk::manage(new Gtk::Label);
|
||||
|
||||
user_row->get_style_context()->add_class("members-row");
|
||||
user_row->get_style_context()->add_class("members-row-member");
|
||||
user_lbl->get_style_context()->add_class("members-row-label");
|
||||
|
||||
user_lbl->set_single_line_mode(true);
|
||||
user_lbl->set_ellipsize(Pango::ELLIPSIZE_END);
|
||||
if (data != nullptr) {
|
||||
std::string display = data->Username + "#" + data->Discriminator;
|
||||
if (user_to_color.find(data->ID) != user_to_color.end()) {
|
||||
auto color = user_to_color.at(data->ID);
|
||||
user_lbl->set_use_markup(true);
|
||||
user_lbl->set_markup("<span color='#" + IntToCSSColor(color) + "'>" + Glib::Markup::escape_text(display) + "</span>");
|
||||
auto *row = Gtk::manage(new MemberListUserRow(m_guild_id, data));
|
||||
m_id_to_row[data->ID] = row;
|
||||
|
||||
if (data->HasAvatar()) {
|
||||
auto buf = Abaddon::Get().GetImageManager().GetFromURLIfCached(data->GetAvatarURL("png", "16"));
|
||||
if (buf) {
|
||||
row->SetAvatarFromPixbuf(buf);
|
||||
} else {
|
||||
user_lbl->set_text(display);
|
||||
}
|
||||
Snowflake id = data->ID;
|
||||
Abaddon::Get().GetImageManager().LoadFromURL(data->GetAvatarURL("png", "16"), [this, id](Glib::RefPtr<Gdk::Pixbuf> pbuf) {
|
||||
Glib::signal_idle().connect([this, id, pbuf]() -> bool {
|
||||
if (m_id_to_row.find(id) != m_id_to_row.end()) {
|
||||
auto *foundrow = static_cast<MemberListUserRow *>(m_id_to_row.at(id));
|
||||
if (foundrow != nullptr)
|
||||
foundrow->SetAvatarFromPixbuf(pbuf);
|
||||
}
|
||||
|
||||
AttachUserMenuHandler(user_row, data->ID);
|
||||
} else {
|
||||
user_lbl->set_use_markup(true);
|
||||
user_lbl->set_markup("<i>[unknown user]</i>");
|
||||
return false;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
user_lbl->set_halign(Gtk::ALIGN_START);
|
||||
user_ev->add(*user_lbl);
|
||||
user_row->add(*user_ev);
|
||||
user_row->show_all();
|
||||
m_listbox->add(*user_row);
|
||||
|
||||
AttachUserMenuHandler(row, data->ID);
|
||||
m_listbox->add(*row);
|
||||
};
|
||||
|
||||
auto add_role = [this](std::string name) {
|
||||
|
@ -1,15 +1,24 @@
|
||||
#pragma once
|
||||
#include <gtkmm.h>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include "../discord/discord.hpp"
|
||||
|
||||
class MemberListUserRow : public Gtk::ListBoxRow {
|
||||
public:
|
||||
MemberListUserRow(Snowflake guild_id, const User *data);
|
||||
void SetAvatarFromPixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf);
|
||||
|
||||
Snowflake ID;
|
||||
|
||||
private:
|
||||
Gtk::Box *m_box;
|
||||
Gtk::Image *m_avatar;
|
||||
Gtk::Label *m_label;
|
||||
};
|
||||
|
||||
class MemberList {
|
||||
public:
|
||||
class MemberListUserRow : public Gtk::ListBoxRow {
|
||||
public:
|
||||
Snowflake ID;
|
||||
};
|
||||
|
||||
MemberList();
|
||||
Gtk::Widget *GetRoot() const;
|
||||
|
||||
@ -38,6 +47,8 @@ private:
|
||||
Snowflake m_guild_id;
|
||||
Snowflake m_chan_id;
|
||||
|
||||
std::unordered_map<Snowflake, Gtk::ListBoxRow *> m_id_to_row;
|
||||
|
||||
public:
|
||||
typedef sigc::signal<void, Snowflake> type_signal_action_insert_mention;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "user.hpp"
|
||||
#include "../abaddon.hpp"
|
||||
|
||||
bool User::HasAvatar() const {
|
||||
return Avatar.size() > 0;
|
||||
@ -8,6 +9,10 @@ std::string User::GetAvatarURL(std::string ext, std::string size) const {
|
||||
return "https://cdn.discordapp.com/avatars/" + std::to_string(ID) + "/" + Avatar + "." + ext + "?size=" + size;
|
||||
}
|
||||
|
||||
Snowflake User::GetHoistedRole(Snowflake guild_id, bool with_color) const {
|
||||
return Abaddon::Get().GetDiscordClient().GetMemberHoistedRole(guild_id, ID, with_color);
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json &j, User &m) {
|
||||
JS_D("id", m.ID);
|
||||
JS_D("username", m.Username);
|
||||
|
@ -28,4 +28,5 @@ struct User {
|
||||
|
||||
bool HasAvatar() const;
|
||||
std::string GetAvatarURL(std::string ext = "png", std::string size = "32") const;
|
||||
Snowflake GetHoistedRole(Snowflake guild_id, bool with_color = false) const;
|
||||
};
|
||||
|
@ -27,3 +27,18 @@ Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetFromURLIfCached(std::string url) {
|
||||
|
||||
return Glib::RefPtr<Gdk::Pixbuf>(nullptr);
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gdk::Pixbuf> ImageManager::GetPlaceholder(int size) {
|
||||
std::string name = "/placeholder" + std::to_string(size);
|
||||
if (m_pixs.find(name) != m_pixs.end())
|
||||
return m_pixs.at(name);
|
||||
|
||||
try {
|
||||
auto buf = Gdk::Pixbuf::create_from_file("res/decamarks.png", size, size);
|
||||
m_pixs[name] = buf;
|
||||
return buf;
|
||||
} catch (std::exception &e) {
|
||||
fprintf(stderr, "error loading placeholder\n");
|
||||
return Glib::RefPtr<Gdk::Pixbuf>(nullptr);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ public:
|
||||
Cache &GetCache();
|
||||
void LoadFromURL(std::string url, std::function<void(Glib::RefPtr<Gdk::Pixbuf>)> cb);
|
||||
Glib::RefPtr<Gdk::Pixbuf> GetFromURLIfCached(std::string url);
|
||||
Glib::RefPtr<Gdk::Pixbuf> GetPlaceholder(int size);
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, Glib::RefPtr<Gdk::Pixbuf>> m_pixs;
|
||||
|
Loading…
Reference in New Issue
Block a user