Skip to content

Commit

Permalink
add mention overlay to folders in classic
Browse files Browse the repository at this point in the history
  • Loading branch information
ouwou committed May 25, 2024
1 parent bf29c44 commit 96687f0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
19 changes: 17 additions & 2 deletions src/components/channellist/classic/guildlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,22 @@ static Gtk::Widget *AddMentionOverlay(Gtk::Widget *widget, Snowflake guild_id) {
mention_overlay->signal_realize().connect([mention_overlay]() {
mention_overlay->get_window()->set_pass_through(true);
});
overlay->show_all();
mention_overlay->show();
overlay->show();
return overlay;
}

static Gtk::Widget *AddMentionOverlay(Gtk::Widget *widget, const UserSettingsGuildFoldersEntry &folder) {
auto *overlay = Gtk::make_managed<Gtk::Overlay>();
overlay->add(*widget);
auto *mention_overlay = Gtk::make_managed<MentionOverlay>(folder);
overlay->add_overlay(*mention_overlay);
overlay->set_overlay_pass_through(*mention_overlay, true);
mention_overlay->signal_realize().connect([mention_overlay]() {
mention_overlay->get_window()->set_pass_through(true);
});
mention_overlay->show();
overlay->show();
return overlay;
}

Expand Down Expand Up @@ -151,7 +166,7 @@ void GuildList::AddFolder(const UserSettingsGuildFoldersEntry &folder) {
}

folder_widget->show();
add(*folder_widget);
add(*AddMentionOverlay(folder_widget, folder));
}

void GuildList::Clear() {
Expand Down
27 changes: 20 additions & 7 deletions src/components/channellist/classic/mentionoverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
#include "abaddon.hpp"

MentionOverlay::MentionOverlay(Snowflake guild_id)
: m_guild_id(guild_id) {
: m_guild_ids({ guild_id }) {
Init();
}

MentionOverlay::MentionOverlay(const UserSettingsGuildFoldersEntry &folder)
: m_guild_ids({ folder.GuildIDs.begin(), folder.GuildIDs.end() }) {
Init();
}

void MentionOverlay::Init() {
m_font.set_family("sans 14");
m_layout = create_pango_layout("12");
m_layout->set_font_description(m_font);
Expand All @@ -24,20 +33,24 @@ MentionOverlay::MentionOverlay(Snowflake guild_id)
});

Abaddon::Get().GetDiscordClient().signal_message_create().connect([this](const Message &msg) {
if (msg.GuildID.has_value() && *msg.GuildID != m_guild_id) return;
if (msg.GuildID.has_value() && m_guild_ids.find(*msg.GuildID) == m_guild_ids.end()) return;
if (!msg.DoesMentionEveryone && msg.Mentions.empty() && msg.MentionRoles.empty()) return;
queue_draw();
});
}

bool MentionOverlay::OnDraw(const Cairo::RefPtr<Cairo::Context> &cr) {
int mentions;
Abaddon::Get().GetDiscordClient().GetUnreadStateForGuild(m_guild_id, mentions);
if (mentions == 0) return true;
m_layout->set_text(std::to_string(mentions));
int total_mentions = 0;
for (auto guild_id : m_guild_ids) {
int mentions;
Abaddon::Get().GetDiscordClient().GetUnreadStateForGuild(guild_id, mentions);
total_mentions += mentions;
}
if (total_mentions == 0) return true;
m_layout->set_text(std::to_string(total_mentions));

const int width = get_allocated_width();
const int height = get_allocated_height();
const int height = std::min(get_allocated_height(), 48); // cope

int lw, lh;
m_layout->get_pixel_size(lw, lh);
Expand Down
6 changes: 5 additions & 1 deletion src/components/channellist/classic/mentionoverlay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
#include <pangomm/fontdescription.h>

#include "discord/snowflake.hpp"
#include "discord/usersettings.hpp"

class MentionOverlay : public Gtk::DrawingArea {
public:
MentionOverlay(Snowflake guild_id);
MentionOverlay(const UserSettingsGuildFoldersEntry &folder);

private:
void Init();

bool OnDraw(const Cairo::RefPtr<Cairo::Context> &cr);

Snowflake m_guild_id;
std::set<Snowflake> m_guild_ids;

Pango::FontDescription m_font;
Glib::RefPtr<Pango::Layout> m_layout;
Expand Down

0 comments on commit 96687f0

Please sign in to comment.