diff options
author | ouwou <26526779+ouwou@users.noreply.github.com> | 2020-10-03 18:49:22 -0400 |
---|---|---|
committer | ouwou <26526779+ouwou@users.noreply.github.com> | 2020-10-03 18:49:22 -0400 |
commit | bbe36a8246041544d6ebe420dfa1b695b6c74521 (patch) | |
tree | aa2c607b812c04afd2ef4a240489e7c8adec7c3b /components | |
parent | c7a958a3bb241b28c766faa3db6f4ff1876bd065 (diff) | |
download | abaddon-portaudio-bbe36a8246041544d6ebe420dfa1b695b6c74521.tar.gz abaddon-portaudio-bbe36a8246041544d6ebe420dfa1b695b6c74521.zip |
maybe slightly better image loading maybe hopefully
Diffstat (limited to 'components')
-rw-r--r-- | components/chatmessage.cpp | 53 | ||||
-rw-r--r-- | components/chatmessage.hpp | 3 | ||||
-rw-r--r-- | components/chatwindow.cpp | 4 |
3 files changed, 20 insertions, 40 deletions
diff --git a/components/chatmessage.cpp b/components/chatmessage.cpp index a219958..55c6efa 100644 --- a/components/chatmessage.cpp +++ b/components/chatmessage.cpp @@ -88,25 +88,22 @@ void ChatMessageItemContainer::UpdateContent() { } } -void ChatMessageItemContainer::UpdateImage() { - for (auto it = m_img_loadmap.cbegin(); it != m_img_loadmap.cend();) { - auto buf = Abaddon::Get().GetImageManager().GetFromURLIfCached(it->first); - if (buf) { - int w, h; - std::tie(w, h) = GetImageDimensions(it->second.second.Width, it->second.second.Height); - it->second.first->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR); - it = m_img_loadmap.erase(it); - } else - it++; +void ChatMessageItemContainer::UpdateImage(std::string url, Glib::RefPtr<Gdk::Pixbuf> buf) { + if (!buf) return; + + if (m_embed_img != nullptr && m_embed_imgurl == url) { + int w, h; + m_embed_img->get_size_request(w, h); + m_embed_img->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR); + + return; } - if (m_embed_img != nullptr) { - auto buf = Abaddon::Get().GetImageManager().GetFromURLIfCached(m_embed_imgurl); - if (buf) { - int w, h; - m_embed_img->get_size_request(w, h); - m_embed_img->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR); - } + auto it = m_img_loadmap.find(url); + if (it != m_img_loadmap.end()) { + int w, h; + GetImageDimensions(it->second.second.Width, it->second.second.Height, w, h); + it->second.first->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR); } } @@ -270,9 +267,9 @@ Gtk::EventBox *ChatMessageItemContainer::CreateEmbedComponent(const Message *dat img->set_halign(Gtk::ALIGN_CENTER); int w, h; if (is_img) - std::tie(w, h) = GetImageDimensions(embed.Image.Width, embed.Image.Height, 200, 150); + GetImageDimensions(embed.Image.Width, embed.Image.Height, w, h, 200, 150); else - std::tie(w, h) = GetImageDimensions(embed.Thumbnail.Width, embed.Thumbnail.Height, 200, 150); + GetImageDimensions(embed.Thumbnail.Width, embed.Thumbnail.Height, w, h, 200, 150); img->set_size_request(w, h); main->pack_start(*img); m_embed_img = img; @@ -319,7 +316,7 @@ Gtk::EventBox *ChatMessageItemContainer::CreateEmbedComponent(const Message *dat Gtk::Image *ChatMessageItemContainer::CreateImageComponent(const AttachmentData &data) { int w, h; - std::tie(w, h) = GetImageDimensions(data.Width, data.Height); + GetImageDimensions(data.Width, data.Height, w, h); auto &im = Abaddon::Get().GetImageManager(); Gtk::Image *widget = Gtk::manage(new Gtk::Image); @@ -350,22 +347,6 @@ Gtk::Box *ChatMessageItemContainer::CreateAttachmentComponent(const AttachmentDa return box; } -std::pair<int, int> ChatMessageItemContainer::GetImageDimensions(int width, int height, int clampw, int clamph) { - const auto frac = static_cast<float>(width) / height; - - if (width > clampw) { - width = clampw; - height = clampw / frac; - } - - if (height > clamph) { - height = clamph; - width = clamph * frac; - } - - return std::make_pair(static_cast<int>(width), static_cast<int>(height)); -} - void ChatMessageItemContainer::HandleImage(const AttachmentData &data, Gtk::Image *img, std::string url) { m_img_loadmap[url] = std::make_pair(img, data); // ask the chatwindow to call UpdateImage because dealing with lifetimes sucks diff --git a/components/chatmessage.hpp b/components/chatmessage.hpp index 73307d2..144118f 100644 --- a/components/chatmessage.hpp +++ b/components/chatmessage.hpp @@ -13,7 +13,7 @@ public: // attributes = edited, deleted void UpdateAttributes(); void UpdateContent(); - void UpdateImage(); + void UpdateImage(std::string url, Glib::RefPtr<Gdk::Pixbuf> buf); protected: bool EmitImageLoad(std::string url); @@ -24,7 +24,6 @@ protected: Gtk::Image *CreateImageComponent(const AttachmentData &data); Gtk::Box *CreateAttachmentComponent(const AttachmentData &data); // non-image attachments void HandleImage(const AttachmentData &data, Gtk::Image *img, std::string url); - static std::pair<int, int> GetImageDimensions(int width, int height, int clampw = 400, int clamph = 300); std::unordered_map<std::string, std::pair<Gtk::Image *, AttachmentData>> m_img_loadmap; diff --git a/components/chatwindow.cpp b/components/chatwindow.cpp index 710d2ad..8b2b649 100644 --- a/components/chatwindow.cpp +++ b/components/chatwindow.cpp @@ -210,11 +210,11 @@ void ChatWindow::ProcessNewMessage(Snowflake id, bool prepend) { }); content->signal_image_load().connect([this, id](std::string url) { auto &mgr = Abaddon::Get().GetImageManager(); - mgr.LoadFromURL(url, [this, id](Glib::RefPtr<Gdk::Pixbuf> buf) { + mgr.LoadFromURL(url, [this, id, url](Glib::RefPtr<Gdk::Pixbuf> buf) { if (m_id_to_widget.find(id) != m_id_to_widget.end()) { auto *x = dynamic_cast<ChatMessageItemContainer *>(m_id_to_widget.at(id)); if (x != nullptr) - x->UpdateImage(); + x->UpdateImage(url, buf); } }); }); |