Skip to content

Commit

Permalink
Load image texture in thread
Browse files Browse the repository at this point in the history
  • Loading branch information
ranfdev committed Feb 1, 2024
1 parent 4105696 commit fe3ae28
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/widgets/message_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::io::Read;
use adw::prelude::*;
use adw::subclass::prelude::*;
use chrono::NaiveDateTime;
use gdk_pixbuf::Pixbuf;
use gtk::gdk_pixbuf;
use gtk::{gdk, gio, glib};
use ntfy_daemon::models;
use tracing::error;
Expand Down Expand Up @@ -165,9 +163,11 @@ impl MessageRow {
fn build_image(&self, url: String) -> gtk::Picture {
let (s, r) = async_channel::unbounded();
gio::spawn_blocking(move || {
if let Err(e) = Self::fetch_image_bytes(&url)
.map(|bytes| s.send_blocking(glib::Bytes::from_owned(bytes)))
{
if let Err(e) = Self::fetch_image_bytes(&url).and_then(|bytes| {
let t = gdk::Texture::from_bytes(&glib::Bytes::from_owned(bytes))?;
s.send_blocking(t)?;
Ok(())
}) {
error!(error = %e)
}
glib::ControlFlow::Break
Expand All @@ -178,10 +178,8 @@ impl MessageRow {
let picturec = picture.clone();

self.spawn_with_near_toast(async move {
let b = r.recv().await?;
let stream = gio::MemoryInputStream::from_bytes(&b);
let pixbuf = Pixbuf::from_stream(&stream, gio::Cancellable::NONE)?;
picturec.set_paintable(Some(&gdk::Texture::for_pixbuf(&pixbuf)));
let t = r.recv().await?;
picturec.set_paintable(Some(&t));
Ok::<(), anyhow::Error>(())
});

Expand Down

0 comments on commit fe3ae28

Please sign in to comment.