]> uap-core.de Git - note.git/commitdiff
load attachment content
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 5 Sep 2026 12:12:09 +0000 (14:12 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 5 Sep 2026 12:12:09 +0000 (14:12 +0200)
application/note/src/attachments_window.rs

index 9352afdd7439074babbbdbeed974ea0f0bcd7938..7ebf530c304dd067d1cd2f091269d2918ed4864b 100644 (file)
@@ -43,9 +43,14 @@ pub fn attachments_window_create(backend: BackendHandle, note_id: i32, selected_
             });
         });
 
-        tabview!(obj, tabview_type = TabViewType::Invisible, varname = "attachment_view_type", |tv|{
+        tabview!(obj, tabview_type = TabViewType::Invisible, varname = "attachment_view_type", fill = true, |tv|{
             tv.tab("img", |obj|{
-                imageviewer!(obj, varname = "image", fill = true);
+                imageviewer!(obj,
+                    varname = "image",
+                    fill = true,
+                    autoscale = true,
+                    scrollview = true,
+                    useradjustable = true);
             });
             tv.tab("webview", |obj|{
                 webview!(obj, varname = "webview", fill = true);
@@ -65,8 +70,7 @@ pub struct AttachmentsWindow {
     note_id: i32,
     backend: BackendHandle,
 
-    attachments: Vec<entity::attachment::Model>,
-    content: Vec<Option<entity::attachmentcontent::Model>>,
+    attachments: Vec<Attachment>,
     selection: Option<i32>,
     selected_index: Option<usize>,
 
@@ -77,6 +81,20 @@ pub struct AttachmentsWindow {
     pub webview: UiWeb,
 }
 
+struct Attachment {
+    attachment: entity::attachment::Model,
+    content: Option<entity::attachmentcontent::Model>
+}
+
+impl Attachment {
+    fn new(attachment: &entity::attachment::Model) -> Attachment {
+        Self {
+            attachment: attachment.clone(),
+            content: None,
+        }
+    }
+}
+
 #[ui_actions]
 impl AttachmentsWindow {
     pub fn new(backend: BackendHandle, note_id: i32) -> AttachmentsWindow {
@@ -85,7 +103,6 @@ impl AttachmentsWindow {
             note_id,
             backend,
             attachments: Vec::new(),
-            content: Vec::new(),
             selection: None,
             selected_index: None,
             image: Default::default(),
@@ -104,11 +121,14 @@ impl AttachmentsWindow {
                 match result {
                     Ok(attachments) => {
                         println!("attachments loaded");
-                        wdata.attachments = attachments;
-                        wdata.content = vec![None; wdata.attachments.len()];
+                        let ats = attachments.iter()
+                            .map(|a| Attachment::new(a) )
+                            .collect::<Vec<_>>();
+
+                        wdata.attachments = ats;
 
                         if let Some(selection) = wdata.selection {
-                            wdata.selected_index = wdata.attachments.iter().position(|a| a.attachment_id == selection);
+                            wdata.selected_index = attachments.iter().position(|a| a.attachment_id == selection);
                             if let Some(index) = wdata.selected_index {
                                 wdata.load_content(index);
                             }
@@ -127,16 +147,34 @@ impl AttachmentsWindow {
         let Some(obj) = self.obj.get_object() else {
             return;
         };
-        if index >= self.content.len() {
+        if index >= self.attachments.len() {
             return;
         }
 
-        let ct = &self.content[index];
-        if let Some(content) = ct {
+        let a = &self.attachments[index];
+        if let Some(content) = &a.content {
             self.image.image_load_data(&content.content);
         } else {
             let proxy = obj.obj_proxy();
-            // TODO: load attachment data
+            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);
+                                }
+                                a.content = Some(content);
+                            }
+                        },
+                        Err(e) => {
+                            println!("Cannot get attachment content: {}", e);
+                        }
+                    }
+                });
+            });
         }
     }