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:
parent
e179b824c8
commit
26268f9771
2 changed files with 23 additions and 26 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -66,8 +66,9 @@ 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();
|
||||
|
||||
if matches!(entry, MaybeImage::Unloaded) {
|
||||
let (task, handle) = Task::perform(
|
||||
async {
|
||||
utils::load_image(&path)
|
||||
|
|
@ -78,12 +79,10 @@ impl State {
|
|||
)
|
||||
.abortable();
|
||||
|
||||
self.images.entry(path_clone).and_modify(|e| {
|
||||
*e = MaybeImage::Loading(handle);
|
||||
});
|
||||
|
||||
*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) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue