]> uap-core.de Git - note.git/commitdiff
implement toolbar button for adding attachments main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 23 Aug 2026 17:07:21 +0000 (19:07 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 23 Aug 2026 17:07:21 +0000 (19:07 +0200)
application/note/src/main.rs
application/note/src/note.rs

index fc01db481a1c12b741c3d521adde165eb66f581e..d820e7306334d14ac630d7155e7feb0a38434eb5 100644 (file)
@@ -163,12 +163,14 @@ fn create_toolbar(app: &AppContext<MainWindow>) {
         .action("notebook_view_changed")
         .varname("note_maximized")
         .create();
+    app.toolbar_item("add_attachment").icon(UiIconSet::Add.as_str()).action("add_attachment").create();
 
     app.toolbar_add_default("new_notebook", ToolbarItemPosition::SidebarLeft);
     app.toolbar_add_default("go_back", ToolbarItemPosition::Left);
     app.toolbar_add_default("go_forward", ToolbarItemPosition::Left);
     app.toolbar_add_default("new_note", ToolbarItemPosition::Left);
 
+    app.toolbar_add_default("add_attachment", ToolbarItemPosition::RightPanelLeft);
     app.toolbar_add_default("note_maximize", ToolbarItemPosition::RightPanelRight);
 
     app.toolbar_appmenu(|menu|{
index 1695c234d3d92c4d05f675c9f9499beec1df0de1..719fe84348decd4a228919bad66265293b106ccb 100644 (file)
@@ -25,7 +25,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
 use std::rc::Rc;
 use std::sync::atomic::{AtomicU64, Ordering};
 use std::time::SystemTime;
@@ -519,6 +519,41 @@ impl Note {
             })
             .create();
     }
+
+    #[action]
+    pub fn add_attachment(&mut self, event: &mut ActionEvent) {
+        let Some(doc) = self.doc.get_doc() else {
+            return;
+        };
+        let Some(obj) = &event.obj else {
+            return;
+        };
+
+        // TODO: currently it is unsupported to add attachments to unsaved notes
+        let NoteId::Id(note_id) = self.id else {
+            return;
+        };
+
+        let proxy = doc.doc_proxy();
+        let backend = self.backend.clone();
+        obj.openfile_dialog(false, move|event|{
+            if let EventType::FileList(file_selection) = event.event_type && let Some(file) = file_selection.get(0) {
+                let path = Path::new(file);
+                backend.add_note_attachment(note_id, path, move|result|{
+                    proxy.call_mainthread(|_doc, note|{
+                        match result {
+                            Ok((attachment, content)) => {
+                                println!("attachment: {} id: {}", attachment.attachment_id, content.attachment_id);
+                            },
+                            Err(e) => {
+                                println!("cannot add attachment: {}", e);
+                            }
+                        }
+                    });
+                });
+            }
+        });
+    }
 }
 
 impl NoteViewModel for Note {