From 3f6024ddf213b83cf0c815b68f8ad8de3f3afa44 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Sat, 16 Jan 2021 23:49:27 -0500 Subject: improve image loading a bit (close #6) --- components/chatmessage.cpp | 68 ++++++++-------------------------------------- 1 file changed, 11 insertions(+), 57 deletions(-) (limited to 'components/chatmessage.cpp') diff --git a/components/chatmessage.cpp b/components/chatmessage.cpp index 40d706b..4692a80 100644 --- a/components/chatmessage.cpp +++ b/components/chatmessage.cpp @@ -1,6 +1,7 @@ #include "chatmessage.hpp" #include "../abaddon.hpp" #include "../util.hpp" +#include "lazyimage.hpp" #include constexpr const int EmojiSize = 24; // settings eventually @@ -116,35 +117,11 @@ void ChatMessageItemContainer::UpdateContent() { if (data->Embeds.size() == 1) { m_embed_component = CreateEmbedComponent(data->Embeds[0]); - if (m_embed_imgurl.size() > 0) { - m_signal_image_load.emit(m_embed_imgurl); - } AttachEventHandlers(*m_embed_component); m_main->add(*m_embed_component); } } -void ChatMessageItemContainer::UpdateImage(std::string url, Glib::RefPtr 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; - } - - auto it = m_img_loadmap.find(url); - if (it != m_img_loadmap.end()) { - const auto inw = it->second.second.first; - const auto inh = it->second.second.second; - int w, h; - GetImageDimensions(inw, inh, w, h); - it->second.first->property_pixbuf() = buf->scale_simple(w, h, Gdk::INTERP_BILINEAR); - } -} - void ChatMessageItemContainer::UpdateReactions() { if (m_reactions_component != nullptr) delete m_reactions_component; @@ -179,11 +156,6 @@ void ChatMessageItemContainer::UpdateAttributes() { m_attrib_label->set_markup("[edited]"); } -bool ChatMessageItemContainer::EmitImageLoad(std::string url) { - m_signal_image_load.emit(url); - return false; -} - void ChatMessageItemContainer::AddClickHandler(Gtk::Widget *widget, std::string url) { // clang-format off widget->signal_button_press_event().connect([url](GdkEventButton *event) -> bool { @@ -351,24 +323,23 @@ Gtk::Widget *ChatMessageItemContainer::CreateEmbedComponent(const EmbedData &emb bool is_img = embed.Image.has_value() && embed.Image->ProxyURL.has_value(); bool is_thumb = embed.Thumbnail.has_value() && embed.Thumbnail->ProxyURL.has_value(); if (is_img || is_thumb) { - auto *img = Gtk::manage(new Gtk::Image); - img->set_halign(Gtk::ALIGN_CENTER); - img->set_margin_top(5); int w = 0, h = 0; if (is_img) GetImageDimensions(*embed.Image->Width, *embed.Image->Height, w, h, EmbedImageWidth, EmbedImageHeight); else GetImageDimensions(*embed.Thumbnail->Width, *embed.Thumbnail->Height, w, h, EmbedImageWidth, EmbedImageHeight); - img->set_size_request(w, h); - main->pack_start(*img); - m_embed_img = img; + + std::string url; if (is_img) - m_embed_imgurl = *embed.Image->ProxyURL; + url = *embed.Image->ProxyURL; else - m_embed_imgurl = *embed.Thumbnail->ProxyURL; + url = *embed.Thumbnail->ProxyURL; - Abaddon::Get().GetImageManager().LoadFromURL(m_embed_imgurl, - sigc::mem_fun(*this, &ChatMessageItemContainer::OnEmbedImageLoad)); + auto *img = Gtk::manage(new LazyImage(url, w, h, false)); + img->set_halign(Gtk::ALIGN_CENTER); + img->set_margin_top(5); + img->set_size_request(w, h); + main->pack_start(*img); } if (embed.Footer.has_value()) { @@ -410,14 +381,13 @@ Gtk::Widget *ChatMessageItemContainer::CreateImageComponent(const std::string &p GetImageDimensions(inw, inh, w, h); Gtk::EventBox *ev = Gtk::manage(new Gtk::EventBox); - Gtk::Image *widget = Gtk::manage(new Gtk::Image); + Gtk::Image *widget = Gtk::manage(new LazyImage(proxy_url, w, h, false)); ev->add(*widget); widget->set_halign(Gtk::ALIGN_START); widget->set_size_request(w, h); AttachEventHandlers(*ev); AddClickHandler(ev, url); - HandleImage(w, h, *widget, proxy_url); return ev; } @@ -608,18 +578,6 @@ void ChatMessageItemContainer::ReactionUpdateImage(Gtk::Image *img, const Glib:: img->property_pixbuf() = pb->scale_simple(16, 16, Gdk::INTERP_BILINEAR); } -void ChatMessageItemContainer::HandleImage(int w, int h, Gtk::Image &img, std::string url) { - m_img_loadmap[url] = { &img, { w, h } }; - // ask the chatwindow to call UpdateImage because dealing with lifetimes sucks - Glib::signal_idle().connect(sigc::bind(sigc::mem_fun(*this, &ChatMessageItemContainer::EmitImageLoad), url)); -} - -void ChatMessageItemContainer::OnEmbedImageLoad(const Glib::RefPtr &pixbuf) { - int w, h; - m_embed_img->get_size_request(w, h); - m_embed_img->property_pixbuf() = pixbuf->scale_simple(w, h, Gdk::INTERP_BILINEAR); -} - Glib::ustring ChatMessageItemContainer::GetText(const Glib::RefPtr &buf) { Gtk::TextBuffer::iterator a, b; buf->get_bounds(a, b); @@ -986,10 +944,6 @@ ChatMessageItemContainer::type_signal_action_reaction_remove ChatMessageItemCont return m_signal_action_reaction_remove; } -ChatMessageItemContainer::type_signal_image_load ChatMessageItemContainer::signal_image_load() { - return m_signal_image_load; -} - void ChatMessageItemContainer::AttachEventHandlers(Gtk::Widget &widget) { const auto on_button_press_event = [this](GdkEventButton *event) -> bool { if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_SECONDARY) { -- cgit v1.2.3