diff --git a/src/app/maybe_image.rs b/src/app/maybe_image.rs index 1984854..c8fff05 100644 --- a/src/app/maybe_image.rs +++ b/src/app/maybe_image.rs @@ -5,7 +5,13 @@ use image::DynamicImage; pub enum MaybeImage { #[default] Unloaded, + + #[allow(dead_code)] + /// should be abort_on_drop, + /// so that it will abort, for instance, + /// when it's set back to `Self::Unloaded` Loading(task::Handle), + Loaded(widget::image::Handle), } impl MaybeImage { @@ -15,14 +21,6 @@ impl MaybeImage { _ => None, } } - - pub fn unload(&mut self) { - match self { - Self::Loading(task_handle) => task_handle.abort(), - _ => {} - } - *self = Self::Unloaded; - } } pub fn handle_from_image(image: DynamicImage) -> widget::image::Handle { diff --git a/src/app/mod.rs b/src/app/mod.rs index 496479c..b384ebd 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -66,23 +66,22 @@ impl State { fn update(&mut self, message: Message) -> Task { match message { Message::LoadImage(path) => { - let path_clone = path.clone(); + let entry = self.images.entry(path.clone()).or_default(); - let (task, handle) = Task::perform( - async { - utils::load_image(&path) - .map(|img| (path, handle_from_image(img))) - .map_err(Arc::new) - }, - Message::ImageLoaded, - ) - .abortable(); + if matches!(entry, MaybeImage::Unloaded) { + let (task, handle) = Task::perform( + async { + utils::load_image(&path) + .map(|img| (path, handle_from_image(img))) + .map_err(Arc::new) + }, + Message::ImageLoaded, + ) + .abortable(); - self.images.entry(path_clone).and_modify(|e| { - *e = MaybeImage::Loading(handle); - }); - - return task; + *entry = MaybeImage::Loading(handle.abort_on_drop()); + return task; + } } Message::ImageLoaded(result) => { self.error = result.as_ref().err().map(|e| e.to_string()); @@ -95,7 +94,7 @@ impl State { } Message::UnloadImage(path) => { - self.images.entry(path).and_modify(MaybeImage::unload); + self.images.entry(path).insert_entry(MaybeImage::Unloaded); } Message::OpenImage(path) => {