fix: two possible causes of flickering

still flickers, likely due to aborting not working
since the async code isn't really async...
This commit is contained in:
electria 2026-08-09 15:27:38 -07:00
commit 26268f9771
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
2 changed files with 23 additions and 26 deletions

View file

@ -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 {

View file

@ -66,23 +66,22 @@ impl State {
fn update(&mut self, message: Message) -> Task<Message> {
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) => {