add http client and channel reordering (waste of time)

This commit is contained in:
ouwou
2020-08-19 21:08:57 -04:00
parent 0cd0260f2e
commit 4b903bbd3e
11 changed files with 279 additions and 48 deletions

View File

@@ -7,8 +7,18 @@
ChannelList::ChannelList() {
m_main = Gtk::manage(new Gtk::ScrolledWindow);
m_list = Gtk::manage(new Gtk::ListBox);
m_list->set_activate_on_single_click(true);
m_guild_menu_up = Gtk::manage(new Gtk::MenuItem("Move _Up", true));
m_guild_menu_up->signal_activate().connect(sigc::mem_fun(*this, &ChannelList::on_menu_move_up));
m_guild_menu.append(*m_guild_menu_up);
m_guild_menu_down = Gtk::manage(new Gtk::MenuItem("Move _Down", true));
m_guild_menu_down->signal_activate().connect(sigc::mem_fun(*this, &ChannelList::on_menu_move_down));
m_guild_menu.append(*m_guild_menu_down);
m_guild_menu.show_all();
m_list->set_activate_on_single_click(true);
m_list->signal_row_activated().connect(sigc::mem_fun(*this, &ChannelList::on_row_activated));
m_main->add(*m_list);
@@ -83,47 +93,14 @@ void ChannelList::SetListingFromGuildsInternal() {
it++;
}
m_guild_count = 0;
if (guilds->empty()) {
std::scoped_lock<std::mutex> guard(m_update_mutex);
m_update_queue.pop();
return;
}
auto &settings = m_abaddon->GetDiscordClient().GetUserSettings();
std::vector<std::pair<Snowflake, GuildData>> sorted_guilds;
if (settings.GuildPositions.size()) {
for (const auto &id : settings.GuildPositions) {
auto &guild = (*guilds)[id];
sorted_guilds.push_back(std::make_pair(id, guild));
}
} else { // default sort is alphabetic
for (auto &it : *guilds)
sorted_guilds.push_back(it);
std::sort(sorted_guilds.begin(), sorted_guilds.end(), [&](auto &a, auto &b) -> bool {
std::string &s1 = a.second.Name;
std::string &s2 = b.second.Name;
if (s1.empty() || s2.empty())
return s1 < s2;
bool ac[] = {
!isalnum(s1[0]),
!isalnum(s2[0]),
isdigit(s1[0]),
isdigit(s2[0]),
isalpha(s1[0]),
isalpha(s2[0]),
};
if ((ac[0] && ac[1]) || (ac[2] && ac[3]) || (ac[4] && ac[5]))
return s1 < s2;
return ac[0] || ac[5];
});
}
// map each category to its channels
std::unordered_map<Snowflake, std::vector<const ChannelData *>> cat_to_channels;
std::unordered_map<Snowflake, std::vector<const ChannelData *>> orphan_channels;
@@ -151,12 +128,14 @@ void ChannelList::SetListingFromGuildsInternal() {
auto add_channel = [&](const Snowflake &id, const ChannelData &channel) -> Gtk::ListBoxRow * {
auto *channel_row = Gtk::manage(new Gtk::ListBoxRow);
auto *channel_ev = Gtk::manage(new Gtk::EventBox);
auto *channel_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *channel_label = Gtk::manage(new Gtk::Label);
channel_label->set_text("#" + channel.Name);
channel_box->set_halign(Gtk::ALIGN_START);
channel_box->pack_start(*channel_label);
channel_row->add(*channel_box);
channel_ev->add(*channel_box);
channel_row->add(*channel_ev);
channel_row->show_all_children();
m_list->add(*channel_row);
@@ -171,6 +150,7 @@ void ChannelList::SetListingFromGuildsInternal() {
auto add_category = [&](const Snowflake &id, const ChannelData &channel) -> Gtk::ListBoxRow * {
auto *category_row = Gtk::manage(new Gtk::ListBoxRow);
auto *category_ev = Gtk::manage(new Gtk::EventBox);
auto *category_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *category_label = Gtk::manage(new Gtk::Label);
auto *category_arrow = Gtk::manage(new Gtk::Arrow(Gtk::ARROW_DOWN, Gtk::SHADOW_NONE));
@@ -178,7 +158,8 @@ void ChannelList::SetListingFromGuildsInternal() {
category_box->set_halign(Gtk::ALIGN_START);
category_box->pack_start(*category_arrow);
category_box->pack_start(*category_label);
category_row->add(*category_box);
category_ev->add(*category_box);
category_row->add(*category_ev);
category_row->show_all_children();
m_list->add(*category_row);
@@ -208,19 +189,23 @@ void ChannelList::SetListingFromGuildsInternal() {
auto add_guild = [&](const Snowflake &id, const GuildData &guild) {
auto *guild_row = Gtk::manage(new Gtk::ListBoxRow);
auto *guild_ev = Gtk::manage(new Gtk::EventBox);
auto *guild_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
auto *guild_label = Gtk::manage(new Gtk::Label);
guild_label->set_markup("<b>" + Glib::Markup::escape_text(guild.Name) + "</b>");
guild_box->set_halign(Gtk::ALIGN_START);
guild_box->pack_start(*guild_label);
guild_row->add(*guild_box);
guild_ev->add(*guild_box);
guild_row->add(*guild_ev);
guild_row->show_all();
m_list->add(*guild_row);
AttachMenuHandler(guild_row);
ListItemInfo info;
info.ID = id;
info.IsUserCollapsed = true;
info.IsHidden = false;
info.GuildIndex = m_guild_count++;
if (orphan_channels.find(id) != orphan_channels.end()) {
std::map<int, const ChannelData *> sorted_orphans;
@@ -256,6 +241,7 @@ void ChannelList::SetListingFromGuildsInternal() {
m_infos[guild_row] = std::move(info);
};
const auto &sorted_guilds = m_abaddon->GetDiscordClient().GetUserSortedGuilds();
for (const auto &[id, guild] : sorted_guilds) {
add_guild(id, guild);
}
@@ -265,3 +251,27 @@ void ChannelList::SetListingFromGuildsInternal() {
m_update_queue.pop();
}
}
void ChannelList::on_menu_move_up() {
auto row = m_list->get_selected_row();
m_abaddon->ActionMoveGuildUp(m_infos[row].ID);
}
void ChannelList::on_menu_move_down() {
auto row = m_list->get_selected_row();
m_abaddon->ActionMoveGuildDown(m_infos[row].ID);
}
void ChannelList::AttachMenuHandler(Gtk::ListBoxRow *row) {
row->signal_button_press_event().connect([&, row](GdkEventButton *e) -> bool {
if (e->type == GDK_BUTTON_PRESS && e->button == GDK_BUTTON_SECONDARY) {
m_list->select_row(*row);
m_guild_menu_up->set_sensitive(m_infos[row].GuildIndex != 0);
m_guild_menu_down->set_sensitive(m_infos[row].GuildIndex != m_guild_count - 1);
m_guild_menu.popup_at_pointer(reinterpret_cast<const GdkEvent *>(e));
return true;
}
return false;
});
}