replace file chooser with native

also remove clipboard since it was just a workaround and i dont want to maintain it
This commit is contained in:
ouwou
2022-04-06 21:07:45 -04:00
parent 56a74fb5dd
commit 57e95c8969
4 changed files with 16 additions and 59 deletions

View File

@@ -119,7 +119,7 @@ void GuildData::update_from_json(const nlohmann::json &j) {
JS_RD("approximate_presence_count", ApproximatePresenceCount);
}
bool GuildData::HasFeature(const std::string &search_feature) {
bool GuildData::HasFeature(const std::string &search_feature) const {
if (!Features.has_value()) return false;
for (const auto &feature : *Features)
if (search_feature == feature)

View File

@@ -91,7 +91,7 @@ struct GuildData {
friend void from_json(const nlohmann::json &j, GuildData &m);
void update_from_json(const nlohmann::json &j);
bool HasFeature(const std::string &feature);
bool HasFeature(const std::string &feature) const;
bool HasIcon() const;
bool HasAnimatedIcon() const;
std::string GetIconURL(const std::string &ext = "png", const std::string &size = "32") const;

View File

@@ -3,8 +3,7 @@
#include <filesystem>
GuildSettingsInfoPane::GuildSettingsInfoPane(Snowflake id)
: m_guild_icon_label("Guild icon")
, m_guild_name_label("Guild name")
: m_guild_name_label("Guild name")
, GuildID(id) {
auto &discord = Abaddon::Get().GetDiscordClient();
const auto guild = *discord.GetGuild(id);
@@ -45,11 +44,8 @@ GuildSettingsInfoPane::GuildSettingsInfoPane(Snowflake id)
m_guild_icon_ev.set_tooltip_text("Click to choose a file, right click to paste");
m_guild_icon_ev.signal_button_press_event().connect([this](GdkEventButton *event) -> bool {
if (event->type == GDK_BUTTON_PRESS) {
if (event->button == GDK_BUTTON_PRIMARY)
UpdateGuildIconPicker();
else if (event->button == GDK_BUTTON_SECONDARY)
UpdateGuildIconClipboard();
if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY) {
UpdateGuildIconPicker();
}
return false;
@@ -140,14 +136,12 @@ void GuildSettingsInfoPane::UpdateGuildIconFromPixbuf(Glib::RefPtr<Gdk::Pixbuf>
}
void GuildSettingsInfoPane::UpdateGuildIconPicker() {
// this picker fucking sucks
Gtk::FileChooserDialog dlg("Choose new guild icon", Gtk::FILE_CHOOSER_ACTION_OPEN);
dlg.get_style_context()->remove_provider(Abaddon::Get().GetStyleProvider());
dlg.set_modal(true);
dlg.signal_response().connect([this, &dlg](int response) {
if (response == Gtk::RESPONSE_OK) {
auto data = ReadWholeFile(dlg.get_filename());
if (GetExtension(dlg.get_filename()) == ".gif")
auto dlg = Gtk::FileChooserNative::create("Choose new guild icon", Gtk::FILE_CHOOSER_ACTION_OPEN);
dlg->set_modal(true);
dlg->signal_response().connect([this, dlg](int response) {
if (response == Gtk::RESPONSE_ACCEPT) {
auto data = ReadWholeFile(dlg->get_filename());
if (GetExtension(dlg->get_filename()) == ".gif")
UpdateGuildIconFromData(data, "image/gif");
else
try {
@@ -160,15 +154,12 @@ void GuildSettingsInfoPane::UpdateGuildIconPicker() {
loader->write(data.data(), data.size());
loader->close();
UpdateGuildIconFromPixbuf(loader->get_pixbuf());
} catch (const std::exception &) {}
} catch (...) {}
}
});
dlg.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
dlg.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
auto filter_images = Gtk::FileFilter::create();
if (Abaddon::Get().GetDiscordClient().GetGuild(GuildID)->HasFeature("ANIMATED_ICON")) {
if (const auto guild = Abaddon::Get().GetDiscordClient().GetGuild(GuildID); guild.has_value() && guild->HasFeature("ANIMATED_ICON")) {
filter_images->set_name("Supported images (*.jpg, *.jpeg, *.png, *.gif)");
filter_images->add_pattern("*.gif");
} else {
@@ -177,44 +168,12 @@ void GuildSettingsInfoPane::UpdateGuildIconPicker() {
filter_images->add_pattern("*.jpg");
filter_images->add_pattern("*.jpeg");
filter_images->add_pattern("*.png");
dlg.add_filter(filter_images);
dlg->add_filter(filter_images);
auto filter_all = Gtk::FileFilter::create();
filter_all->set_name("All files (*.*)");
filter_all->add_pattern("*.*");
dlg.add_filter(filter_all);
dlg->add_filter(filter_all);
dlg.run();
}
void GuildSettingsInfoPane::UpdateGuildIconClipboard() {
std::vector<uint8_t> icon_data;
auto cb = Gtk::Clipboard::get();
// query for file path then for actual image
if (cb->wait_is_text_available()) {
auto path = cb->wait_for_text();
if (!std::filesystem::exists(path.c_str())) return;
auto data = ReadWholeFile(path);
try {
auto loader = Gdk::PixbufLoader::create();
loader->signal_size_prepared().connect([&loader](int inw, int inh) {
int w, h;
GetImageDimensions(inw, inh, w, h, 1024, 1024);
loader->set_size(w, h);
});
loader->write(data.data(), data.size());
loader->close();
auto pb = loader->get_pixbuf();
UpdateGuildIconFromPixbuf(pb);
return;
} catch (const std::exception &) {}
}
if (cb->wait_is_image_available()) {
auto pb = cb->wait_for_image();
UpdateGuildIconFromPixbuf(pb);
return;
}
dlg->run();
}

View File

@@ -13,9 +13,7 @@ private:
void UpdateGuildIconFromData(const std::vector<uint8_t> &data, const std::string &mime);
void UpdateGuildIconFromPixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf);
void UpdateGuildIconPicker();
void UpdateGuildIconClipboard();
Gtk::Label m_guild_icon_label;
Gtk::EventBox m_guild_icon_ev; // necessary to make custom cursor behave properly
Gtk::Image m_guild_icon;