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 { pub enum MaybeImage {
#[default] #[default]
Unloaded, 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), Loading(task::Handle),
Loaded(widget::image::Handle), Loaded(widget::image::Handle),
} }
impl MaybeImage { impl MaybeImage {
@ -15,14 +21,6 @@ impl MaybeImage {
_ => None, _ => 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 { pub fn handle_from_image(image: DynamicImage) -> widget::image::Handle {

View file

@ -66,8 +66,9 @@ impl State {
fn update(&mut self, message: Message) -> Task<Message> { fn update(&mut self, message: Message) -> Task<Message> {
match message { match message {
Message::LoadImage(path) => { Message::LoadImage(path) => {
let path_clone = path.clone(); let entry = self.images.entry(path.clone()).or_default();
if matches!(entry, MaybeImage::Unloaded) {
let (task, handle) = Task::perform( let (task, handle) = Task::perform(
async { async {
utils::load_image(&path) utils::load_image(&path)
@ -78,12 +79,10 @@ impl State {
) )
.abortable(); .abortable();
self.images.entry(path_clone).and_modify(|e| { *entry = MaybeImage::Loading(handle.abort_on_drop());
*e = MaybeImage::Loading(handle);
});
return task; return task;
} }
}
Message::ImageLoaded(result) => { Message::ImageLoaded(result) => {
self.error = result.as_ref().err().map(|e| e.to_string()); self.error = result.as_ref().err().map(|e| e.to_string());
@ -95,7 +94,7 @@ impl State {
} }
Message::UnloadImage(path) => { Message::UnloadImage(path) => {
self.images.entry(path).and_modify(MaybeImage::unload); self.images.entry(path).insert_entry(MaybeImage::Unloaded);
} }
Message::OpenImage(path) => { Message::OpenImage(path) => {