Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/abaddon.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
#include "abaddon.hpp"
#include <memory>
#include <spdlog/spdlog.h>
#include <spdlog/cfg/env.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <string>
#include <algorithm>
#include <gtkmm.h>

#include "platform.hpp"
#include "startup.hpp"
#include "util.hpp"
#include "audio/manager.hpp"
#include "discord/discord.hpp"
#include "dialogs/token.hpp"
#include "dialogs/confirm.hpp"
#include "dialogs/setstatus.hpp"
#include "dialogs/friendpicker.hpp"
#include "dialogs/verificationgate.hpp"
#include "dialogs/setstatus.hpp"
#include "dialogs/textinput.hpp"
#include "dialogs/token.hpp"
#include "dialogs/verificationgate.hpp"
#include "dialogs/quickswitcher/quickswitcher.hpp"
#include "discord/discord.hpp"
#include "notifications/notifications.hpp"
#include "remoteauth/remoteauthdialog.hpp"
#include "windows/guildsettingswindow.hpp"
#include "windows/profilewindow.hpp"
#include "windows/pinnedwindow.hpp"
#include "windows/profilewindow.hpp"
#include "windows/threadswindow.hpp"
#include "windows/voicewindow.hpp"
#include "startup.hpp"
#include "notifications/notifications.hpp"
#include "remoteauth/remoteauthdialog.hpp"
#include "util.hpp"

#include <algorithm>
#include <gtkmm.h>
#include <memory>
#include <string>
#include <spdlog/spdlog.h>
#include <spdlog/cfg/env.h>
#include <spdlog/sinks/stdout_color_sinks.h>

#if defined(__APPLE__)
#include <CoreFoundation/CoreFoundation.h>
Expand Down Expand Up @@ -1109,6 +1112,11 @@ bool Abaddon::ShowConfirm(const Glib::ustring &prompt, Gtk::Window *window) {
return dlg.run() == Gtk::RESPONSE_OK;
}

void Abaddon::ShowQuickSwitcher(Gtk::Window *window) {
QuickSwitcher dlg(*window);
dlg.run();
}

