]> uap-core.de Git - note.git/commitdiff
small refactoring in the attachments window
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 5 Sep 2026 12:32:54 +0000 (14:32 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 5 Sep 2026 12:32:54 +0000 (14:32 +0200)
application/note/src/attachments_window.rs

index 7ebf530c304dd067d1cd2f091269d2918ed4864b..765601073d3bd2a0e51f45c3428de53f6710b03c 100644 (file)
@@ -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
     }