Merge pull request #186 from jkozera/unread-category-indicators

Show unread indicators next to categories containing unread channels
This commit is contained in:
ouwou
2023-07-05 05:01:08 +00:00
committed by GitHub
5 changed files with 46 additions and 16 deletions

View File

@@ -1102,15 +1102,25 @@ void ChannelList::SetDMChannelIcon(Gtk::TreeIter iter, const ChannelData &dm) {
}
}
void ChannelList::RedrawUnreadIndicatorsForChannel(const ChannelData &channel) {
if (channel.GuildID.has_value()) {
auto iter = GetIteratorForGuildFromID(*channel.GuildID);
if (iter) m_model->row_changed(m_model->get_path(iter), iter);
}
if (channel.ParentID.has_value()) {
auto iter = GetIteratorForRowFromIDOfType(*channel.ParentID, RenderType::Category);
if (iter) m_model->row_changed(m_model->get_path(iter), iter);
}
}
void ChannelList::OnMessageAck(const MessageAckData &data) {
// trick renderer into redrawing
m_model->row_changed(Gtk::TreeModel::Path("0"), m_model->get_iter("0")); // 0 is always path for dm header
auto iter = GetIteratorForRowFromID(data.ChannelID);
if (iter) m_model->row_changed(m_model->get_path(iter), iter);
auto channel = Abaddon::Get().GetDiscordClient().GetChannel(data.ChannelID);
if (channel.has_value() && channel->GuildID.has_value()) {
iter = GetIteratorForGuildFromID(*channel->GuildID);
if (iter) m_model->row_changed(m_model->get_path(iter), iter);
if (channel.has_value()) {
RedrawUnreadIndicatorsForChannel(*channel);
}
}
@@ -1123,9 +1133,7 @@ void ChannelList::OnMessageCreate(const Message &msg) {
if (iter)
(*iter)[m_columns.m_sort] = static_cast<int64_t>(-msg.ID);
}
if (channel->GuildID.has_value())
if ((iter = GetIteratorForGuildFromID(*channel->GuildID)))
m_model->row_changed(m_model->get_path(iter), iter);
RedrawUnreadIndicatorsForChannel(*channel);
}
bool ChannelList::OnButtonPressEvent(GdkEventButton *ev) {

View File

@@ -116,9 +116,10 @@ protected:
void UpdateCreateDMChannel(const ChannelData &channel);
void SetDMChannelIcon(Gtk::TreeIter iter, const ChannelData &dm);
void RedrawUnreadIndicatorsForChannel(const ChannelData& channel);
void OnMessageAck(const MessageAckData &data);
void OnMessageCreate(const Message &msg);
Gtk::TreeModel::Path m_path_for_menu;
// cant be recovered through selection

View File

@@ -410,6 +410,17 @@ void CellRendererChannels::get_preferred_height_for_width_vfunc_category(Gtk::Wi
m_renderer_text.get_preferred_height_for_width(widget, width, minimum_height, natural_height);
}
void AddUnreadIndicator(const Cairo::RefPtr<Cairo::Context> &cr, const Gdk::Rectangle &background_area) {
static const auto color = Gdk::RGBA(Abaddon::Get().GetSettings().UnreadIndicatorColor);
cr->set_source_rgb(color.get_red(), color.get_green(), color.get_blue());
const auto x = background_area.get_x();
const auto y = background_area.get_y();
const auto w = background_area.get_width();
const auto h = background_area.get_height();
cr->rectangle(x, y, 3, h);
cr->fill();
}
void CellRendererChannels::render_vfunc_category(const Cairo::RefPtr<Cairo::Context> &cr, Gtk::Widget &widget, const Gdk::Rectangle &background_area, const Gdk::Rectangle &cell_area, Gtk::CellRendererState flags) {
// todo: figure out how Gtk::Arrow is rendered because i like it better :^)
constexpr static int len = 5;
@@ -447,13 +458,18 @@ void CellRendererChannels::render_vfunc_category(const Cairo::RefPtr<Cairo::Cont
Gdk::Rectangle text_cell_area(text_x, text_y, text_w, text_h);
static const auto color = Gdk::RGBA(Abaddon::Get().GetSettings().ChannelColor);
if (Abaddon::Get().GetDiscordClient().IsChannelMuted(m_property_id.get_value())) {
auto &discord = Abaddon::Get().GetDiscordClient();
const auto id = m_property_id.get_value();
if (discord.IsChannelMuted(m_property_id.get_value())) {
auto muted = color;
muted.set_red(muted.get_red() * 0.5);
muted.set_green(muted.get_green() * 0.5);
muted.set_blue(muted.get_blue() * 0.5);
m_renderer_text.property_foreground_rgba() = muted;
} else {
if (discord.GetUnreadChannelsCountForCategory(id) > 0) {
AddUnreadIndicator(cr, background_area);
}
m_renderer_text.property_foreground_rgba() = color;
}
m_renderer_text.render(cr, widget, background_area, text_cell_area, flags);
@@ -519,14 +535,7 @@ void CellRendererChannels::render_vfunc_channel(const Cairo::RefPtr<Cairo::Conte
if (unread_state < 0) return;
if (!is_muted) {
static const auto color = Gdk::RGBA(Abaddon::Get().GetSettings().UnreadIndicatorColor);
cr->set_source_rgb(color.get_red(), color.get_green(), color.get_blue());
const auto x = background_area.get_x();
const auto y = background_area.get_y();
const auto w = background_area.get_width();
const auto h = background_area.get_height();
cr->rectangle(x, y, 3, h);
cr->fill();
AddUnreadIndicator(cr, background_area);
}
if (unread_state < 1) return;

View File

@@ -1317,6 +1317,17 @@ int DiscordClient::GetUnreadStateForChannel(Snowflake id) const noexcept {
return iter->second;
}
int DiscordClient::GetUnreadChannelsCountForCategory(Snowflake id) const noexcept {
int result = 0;
for (Snowflake channel_id : m_store.GetChannelIDsWithParentID(id)) {
if (IsChannelMuted(channel_id)) continue;
const auto iter = m_unread.find(channel_id);
if (iter == m_unread.end()) continue;
result += 1;
}
return result;
}
bool DiscordClient::GetUnreadStateForGuild(Snowflake id, int &total_mentions) const noexcept {
total_mentions = 0;
bool has_any_unread = false;

View File

@@ -214,6 +214,7 @@ public:
bool IsChannelMuted(Snowflake id) const noexcept;
bool IsGuildMuted(Snowflake id) const noexcept;
int GetUnreadStateForChannel(Snowflake id) const noexcept;
int GetUnreadChannelsCountForCategory(Snowflake id) const noexcept;
bool GetUnreadStateForGuild(Snowflake id, int &total_mentions) const noexcept;
int GetUnreadDMsCount() const;