void Abaddon::ActionReloadCSS() {
try {
Gtk::StyleContext::remove_provider_for_screen(Gdk::Screen::get_default(), m_css_provider);
Expand Down
1 change: 1 addition & 0 deletions src/abaddon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Abaddon {

std::optional<Glib::ustring> ShowTextPrompt(const Glib::ustring &prompt, const Glib::ustring &title, const Glib::ustring &placeholder = "", Gtk::Window *window = nullptr);
bool ShowConfirm(const Glib::ustring &prompt, Gtk::Window *window = nullptr);
void ShowQuickSwitcher(Gtk::Window *window);

void ActionReloadCSS();

Expand Down
2 changes: 1 addition & 1 deletion src/components/channellist/channellist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void ChannelList::UpdateListing() {
}

void ChannelList::SetActiveChannel(Snowflake id, bool expand_to) {
if (Abaddon::Get().GetSettings().ClassicChangeGuildOnOpen) {
if (Abaddon::Get().GetSettings().ClassicChannels && Abaddon::Get().GetSettings().ClassicChangeGuildOnOpen) {
if (const auto channel = Abaddon::Get().GetDiscordClient().GetChannel(id); channel.has_value() && channel->GuildID.has_value()) {
m_tree.SetSelectedGuild(*channel->GuildID);
} else {
Expand Down
59 changes: 7 additions & 52 deletions src/components/channellist/channellisttree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,58 +1235,13 @@ void ChannelListTree::UpdateCreateDMChannel(const ChannelData &dm) {
void ChannelListTree::SetDMChannelIcon(Gtk::TreeIter iter, const ChannelData &dm) {
auto &img = Abaddon::Get().GetImageManager();

std::optional<UserData> top_recipient;
const auto recipients = dm.GetDMRecipients();
if (!recipients.empty())
top_recipient = recipients[0];

if (dm.HasIcon()) {
const auto cb = [this, iter](const Glib::RefPtr<Gdk::Pixbuf> &pb) {
if (iter)
(*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR);
};
img.LoadFromURL(dm.GetIconURL(), sigc::track_obj(cb, *this));
} else if (dm.Type == ChannelType::DM && top_recipient.has_value()) {
const auto cb = [this, iter](const Glib::RefPtr<Gdk::Pixbuf> &pb) {
if (iter)
(*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR);
};
img.LoadFromURL(top_recipient->GetAvatarURL("png", "32"), sigc::track_obj(cb, *this));
} else { // GROUP_DM
std::string hash;
switch (dm.ID.GetUnixMilliseconds() % 8) {
case 0:
hash = "ee9275c5a437f7dc7f9430ba95f12ebd";
break;
case 1:
hash = "9baf45aac2a0ec2e2dab288333acb9d9";
break;
case 2:
hash = "7ba11ffb1900fa2b088cb31324242047";
break;
case 3:
hash = "f90fca70610c4898bc57b58bce92f587";
break;
case 4:
hash = "e2779af34b8d9126b77420e5f09213ce";
break;
case 5:
hash = "c6851bd0b03f1cca5a8c1e720ea6ea17";
break;
case 6:
hash = "f7e38ac976a2a696161c923502a8345b";
break;
case 7:
default:
hash = "3cb840d03313467838d658bbec801fcd";
break;
}
const auto cb = [this, iter](const Glib::RefPtr<Gdk::Pixbuf> &pb) {
if (iter)
(*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR);
};
img.LoadFromURL("https://discord.com/assets/" + hash + ".png", sigc::track_obj(cb, *this));
}
auto icon_url = dm.GetIconURL();

const auto cb = [this, iter](const Glib::RefPtr<Gdk::Pixbuf> &pb) {
if (iter)
(*iter)[m_columns.m_icon] = pb->scale_simple(DMIconSize, DMIconSize, Gdk::INTERP_BILINEAR);
};
img.LoadFromURL(icon_url, sigc::track_obj(cb, *this));
}

void ChannelListTree::RedrawUnreadIndicatorsForChannel(const ChannelData &channel) {
Expand Down
228 changes: 228 additions & 0 deletions src/dialogs/quickswitcher/quickswitcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
#include "quickswitcher.hpp"

#include "abaddon.hpp"
#include "quickswitcherresultrow.hpp"
#include "util.hpp"

QuickSwitcher::QuickSwitcher(Gtk::Window &parent)
: Gtk::Dialog("Quick Switcher", parent, true) {
Index();

set_decorated(false);
set_default_size(350, 175);

m_entry.set_placeholder_text("Where would you like to go?");
m_entry.set_hexpand(true);
m_entry.signal_stop_search().connect([this]() {
response(Gtk::RESPONSE_OK);
});
m_entry.signal_activate().connect(sigc::mem_fun(*this, &QuickSwitcher::OnEntryActivate));
m_entry.signal_changed().connect(sigc::mem_fun(*this, &QuickSwitcher::Search));
m_entry.signal_previous_match().connect([this]() { GoDown(); });
m_entry.signal_next_match().connect([this]() { GoUp(); });
m_entry.signal_key_press_event().connect(sigc::mem_fun(*this, &QuickSwitcher::OnEntryKeyPress), false);
get_content_area()->add(m_entry);

m_results.set_activate_on_single_click(true);
m_results.set_selection_mode(Gtk::SELECTION_SINGLE);
m_results.set_vexpand(true);
m_results.signal_row_activated().connect(sigc::mem_fun(*this, &QuickSwitcher::OnResultRowActivate));
m_results_scroll.add(m_results);
get_content_area()->add(m_results_scroll);

show_all_children();
}

void QuickSwitcher::Index() {
m_index.clear();
IndexPrivateChannels();
IndexChannels();
IndexGuilds();
}

void QuickSwitcher::IndexPrivateChannels() {
auto &discord = Abaddon::Get().GetDiscordClient();
for (auto &dm_id : discord.GetPrivateChannels()) {
if (auto dm = discord.GetChannel(dm_id); dm.has_value()) {
const auto sort = static_cast<uint64_t>(-(dm->LastMessageID.has_value() ? *dm->LastMessageID : dm_id));
m_index[dm_id] = { SwitcherEntry::ResultType::DM,
dm->GetDisplayName(),
sort,
dm_id };
}
}
}

void QuickSwitcher::IndexChannels() {
auto &discord = Abaddon::Get().GetDiscordClient();

const auto channels = discord.GetAllChannelData();
// grab literally everything to do in memory otherwise we get a shit ton of IOs
auto overwrites = discord.GetAllPermissionOverwrites();

auto member_roles = discord.GetAllMemberRoles(discord.GetUserData().ID);
std::unordered_map<Snowflake, RoleData> roles;
for (const auto &[guild_id, guild_roles] : member_roles) {
for (const auto &role_data : guild_roles) {
roles.emplace(role_data.ID, role_data);
}
}

for (auto &channel : channels) {
if (!channel.Name.has_value()) continue;
if (!channel.IsText()) continue;
if (channel.GuildID.has_value() &&
!discord.HasSelfChannelPermission(channel, Permission::VIEW_CHANNEL, roles, member_roles[*channel.GuildID], overwrites[channel.ID])) continue;
m_index[channel.ID] = { SwitcherEntry::ResultType::Channel,
*channel.Name,
static_cast<uint64_t>(channel.ID),
channel.ID };
}
}

void QuickSwitcher::IndexGuilds() {
auto &discord = Abaddon::Get().GetDiscordClient();
const auto guilds = discord.GetGuilds();
for (auto guild_id : guilds) {
const auto guild = discord.GetGuild(guild_id);
if (!guild.has_value()) continue;
// todo make this smart
m_index[guild->ID] = { SwitcherEntry::ResultType::Guild,
guild->Name,
static_cast<uint64_t>(guild->ID),
guild->ID };
}
}

void QuickSwitcher::Search() {
for (auto child : m_results.get_children()) delete child;

const auto query = m_entry.get_text();
if (query.empty()) return;

std::vector<SwitcherEntry> results;
for (auto &[id, item] : m_index) {
if (StringContainsCaseless(item.Name, query)) {
results.push_back(item);
}
}

std::sort(results.begin(), results.end(), [](const SwitcherEntry &a, const SwitcherEntry &b) -> bool {
return a.Sort < b.Sort;
});

auto &discord = Abaddon::Get().GetDiscordClient();

int result_count = 0;
const int MAX_RESULTS = 15;
for (auto &result : results) {
QuickSwitcherResultRow *row = nullptr;
switch (result.Type) {
case SwitcherEntry::ResultType::DM: {
if (const auto channel = discord.GetChannel(result.ID); channel.has_value()) {
row = Gtk::make_managed<QuickSwitcherResultRowDM>(*channel);
}
} break;
case SwitcherEntry::ResultType::Channel: {
if (const auto channel = discord.GetChannel(result.ID); channel.has_value()) {
row = Gtk::make_managed<QuickSwitcherResultRowChannel>(*channel);
}
} break;
case SwitcherEntry::ResultType::Guild: {
if (const auto guild = discord.GetGuild(result.ID); guild.has_value()) {
row = Gtk::make_managed<QuickSwitcherResultRowGuild>(*guild);
}
} break;
}
if (row != nullptr) {
row->show();
m_results.add(*row);
if (++result_count >= MAX_RESULTS) break;
}
}

GoUp();
}

void QuickSwitcher::GoUp() {
Move(-1);
}

void QuickSwitcher::GoDown() {
Move(1);
}

void QuickSwitcher::Move(int dir) {
auto children = m_results.get_children();
if (children.empty()) return;

auto selected = m_results.get_selected_row();
if (selected == nullptr) {
if (auto row = dynamic_cast<Gtk::ListBoxRow *>(children[0])) {
m_results.select_row(*row);
}
return;
}

int idx = selected->get_index() + dir;
if (idx < 0) {
idx = children.size() - 1;
} else if (idx >= children.size()) {
idx = 0;
}

if (auto row = dynamic_cast<Gtk::ListBoxRow *>(children[idx])) {
m_results.select_row(*row);
}

ScrollListBoxToSelected(m_results);
}

void QuickSwitcher::AcceptResult(Snowflake id) {
const auto result = m_index.find(id);
if (result == m_index.end()) return;
const auto &entry = result->second;

switch (entry.Type) {
case SwitcherEntry::ResultType::Channel:
case SwitcherEntry::ResultType::DM:
Abaddon::Get().ActionChannelOpened(entry.ID, false);
break;
case SwitcherEntry::ResultType::Guild: {
const auto guild = Abaddon::Get().GetDiscordClient().GetGuild(entry.ID);
if (!guild.has_value()) return;
const auto channel = guild->GetDefaultTextChannel();
if (channel.has_value()) {
Abaddon::Get().ActionChannelOpened(*channel, false);
}
} break;
}
}

void QuickSwitcher::OnEntryActivate() {
if (auto *row = dynamic_cast<QuickSwitcherResultRow *>(m_results.get_selected_row())) {
AcceptResult(row->ID);
}
response(Gtk::RESPONSE_OK);
}

bool QuickSwitcher::OnEntryKeyPress(GdkEventKey *event) {
if (event->type != GDK_KEY_PRESS) return false;
switch (event->keyval) {
case GDK_KEY_Up:
GoUp();
return true;
case GDK_KEY_Down:
GoDown();
return true;
default:
return false;
}
}

void QuickSwitcher::OnResultRowActivate(Gtk::ListBoxRow *row_) {
if (auto *row = dynamic_cast<QuickSwitcherResultRow *>(row_)) {
AcceptResult(row->ID);
}
response(Gtk::RESPONSE_OK);
}
Loading