Skip to content

Commit

Permalink
use better async
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyyxxxx committed Oct 17, 2024
1 parent bec57cd commit ea28152
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ parking_lot = "0.12.1"
rayon = "1.7"
num_cpus = "1.15"
rand = "0.8"
crossbeam-channel = "0.5"

[profile.release]
lto = true
Expand Down
14 changes: 8 additions & 6 deletions src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crossbeam_channel::unbounded;
use glib::ControlFlow;
use gtk::gdk::Texture;
use gtk::gdk_pixbuf::Pixbuf;
Expand All @@ -15,8 +16,8 @@ use std::{
io::Read,
path::{Path, PathBuf},
rc::Rc,
sync::{mpsc, Arc},
sync::atomic::{AtomicBool, Ordering},
sync::Arc,
};

const CONFIG_FILE: &str = "~/.config/hyprwall/config.ini";
Expand Down Expand Up @@ -242,7 +243,7 @@ fn load_images(
let cache = Arc::clone(&image_loader.cache);

let flowbox_clone = Rc::clone(flowbox);
let (sender, receiver) = mpsc::channel::<(Texture, String)>();
let (sender, receiver) = unbounded::<(Texture, String)>();

while let Some(child) = flowbox.borrow().first_child() {
flowbox.borrow().remove(&child);
Expand Down Expand Up @@ -272,8 +273,9 @@ fn load_images(
};

let path_clone = path.to_str().unwrap_or("").to_string();
s.send((texture, path_clone))
.expect("Failed to send texture");
if s.send((texture, path_clone)).is_err() {
eprintln!("Failed to send texture: receiver disconnected");
}
});
});

Expand Down Expand Up @@ -314,8 +316,8 @@ fn load_images(

flowbox.insert(&button, -1);
}
Err(mpsc::TryRecvError::Empty) => break,
Err(mpsc::TryRecvError::Disconnected) => return ControlFlow::Break,
Err(crossbeam_channel::TryRecvError::Empty) => break,
Err(crossbeam_channel::TryRecvError::Disconnected) => return ControlFlow::Break,
}
}
ControlFlow::Continue
Expand Down

0 comments on commit ea28152

Please sign in to comment.