});
});
}
- pub fn load_content(&mut self, index: usize) {
+
+ pub fn show_attachment(&mut self, index: usize) {
+ let a = self.attachments.get(index);
+ if let Some(attachment) = self.attachments.get(index) {
+ if let Some(content) = &attachment.content {
+ self.image.image_load_data(&content.content);
+ }
+ }
+ }
+
+ pub fn load_content(&mut self, index: usize) -> Option<&Attachment> {
let Some(obj) = self.obj.get_object() else {
- return;
+ return None;
};
- if index >= self.attachments.len() {
- return;
+
+ // show the attachment content, if already available
+ if let Some(selected_index) = self.selected_index && selected_index == index {
+ self.show_attachment(index);
}
- let a = &self.attachments[index];
- if let Some(content) = &a.content {
- self.image.image_load_data(&content.content);
- } else {
+ let a = self.attachments.get(index);
+ if a?.content.is_none() {
+ // content not loaded yet, get attachment content from the backend
let proxy = obj.obj_proxy();
- let attachment_id = a.attachment.attachment_id;
+ let attachment_id = a?.attachment.attachment_id;
self.backend.get_attachment_content(attachment_id, move|result|{
proxy.call_mainthread(move |doc, wdata|{
match result {
Ok(content) => {
let a = wdata.attachments.get_mut(index);
- if let Some(a) = a {
- if let Some(selection) = wdata.selection && selection == a.attachment.attachment_id {
- wdata.image.image_load_data(&content.content);
- }
+ // make sure the index still points to the same attachment_id
+ if let Some(a) = a && a.attachment.attachment_id == attachment_id {
a.content = Some(content);
+ // Is this attachment still selected? Then we can show it
+ if let Some(selection) = wdata.selected_index {
+ wdata.show_attachment(selection);
+ }
}
},
Err(e) => {
});
});
}
+ a
}