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 {
|
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 {
|
||||||
|
|
|
||||||
|
|
@ -66,23 +66,22 @@ 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();
|
||||||
|
|
||||||
let (task, handle) = Task::perform(
|
if matches!(entry, MaybeImage::Unloaded) {
|
||||||
async {
|
let (task, handle) = Task::perform(
|
||||||
utils::load_image(&path)
|
async {
|
||||||
.map(|img| (path, handle_from_image(img)))
|
utils::load_image(&path)
|
||||||
.map_err(Arc::new)
|
.map(|img| (path, handle_from_image(img)))
|
||||||
},
|
.map_err(Arc::new)
|
||||||
Message::ImageLoaded,
|
},
|
||||||
)
|
Message::ImageLoaded,
|
||||||
.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) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue