From ea4381f54ccc13f8586e5548edebb047b01282a8 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sat, 5 Sep 2026 14:32:54 +0200 Subject: [PATCH] small refactoring in the attachments window --- application/note/src/attachments_window.rs | 40 +++++++++++++++------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/application/note/src/attachments_window.rs b/application/note/src/attachments_window.rs index 7ebf530..7656010 100644 --- a/application/note/src/attachments_window.rs +++ b/application/note/src/attachments_window.rs @@ -143,30 +143,43 @@ impl AttachmentsWindow { }); }); } - 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) => { @@ -176,6 +189,7 @@ impl AttachmentsWindow { }); }); } + a } -- 2.52.0