add ability to set image alt text (closes #253)

This commit is contained in:
ouwou
2023-12-15 01:16:11 -05:00
parent 23bf237e4e
commit 4bce7b7523
6 changed files with 54 additions and 7 deletions

View File

@@ -295,7 +295,7 @@ std::vector<ChatSubmitParams::Attachment> ChatInputAttachmentContainer::GetAttac
for (auto *x : m_attachments) {
if (!x->GetFile()->query_exists())
puts("bad!");
ret.push_back({ x->GetFile(), x->GetType(), x->GetFilename() });
ret.push_back({ x->GetFile(), x->GetType(), x->GetFilename(), x->GetDescription() });
}
return ret;
}
@@ -343,6 +343,7 @@ ChatInputAttachmentItem::ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &
, m_img(Gtk::make_managed<Gtk::Image>())
, m_type(is_extant ? ChatSubmitParams::ExtantFile : ChatSubmitParams::PastedImage)
, m_filename("unknown.png")
, m_is_image(true)
, m_label("unknown.png")
, m_box(Gtk::ORIENTATION_VERTICAL) {
get_style_context()->add_class("attachment-item");
@@ -389,10 +390,18 @@ std::string ChatInputAttachmentItem::GetFilename() const {
return m_filename;
}
std::optional<std::string> ChatInputAttachmentItem::GetDescription() const {
return m_description.empty() ? std::nullopt : std::optional<std::string>(m_description);
}
bool ChatInputAttachmentItem::IsTemp() const noexcept {
return m_type == ChatSubmitParams::PastedImage;
}
bool ChatInputAttachmentItem::IsImage() const noexcept {
return m_is_image;
}
void ChatInputAttachmentItem::RemoveIfTemp() {
if (IsTemp())
m_file->remove();
@@ -420,12 +429,22 @@ void ChatInputAttachmentItem::SetupMenu() {
}
});
m_menu_set_alt_text.set_label("Change Alt-Text");
m_menu_set_alt_text.signal_activate().connect([this]() {
const auto description = Abaddon::Get().ShowTextPrompt("Enter description (alt-text) for attachment", "Enter alt-text", m_description);
if (description.has_value()) {
m_description = *description;
}
});
m_menu.add(m_menu_set_filename);
m_menu.add(m_menu_set_alt_text);
m_menu.add(m_menu_remove);
m_menu.show_all();
signal_button_press_event().connect([this](GdkEventButton *ev) -> bool {
if (ev->button == GDK_BUTTON_SECONDARY) {
m_menu_set_alt_text.set_visible(IsImage());
m_menu.popup_at_pointer(reinterpret_cast<GdkEvent *>(ev));
return true;
}

View File

@@ -7,10 +7,13 @@ public:
ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &file);
ChatInputAttachmentItem(const Glib::RefPtr<Gio::File> &file, const Glib::RefPtr<Gdk::Pixbuf> &pb, bool is_extant = false);
[[nodiscard]] Glib::RefPtr<Gio::File> GetFile() const;
[[nodiscard]] ChatSubmitParams::AttachmentType GetType() const;
[[nodiscard]] std::string GetFilename() const;
[[nodiscard]] bool IsTemp() const noexcept;
Glib::RefPtr<Gio::File> GetFile() const;
ChatSubmitParams::AttachmentType GetType() const;
std::string GetFilename() const;
std::optional<std::string> GetDescription() const;
bool IsTemp() const noexcept;
bool IsImage() const noexcept;
void RemoveIfTemp();
private:
@@ -21,6 +24,7 @@ private:
Gtk::Menu m_menu;
Gtk::MenuItem m_menu_remove;
Gtk::MenuItem m_menu_set_filename;
Gtk::MenuItem m_menu_set_alt_text;
Gtk::Box m_box;
Gtk::Label m_label;
@@ -29,6 +33,8 @@ private:
Glib::RefPtr<Gio::File> m_file;
ChatSubmitParams::AttachmentType m_type;
std::string m_filename;
std::string m_description;
bool m_is_image = false;
private:
using type_signal_item_removed = sigc::signal<void>;

View File

@@ -1,4 +1,5 @@
#pragma once
#include <optional>
#include <vector>
#include <string>
#include <glibmm/ustring.h>
@@ -15,6 +16,7 @@ struct ChatSubmitParams {
Glib::RefPtr<Gio::File> File;
AttachmentType Type;
std::string Filename;
std::optional<std::string> Description;
};
bool Silent = false;

View File

@@ -518,6 +518,7 @@ void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams &params, c
CreateMessageObject obj;
obj.Content = params.Message;
obj.Nonce = nonce;
obj.Attachments.emplace();
if (params.Silent) {
obj.Flags |= MessageFlags::SUPPRESS_NOTIFICATIONS;
}
@@ -541,11 +542,16 @@ void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams &params, c
m_generic_dispatch.emit();
});
req.make_form();
req.add_field("payload_json", nlohmann::json(obj).dump().c_str(), CURL_ZERO_TERMINATED);
for (size_t i = 0; i < params.Attachments.size(); i++) {
auto &attachment = params.Attachments.at(i);
const auto field_name = "files[" + std::to_string(i) + "]";
req.add_file(field_name, params.Attachments.at(i).File, params.Attachments.at(i).Filename);
req.add_file(field_name, attachment.File, attachment.Filename);
obj.Attachments->push_back({ static_cast<int>(i), attachment.Description });
}
req.add_field("payload_json", nlohmann::json(obj).dump().c_str(), CURL_ZERO_TERMINATED);
m_http.Execute(std::move(req), [this, params, nonce, callback](const http::response_type &res) {
for (const auto &attachment : params.Attachments) {
if (attachment.Type == ChatSubmitParams::AttachmentType::PastedImage) {

View File

@@ -306,9 +306,15 @@ void to_json(nlohmann::json &j, const HeartbeatMessage &m) {
j["d"] = m.Sequence;
}
void to_json(nlohmann::json &j, const CreateMessageAttachmentObject &m) {
j["id"] = m.ID;
JS_IF("description", m.Description);
}
void to_json(nlohmann::json &j, const CreateMessageObject &m) {
j["content"] = m.Content;
j["flags"] = m.Flags;
JS_IF("attachments", m.Attachments);
JS_IF("message_reference", m.MessageReference);
JS_IF("nonce", m.Nonce);
}

View File

@@ -433,11 +433,19 @@ struct HeartbeatMessage : GatewayMessage {
friend void to_json(nlohmann::json &j, const HeartbeatMessage &m);
};
struct CreateMessageAttachmentObject {
int ID;
std::optional<std::string> Description;
friend void to_json(nlohmann::json &j, const CreateMessageAttachmentObject &m);
};
struct CreateMessageObject {
std::string Content;
MessageFlags Flags = MessageFlags::NONE;
std::optional<MessageReferenceData> MessageReference;
std::optional<std::string> Nonce;
std::optional<std::vector<CreateMessageAttachmentObject>> Attachments;
friend void to_json(nlohmann::json &j, const CreateMessageObject &m);
};